File util-linux-lib-strutils-add-strtoux-16-32-64-_or_err-functions.patch of Package util-linux.25241

From 54394eab03d57ff7d4ffbd333c1955112bc70c5d Mon Sep 17 00:00:00 2001
From: Heiko Carstens <heiko.carstens@de.ibm.com>
Date: Wed, 12 Oct 2016 14:00:44 +0200
Subject: [PATCH] lib,strutils: add strtoux[16|32|64]_or_err functions

Add helper functions which allow to parse hexadecimal numbers.
Based on a patch from Clemens von Mann.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
---
 include/strutils.h |    3 +++
 lib/strutils.c     |   44 ++++++++++++++++++++++++++++++++++++++------
 2 files changed, 41 insertions(+), 6 deletions(-)

--- a/include/strutils.h
+++ b/include/strutils.h
@@ -21,12 +21,15 @@ extern uintmax_t strtosize_or_err(const
 
 extern int16_t strtos16_or_err(const char *str, const char *errmesg);
 extern uint16_t strtou16_or_err(const char *str, const char *errmesg);
+extern uint16_t strtox16_or_err(const char *str, const char *errmesg);
 
 extern int32_t strtos32_or_err(const char *str, const char *errmesg);
 extern uint32_t strtou32_or_err(const char *str, const char *errmesg);
+extern uint32_t strtox32_or_err(const char *str, const char *errmesg);
 
 extern int64_t strtos64_or_err(const char *str, const char *errmesg);
 extern uint64_t strtou64_or_err(const char *str, const char *errmesg);
+extern uint64_t strtox64_or_err(const char *str, const char *errmesg);
 
 extern double strtod_or_err(const char *str, const char *errmesg);
 
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -269,6 +269,9 @@ char *strndup(const char *s, size_t n)
 }
 #endif
 
+static uint32_t _strtou32_or_err(const char *str, const char *errmesg, int base);
+static uint64_t _strtou64_or_err(const char *str, const char *errmesg, int base);
+
 int16_t strtos16_or_err(const char *str, const char *errmesg)
 {
 	int32_t num = strtos32_or_err(str, errmesg);
@@ -280,9 +283,9 @@ int16_t strtos16_or_err(const char *str,
 	return num;
 }
 
-uint16_t strtou16_or_err(const char *str, const char *errmesg)
+static uint16_t _strtou16_or_err(const char *str, const char *errmesg, int base)
 {
-	uint32_t num = strtou32_or_err(str, errmesg);
+	uint32_t num = _strtou32_or_err(str, errmesg, base);
 
 	if (num > UINT16_MAX) {
 		errno = ERANGE;
@@ -291,6 +294,16 @@ uint16_t strtou16_or_err(const char *str
 	return num;
 }
 
+uint16_t strtou16_or_err(const char *str, const char *errmesg)
+{
+	return _strtou16_or_err(str, errmesg, 10);
+}
+
+uint16_t strtox16_or_err(const char *str, const char *errmesg)
+{
+	return _strtou16_or_err(str, errmesg, 16);
+}
+
 int32_t strtos32_or_err(const char *str, const char *errmesg)
 {
 	int64_t num = strtos64_or_err(str, errmesg);
@@ -302,9 +315,9 @@ int32_t strtos32_or_err(const char *str,
 	return num;
 }
 
-uint32_t strtou32_or_err(const char *str, const char *errmesg)
+static uint32_t _strtou32_or_err(const char *str, const char *errmesg, int base)
 {
-	uint64_t num = strtou64_or_err(str, errmesg);
+	uint64_t num = _strtou64_or_err(str, errmesg, base);
 
 	if (num > UINT32_MAX) {
 		errno = ERANGE;
@@ -313,6 +326,16 @@ uint32_t strtou32_or_err(const char *str
 	return num;
 }
 
+uint32_t strtou32_or_err(const char *str, const char *errmesg)
+{
+	return _strtou32_or_err(str, errmesg, 10);
+}
+
+uint32_t strtox32_or_err(const char *str, const char *errmesg)
+{
+	return _strtou32_or_err(str, errmesg, 16);
+}
+
 int64_t strtos64_or_err(const char *str, const char *errmesg)
 {
 	int64_t num;
@@ -334,7 +357,7 @@ err:
 	errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
 }
 
-uint64_t strtou64_or_err(const char *str, const char *errmesg)
+static uint64_t _strtou64_or_err(const char *str, const char *errmesg, int base)
 {
 	uintmax_t num;
 	char *end = NULL;
@@ -342,7 +365,7 @@ uint64_t strtou64_or_err(const char *str
 	errno = 0;
 	if (str == NULL || *str == '\0')
 		goto err;
-	num = strtoumax(str, &end, 10);
+	num = strtoumax(str, &end, base);
 
 	if (errno || str == end || (end && *end))
 		goto err;
@@ -355,6 +378,15 @@ err:
 	errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str);
 }
 
+uint64_t strtou64_or_err(const char *str, const char *errmesg)
+{
+	return _strtou64_or_err(str, errmesg, 10);
+}
+
+uint64_t strtox64_or_err(const char *str, const char *errmesg)
+{
+	return _strtou64_or_err(str, errmesg, 16);
+}
 
 double strtod_or_err(const char *str, const char *errmesg)
 {
openSUSE Build Service is sponsored by