summaryrefslogtreecommitdiffstats
path: root/lib/strutils.c
diff options
context:
space:
mode:
authorHeiko Carstens2016-10-12 14:00:44 +0200
committerKarel Zak2016-11-09 10:02:32 +0100
commit54394eab03d57ff7d4ffbd333c1955112bc70c5d (patch)
tree3ddb7b0ab9ffdbee50dc7f5dc17de9273f1d8950 /lib/strutils.c
parentbuild-sys: release++ (v2.29) (diff)
downloadkernel-qcow2-util-linux-54394eab03d57ff7d4ffbd333c1955112bc70c5d.tar.gz
kernel-qcow2-util-linux-54394eab03d57ff7d4ffbd333c1955112bc70c5d.tar.xz
kernel-qcow2-util-linux-54394eab03d57ff7d4ffbd333c1955112bc70c5d.zip
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>
Diffstat (limited to 'lib/strutils.c')
-rw-r--r--lib/strutils.c44
1 files changed, 38 insertions, 6 deletions
diff --git a/lib/strutils.c b/lib/strutils.c
index d3b998f02..8a52be048 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -264,6 +264,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);
@@ -275,9 +278,9 @@ int16_t strtos16_or_err(const char *str, const char *errmesg)
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;
@@ -286,6 +289,16 @@ uint16_t strtou16_or_err(const char *str, const char *errmesg)
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);
@@ -297,9 +310,9 @@ int32_t strtos32_or_err(const char *str, const char *errmesg)
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;
@@ -308,6 +321,16 @@ uint32_t strtou32_or_err(const char *str, const char *errmesg)
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;
@@ -329,7 +352,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;
@@ -337,7 +360,7 @@ uint64_t strtou64_or_err(const char *str, const char *errmesg)
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;
@@ -350,6 +373,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)
{