1 /* strtol - Convert string representation of a number into an integer value.
2 Copyright (C) 1991, 92, 94, 95, 96 Free Software Foundation, Inc.
3 NOTE: The canonical source of this file is maintained with the GNU C
4 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 # define USE_NUMBER_GROUPING
27 # define HAVE_LIMITS_H
36 # define __set_errno(Val) errno = (Val)
53 #ifdef USE_NUMBER_GROUPING
54 # include "../locale/localeinfo.h"
57 /* Nonzero if we are defining `strtoul' or `strtouq', operating on
63 # define INT unsigned LONG int
66 /* Determine the name. */
70 # define strtol wcstouq
72 # define strtol wcstoul
76 # define strtol strtouq
78 # define strtol strtoul
84 # define strtol wcstoq
86 # define strtol wcstol
90 # define strtol strtoq
95 /* If QUAD is defined, we are defining `strtoq' or `strtouq',
96 operating on `long long int's. */
98 # define LONG long long
100 # define LONG_MIN LONG_LONG_MIN
102 # define LONG_MAX LONG_LONG_MAX
104 # define ULONG_MAX ULONG_LONG_MAX
105 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
106 /* Work around gcc bug with using this constant. */
107 static const unsigned long long int maxquad = ULONG_LONG_MAX;
109 # define ULONG_MAX maxquad
115 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
118 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
125 # define L_(Ch) L##Ch
126 # define UCHAR_TYPE wint_t
127 # define STRING_TYPE wchar_t
128 # define ISSPACE(Ch) iswspace (Ch)
129 # define ISALPHA(Ch) iswalpha (Ch)
130 # define TOUPPER(Ch) towupper (Ch)
133 # define UCHAR_TYPE unsigned char
134 # define STRING_TYPE char
135 # define ISSPACE(Ch) isspace (Ch)
136 # define ISALPHA(Ch) isalpha (Ch)
137 # define TOUPPER(Ch) toupper (Ch)
141 # define INTERNAL(X) INTERNAL1(X)
142 # define INTERNAL1(X) __##X##_internal
143 # define WEAKNAME(X) WEAKNAME1(X)
145 # define INTERNAL(X) __/**/X/**/_internal
148 #ifdef USE_NUMBER_GROUPING
149 /* This file defines a function to check for correct grouping. */
150 # include "grouping.h"
154 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
155 If BASE is 0 the base is determined by the presence of a leading
156 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
157 If BASE is < 2 or > 36, it is reset to 10.
158 If ENDPTR is not NULL, a pointer to the character after the last
159 one converted is stored in *ENDPTR. */
162 INTERNAL (strtol) (nptr, endptr, base, group)
163 const STRING_TYPE *nptr;
164 STRING_TYPE **endptr;
169 register unsigned LONG int cutoff;
170 register unsigned int cutlim;
171 register unsigned LONG int i;
172 register const STRING_TYPE *s;
173 register UCHAR_TYPE c;
174 const STRING_TYPE *save, *end;
177 #ifdef USE_NUMBER_GROUPING
178 /* The thousands character of the current locale. */
180 /* The numeric grouping specification of the current locale,
181 in the format described in <locale.h>. */
182 const char *grouping;
186 grouping = _NL_CURRENT (LC_NUMERIC, GROUPING);
187 if (*grouping <= 0 || *grouping == CHAR_MAX)
191 /* Figure out the thousands separator character. */
192 if (mbtowc (&thousands, _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP),
193 strlen (_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP))) <= 0)
194 thousands = (wchar_t) *_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
195 if (thousands == L'\0')
203 if (base < 0 || base == 1 || base > 36)
208 /* Skip white space. */
214 /* Check for a sign. */
220 else if (*s == L_('+'))
228 if (base == 16 && s[0] == L_('0') && TOUPPER (s[1]) == L_('X'))
231 /* If BASE is zero, figure it out ourselves. */
235 if (TOUPPER (s[1]) == L_('X'))
246 /* Save the pointer so we can check later if anything happened. */
249 #ifdef USE_NUMBER_GROUPING
252 /* Find the end of the digit string and check its grouping. */
254 for (c = *end; c != L_('\0'); c = *++end)
255 if ((wchar_t) c != thousands
256 && ((wchar_t) c < L_('0') || (wchar_t) c > L_('9'))
257 && (!ISALPHA (c) || (int) (TOUPPER (c) - L_('A') + 10) >= base))
262 end = correctly_grouped_prefix (s, end, thousands, grouping);
268 cutoff = ULONG_MAX / (unsigned LONG int) base;
269 cutlim = ULONG_MAX % (unsigned LONG int) base;
273 for (c = *s; c != L_('\0'); c = *++s)
277 if (c >= L_('0') && c <= L_('9'))
279 else if (ISALPHA (c))
280 c = TOUPPER (c) - L_('A') + 10;
285 /* Check for overflow. */
286 if (i > cutoff || (i == cutoff && c > cutlim))
290 i *= (unsigned LONG int) base;
295 /* Check if anything actually happened. */
299 /* Store in ENDPTR the address of one character
300 past the last character we converted. */
302 *endptr = (STRING_TYPE *) s;
305 /* Check for a value that is within the range of
306 `unsigned LONG int', but outside the range of `LONG int'. */
309 ? -((unsigned LONG int) (LONG_MIN + 1)) + 1
310 : (unsigned LONG int) LONG_MAX))
316 __set_errno (ERANGE);
320 return negative ? LONG_MIN : LONG_MAX;
324 /* Return the result of the appropriate sign. */
325 return (negative ? -i : i);
328 /* We must handle a special case here: the base is 0 or 16 and the
329 first two characters are '0' and 'x', but the rest are no
330 hexadecimal digits. This is no error case. We return 0 and
331 ENDPTR points to the `x`. */
333 if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X')
334 && save[-2] == L_('0'))
335 *endptr = (STRING_TYPE *) &save[-1];
337 /* There was no number to convert. */
338 *endptr = (STRING_TYPE *) nptr;
343 /* External user entry point. */
347 # if defined (__STDC__) && __STDC__
348 # define PARAMS(Args) Args
350 # define PARAMS(Args) ()
354 INT strtol PARAMS ((const STRING_TYPE *nptr, STRING_TYPE **endptr, int base));
362 strtol (nptr, endptr, base)
363 const STRING_TYPE *nptr;
364 STRING_TYPE **endptr;
367 return INTERNAL (strtol) (nptr, endptr, base, 0);