1 /* Copyright (C) 1994 DJ Delorie, see COPYING.DJ for details */
3 #include <msvcrt/ctype.h>
4 #include <msvcrt/errno.h>
5 #include <msvcrt/stdlib.h>
8 * Convert a string to an unsigned long integer.
10 * Ignores `locale' stuff. Assumes that the upper and lower case
11 * alphabets and digits are each contiguous.
14 strtoul(const char *nptr, char **endptr, int base)
20 int neg = 0, any, cutlim;
23 * See strtol for comments as to the logic used.
35 if ((base == 0 || base == 16) &&
36 c == '0' && (*s == 'x' || *s == 'X'))
43 base = c == '0' ? 8 : 10;
44 cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
45 cutlim = (unsigned long)ULONG_MAX % (unsigned long)base;
46 for (acc = 0, any = 0;; c = *s++)
51 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
56 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
72 *endptr = any ? (char *)s - 1 : (char *)nptr;