]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
parse-util: similar to safe_atou16_full() add safe_atou_full() 8534/head
authorLennart Poettering <lennart@poettering.net>
Wed, 21 Mar 2018 21:31:40 +0000 (22:31 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 21 Mar 2018 21:31:40 +0000 (22:31 +0100)
What's good for uint16_t is also good for unsigned.

This is preparation for: #8140

src/basic/parse-util.c
src/basic/parse-util.h

index f0bf57916e86b14c5379be348fb7a85f0c7393d6..32ea870e3410d788958e5102abdf6894b19ede4b 100644 (file)
@@ -371,12 +371,13 @@ finish:
 
 }
 
-int safe_atou(const char *s, unsigned *ret_u) {
+int safe_atou_full(const char *s, unsigned base, unsigned *ret_u) {
         char *x = NULL;
         unsigned long l;
 
         assert(s);
         assert(ret_u);
+        assert(base <= 16);
 
         /* strtoul() is happy to parse negative values, and silently
          * converts them to unsigned values without generating an
@@ -389,7 +390,7 @@ int safe_atou(const char *s, unsigned *ret_u) {
         s += strspn(s, WHITESPACE);
 
         errno = 0;
-        l = strtoul(s, &x, 0);
+        l = strtoul(s, &x, base);
         if (errno > 0)
                 return -errno;
         if (!x || x == s || *x != 0)
index 82265fcd0cd043e70e5fdaeb8d0f84bf4c549f8a..1605cc4ff5cf65c40b199e05c974abf2280e41cc 100644 (file)
@@ -44,7 +44,12 @@ int parse_syscall_and_errno(const char *in, char **name, int *error);
 #define FORMAT_BYTES_MAX 8
 char *format_bytes(char *buf, size_t l, uint64_t t);
 
-int safe_atou(const char *s, unsigned *ret_u);
+int safe_atou_full(const char *s, unsigned base, unsigned *ret_u);
+
+static inline int safe_atou(const char *s, unsigned *ret_u) {
+        return safe_atou_full(s, 0, ret_u);
+}
+
 int safe_atoi(const char *s, int *ret_i);
 int safe_atollu(const char *s, unsigned long long *ret_u);
 int safe_atolli(const char *s, long long int *ret_i);