From: Swen Schillig Date: Mon, 28 Jan 2019 08:42:13 +0000 (+0100) Subject: util: Add two wrapper for string to int conversion X-Git-Tag: talloc-2.2.0~238 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cef18c2dfd60be372eee10cbcefd5849ae979d31;p=thirdparty%2Fsamba.git util: Add two wrapper for string to int conversion Adding wrapper strtoull_err and strtoul_err to handle error conditions of the conversion process. Signed-off-by: Swen Schillig Reviewed-by: Ralph Böhme Reviewed-by: Jeremy Allison --- diff --git a/lib/util/util.c b/lib/util/util.c index f52f69c6ef0..855642c06bc 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -47,6 +47,56 @@ * @brief Misc utility functions */ +/** + * Convert a string to an unsigned long integer + * + * @param nptr pointer to string which is to be converted + * @param endptr [optional] reference to remainder of the string + * @param base base of the numbering scheme + * @param err error occured during conversion + * @result result of the conversion as provided by strtoul + * + * Currently the only errors detected are wrong base and a value overflow. + */ +unsigned long int +strtoul_err(const char *nptr, char **endptr, int base, int *err) +{ + unsigned long int val; + int saved_errno = errno; + + errno = 0; + val = strtoul(nptr, endptr, base); + *err = errno; + + errno = saved_errno; + return val; +} + +/** + * Convert a string to an unsigned long long integer + * + * @param nptr pointer to string which is to be converted + * @param endptr [optional] reference to remainder of the string + * @param base base of the numbering scheme + * @param err error occured during conversion + * @result result of the conversion as provided by strtoull + * + * Currently the only errors detected are wrong base and a value overflow. + */ +unsigned long long int +strtoull_err(const char *nptr, char **endptr, int base, int *err) +{ + unsigned long long int val; + int saved_errno = errno; + + errno = 0; + val = strtoull(nptr, endptr, base); + *err = errno; + + errno = saved_errno; + return val; +} + /** Find a suitable temporary directory. The result should be copied immediately as it may be overwritten by a subsequent call. diff --git a/lib/util/util.h b/lib/util/util.h index 5a0ce5cdb2a..fcd81759b9c 100644 --- a/lib/util/util.h +++ b/lib/util/util.h @@ -21,6 +21,13 @@ #ifndef __UTIL_SAMBA_UTIL_H__ #define __UTIL_SAMBA_UTIL_H__ +unsigned long int +strtoul_err(const char *nptr, char **endptr, int base, int *err); + +unsigned long long int +strtoull_err(const char *nptr, char **endptr, int base, int *err); + + /** * Write dump of binary data to a callback */