Adding upstream version 4.00~pre55+dfsg.
[syslinux-debian/hramrach.git] / memdump / strtoul.c
blobc7c81d620c9b8404f31e06f4389b98bb887cb725
1 /*
2 * strtoul.c
4 */
6 #include "mystuff.h"
8 static inline int isspace(int c)
10 return (c <= ' '); /* Close enough */
13 static inline int digitval(int ch)
15 if (ch >= '0' && ch <= '9') {
16 return ch - '0';
17 } else if (ch >= 'A' && ch <= 'Z') {
18 return ch - 'A' + 10;
19 } else if (ch >= 'a' && ch <= 'z') {
20 return ch - 'a' + 10;
21 } else {
22 return -1;
26 unsigned long strtoul(const char *nptr, char **endptr, int base)
28 int minus = 0;
29 unsigned long v = 0;
30 int d;
32 while (isspace((unsigned char)*nptr)) {
33 nptr++;
36 /* Single optional + or - */
38 char c = *nptr;
39 if (c == '-' || c == '+') {
40 minus = (c == '-');
41 nptr++;
45 if (base == 0) {
46 if (nptr[0] == '0' && (nptr[1] == 'x' || nptr[1] == 'X')) {
47 nptr += 2;
48 base = 16;
49 } else if (nptr[0] == '0') {
50 nptr++;
51 base = 8;
52 } else {
53 base = 10;
55 } else if (base == 16) {
56 if (nptr[0] == '0' && (nptr[1] == 'x' || nptr[1] == 'X')) {
57 nptr += 2;
61 while ((d = digitval(*nptr)) >= 0 && d < base) {
62 v = v * base + d;
63 nptr++;
66 if (endptr)
67 *endptr = (char *)nptr;
69 return minus ? -v : v;