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