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