aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorCarlos L. Torres <carlos.torres@rackspace.com>2015-07-19 18:02:18 -0500
committerPaolo Bonzini <pbonzini@redhat.com>2015-09-09 15:34:54 +0200
commitc817c01548b1500753d0bea3852938d919161778 (patch)
tree9291ce2e432f637a628f0ba747cbc67ecf8d77a6 /util
parent764e0fa497ff5bbc9c9d7c116da2f00f34e71716 (diff)
cutils: Add qemu_strtoul() wrapper
Add wrapper for strtoul() function. Include unit tests. Signed-off-by: Carlos L. Torres <carlos.torres@rackspace.com> Message-Id: <9621b4ae8e35fded31c715c2ae2a98f904f07ad0.1437346779.git.carlos.torres@rackspace.com> [Fix tests for 32-bit build. - Paolo] Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'util')
-rw-r--r--util/cutils.c32
1 files changed, 32 insertions, 0 deletions
diff --git a/util/cutils.c b/util/cutils.c
index 3330360abf..8ee3d5ee9d 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -416,6 +416,38 @@ int qemu_strtol(const char *nptr, const char **endptr, int base,
}
return err;
}
+
+/**
+ * Converts ASCII string to an unsigned long integer.
+ *
+ * If string contains a negative number, value will be converted to
+ * the unsigned representation of the signed value, unless the original
+ * (nonnegated) value would overflow, in this case, it will set @result
+ * to ULONG_MAX, and return ERANGE.
+ *
+ * The same behavior holds, for qemu_strtoull() but sets @result to
+ * ULLONG_MAX instead of ULONG_MAX.
+ *
+ * See qemu_strtol() documentation for more info.
+ */
+int qemu_strtoul(const char *nptr, const char **endptr, int base,
+ unsigned long *result)
+{
+ char *p;
+ int err = 0;
+ if (!nptr) {
+ if (endptr) {
+ *endptr = nptr;
+ }
+ err = -EINVAL;
+ } else {
+ errno = 0;
+ *result = strtoul(nptr, &p, base);
+ err = check_strtox_error(endptr, p, errno);
+ }
+ return err;
+}
+
/**
* parse_uint:
*