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