util: Add two wrapper for string to int conversion
authorSwen Schillig <swen@linux.ibm.com>
Mon, 28 Jan 2019 08:42:13 +0000 (09:42 +0100)
committerJeremy Allison <jra@samba.org>
Fri, 1 Mar 2019 00:32:09 +0000 (00:32 +0000)
Adding wrapper strtoull_err and strtoul_err to handle
error conditions of the conversion process.

Signed-off-by: Swen Schillig <swen@linux.ibm.com>
Reviewed-by: Ralph Böhme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/util.c
lib/util/util.h

index f52f69c6ef01599e5099c799a1f8e59f164a9294..855642c06bcc3a6f3ee5fcab07f48e94184b5e9d 100644 (file)
  * @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.
index 5a0ce5cdb2a3521bcfc7657a11a2513da66aa3af..fcd81759b9ce25002dc6cea9e3b89a6989488b37 100644 (file)
 #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
  */