- int negative;
- register unsigned LONG int cutoff;
- register unsigned int cutlim;
- register unsigned LONG int i;
- register const STRING_TYPE *s;
- register UCHAR_TYPE c;
- const STRING_TYPE *save, *end;
- int overflow;
-#ifndef USE_WIDE_CHAR
- size_t cnt;
-#endif
-
-#ifdef USE_NUMBER_GROUPING
-# ifdef USE_IN_EXTENDED_LOCALE_MODEL
- struct locale_data *current = loc->__locales[LC_NUMERIC];
-# endif
- /* The thousands character of the current locale. */
-# ifdef USE_WIDE_CHAR
- wchar_t thousands = L'\0';
-# else
- const char *thousands = NULL;
- size_t thousands_len = 0;
-# endif
- /* The numeric grouping specification of the current locale,
- in the format described in <locale.h>. */
- const char *grouping;
-
- if (__builtin_expect (group, 0))
- {
- grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
- if (*grouping <= 0 || *grouping == CHAR_MAX)
- grouping = NULL;
- else
- {
- /* Figure out the thousands separator character. */
-# ifdef USE_WIDE_CHAR
-# ifdef _LIBC
- thousands = _NL_CURRENT_WORD (LC_NUMERIC,
- _NL_NUMERIC_THOUSANDS_SEP_WC);
-# endif
- if (thousands == L'\0')
- grouping = NULL;
-# else
-# ifdef _LIBC
- thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
-# endif
- if (*thousands == '\0')
- {
- thousands = NULL;
- grouping = NULL;
- }
-# endif
- }
- }
- else
- grouping = NULL;
-#endif
-
- if (base < 0 || base == 1 || base > 36)
- {
- __set_errno (EINVAL);
- return 0;
- }
-
- save = s = nptr;
-
- /* Skip white space. */
- while (ISSPACE (*s))
- ++s;
- if (__builtin_expect (*s == L_('\0'), 0))
- goto noconv;
-
- /* Check for a sign. */
- negative = 0;
- if (*s == L_('-'))
- {
- negative = 1;
- ++s;
- }
- else if (*s == L_('+'))
- ++s;
-
- /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
- if (*s == L_('0'))
- {
- if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
- {
- s += 2;
- base = 16;
- }
- else if (base == 0)
- base = 8;
- }
- else if (base == 0)
- base = 10;
-
- /* Save the pointer so we can check later if anything happened. */
- save = s;
-
-#ifdef USE_NUMBER_GROUPING
- if (base != 10)
- grouping = NULL;
-
- if (__builtin_expect (grouping != NULL, 0))
- {
-# ifndef USE_WIDE_CHAR
- thousands_len = strlen (thousands);
-# endif
-
- /* Find the end of the digit string and check its grouping. */
- end = s;
- if (
-# ifdef USE_WIDE_CHAR
- *s != thousands
-# else
- ({ for (cnt = 0; cnt < thousands_len; ++cnt)
- if (thousands[cnt] != end[cnt])
- break;
- cnt < thousands_len; })
-# endif
- )
- {
- for (c = *end; c != L_('\0'); c = *++end)
- if (((STRING_TYPE) c < L_('0') || (STRING_TYPE) c > L_('9'))
-# ifdef USE_WIDE_CHAR
- && (wchar_t) c != thousands
-# else
- && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
- if (thousands[cnt] != end[cnt])
- break;
- cnt < thousands_len; })
-# endif
- && (!ISALPHA (c)
- || (int) (TOUPPER (c) - L_('A') + 10) >= base))
- break;
-
-# ifdef USE_WIDE_CHAR
- end = __correctly_grouped_prefixwc (s, end, thousands, grouping);
-# else
- end = __correctly_grouped_prefixmb (s, end, thousands, grouping);
-# endif
- }
- }
- else
-#endif
- end = NULL;
-
- cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
- cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
-
- overflow = 0;
- i = 0;
- c = *s;
- if (sizeof (long int) != sizeof (LONG int))
- {
- unsigned long int j = 0;
- unsigned long int jmax = ULONG_MAX / base;
-
- for (;c != L_('\0'); c = *++s)
- {
- if (s == end)
- break;
- if (c >= L_('0') && c <= L_('9'))
- c -= L_('0');
-#ifdef USE_NUMBER_GROUPING
-# ifdef USE_WIDE_CHAR
- else if (grouping && (wchar_t) c == thousands)
- continue;
-# else
- else if (thousands_len)
- {
- for (cnt = 0; cnt < thousands_len; ++cnt)
- if (thousands[cnt] != s[cnt])
- break;
- if (cnt == thousands_len)
- {
- s += thousands_len - 1;
- continue;
- }
- if (ISALPHA (c))
- c = TOUPPER (c) - L_('A') + 10;
- else
- break;
- }
-# endif
-#endif
- else if (ISALPHA (c))
- c = TOUPPER (c) - L_('A') + 10;
- else
- break;
- if ((int) c >= base)
- break;
- /* Note that we never can have an overflow. */
- else if (j >= jmax)
- {
- /* We have an overflow. Now use the long representation. */
- i = (unsigned LONG int) j;
- goto use_long;
- }
- else
- j = j * (unsigned long int) base + c;
- }
-
- i = (unsigned LONG int) j;
- }
- else
- for (;c != L_('\0'); c = *++s)
- {
- if (s == end)
- break;
- if (c >= L_('0') && c <= L_('9'))
- c -= L_('0');
-#ifdef USE_NUMBER_GROUPING
-# ifdef USE_WIDE_CHAR
- else if (grouping && (wchar_t) c == thousands)
- continue;
-# else
- else if (thousands_len)
- {
- for (cnt = 0; cnt < thousands_len; ++cnt)
- if (thousands[cnt] != s[cnt])
- break;
- if (cnt == thousands_len)
- {
- s += thousands_len - 1;
- continue;
- }
- if (ISALPHA (c))
- c = TOUPPER (c) - L_('A') + 10;
- else
- break;
- }
-# endif
-#endif
- else if (ISALPHA (c))
- c = TOUPPER (c) - L_('A') + 10;
- else
- break;
- if ((int) c >= base)
- break;
- /* Check for overflow. */
- if (i > cutoff || (i == cutoff && c > cutlim))
- overflow = 1;
- else
- {
- use_long:
- i *= (unsigned LONG int) base;
- i += c;
- }
- }
-
- /* Check if anything actually happened. */
- if (s == save)
- goto noconv;
-
- /* Store in ENDPTR the address of one character
- past the last character we converted. */
- if (endptr != NULL)
- *endptr = (STRING_TYPE *) s;
-
-#if !UNSIGNED
- /* Check for a value that is within the range of
- `unsigned LONG int', but outside the range of `LONG int'. */
- if (overflow == 0
- && i > (negative
- ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
- : (unsigned LONG int) STRTOL_LONG_MAX))
- overflow = 1;
-#endif
-
- if (__builtin_expect (overflow, 0))
- {
- __set_errno (ERANGE);
-#if UNSIGNED
- return STRTOL_ULONG_MAX;
-#else
- return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
-#endif
- }
-
- /* Return the result of the appropriate sign. */
- return negative ? -i : i;
-
-noconv:
- /* We must handle a special case here: the base is 0 or 16 and the
- first two characters are '0' and 'x', but the rest are no
- hexadecimal digits. This is no error case. We return 0 and
- ENDPTR points to the `x`. */
- if (endptr != NULL)
- {
- if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
- && save[-2] == L_('0'))
- *endptr = (STRING_TYPE *) &save[-1];
- else
- /* There was no number to convert. */
- *endptr = (STRING_TYPE *) nptr;
- }
-
- return 0L;