From c817c01548b1500753d0bea3852938d919161778 Mon Sep 17 00:00:00 2001 From: "Carlos L. Torres" Date: Sun, 19 Jul 2015 18:02:18 -0500 Subject: cutils: Add qemu_strtoul() wrapper Add wrapper for strtoul() function. Include unit tests. Signed-off-by: Carlos L. Torres Message-Id: <9621b4ae8e35fded31c715c2ae2a98f904f07ad0.1437346779.git.carlos.torres@rackspace.com> [Fix tests for 32-bit build. - Paolo] Signed-off-by: Paolo Bonzini --- util/cutils.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'util/cutils.c') diff --git a/util/cutils.c b/util/cutils.c index 3330360..8ee3d5e 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: * -- cgit v1.1