Thu May 9 01:24:00 1996 Ulrich Drepper <drepper@cygnus.com>
[glibc/history.git] / stdlib / strtol.c
blob2a2a8ea5e7e37fc110b2941a4f3fd94854d6c0c2
1 /* Copyright (C) 1991, 92, 94, 95, 96 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #ifdef _LIBC
24 # define USE_NUMBER_GROUPING
25 # define STDC_HEADERS
26 # define HAVE_LIMITS_H
27 #endif
29 #include <ctype.h>
30 #include <errno.h>
31 #ifndef errno
32 extern int errno;
33 #endif
35 #ifdef HAVE_LIMITS_H
36 # include <limits.h>
37 #endif
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
48 #ifdef USE_NUMBER_GROUPING
49 # include "../locale/localeinfo.h"
50 #endif
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
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
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 #endif
110 #ifdef USE_WIDE_CHAR
111 # include <wchar.h>
112 # include <wctype.h>
113 # define L_(ch) L##ch
114 # define UCHAR_TYPE wint_t
115 # define STRING_TYPE wchar_t
116 # define ISSPACE(ch) iswspace (ch)
117 # define ISALPHA(ch) iswalpha (ch)
118 # define TOUPPER(ch) towupper (ch)
119 #else
120 # define L_(ch) ch
121 # define UCHAR_TYPE unsigned char
122 # define STRING_TYPE char
123 # define ISSPACE(ch) isspace (ch)
124 # define ISALPHA(ch) isalpha (ch)
125 # define TOUPPER(ch) toupper (ch)
126 #endif
128 #ifdef __STDC__
129 # define INTERNAL(x) INTERNAL1(x)
130 # define INTERNAL1(x) __##x##_internal
131 #else
132 # define INTERNAL(x) __/**/x/**/_internal
133 #endif
135 #ifdef USE_NUMBER_GROUPING
136 /* This file defines a function to check for correct grouping. */
137 # include "grouping.h"
138 #endif
141 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
142 If BASE is 0 the base is determined by the presence of a leading
143 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
144 If BASE is < 2 or > 36, it is reset to 10.
145 If ENDPTR is not NULL, a pointer to the character after the last
146 one converted is stored in *ENDPTR. */
149 INTERNAL (strtol) (nptr, endptr, base, group)
150 const STRING_TYPE *nptr;
151 STRING_TYPE **endptr;
152 int base;
153 int group;
155 int negative;
156 register unsigned LONG int cutoff;
157 register unsigned int cutlim;
158 register unsigned LONG int i;
159 register const STRING_TYPE *s;
160 register UCHAR_TYPE c;
161 const STRING_TYPE *save, *end;
162 int overflow;
164 #ifdef USE_NUMBER_GROUPING
165 /* The thousands character of the current locale. */
166 wchar_t thousands;
167 /* The numeric grouping specification of the current locale,
168 in the format described in <locale.h>. */
169 const char *grouping;
171 if (group)
173 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
174 if (*grouping <= 0 || *grouping == CHAR_MAX)
175 grouping = NULL;
176 else
178 /* Figure out the thousands separator character. */
179 if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
180 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
181 thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
182 if (thousands == L'\0')
183 grouping = NULL;
186 else
187 grouping = NULL;
188 #endif
190 if (base < 0 || base == 1 || base > 36)
191 base = 10;
193 save = s = nptr;
195 /* Skip white space. */
196 while (ISSPACE (*s))
197 ++s;
198 if (*s == L_('\0'))
199 goto noconv;
201 /* Check for a sign. */
202 if (*s == L_('-'))
204 negative = 1;
205 ++s;
207 else if (*s == L_('+'))
209 negative = 0;
210 ++s;
212 else
213 negative = 0;
215 if (base == 16 && s[0] == L_('0') && TOUPPER (s[1]) == L_('X'))
216 s += 2;
218 /* If BASE is zero, figure it out ourselves. */
219 if (base == 0)
220 if (*s == L_('0'))
222 if (TOUPPER (s[1]) == L_('X'))
224 s += 2;
225 base = 16;
227 else
228 base = 8;
230 else
231 base = 10;
233 /* Save the pointer so we can check later if anything happened. */
234 save = s;
236 #ifdef USE_NUMBER_GROUPING
237 if (group)
239 /* Find the end of the digit string and check its grouping. */
240 end = s;
241 for (c = *end; c != L_('\0'); c = *++end)
242 if (c != thousands && (c < L_('0') || c > L_('9'))
243 && (!ISALPHA (c) || TOUPPER (c) - L_('A') + 10 >= base))
244 break;
245 if (*s == thousands)
246 end = s;
247 else
248 end = correctly_grouped_prefix (s, end, thousands, grouping);
250 else
251 #endif
252 end = NULL;
254 cutoff = ULONG_MAX / (unsigned LONG int) base;
255 cutlim = ULONG_MAX % (unsigned LONG int) base;
257 overflow = 0;
258 i = 0;
259 for (c = *s; c != L_('\0'); c = *++s)
261 if (s == end)
262 break;
263 if (c >= L_('0') && c <= L_('9'))
264 c -= L_('0');
265 else if (ISALPHA (c))
266 c = TOUPPER (c) - L_('A') + 10;
267 else
268 break;
269 if (c >= base)
270 break;
271 /* Check for overflow. */
272 if (i > cutoff || (i == cutoff && c > cutlim))
273 overflow = 1;
274 else
276 i *= (unsigned LONG int) base;
277 i += c;
281 /* Check if anything actually happened. */
282 if (s == save)
283 goto noconv;
285 /* Store in ENDPTR the address of one character
286 past the last character we converted. */
287 if (endptr != NULL)
288 *endptr = (STRING_TYPE *) s;
290 #if !UNSIGNED
291 /* Check for a value that is within the range of
292 `unsigned LONG int', but outside the range of `LONG int'. */
293 if (i > (negative ?
294 -(unsigned LONG int) LONG_MIN : (unsigned LONG int) LONG_MAX))
295 overflow = 1;
296 #endif
298 if (overflow)
300 errno = ERANGE;
301 #if UNSIGNED
302 return ULONG_MAX;
303 #else
304 return negative ? LONG_MIN : LONG_MAX;
305 #endif
308 /* Return the result of the appropriate sign. */
309 return (negative ? -i : i);
311 noconv:
312 /* We must handle a special case here: the base is 0 or 16 and the
313 first two characters and '0' and 'x', but the rest are no
314 hexadecimal digits. This is no error case. We return 0 and
315 ENDPTR points to the `x`. */
316 if (endptr != NULL)
317 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
318 && save[-2] == L_('0'))
319 *endptr = (STRING_TYPE *) &save[-1];
320 else
321 /* There was no number to convert. */
322 *endptr = (STRING_TYPE *) nptr;
324 return 0L;
327 /* External user entry point. */
330 strtol (nptr, endptr, base)
331 const STRING_TYPE *nptr;
332 STRING_TYPE **endptr;
333 int base;
335 return INTERNAL (strtol) (nptr, endptr, base, 0);
338 #ifdef weak_symbol
339 /* We need this indirection when `strtol' is defined as a macro
340 for one of the other names. */
341 #define weak1(x) weak_symbol(x)
342 weak1 (strtol)
343 #endif