Adding upstream version 6.03~pre1+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / strntoumax.c
blobd8bc73bcce1869ed16d67bb9609c906b6744f754
1 /*
2 * strntoumax.c
4 * The strntoumax() function and associated
5 */
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <ctype.h>
11 static inline int digitval(int ch)
13 if (ch >= '0' && ch <= '9') {
14 return ch - '0';
15 } else if (ch >= 'A' && ch <= 'Z') {
16 return ch - 'A' + 10;
17 } else if (ch >= 'a' && ch <= 'z') {
18 return ch - 'a' + 10;
19 } else {
20 return -1;
24 uintmax_t strntoumax(const char *nptr, char **endptr, int base, size_t n)
26 int minus = 0;
27 uintmax_t v = 0;
28 int d;
30 while (n && isspace((unsigned char)*nptr)) {
31 nptr++;
32 n--;
35 /* Single optional + or - */
36 if (n && *nptr == '-') {
37 minus = 1;
38 nptr++;
39 n--;
40 } else if (n && *nptr == '+') {
41 nptr++;
44 if (base == 0) {
45 if (n >= 2 && nptr[0] == '0' && (nptr[1] == 'x' || nptr[1] == 'X')) {
46 n -= 2;
47 nptr += 2;
48 base = 16;
49 } else if (n >= 1 && nptr[0] == '0') {
50 n--;
51 nptr++;
52 base = 8;
53 } else {
54 base = 10;
56 } else if (base == 16) {
57 if (n >= 2 && nptr[0] == '0' && (nptr[1] == 'x' || nptr[1] == 'X')) {
58 n -= 2;
59 nptr += 2;
63 while (n && (d = digitval(*nptr)) >= 0 && d < base) {
64 v = v * base + d;
65 n--;
66 nptr++;
69 if (endptr)
70 *endptr = (char *)nptr;
72 return minus ? -v : v;