diff options
author | Stephen Gallagher <sgallagh@redhat.com> | 2012-01-18 13:22:45 -0500 |
---|---|---|
committer | Stephen Gallagher <sgallagh@redhat.com> | 2012-01-23 08:31:33 -0500 |
commit | eac695ff9189fcfcea0f233a01abf582ff685c30 (patch) | |
tree | 5b6535f8c515fec1d1a9120d05516479fc9a1071 /src/util | |
parent | 01171ddb20034578c616d0ba07f0268ab71809da (diff) | |
download | sssd-eac695ff9189fcfcea0f233a01abf582ff685c30.tar.gz sssd-eac695ff9189fcfcea0f233a01abf582ff685c30.tar.bz2 sssd-eac695ff9189fcfcea0f233a01abf582ff685c30.zip |
UTIL: Add strtouint16
Diffstat (limited to 'src/util')
-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 bf1b21a6..22e682b4 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 45095962..d9c31e9c 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_ */ |