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