Remove *xattr syscalls.
[glibc.git] / sysdeps / generic / strtol.c
blob7e174d488b653bc8a45475e8452e4785155750d7
1 /* Convert string representation of a number into an integer value.
2 Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #if HAVE_CONFIG_H
22 # include <config.h>
23 #endif
25 #ifdef _LIBC
26 # define USE_NUMBER_GROUPING
27 # define STDC_HEADERS
28 # define HAVE_LIMITS_H
29 #endif
31 #include <ctype.h>
32 #include <errno.h>
33 #ifndef errno
34 extern int errno;
35 #endif
36 #ifndef __set_errno
37 # define __set_errno(Val) errno = (Val)
38 #endif
40 #ifdef HAVE_LIMITS_H
41 # include <limits.h>
42 #endif
44 #ifdef STDC_HEADERS
45 # include <stddef.h>
46 # include <stdlib.h>
47 # include <string.h>
48 # include <locale.h>
49 #else
50 # ifndef NULL
51 # define NULL 0
52 # endif
53 #endif
55 #ifdef USE_NUMBER_GROUPING
56 # include "../locale/localeinfo.h"
57 #endif
59 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
60 unsigned integers. */
61 #ifndef UNSIGNED
62 # define UNSIGNED 0
63 # define INT LONG int
64 #else
65 # define INT unsigned LONG int
66 #endif
68 /* Determine the name. */
69 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
70 # if UNSIGNED
71 # ifdef USE_WIDE_CHAR
72 # ifdef QUAD
73 # define strtol __wcstoull_l
74 # else
75 # define strtol __wcstoul_l
76 # endif
77 # else
78 # ifdef QUAD
79 # define strtol __strtoull_l
80 # else
81 # define strtol __strtoul_l
82 # endif
83 # endif
84 # else
85 # ifdef USE_WIDE_CHAR
86 # ifdef QUAD
87 # define strtol __wcstoll_l
88 # else
89 # define strtol __wcstol_l
90 # endif
91 # else
92 # ifdef QUAD
93 # define strtol __strtoll_l
94 # else
95 # define strtol __strtol_l
96 # endif
97 # endif
98 # endif
99 #else
100 # if UNSIGNED
101 # ifdef USE_WIDE_CHAR
102 # ifdef QUAD
103 # define strtol wcstoull
104 # else
105 # define strtol wcstoul
106 # endif
107 # else
108 # ifdef QUAD
109 # define strtol strtoull
110 # else
111 # define strtol strtoul
112 # endif
113 # endif
114 # else
115 # ifdef USE_WIDE_CHAR
116 # ifdef QUAD
117 # define strtol wcstoll
118 # else
119 # define strtol wcstol
120 # endif
121 # else
122 # ifdef QUAD
123 # define strtol strtoll
124 # endif
125 # endif
126 # endif
127 #endif
129 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
130 operating on `long long int's. */
131 #ifdef QUAD
132 # define LONG long long
133 # define STRTOL_LONG_MIN LONG_LONG_MIN
134 # define STRTOL_LONG_MAX LONG_LONG_MAX
135 # define STRTOL_ULONG_MAX ULONG_LONG_MAX
136 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
137 /* Work around gcc bug with using this constant. */
138 static const unsigned long long int maxquad = ULONG_LONG_MAX;
139 # undef STRTOL_ULONG_MAX
140 # define STRTOL_ULONG_MAX maxquad
141 # endif
142 #else
143 # define LONG long
145 # ifndef ULONG_MAX
146 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
147 # endif
148 # ifndef LONG_MAX
149 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
150 # endif
151 # define STRTOL_LONG_MIN LONG_MIN
152 # define STRTOL_LONG_MAX LONG_MAX
153 # define STRTOL_ULONG_MAX ULONG_MAX
154 #endif
157 /* We use this code also for the extended locale handling where the
158 function gets as an additional argument the locale which has to be
159 used. To access the values we have to redefine the _NL_CURRENT
160 macro. */
161 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
162 # undef _NL_CURRENT
163 # define _NL_CURRENT(category, item) \
164 (current->values[_NL_ITEM_INDEX (item)].string)
165 # define LOCALE_PARAM , loc
166 # define LOCALE_PARAM_DECL __locale_t loc;
167 #else
168 # define LOCALE_PARAM
169 # define LOCALE_PARAM_DECL
170 #endif
172 #if defined _LIBC || defined HAVE_WCHAR_H
173 # include <wchar.h>
174 #endif
176 #ifdef USE_WIDE_CHAR
177 # include <wctype.h>
178 # define L_(Ch) L##Ch
179 # define UCHAR_TYPE wint_t
180 # define STRING_TYPE wchar_t
181 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
182 # define ISSPACE(Ch) __iswspace_l ((Ch), loc)
183 # define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
184 # define TOUPPER(Ch) __towupper_l ((Ch), loc)
185 # else
186 # define ISSPACE(Ch) iswspace (Ch)
187 # define ISALPHA(Ch) iswalpha (Ch)
188 # define TOUPPER(Ch) towupper (Ch)
189 # endif
190 # else
191 # if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
192 # define IN_CTYPE_DOMAIN(c) 1
193 # else
194 # define IN_CTYPE_DOMAIN(c) isascii(c)
195 # endif
196 # define L_(Ch) Ch
197 # define UCHAR_TYPE unsigned char
198 # define STRING_TYPE char
199 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
200 # define ISSPACE(Ch) __isspace_l ((Ch), loc)
201 # define ISALPHA(Ch) __isalpha_l ((Ch), loc)
202 # define TOUPPER(Ch) __toupper_l ((Ch), loc)
203 # else
204 # define ISSPACE(Ch) (IN_CTYPE_DOMAIN (Ch) && isspace (Ch))
205 # define ISALPHA(Ch) (IN_CTYPE_DOMAIN (Ch) && isalpha (Ch))
206 # define TOUPPER(Ch) (IN_CTYPE_DOMAIN (Ch) ? toupper (Ch) : (Ch))
207 # endif
208 #endif
210 #ifdef __STDC__
211 # define INTERNAL(X) INTERNAL1(X)
212 # define INTERNAL1(X) __##X##_internal
213 # define WEAKNAME(X) WEAKNAME1(X)
214 #else
215 # define INTERNAL(X) __/**/X/**/_internal
216 #endif
218 #ifdef USE_NUMBER_GROUPING
219 /* This file defines a function to check for correct grouping. */
220 # include "grouping.h"
221 #endif
225 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
226 If BASE is 0 the base is determined by the presence of a leading
227 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
228 If BASE is < 2 or > 36, it is reset to 10.
229 If ENDPTR is not NULL, a pointer to the character after the last
230 one converted is stored in *ENDPTR. */
233 INTERNAL (strtol) (nptr, endptr, base, group LOCALE_PARAM)
234 const STRING_TYPE *nptr;
235 STRING_TYPE **endptr;
236 int base;
237 int group;
238 LOCALE_PARAM_DECL
240 int negative;
241 register unsigned LONG int cutoff;
242 register unsigned int cutlim;
243 register unsigned LONG int i;
244 register const STRING_TYPE *s;
245 register UCHAR_TYPE c;
246 const STRING_TYPE *save, *end;
247 int overflow;
248 #ifndef USE_WIDE_CHAR
249 size_t cnt;
250 #endif
252 #ifdef USE_NUMBER_GROUPING
253 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
254 struct locale_data *current = loc->__locales[LC_NUMERIC];
255 # endif
256 /* The thousands character of the current locale. */
257 # ifdef USE_WIDE_CHAR
258 wchar_t thousands = L'\0';
259 # else
260 const char *thousands = NULL;
261 size_t thousands_len = 0;
262 # endif
263 /* The numeric grouping specification of the current locale,
264 in the format described in <locale.h>. */
265 const char *grouping;
267 if (__builtin_expect (group, 0))
269 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
270 if (*grouping <= 0 || *grouping == CHAR_MAX)
271 grouping = NULL;
272 else
274 /* Figure out the thousands separator character. */
275 # ifdef USE_WIDE_CHAR
276 # ifdef _LIBC
277 thousands = _NL_CURRENT_WORD (LC_NUMERIC,
278 _NL_NUMERIC_THOUSANDS_SEP_WC);
279 # endif
280 if (thousands == L'\0')
281 grouping = NULL;
282 # else
283 # ifdef _LIBC
284 thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
285 # endif
286 if (*thousands == '\0')
288 thousands = NULL;
289 grouping = NULL;
291 # endif
294 else
295 grouping = NULL;
296 #endif
298 if (base < 0 || base == 1 || base > 36)
300 __set_errno (EINVAL);
301 return 0;
304 save = s = nptr;
306 /* Skip white space. */
307 while (ISSPACE (*s))
308 ++s;
309 if (__builtin_expect (*s == L_('\0'), 0))
310 goto noconv;
312 /* Check for a sign. */
313 negative = 0;
314 if (*s == L_('-'))
316 negative = 1;
317 ++s;
319 else if (*s == L_('+'))
320 ++s;
322 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
323 if (*s == L_('0'))
325 if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
327 s += 2;
328 base = 16;
330 else if (base == 0)
331 base = 8;
333 else if (base == 0)
334 base = 10;
336 /* Save the pointer so we can check later if anything happened. */
337 save = s;
339 #ifdef USE_NUMBER_GROUPING
340 if (base != 10)
341 grouping = NULL;
343 if (__builtin_expect (grouping != NULL, 0))
345 # ifndef USE_WIDE_CHAR
346 thousands_len = strlen (thousands);
347 # endif
349 /* Find the end of the digit string and check its grouping. */
350 end = s;
351 if (
352 # ifdef USE_WIDE_CHAR
353 *s != thousands
354 # else
355 ({ for (cnt = 0; cnt < thousands_len; ++cnt)
356 if (thousands[cnt] != end[cnt])
357 break;
358 cnt < thousands_len; })
359 # endif
362 for (c = *end; c != L_('\0'); c = *++end)
363 if (((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
364 # ifdef USE_WIDE_CHAR
365 && c != thousands
366 # else
367 && ({ for (cnt = 0; cnt < thousands_len; ++cnt)
368 if (thousands[cnt] != end[cnt])
369 break;
370 cnt < thousands_len; })
371 # endif
372 && (!ISALPHA (c)
373 || (int) (TOUPPER (c) - L_('A') + 10) >= base))
374 break;
376 end = correctly_grouped_prefix (s, end, thousands, grouping);
379 else
380 #endif
381 end = NULL;
383 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
384 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
386 overflow = 0;
387 i = 0;
388 c = *s;
389 if (sizeof (long int) != sizeof (LONG int))
391 unsigned long int j = 0;
392 unsigned long int jmax = ULONG_MAX / base;
394 for (;c != L_('\0'); c = *++s)
396 if (s == end)
397 break;
398 if (c >= L_('0') && c <= L_('9'))
399 c -= L_('0');
400 #ifdef USE_NUMBER_GROUPING
401 # ifdef USE_WIDE_CHAR
402 else if (grouping && c == thousands)
403 continue;
404 # else
405 else if (thousands_len)
407 for (cnt = 0; cnt < thousands_len; ++cnt)
408 if (thousands[cnt] != s[cnt])
409 break;
410 if (cnt == thousands_len)
412 s += thousands_len - 1;
413 continue;
415 if (ISALPHA (c))
416 c = TOUPPER (c) - L_('A') + 10;
417 else
418 break;
420 # endif
421 #endif
422 else if (ISALPHA (c))
423 c = TOUPPER (c) - L_('A') + 10;
424 else
425 break;
426 if ((int) c >= base)
427 break;
428 /* Note that we never can have an overflow. */
429 else if (j >= jmax)
431 /* We have an overflow. Now use the long representation. */
432 i = (unsigned LONG int) j;
433 goto use_long;
435 else
436 j = j * (unsigned long int) base + c;
439 i = (unsigned LONG int) j;
441 else
442 for (;c != L_('\0'); c = *++s)
444 if (s == end)
445 break;
446 if (c >= L_('0') && c <= L_('9'))
447 c -= L_('0');
448 #ifdef USE_NUMBER_GROUPING
449 # ifdef USE_WIDE_CHAR
450 else if (grouping && c == thousands)
451 continue;
452 # else
453 else if (thousands_len)
455 for (cnt = 0; cnt < thousands_len; ++cnt)
456 if (thousands[cnt] != s[cnt])
457 break;
458 if (cnt == thousands_len)
460 s += thousands_len - 1;
461 continue;
463 if (ISALPHA (c))
464 c = TOUPPER (c) - L_('A') + 10;
465 else
466 break;
468 # endif
469 #endif
470 else if (ISALPHA (c))
471 c = TOUPPER (c) - L_('A') + 10;
472 else
473 break;
474 if ((int) c >= base)
475 break;
476 /* Check for overflow. */
477 if (i > cutoff || (i == cutoff && c > cutlim))
478 overflow = 1;
479 else
481 use_long:
482 i *= (unsigned LONG int) base;
483 i += c;
487 /* Check if anything actually happened. */
488 if (s == save)
489 goto noconv;
491 /* Store in ENDPTR the address of one character
492 past the last character we converted. */
493 if (endptr != NULL)
494 *endptr = (STRING_TYPE *) s;
496 #if !UNSIGNED
497 /* Check for a value that is within the range of
498 `unsigned LONG int', but outside the range of `LONG int'. */
499 if (overflow == 0
500 && i > (negative
501 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
502 : (unsigned LONG int) STRTOL_LONG_MAX))
503 overflow = 1;
504 #endif
506 if (__builtin_expect (overflow, 0))
508 __set_errno (ERANGE);
509 #if UNSIGNED
510 return STRTOL_ULONG_MAX;
511 #else
512 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
513 #endif
516 /* Return the result of the appropriate sign. */
517 return negative ? -i : i;
519 noconv:
520 /* We must handle a special case here: the base is 0 or 16 and the
521 first two characters are '0' and 'x', but the rest are no
522 hexadecimal digits. This is no error case. We return 0 and
523 ENDPTR points to the `x`. */
524 if (endptr != NULL)
526 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
527 && save[-2] == L_('0'))
528 *endptr = (STRING_TYPE *) &save[-1];
529 else
530 /* There was no number to convert. */
531 *endptr = (STRING_TYPE *) nptr;
534 return 0L;
536 #if defined _LIBC \
537 && !(defined USE_IN_EXTENDED_LOCALE_MODEL && defined USE_WIDE_CHAR)
538 libc_hidden_def (INTERNAL (strtol))
539 #endif
541 /* External user entry point. */
543 #if _LIBC - 0 == 0
544 # undef PARAMS
545 # if defined (__STDC__) && __STDC__
546 # define PARAMS(Args) Args
547 # else
548 # define PARAMS(Args) ()
549 # endif
551 /* Prototype. */
552 INT strtol PARAMS ((const STRING_TYPE *nptr, STRING_TYPE **endptr, int base));
553 #endif
557 #ifdef weak_function
558 weak_function
559 #endif
560 strtol (nptr, endptr, base LOCALE_PARAM)
561 const STRING_TYPE *nptr;
562 STRING_TYPE **endptr;
563 int base;
564 LOCALE_PARAM_DECL
566 return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);