Adding upstream version 3.53.
[syslinux-debian/hramrach.git] / memdump / strtoul.c
blobcd09d3d0bd7fb6bae4814f7a09411f852d076be6
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' &&
47 (nptr[1] == 'x' || nptr[1] == 'X')) {
48 nptr += 2;
49 base = 16;
50 } else if (nptr[0] == '0') {
51 nptr++;
52 base = 8;
53 } else {
54 base = 10;
56 } else if (base == 16) {
57 if (nptr[0] == '0' &&
58 (nptr[1] == 'x' || nptr[1] == 'X')) {
59 nptr += 2;
63 while ((d = digitval(*nptr)) >= 0 && d < base) {
64 v = v * base + d;
65 nptr++;
68 if (endptr)
69 *endptr = (char *)nptr;
71 return minus ? -v : v;