dd92fb250cc3feec4739778a50f745be33c2d8f7
[platform/upstream/glibc.git] / stdlib / strtol.c
1 /* Copyright (C) 1991, 92, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB.  If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #ifdef _LIBC
24 # define USE_NUMBER_GROUPING
25 # define STDC_HEADERS
26 # define HAVE_LIMITS_H
27 #endif
28
29 #include <ctype.h>
30 #include <errno.h>
31 #ifndef errno
32 extern int errno;
33 #endif
34
35 #ifdef HAVE_LIMITS_H
36 # include <limits.h>
37 #endif
38
39 #ifdef STDC_HEADERS
40 # include <stddef.h>
41 # include <stdlib.h>
42 #else
43 # ifndef NULL
44 #  define NULL 0
45 # endif
46 #endif
47
48 #ifdef USE_NUMBER_GROUPING
49 # include "../locale/localeinfo.h"
50 #endif
51
52 /* Nonzero if we are defining `strtoul' or `strtouq', operating on
53    unsigned integers.  */
54 #ifndef UNSIGNED
55 # define UNSIGNED 0
56 # define INT LONG int
57 #else
58 # define INT unsigned LONG int
59 #endif
60
61 /* Determine the name.  */
62 #if UNSIGNED
63 # ifdef USE_WIDE_CHAR
64 #  ifdef QUAD
65 #   define strtol wcstouq
66 #  else
67 #   define strtol wcstoul
68 #  endif
69 # else
70 #  ifdef QUAD
71 #   define strtol strtouq
72 #  else
73 #   define strtol strtoul
74 #  endif
75 # endif
76 #else
77 # ifdef USE_WIDE_CHAR
78 #  ifdef QUAD
79 #   define strtol wcstoq
80 #  else
81 #   define strtol wcstol
82 #  endif
83 # else
84 #  ifdef QUAD
85 #   define strtol strtoq
86 #  endif
87 # endif
88 #endif
89
90 /* If QUAD is defined, we are defining `strtoq' or `strtouq',
91    operating on `long long int's.  */
92 #ifdef QUAD
93 # define LONG long long
94 # undef LONG_MIN
95 # define LONG_MIN LONG_LONG_MIN
96 # undef LONG_MAX
97 # define LONG_MAX LONG_LONG_MAX
98 # undef ULONG_MAX
99 # define ULONG_MAX ULONG_LONG_MAX
100 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
101    /* Work around gcc bug with using this constant.  */
102    static const unsigned long long int maxquad = ULONG_LONG_MAX;
103 #  undef ULONG_MAX
104 #  define ULONG_MAX maxquad
105 # endif
106 #else
107 # define LONG long
108
109 #ifndef ULONG_MAX
110 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
111 #endif
112 #ifndef LONG_MAX
113 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
114 #endif
115 #endif
116
117 #ifdef USE_WIDE_CHAR
118 # include <wchar.h>
119 # include <wctype.h>
120 # define L_(ch) L##ch
121 # define UCHAR_TYPE wint_t
122 # define STRING_TYPE wchar_t
123 # define ISSPACE(ch) iswspace (ch)
124 # define ISALPHA(ch) iswalpha (ch)
125 # define TOUPPER(ch) towupper (ch)
126 #else
127 # define L_(ch) ch
128 # define UCHAR_TYPE unsigned char
129 # define STRING_TYPE char
130 # define ISSPACE(ch) isspace (ch)
131 # define ISALPHA(ch) isalpha (ch)
132 # define TOUPPER(ch) toupper (ch)
133 #endif
134
135 #ifdef __STDC__
136 # define INTERNAL(x) INTERNAL1(x)
137 # define INTERNAL1(x) __##x##_internal
138 # define WEAKNAME(x) WEAKNAME1(x)
139 #else
140 # define INTERNAL(x) __/**/x/**/_internal
141 #endif
142
143 #ifdef USE_NUMBER_GROUPING
144 /* This file defines a function to check for correct grouping.  */
145 # include "grouping.h"
146 #endif
147
148
149 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
150    If BASE is 0 the base is determined by the presence of a leading
151    zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
152    If BASE is < 2 or > 36, it is reset to 10.
153    If ENDPTR is not NULL, a pointer to the character after the last
154    one converted is stored in *ENDPTR.  */
155
156 INT
157 INTERNAL (strtol) (nptr, endptr, base, group)
158      const STRING_TYPE *nptr;
159      STRING_TYPE **endptr;
160      int base;
161      int group;
162 {
163   int negative;
164   register unsigned LONG int cutoff;
165   register unsigned int cutlim;
166   register unsigned LONG int i;
167   register const STRING_TYPE *s;
168   register UCHAR_TYPE c;
169   const STRING_TYPE *save, *end;
170   int overflow;
171
172 #ifdef USE_NUMBER_GROUPING
173   /* The thousands character of the current locale.  */
174   wchar_t thousands;
175   /* The numeric grouping specification of the current locale,
176      in the format described in <locale.h>.  */
177   const char *grouping;
178
179   if (group)
180     {
181       grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
182       if (*grouping <= 0 || *grouping == CHAR_MAX)
183         grouping = NULL;
184       else
185         {
186           /* Figure out the thousands separator character.  */
187           if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
188                       strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
189             thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
190           if (thousands == L'\0')
191             grouping = NULL;
192         }
193     }
194   else
195     grouping = NULL;
196 #endif
197
198   if (base < 0 || base == 1 || base > 36)
199     base = 10;
200
201   save = s = nptr;
202
203   /* Skip white space.  */
204   while (ISSPACE (*s))
205     ++s;
206   if (*s == L_('\0'))
207     goto noconv;
208
209   /* Check for a sign.  */
210   if (*s == L_('-'))
211     {
212       negative = 1;
213       ++s;
214     }
215   else if (*s == L_('+'))
216     {
217       negative = 0;
218       ++s;
219     }
220   else
221     negative = 0;
222
223   if (base == 16 && s[0] == L_('0') && TOUPPER (s[1]) == L_('X'))
224     s += 2;
225
226   /* If BASE is zero, figure it out ourselves.  */
227   if (base == 0)
228     if (*s == L_('0'))
229       {
230         if (TOUPPER (s[1]) == L_('X'))
231           {
232             s += 2;
233             base = 16;
234           }
235         else
236           base = 8;
237       }
238     else
239       base = 10;
240
241   /* Save the pointer so we can check later if anything happened.  */
242   save = s;
243
244 #ifdef USE_NUMBER_GROUPING
245   if (group)
246     {
247       /* Find the end of the digit string and check its grouping.  */
248       end = s;
249       for (c = *end; c != L_('\0'); c = *++end)
250         if (c != thousands && (c < L_('0') || c > L_('9'))
251             && (!ISALPHA (c) || TOUPPER (c) - L_('A') + 10 >= base))
252           break;
253       if (*s == thousands)
254         end = s;
255       else
256         end = correctly_grouped_prefix (s, end, thousands, grouping);
257     }
258   else
259 #endif
260     end = NULL;
261
262   cutoff = ULONG_MAX / (unsigned LONG int) base;
263   cutlim = ULONG_MAX % (unsigned LONG int) base;
264
265   overflow = 0;
266   i = 0;
267   for (c = *s; c != L_('\0'); c = *++s)
268     {
269       if (s == end)
270         break;
271       if (c >= L_('0') && c <= L_('9'))
272         c -= L_('0');
273       else if (ISALPHA (c))
274         c = TOUPPER (c) - L_('A') + 10;
275       else
276         break;
277       if (c >= base)
278         break;
279       /* Check for overflow.  */
280       if (i > cutoff || (i == cutoff && c > cutlim))
281         overflow = 1;
282       else
283         {
284           i *= (unsigned LONG int) base;
285           i += c;
286         }
287     }
288
289   /* Check if anything actually happened.  */
290   if (s == save)
291     goto noconv;
292
293   /* Store in ENDPTR the address of one character
294      past the last character we converted.  */
295   if (endptr != NULL)
296     *endptr = (STRING_TYPE *) s;
297
298 #if !UNSIGNED
299   /* Check for a value that is within the range of
300      `unsigned LONG int', but outside the range of `LONG int'.  */
301   if (overflow == 0
302       && i > (negative
303               ? -((unsigned LONG int) (LONG_MIN + 1)) + 1
304               : (unsigned LONG int) LONG_MAX))
305     overflow = 1;
306 #endif
307
308   if (overflow)
309     {
310       errno = ERANGE;
311 #if UNSIGNED
312       return ULONG_MAX;
313 #else
314       return negative ? LONG_MIN : LONG_MAX;
315 #endif
316     }
317
318   /* Return the result of the appropriate sign.  */
319   return (negative ? -i : i);
320
321 noconv:
322   /* We must handle a special case here: the base is 0 or 16 and the
323      first two characters are '0' and 'x', but the rest are no
324      hexadecimal digits.  This is no error case.  We return 0 and
325      ENDPTR points to the `x`.  */
326   if (endptr != NULL)
327     if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
328         && save[-2] == L_('0'))
329       *endptr = (STRING_TYPE *) &save[-1];
330     else
331       /*  There was no number to convert.  */
332       *endptr = (STRING_TYPE *) nptr;
333
334   return 0L;
335 }
336 \f
337 /* External user entry point.  */
338 INT
339 strtol (nptr, endptr, base)
340      const STRING_TYPE *nptr;
341      STRING_TYPE **endptr;
342      int base;
343 {
344   return INTERNAL (strtol) (nptr, endptr, base, 0);
345 }
346 #ifdef weak_symbol
347 /* We need to weaken this symbol because some the the defined
348    functions do not come from ANSI.  The indirection is necessary
349    because `strtol' might be a macro.  */
350 #define weak_this(x) weak_symbol (x)
351 weak_this (strtol)
352 #endif