]> git.cworth.org Git - tar/blob - gnu/strtol.c
Imported Upstream version 1.24
[tar] / gnu / strtol.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Convert string representation of a number into an integer value.
4
5    Copyright (C) 1991, 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2003, 2005,
6    2006, 2007, 2009, 2010 Free Software Foundation, Inc.
7
8    NOTE: The canonical source of this file is maintained with the GNU C
9    Library.  Bugs can be reported to bug-glibc@gnu.org.
10
11    This program is free software: you can redistribute it and/or modify it
12    under the terms of the GNU General Public License as published by the
13    Free Software Foundation; either version 3 of the License, or any
14    later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
23
24 #ifdef _LIBC
25 # define USE_NUMBER_GROUPING
26 #else
27 # include <config.h>
28 #endif
29
30 #include <ctype.h>
31 #include <errno.h>
32 #ifndef __set_errno
33 # define __set_errno(Val) errno = (Val)
34 #endif
35
36 #include <limits.h>
37 #include <stddef.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #ifdef USE_NUMBER_GROUPING
42 # include "../locale/localeinfo.h"
43 #endif
44
45 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
46    unsigned integers.  */
47 #ifndef UNSIGNED
48 # define UNSIGNED 0
49 # define INT LONG int
50 #else
51 # define INT unsigned LONG int
52 #endif
53
54 /* Determine the name.  */
55 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
56 # if UNSIGNED
57 #  ifdef USE_WIDE_CHAR
58 #   ifdef QUAD
59 #    define strtol __wcstoull_l
60 #   else
61 #    define strtol __wcstoul_l
62 #   endif
63 #  else
64 #   ifdef QUAD
65 #    define strtol __strtoull_l
66 #   else
67 #    define strtol __strtoul_l
68 #   endif
69 #  endif
70 # else
71 #  ifdef USE_WIDE_CHAR
72 #   ifdef QUAD
73 #    define strtol __wcstoll_l
74 #   else
75 #    define strtol __wcstol_l
76 #   endif
77 #  else
78 #   ifdef QUAD
79 #    define strtol __strtoll_l
80 #   else
81 #    define strtol __strtol_l
82 #   endif
83 #  endif
84 # endif
85 #else
86 # if UNSIGNED
87 #  ifdef USE_WIDE_CHAR
88 #   ifdef QUAD
89 #    define strtol wcstoull
90 #   else
91 #    define strtol wcstoul
92 #   endif
93 #  else
94 #   ifdef QUAD
95 #    define strtol strtoull
96 #   else
97 #    define strtol strtoul
98 #   endif
99 #  endif
100 # else
101 #  ifdef USE_WIDE_CHAR
102 #   ifdef QUAD
103 #    define strtol wcstoll
104 #   else
105 #    define strtol wcstol
106 #   endif
107 #  else
108 #   ifdef QUAD
109 #    define strtol strtoll
110 #   endif
111 #  endif
112 # endif
113 #endif
114
115 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
116    operating on `long long int's.  */
117 #ifdef QUAD
118 # define LONG long long
119 # define STRTOL_LONG_MIN LLONG_MIN
120 # define STRTOL_LONG_MAX LLONG_MAX
121 # define STRTOL_ULONG_MAX ULLONG_MAX
122
123 /* The extra casts in the following macros work around compiler bugs,
124    e.g., in Cray C 5.0.3.0.  */
125
126 /* True if negative values of the signed integer type T use two's
127    complement, ones' complement, or signed magnitude representation,
128    respectively.  Much GNU code assumes two's complement, but some
129    people like to be portable to all possible C hosts.  */
130 # define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
131 # define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
132 # define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
133
134 /* True if the arithmetic type T is signed.  */
135 # define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
136
137 /* The maximum and minimum values for the integer type T.  These
138    macros have undefined behavior if T is signed and has padding bits.
139    If this is a problem for you, please let us know how to fix it for
140    your host.  */
141 # define TYPE_MINIMUM(t) \
142    ((t) (! TYPE_SIGNED (t) \
143          ? (t) 0 \
144          : TYPE_SIGNED_MAGNITUDE (t) \
145          ? ~ (t) 0 \
146          : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
147 # define TYPE_MAXIMUM(t) \
148    ((t) (! TYPE_SIGNED (t) \
149          ? (t) -1 \
150          : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
151
152 # ifndef ULLONG_MAX
153 #  define ULLONG_MAX TYPE_MAXIMUM (unsigned long long)
154 # endif
155 # ifndef LLONG_MAX
156 #  define LLONG_MAX TYPE_MAXIMUM (long long int)
157 # endif
158 # ifndef LLONG_MIN
159 #  define LLONG_MIN TYPE_MINIMUM (long long int)
160 # endif
161
162 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
163    /* Work around gcc bug with using this constant.  */
164    static const unsigned long long int maxquad = ULLONG_MAX;
165 #  undef STRTOL_ULONG_MAX
166 #  define STRTOL_ULONG_MAX maxquad
167 # endif
168 #else
169 # define LONG long
170 # define STRTOL_LONG_MIN LONG_MIN
171 # define STRTOL_LONG_MAX LONG_MAX
172 # define STRTOL_ULONG_MAX ULONG_MAX
173 #endif
174
175
176 /* We use this code also for the extended locale handling where the
177    function gets as an additional argument the locale which has to be
178    used.  To access the values we have to redefine the _NL_CURRENT
179    macro.  */
180 #ifdef USE_IN_EXTENDED_LOCALE_MODEL
181 # undef _NL_CURRENT
182 # define _NL_CURRENT(category, item) \
183   (current->values[_NL_ITEM_INDEX (item)].string)
184 # define LOCALE_PARAM , loc
185 # define LOCALE_PARAM_PROTO , __locale_t loc
186 #else
187 # define LOCALE_PARAM
188 # define LOCALE_PARAM_PROTO
189 #endif
190
191 #include <wchar.h>
192
193 #ifdef USE_WIDE_CHAR
194 # include <wctype.h>
195 # define L_(Ch) L##Ch
196 # define UCHAR_TYPE wint_t
197 # define STRING_TYPE wchar_t
198 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
199 #  define ISSPACE(Ch) __iswspace_l ((Ch), loc)
200 #  define ISALPHA(Ch) __iswalpha_l ((Ch), loc)
201 #  define TOUPPER(Ch) __towupper_l ((Ch), loc)
202 # else
203 #  define ISSPACE(Ch) iswspace (Ch)
204 #  define ISALPHA(Ch) iswalpha (Ch)
205 #  define TOUPPER(Ch) towupper (Ch)
206 # endif
207 #else
208 # define L_(Ch) Ch
209 # define UCHAR_TYPE unsigned char
210 # define STRING_TYPE char
211 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
212 #  define ISSPACE(Ch) __isspace_l ((Ch), loc)
213 #  define ISALPHA(Ch) __isalpha_l ((Ch), loc)
214 #  define TOUPPER(Ch) __toupper_l ((Ch), loc)
215 # else
216 #  define ISSPACE(Ch) isspace (Ch)
217 #  define ISALPHA(Ch) isalpha (Ch)
218 #  define TOUPPER(Ch) toupper (Ch)
219 # endif
220 #endif
221
222 #define INTERNAL(X) INTERNAL1(X)
223 #define INTERNAL1(X) __##X##_internal
224 #define WEAKNAME(X) WEAKNAME1(X)
225
226 #ifdef USE_NUMBER_GROUPING
227 /* This file defines a function to check for correct grouping.  */
228 # include "grouping.h"
229 #endif
230
231
232
233 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
234    If BASE is 0 the base is determined by the presence of a leading
235    zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
236    If BASE is < 2 or > 36, it is reset to 10.
237    If ENDPTR is not NULL, a pointer to the character after the last
238    one converted is stored in *ENDPTR.  */
239
240 INT
241 INTERNAL (strtol) (const STRING_TYPE *nptr, STRING_TYPE **endptr,
242                    int base, int group LOCALE_PARAM_PROTO)
243 {
244   int negative;
245   register unsigned LONG int cutoff;
246   register unsigned int cutlim;
247   register unsigned LONG int i;
248   register const STRING_TYPE *s;
249   register UCHAR_TYPE c;
250   const STRING_TYPE *save, *end;
251   int overflow;
252
253 #ifdef USE_NUMBER_GROUPING
254 # ifdef USE_IN_EXTENDED_LOCALE_MODEL
255   struct locale_data *current = loc->__locales[LC_NUMERIC];
256 # endif
257   /* The thousands character of the current locale.  */
258   wchar_t thousands = L'\0';
259   /* The numeric grouping specification of the current locale,
260      in the format described in <locale.h>.  */
261   const char *grouping;
262
263   if (group)
264     {
265       grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
266       if (*grouping <= 0 || *grouping == CHAR_MAX)
267         grouping = NULL;
268       else
269         {
270           /* Figure out the thousands separator character.  */
271 # if defined _LIBC || defined _HAVE_BTOWC
272           thousands = __btowc (*_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP));
273           if (thousands == WEOF)
274             thousands = L'\0';
275 # endif
276           if (thousands == L'\0')
277             grouping = NULL;
278         }
279     }
280   else
281     grouping = NULL;
282 #endif
283
284   if (base < 0 || base == 1 || base > 36)
285     {
286       __set_errno (EINVAL);
287       return 0;
288     }
289
290   save = s = nptr;
291
292   /* Skip white space.  */
293   while (ISSPACE (*s))
294     ++s;
295   if (*s == L_('\0'))
296     goto noconv;
297
298   /* Check for a sign.  */
299   if (*s == L_('-'))
300     {
301       negative = 1;
302       ++s;
303     }
304   else if (*s == L_('+'))
305     {
306       negative = 0;
307       ++s;
308     }
309   else
310     negative = 0;
311
312   /* Recognize number prefix and if BASE is zero, figure it out ourselves.  */
313   if (*s == L_('0'))
314     {
315       if ((base == 0 || base == 16) && TOUPPER (s[1]) == L_('X'))
316         {
317           s += 2;
318           base = 16;
319         }
320       else if (base == 0)
321         base = 8;
322     }
323   else if (base == 0)
324     base = 10;
325
326   /* Save the pointer so we can check later if anything happened.  */
327   save = s;
328
329 #ifdef USE_NUMBER_GROUPING
330   if (group)
331     {
332       /* Find the end of the digit string and check its grouping.  */
333       end = s;
334       for (c = *end; c != L_('\0'); c = *++end)
335         if ((wchar_t) c != thousands
336             && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
337             && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
338           break;
339       if (*s == thousands)
340         end = s;
341       else
342         end = correctly_grouped_prefix (s, end, thousands, grouping);
343     }
344   else
345 #endif
346     end = NULL;
347
348   cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
349   cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
350
351   overflow = 0;
352   i = 0;
353   for (c = *s; c != L_('\0'); c = *++s)
354     {
355       if (s == end)
356         break;
357       if (c >= L_('0') && c <= L_('9'))
358         c -= L_('0');
359       else if (ISALPHA (c))
360         c = TOUPPER (c) - L_('A') + 10;
361       else
362         break;
363       if ((int) c >= base)
364         break;
365       /* Check for overflow.  */
366       if (i > cutoff || (i == cutoff && c > cutlim))
367         overflow = 1;
368       else
369         {
370           i *= (unsigned LONG int) base;
371           i += c;
372         }
373     }
374
375   /* Check if anything actually happened.  */
376   if (s == save)
377     goto noconv;
378
379   /* Store in ENDPTR the address of one character
380      past the last character we converted.  */
381   if (endptr != NULL)
382     *endptr = (STRING_TYPE *) s;
383
384 #if !UNSIGNED
385   /* Check for a value that is within the range of
386      `unsigned LONG int', but outside the range of `LONG int'.  */
387   if (overflow == 0
388       && i > (negative
389               ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
390               : (unsigned LONG int) STRTOL_LONG_MAX))
391     overflow = 1;
392 #endif
393
394   if (overflow)
395     {
396       __set_errno (ERANGE);
397 #if UNSIGNED
398       return STRTOL_ULONG_MAX;
399 #else
400       return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
401 #endif
402     }
403
404   /* Return the result of the appropriate sign.  */
405   return negative ? -i : i;
406
407 noconv:
408   /* We must handle a special case here: the base is 0 or 16 and the
409      first two characters are '0' and 'x', but the rest are no
410      hexadecimal digits.  This is no error case.  We return 0 and
411      ENDPTR points to the `x`.  */
412   if (endptr != NULL)
413     {
414       if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
415           && save[-2] == L_('0'))
416         *endptr = (STRING_TYPE *) &save[-1];
417       else
418         /*  There was no number to convert.  */
419         *endptr = (STRING_TYPE *) nptr;
420     }
421
422   return 0L;
423 }
424 \f
425 /* External user entry point.  */
426
427
428 INT
429 #ifdef weak_function
430 weak_function
431 #endif
432 strtol (const STRING_TYPE *nptr, STRING_TYPE **endptr,
433         int base LOCALE_PARAM_PROTO)
434 {
435   return INTERNAL (strtol) (nptr, endptr, base, 0 LOCALE_PARAM);
436 }