1#include <ctype.h>2#include <errno.h>3#include <inttypes.h>4#include <limits.h>5#include <string.h>67#include "../libc.h"89#undef strtoumax1011uintmax_t12strtoumax(const char * restrict ptr, char ** restrict end, int base)13{14 int d, sign = 1;15 uintmax_t n;16 char *t, *s = (char *) ptr;1718 if (end)19 *end = s;2021 while (isspace(*s))22 ++s;2324 switch (*s) {25 case '-':26 sign = -1;27 case '+':28 ++s;29 }3031 if (base == 0) {32 if (*s == '0')33 base = toupper(s[1]) == 'X' ? 16 : 8;34 else35 base = 10;36 }37 if (base == 16 && *s == '0' && toupper(s[1]) == 'X')38 s += 2;3940 n = 0;41 for (t = s; (d = _dtoi(*t)) < base; ++t) {42 if (n > UINTMAX_MAX/base)43 goto overflow;44 n *= base;45 if (d > UINTMAX_MAX - n)46 goto overflow;47 n += d;48 }4950 if (end && t != s)51 *end = t;5253 return n*sign;5455overflow:56 if (end)57 *end = t;58 errno = ERANGE;5960 return UINTMAX_MAX;61}