| 124 | } |
| 125 | |
| 126 | uintmax_t |
| 127 | malloc_strtoumax(const char *restrict nptr, char **restrict endptr, int base) { |
| 128 | uintmax_t ret, digit; |
| 129 | unsigned b; |
| 130 | bool neg; |
| 131 | const char *p, *ns; |
| 132 | |
| 133 | p = nptr; |
| 134 | if (base < 0 || base == 1 || base > 36) { |
| 135 | ns = p; |
| 136 | set_errno(EINVAL); |
| 137 | ret = UINTMAX_MAX; |
| 138 | goto label_return; |
| 139 | } |
| 140 | b = base; |
| 141 | |
| 142 | /* Swallow leading whitespace and get sign, if any. */ |
| 143 | neg = false; |
| 144 | while (true) { |
| 145 | switch (*p) { |
| 146 | case '\t': case '\n': case '\v': case '\f': case '\r': case ' ': |
| 147 | p++; |
| 148 | break; |
| 149 | case '-': |
| 150 | neg = true; |
| 151 | /* Fall through. */ |
| 152 | case '+': |
| 153 | p++; |
| 154 | /* Fall through. */ |
| 155 | default: |
| 156 | goto label_prefix; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* Get prefix, if any. */ |
| 161 | label_prefix: |
| 162 | /* |
| 163 | * Note where the first non-whitespace/sign character is so that it is |
| 164 | * possible to tell whether any digits are consumed (e.g., " 0" vs. |
| 165 | * " -x"). |
| 166 | */ |
| 167 | ns = p; |
| 168 | if (*p == '0') { |
| 169 | switch (p[1]) { |
| 170 | case '0': case '1': case '2': case '3': case '4': case '5': |
| 171 | case '6': case '7': |
| 172 | if (b == 0) { |
| 173 | b = 8; |
| 174 | } |
| 175 | if (b == 8) { |
| 176 | p++; |
| 177 | } |
| 178 | break; |
| 179 | case 'X': case 'x': |
| 180 | switch (p[2]) { |
| 181 | case '0': case '1': case '2': case '3': case '4': |
| 182 | case '5': case '6': case '7': case '8': case '9': |
| 183 | case 'A': case 'B': case 'C': case 'D': case 'E': |