From a9768580a49403a6ed4fcbc0403936073e301cdb Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Tue, 22 Jun 2021 14:20:42 +0200
Subject: [PATCH] include/strutils: cleanup strto..() functions

* add ul_strtos64() and ul_strtou64()
* add simple test

Addresses: https://github.com/karelzak/util-linux/issues/1358
Signed-off-by: Karel Zak <kzak@redhat.com>
Reference:https://github.com/util-linux/util-linux/commit/a9768580a49403a6ed4fcbc0403936073e301cdb
Conflict:add ul_strtou64
---
 include/strutils.h |  2 ++
 lib/strutils.c     | 14 ++++++++++++++
 2 files changed, 16 insertions(+)

diff --git a/include/strutils.h b/include/strutils.h
index 4b3182f..65b2934 100644
--- a/include/strutils.h
+++ b/include/strutils.h
@@ -16,6 +16,8 @@ extern int parse_size(const char *str, uintmax_t *res, int *power);
 extern int strtosize(const char *str, uintmax_t *res);
 extern uintmax_t strtosize_or_err(const char *str, const char *errmesg);
 
+extern int ul_strtou64(const char *str, uint64_t *num, int base);
+
 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);
diff --git a/lib/strutils.c b/lib/strutils.c
index 6c33820..7befec1 100644
--- a/lib/strutils.c
+++ b/lib/strutils.c
@@ -320,6 +320,20 @@ char *strndup(const char *s, size_t n)
 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);
 
+int ul_strtou64(const char *str, uint64_t *num, int base)
+{
+	char *end = NULL;
+
+	errno = 0;
+	if (str == NULL || *str == '\0')
+		return -EINVAL;
+	*num = (uint64_t) strtoumax(str, &end, base);
+
+	if (errno || str == end || (end && *end))
+		return -EINVAL;
+	return 0;
+}
+
 int16_t strtos16_or_err(const char *str, const char *errmesg)
 {
 	int32_t num = strtos32_or_err(str, errmesg);
-- 
2.27.0

