1*63eb84d1Schristos /* Convert string representation of a number into an integer value.
2*63eb84d1Schristos 
3*63eb84d1Schristos    Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2003, 2005, 2006
4*63eb84d1Schristos    Free Software Foundation, Inc.
5*63eb84d1Schristos 
6*63eb84d1Schristos    NOTE: The canonical source of this file is maintained with the GNU C
7*63eb84d1Schristos    Library.  Bugs can be reported to bug-glibc@gnu.org.
8*63eb84d1Schristos 
9*63eb84d1Schristos    This program is free software; you can redistribute it and/or modify it
10*63eb84d1Schristos    under the terms of the GNU General Public License as published by the
11*63eb84d1Schristos    Free Software Foundation; either version 2, or (at your option) any
12*63eb84d1Schristos    later version.
13*63eb84d1Schristos 
14*63eb84d1Schristos    This program is distributed in the hope that it will be useful,
15*63eb84d1Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
16*63eb84d1Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17*63eb84d1Schristos    GNU General Public License for more details.
18*63eb84d1Schristos 
19*63eb84d1Schristos    You should have received a copy of the GNU General Public License
20*63eb84d1Schristos    along with this program; if not, write to the Free Software Foundation,
21*63eb84d1Schristos    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
22*63eb84d1Schristos 
23*63eb84d1Schristos #ifdef _LIBC
24*63eb84d1Schristos # define USE_NUMBER_GROUPING
25*63eb84d1Schristos #else
26*63eb84d1Schristos # include <config.h>
27*63eb84d1Schristos #endif
28*63eb84d1Schristos 
29*63eb84d1Schristos #include <ctype.h>
30*63eb84d1Schristos #include <errno.h>
31*63eb84d1Schristos #ifndef __set_errno
32*63eb84d1Schristos # define __set_errno(Val) errno = (Val)
33*63eb84d1Schristos #endif
34*63eb84d1Schristos 
35*63eb84d1Schristos #include <limits.h>
36*63eb84d1Schristos #include <stddef.h>
37*63eb84d1Schristos #include <stdlib.h>
38*63eb84d1Schristos #include <string.h>
39*63eb84d1Schristos 
40*63eb84d1Schristos #ifdef USE_NUMBER_GROUPING
41*63eb84d1Schristos # include "../locale/localeinfo.h"
42*63eb84d1Schristos #endif
43*63eb84d1Schristos 
44*63eb84d1Schristos /* Nonzero if we are defining `strtoul' or `strtoull', operating on
45*63eb84d1Schristos    unsigned integers.  */
46*63eb84d1Schristos #ifndef UNSIGNED
47*63eb84d1Schristos # define UNSIGNED 0
48*63eb84d1Schristos # define INT LONG int
49*63eb84d1Schristos #else
50*63eb84d1Schristos # define INT unsigned LONG int
51*63eb84d1Schristos #endif
52*63eb84d1Schristos 
53*63eb84d1Schristos /* Determine the name.  */
54*63eb84d1Schristos #ifdef USE_IN_EXTENDED_LOCALE_MODEL
55*63eb84d1Schristos # if UNSIGNED
56*63eb84d1Schristos #  ifdef USE_WIDE_CHAR
57*63eb84d1Schristos #   ifdef QUAD
58*63eb84d1Schristos #    define strtol __wcstoull_l
59*63eb84d1Schristos #   else
60*63eb84d1Schristos #    define strtol __wcstoul_l
61*63eb84d1Schristos #   endif
62*63eb84d1Schristos #  else
63*63eb84d1Schristos #   ifdef QUAD
64*63eb84d1Schristos #    define strtol __strtoull_l
65*63eb84d1Schristos #   else
66*63eb84d1Schristos #    define strtol __strtoul_l
67*63eb84d1Schristos #   endif
68*63eb84d1Schristos #  endif
69*63eb84d1Schristos # else
70*63eb84d1Schristos #  ifdef USE_WIDE_CHAR
71*63eb84d1Schristos #   ifdef QUAD
72*63eb84d1Schristos #    define strtol __wcstoll_l
73*63eb84d1Schristos #   else
74*63eb84d1Schristos #    define strtol __wcstol_l
75*63eb84d1Schristos #   endif
76*63eb84d1Schristos #  else
77*63eb84d1Schristos #   ifdef QUAD
78*63eb84d1Schristos #    define strtol __strtoll_l
79*63eb84d1Schristos #   else
80*63eb84d1Schristos #    define strtol __strtol_l
81*63eb84d1Schristos #   endif
82*63eb84d1Schristos #  endif
83*63eb84d1Schristos # endif
84*63eb84d1Schristos #else
85*63eb84d1Schristos # if UNSIGNED
86*63eb84d1Schristos #  ifdef USE_WIDE_CHAR
87*63eb84d1Schristos #   ifdef QUAD
88*63eb84d1Schristos #    define strtol wcstoull
89*63eb84d1Schristos #   else
90*63eb84d1Schristos #    define strtol wcstoul
91*63eb84d1Schristos #   endif
92*63eb84d1Schristos #  else
93*63eb84d1Schristos #   ifdef QUAD
94*63eb84d1Schristos #    define strtol strtoull
95*63eb84d1Schristos #   else
96*63eb84d1Schristos #    define strtol strtoul
97*63eb84d1Schristos #   endif
98*63eb84d1Schristos #  endif
99*63eb84d1Schristos # else
100*63eb84d1Schristos #  ifdef USE_WIDE_CHAR
101*63eb84d1Schristos #   ifdef QUAD
102*63eb84d1Schristos #    define strtol wcstoll
103*63eb84d1Schristos #   else
104*63eb84d1Schristos #    define strtol wcstol
105*63eb84d1Schristos #   endif
106*63eb84d1Schristos #  else
107*63eb84d1Schristos #   ifdef QUAD
108*63eb84d1Schristos #    define strtol strtoll
109*63eb84d1Schristos #   endif
110*63eb84d1Schristos #  endif
111*63eb84d1Schristos # endif
112*63eb84d1Schristos #endif
113*63eb84d1Schristos 
114*63eb84d1Schristos /* If QUAD is defined, we are defining `strtoll' or `strtoull',
115*63eb84d1Schristos    operating on `long long int's.  */
116*63eb84d1Schristos #ifdef QUAD
117*63eb84d1Schristos # define LONG long long
118*63eb84d1Schristos # define STRTOL_LONG_MIN LONG_LONG_MIN
119*63eb84d1Schristos # define STRTOL_LONG_MAX LONG_LONG_MAX
120*63eb84d1Schristos # define STRTOL_ULONG_MAX ULONG_LONG_MAX
121*63eb84d1Schristos 
122*63eb84d1Schristos /* The extra casts in the following macros work around compiler bugs,
123*63eb84d1Schristos    e.g., in Cray C 5.0.3.0.  */
124*63eb84d1Schristos 
125*63eb84d1Schristos /* True if negative values of the signed integer type T use two's
126*63eb84d1Schristos    complement, ones' complement, or signed magnitude representation,
127*63eb84d1Schristos    respectively.  Much GNU code assumes two's complement, but some
128*63eb84d1Schristos    people like to be portable to all possible C hosts.  */
129*63eb84d1Schristos # define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
130*63eb84d1Schristos # define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
131*63eb84d1Schristos # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
132*63eb84d1Schristos 
133*63eb84d1Schristos /* True if the arithmetic type T is signed.  */
134*63eb84d1Schristos # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
135*63eb84d1Schristos 
136*63eb84d1Schristos /* The maximum and minimum values for the integer type T.  These
137*63eb84d1Schristos    macros have undefined behavior if T is signed and has padding bits.
138*63eb84d1Schristos    If this is a problem for you, please let us know how to fix it for
139*63eb84d1Schristos    your host.  */
140*63eb84d1Schristos # define TYPE_MINIMUM(t) \
141*63eb84d1Schristos    ((t) (! TYPE_SIGNED (t) \
142*63eb84d1Schristos 	 ? (t) 0 \
143*63eb84d1Schristos 	 : TYPE_SIGNED_MAGNITUDE (t) \
144*63eb84d1Schristos 	 ? ~ (t) 0 \
145*63eb84d1Schristos 	 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
146*63eb84d1Schristos # define TYPE_MAXIMUM(t) \
147*63eb84d1Schristos    ((t) (! TYPE_SIGNED (t) \
148*63eb84d1Schristos 	 ? (t) -1 \
149*63eb84d1Schristos 	 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
150*63eb84d1Schristos 
151*63eb84d1Schristos # ifndef ULONG_LONG_MAX
152*63eb84d1Schristos #  define ULONG_LONG_MAX TYPE_MAXIMUM (unsigned long long)
153*63eb84d1Schristos # endif
154*63eb84d1Schristos # ifndef LONG_LONG_MAX
155*63eb84d1Schristos #  define LONG_LONG_MAX TYPE_MAXIMUM (long long int)
156*63eb84d1Schristos # endif
157*63eb84d1Schristos # ifndef LONG_LONG_MIN
158*63eb84d1Schristos #  define LONG_LONG_MIN TYPE_MINIMUM (long long int)
159*63eb84d1Schristos # endif
160*63eb84d1Schristos 
161*63eb84d1Schristos # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
162*63eb84d1Schristos    /* Work around gcc bug with using this constant.  */
163*63eb84d1Schristos    static const unsigned long long int maxquad = ULONG_LONG_MAX;
164*63eb84d1Schristos #  undef STRTOL_ULONG_MAX
165*63eb84d1Schristos #  define STRTOL_ULONG_MAX maxquad
166*63eb84d1Schristos # endif
167*63eb84d1Schristos #else
168*63eb84d1Schristos # define LONG long
169*63eb84d1Schristos # define STRTOL_LONG_MIN LONG_MIN
170*63eb84d1Schristos # define STRTOL_LONG_MAX LONG_MAX
171*63eb84d1Schristos # define STRTOL_ULONG_MAX ULONG_MAX
172*63eb84d1Schristos #endif
173*63eb84d1Schristos 
174*63eb84d1Schristos 
175*63eb84d1Schristos /* We use this code also for the extended locale handling where the
176*63eb84d1Schristos    function gets as an additional argument the locale which has to be
177*63eb84d1Schristos    used.  To access the values we have to redefine the _NL_CURRENT
178*63eb84d1Schristos    macro.  */
179*63eb84d1Schristos #ifdef USE_IN_EXTENDED_LOCALE_MODEL
180*63eb84d1Schristos # undef _NL_CURRENT
181*63eb84d1Schristos # define _NL_CURRENT(category, item) \
182*63eb84d1Schristos   (current->values[_NL_ITEM_INDEX (item)].string)
183*63eb84d1Schristos # define LOCALE_PARAM , loc
184*63eb84d1Schristos # define LOCALE_PARAM_PROTO , __locale_t loc
185*63eb84d1Schristos #else
186*63eb84d1Schristos # define LOCALE_PARAM
187*63eb84d1Schristos # define LOCALE_PARAM_PROTO
188*63eb84d1Schristos #endif
189*63eb84d1Schristos 
190*63eb84d1Schristos #if defined _LIBC || defined HAVE_WCHAR_H
191*63eb84d1Schristos # include <wchar.h>
192*63eb84d1Schristos #endif
193*63eb84d1Schristos 
194*63eb84d1Schristos #ifdef USE_WIDE_CHAR
195*63eb84d1Schristos # include <wctype.h>
196*63eb84d1Schristos # define L_(Ch) L##Ch
197*63eb84d1Schristos # define UCHAR_TYPE wint_t
198*63eb84d1Schristos # define STRING_TYPE wchar_t
199*63eb84d1Schristos # ifdef USE_IN_EXTENDED_LOCALE_MODEL
200*63eb84d1Schristos #  define ISSPACE(Ch) __iswspace_l ((Ch), loc)
201*63eb84d1Schristos #  define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
202*63eb84d1Schristos #  define TOUPPER(Ch) __towupper_l ((Ch), loc)
203*63eb84d1Schristos # else
204*63eb84d1Schristos #  define ISSPACE(Ch) iswspace (Ch)
205*63eb84d1Schristos #  define ISALPHA(Ch) iswalpha (Ch)
206*63eb84d1Schristos #  define TOUPPER(Ch) towupper (Ch)
207*63eb84d1Schristos # endif
208*63eb84d1Schristos #else
209*63eb84d1Schristos # define L_(Ch) Ch
210*63eb84d1Schristos # define UCHAR_TYPE unsigned char
211*63eb84d1Schristos # define STRING_TYPE char
212*63eb84d1Schristos # ifdef USE_IN_EXTENDED_LOCALE_MODEL
213*63eb84d1Schristos #  define ISSPACE(Ch) __isspace_l ((Ch), loc)
214*63eb84d1Schristos #  define ISALPHA(Ch) __isalpha_l ((Ch), loc)
215*63eb84d1Schristos #  define TOUPPER(Ch) __toupper_l ((Ch), loc)
216*63eb84d1Schristos # else
217*63eb84d1Schristos #  define ISSPACE(Ch) isspace (Ch)
218*63eb84d1Schristos #  define ISALPHA(Ch) isalpha (Ch)
219*63eb84d1Schristos #  define TOUPPER(Ch) toupper (Ch)
220*63eb84d1Schristos # endif
221*63eb84d1Schristos #endif
222*63eb84d1Schristos 
223*63eb84d1Schristos #define INTERNAL(X) INTERNAL1(X)
224*63eb84d1Schristos #define INTERNAL1(X) __##X##_internal
225*63eb84d1Schristos #define WEAKNAME(X) WEAKNAME1(X)
226*63eb84d1Schristos 
227*63eb84d1Schristos #ifdef USE_NUMBER_GROUPING
228*63eb84d1Schristos /* This file defines a function to check for correct grouping.  */
229*63eb84d1Schristos # include "grouping.h"
230*63eb84d1Schristos #endif
231*63eb84d1Schristos 
232*63eb84d1Schristos 
233*63eb84d1Schristos 
234*63eb84d1Schristos /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
235*63eb84d1Schristos    If BASE is 0 the base is determined by the presence of a leading
236*63eb84d1Schristos    zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
237*63eb84d1Schristos    If BASE is < 2 or > 36, it is reset to 10.
238*63eb84d1Schristos    If ENDPTR is not NULL, a pointer to the character after the last
239*63eb84d1Schristos    one converted is stored in *ENDPTR.  */
240*63eb84d1Schristos 
241*63eb84d1Schristos INT
INTERNAL(strtol)242*63eb84d1Schristos INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
243*63eb84d1Schristos 		   int base, int group LOCALE_PARAM_PROTO)
244*63eb84d1Schristos {
245*63eb84d1Schristos   int negative;
246*63eb84d1Schristos   register unsigned LONG int cutoff;
247*63eb84d1Schristos   register unsigned int cutlim;
248*63eb84d1Schristos   register unsigned LONG int i;
249*63eb84d1Schristos   register const STRING_TYPE *s;
250*63eb84d1Schristos   register UCHAR_TYPE c;
251*63eb84d1Schristos   const STRING_TYPE *save, *end;
252*63eb84d1Schristos   int overflow;
253*63eb84d1Schristos 
254*63eb84d1Schristos #ifdef USE_NUMBER_GROUPING
255*63eb84d1Schristos # ifdef USE_IN_EXTENDED_LOCALE_MODEL
256*63eb84d1Schristos   struct locale_data *current = loc->__locales[LC_NUMERIC];
257*63eb84d1Schristos # endif
258*63eb84d1Schristos   /* The thousands character of the current locale.  */
259*63eb84d1Schristos   wchar_t thousands = L'\0';
260*63eb84d1Schristos   /* The numeric grouping specification of the current locale,
261*63eb84d1Schristos      in the format described in <locale.h>.  */
262*63eb84d1Schristos   const char *grouping;
263*63eb84d1Schristos 
264*63eb84d1Schristos   if (group)
265*63eb84d1Schristos     {
266*63eb84d1Schristos       grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
267*63eb84d1Schristos       if (*grouping <= 0 || *grouping == CHAR_MAX)
268*63eb84d1Schristos 	grouping = NULL;
269*63eb84d1Schristos       else
270*63eb84d1Schristos 	{
271*63eb84d1Schristos 	  /* Figure out the thousands separator character.  */
272*63eb84d1Schristos # if defined _LIBC || defined _HAVE_BTOWC
273*63eb84d1Schristos 	  thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
274*63eb84d1Schristos 	  if (thousands == WEOF)
275*63eb84d1Schristos 	    thousands = L'\0';
276*63eb84d1Schristos # endif
277*63eb84d1Schristos 	  if (thousands == L'\0')
278*63eb84d1Schristos 	    grouping = NULL;
279*63eb84d1Schristos 	}
280*63eb84d1Schristos     }
281*63eb84d1Schristos   else
282*63eb84d1Schristos     grouping = NULL;
283*63eb84d1Schristos #endif
284*63eb84d1Schristos 
285*63eb84d1Schristos   if (base < 0 || base == 1 || base > 36)
286*63eb84d1Schristos     {
287*63eb84d1Schristos       __set_errno (EINVAL);
288*63eb84d1Schristos       return 0;
289*63eb84d1Schristos     }
290*63eb84d1Schristos 
291*63eb84d1Schristos   save = s = nptr;
292*63eb84d1Schristos 
293*63eb84d1Schristos   /* Skip white space.  */
294*63eb84d1Schristos   while (ISSPACE (*s))
295*63eb84d1Schristos     ++s;
296*63eb84d1Schristos   if (*s == L_('\0'))
297*63eb84d1Schristos     goto noconv;
298*63eb84d1Schristos 
299*63eb84d1Schristos   /* Check for a sign.  */
300*63eb84d1Schristos   if (*s == L_('-'))
301*63eb84d1Schristos     {
302*63eb84d1Schristos       negative = 1;
303*63eb84d1Schristos       ++s;
304*63eb84d1Schristos     }
305*63eb84d1Schristos   else if (*s == L_('+'))
306*63eb84d1Schristos     {
307*63eb84d1Schristos       negative = 0;
308*63eb84d1Schristos       ++s;
309*63eb84d1Schristos     }
310*63eb84d1Schristos   else
311*63eb84d1Schristos     negative = 0;
312*63eb84d1Schristos 
313*63eb84d1Schristos   /* Recognize number prefix and if BASE is zero, figure it out ourselves.  */
314*63eb84d1Schristos   if (*s == L_('0'))
315*63eb84d1Schristos     {
316*63eb84d1Schristos       if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
317*63eb84d1Schristos 	{
318*63eb84d1Schristos 	  s += 2;
319*63eb84d1Schristos 	  base = 16;
320*63eb84d1Schristos 	}
321*63eb84d1Schristos       else if (base == 0)
322*63eb84d1Schristos 	base = 8;
323*63eb84d1Schristos     }
324*63eb84d1Schristos   else if (base == 0)
325*63eb84d1Schristos     base = 10;
326*63eb84d1Schristos 
327*63eb84d1Schristos   /* Save the pointer so we can check later if anything happened.  */
328*63eb84d1Schristos   save = s;
329*63eb84d1Schristos 
330*63eb84d1Schristos #ifdef USE_NUMBER_GROUPING
331*63eb84d1Schristos   if (group)
332*63eb84d1Schristos     {
333*63eb84d1Schristos       /* Find the end of the digit string and check its grouping.  */
334*63eb84d1Schristos       end = s;
335*63eb84d1Schristos       for (c = *end; c != L_('\0'); c = *++end)
336*63eb84d1Schristos 	if ((wchar_t) c != thousands
337*63eb84d1Schristos 	    && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
338*63eb84d1Schristos 	    && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
339*63eb84d1Schristos 	  break;
340*63eb84d1Schristos       if (*s == thousands)
341*63eb84d1Schristos 	end = s;
342*63eb84d1Schristos       else
343*63eb84d1Schristos 	end = correctly_grouped_prefix (s, end, thousands, grouping);
344*63eb84d1Schristos     }
345*63eb84d1Schristos   else
346*63eb84d1Schristos #endif
347*63eb84d1Schristos     end = NULL;
348*63eb84d1Schristos 
349*63eb84d1Schristos   cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
350*63eb84d1Schristos   cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
351*63eb84d1Schristos 
352*63eb84d1Schristos   overflow = 0;
353*63eb84d1Schristos   i = 0;
354*63eb84d1Schristos   for (c = *s; c != L_('\0'); c = *++s)
355*63eb84d1Schristos     {
356*63eb84d1Schristos       if (s == end)
357*63eb84d1Schristos 	break;
358*63eb84d1Schristos       if (c >= L_('0') && c <= L_('9'))
359*63eb84d1Schristos 	c -= L_('0');
360*63eb84d1Schristos       else if (ISALPHA (c))
361*63eb84d1Schristos 	c = TOUPPER (c) - L_('A') + 10;
362*63eb84d1Schristos       else
363*63eb84d1Schristos 	break;
364*63eb84d1Schristos       if ((int) c >= base)
365*63eb84d1Schristos 	break;
366*63eb84d1Schristos       /* Check for overflow.  */
367*63eb84d1Schristos       if (i > cutoff || (i == cutoff && c > cutlim))
368*63eb84d1Schristos 	overflow = 1;
369*63eb84d1Schristos       else
370*63eb84d1Schristos 	{
371*63eb84d1Schristos 	  i *= (unsigned LONG int) base;
372*63eb84d1Schristos 	  i += c;
373*63eb84d1Schristos 	}
374*63eb84d1Schristos     }
375*63eb84d1Schristos 
376*63eb84d1Schristos   /* Check if anything actually happened.  */
377*63eb84d1Schristos   if (s == save)
378*63eb84d1Schristos     goto noconv;
379*63eb84d1Schristos 
380*63eb84d1Schristos   /* Store in ENDPTR the address of one character
381*63eb84d1Schristos      past the last character we converted.  */
382*63eb84d1Schristos   if (endptr != NULL)
383*63eb84d1Schristos     *endptr = (STRING_TYPE *) s;
384*63eb84d1Schristos 
385*63eb84d1Schristos #if !UNSIGNED
386*63eb84d1Schristos   /* Check for a value that is within the range of
387*63eb84d1Schristos      `unsigned LONG int', but outside the range of `LONG int'.  */
388*63eb84d1Schristos   if (overflow == 0
389*63eb84d1Schristos       && i > (negative
390*63eb84d1Schristos 	      ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
391*63eb84d1Schristos 	      : (unsigned LONG int) STRTOL_LONG_MAX))
392*63eb84d1Schristos     overflow = 1;
393*63eb84d1Schristos #endif
394*63eb84d1Schristos 
395*63eb84d1Schristos   if (overflow)
396*63eb84d1Schristos     {
397*63eb84d1Schristos       __set_errno (ERANGE);
398*63eb84d1Schristos #if UNSIGNED
399*63eb84d1Schristos       return STRTOL_ULONG_MAX;
400*63eb84d1Schristos #else
401*63eb84d1Schristos       return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
402*63eb84d1Schristos #endif
403*63eb84d1Schristos     }
404*63eb84d1Schristos 
405*63eb84d1Schristos   /* Return the result of the appropriate sign.  */
406*63eb84d1Schristos   return negative ? -i : i;
407*63eb84d1Schristos 
408*63eb84d1Schristos noconv:
409*63eb84d1Schristos   /* We must handle a special case here: the base is 0 or 16 and the
410*63eb84d1Schristos      first two characters are '0' and 'x', but the rest are no
411*63eb84d1Schristos      hexadecimal digits.  This is no error case.  We return 0 and
412*63eb84d1Schristos      ENDPTR points to the `x`.  */
413*63eb84d1Schristos   if (endptr != NULL)
414*63eb84d1Schristos     {
415*63eb84d1Schristos       if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
416*63eb84d1Schristos 	  && save[-2] == L_('0'))
417*63eb84d1Schristos 	*endptr = (STRING_TYPE *) &save[-1];
418*63eb84d1Schristos       else
419*63eb84d1Schristos 	/*  There was no number to convert.  */
420*63eb84d1Schristos 	*endptr = (STRING_TYPE *) nptr;
421*63eb84d1Schristos     }
422*63eb84d1Schristos 
423*63eb84d1Schristos   return 0L;
424*63eb84d1Schristos }
425*63eb84d1Schristos 
426*63eb84d1Schristos /* External user entry point.  */
427*63eb84d1Schristos 
428*63eb84d1Schristos 
429*63eb84d1Schristos INT
430*63eb84d1Schristos #ifdef weak_function
431*63eb84d1Schristos weak_function
432*63eb84d1Schristos #endif
strtol(const STRING_TYPE * nptr,STRING_TYPE ** endptr,int base LOCALE_PARAM_PROTO)433*63eb84d1Schristos strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
434*63eb84d1Schristos 	int base LOCALE_PARAM_PROTO)
435*63eb84d1Schristos {
436*63eb84d1Schristos   return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);
437*63eb84d1Schristos }
438