diff options
Diffstat (limited to 'src/basic/parse-util.c')
-rw-r--r-- | src/basic/parse-util.c | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/basic/parse-util.c b/src/basic/parse-util.c index 54d1081a0..c8bca5a30 100644 --- a/src/basic/parse-util.c +++ b/src/basic/parse-util.c @@ -536,6 +536,30 @@ int safe_atoi16(const char *s, int16_t *ret) { return 0; } +int safe_atoux16(const char *s, uint16_t *ret) { + char *x = NULL; + unsigned long l; + + assert(s); + assert(ret); + + s += strspn(s, WHITESPACE); + + errno = 0; + l = strtoul(s, &x, 16); + if (errno > 0) + return -errno; + if (!x || x == s || *x != 0) + return -EINVAL; + if (s[0] == '-') + return -ERANGE; + if ((unsigned long) (uint16_t) l != l) + return -ERANGE; + + *ret = (uint16_t) l; + return 0; +} + int safe_atod(const char *s, double *ret_d) { _cleanup_(freelocalep) locale_t loc = (locale_t) 0; char *x = NULL; |