diff options
-rw-r--r-- | src/util/strtonum.c | 18 | ||||
-rw-r--r-- | src/util/strtonum.h | 2 |
2 files changed, 20 insertions, 0 deletions
diff --git a/src/util/strtonum.c b/src/util/strtonum.c index bf1b21a62..22e682b4b 100644 --- a/src/util/strtonum.c +++ b/src/util/strtonum.c @@ -63,3 +63,21 @@ uint32_t strtouint32(const char *nptr, char **endptr, int base) /* If errno was set by strtoll, we'll pass it back as-is */ return (uint32_t)ret; } + + +/* strtouint16 */ +uint16_t strtouint16(const char *nptr, char **endptr, int base) +{ + unsigned long long ret = 0; + errno = 0; + ret = strtoull(nptr, endptr, base); + + if (ret > UINT16_MAX) { + errno = ERANGE; + return UINT16_MAX; + } + + /* If errno was set by strtoll, we'll pass it back as-is */ + return (uint16_t)ret; +} + diff --git a/src/util/strtonum.h b/src/util/strtonum.h index 450959621..d9c31e9cd 100644 --- a/src/util/strtonum.h +++ b/src/util/strtonum.h @@ -29,4 +29,6 @@ int32_t strtoint32(const char *nptr, char **endptr, int base); uint32_t strtouint32(const char *nptr, char **endptr, int base); +uint16_t strtouint16(const char *nptr, char **endptr, int base); + #endif /* _STRTONUM_H_ */ |