parse-util: similar to safe_atou16_full() add safe_atou_full()

What's good for uint16_t is also good for unsigned.

This is preparation for: #8140
This commit is contained in:
Lennart Poettering
2018-03-21 22:31:40 +01:00
parent 5ef56aa205
commit 65baa289cf
2 changed files with 9 additions and 3 deletions
+3 -2
View 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)