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