a2bb4d7dd76393950414f8b5de1f9917a1aa7c76
[musl] / src / stdlib / strtoumax.c
1 #include <inttypes.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <ctype.h>
5 #include "intparse.h"
6
7 uintmax_t strtoumax(const char *s1, char **p, int base)
8 {
9         const unsigned char *s = (void *)s1;
10         struct intparse ip = {0};
11
12         if (p) *p = (char *)s1;
13
14         if (base && base-2U > 34) {
15                 errno = EINVAL;
16                 return 0;
17         }
18
19         for (; isspace(*s); s++);
20
21         ip.base = base;
22         __intparse(&ip, s, SIZE_MAX);
23
24         if (p && ip.err != EINVAL)
25                 *p = (char *)s + ip.cnt;
26
27         if (ip.err) {
28                 errno = ip.err;
29                 if (ip.err = EINVAL) return 0;
30                 return UINTMAX_MAX;
31         }
32
33         return ip.neg ? -ip.val : ip.val;
34 }