1 /* 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 Library.
4 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
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
26 # define USE_NUMBER_GROUPING
28 # define HAVE_LIMITS_H
50 #ifdef USE_NUMBER_GROUPING
51 # include "../locale/localeinfo.h"
54 /* Nonzero if we are defining `strtoul' or `strtouq', operating on
60 # define INT unsigned LONG int
63 /* Determine the name. */
67 # define strtol wcstouq
69 # define strtol wcstoul
73 # define strtol strtouq
75 # define strtol strtoul
81 # define strtol wcstoq
83 # define strtol wcstol
87 # define strtol strtoq
92 /* If QUAD is defined, we are defining `strtoq' or `strtouq',
93 operating on `long long int's. */
95 # define LONG long long
97 # define LONG_MIN LONG_LONG_MIN
99 # define LONG_MAX LONG_LONG_MAX
101 # define ULONG_MAX ULONG_LONG_MAX
102 # if __GNUC__ == 2 && __GNUC_MINOR__ < 7
103 /* Work around gcc bug with using this constant. */
104 static const unsigned long long int maxquad
= ULONG_LONG_MAX
;
106 # define ULONG_MAX maxquad
112 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
115 # define LONG_MAX ((long int) (ULONG_MAX >> 1))
122 # define L_(ch) L##ch
123 # define UCHAR_TYPE wint_t
124 # define STRING_TYPE wchar_t
125 # define ISSPACE(ch) iswspace (ch)
126 # define ISALPHA(ch) iswalpha (ch)
127 # define TOUPPER(ch) towupper (ch)
130 # define UCHAR_TYPE unsigned char
131 # define STRING_TYPE char
132 # define ISSPACE(ch) isspace (ch)
133 # define ISALPHA(ch) isalpha (ch)
134 # define TOUPPER(ch) toupper (ch)
138 # define INTERNAL(x) INTERNAL1(x)
139 # define INTERNAL1(x) __##x##_internal
140 # define WEAKNAME(x) WEAKNAME1(x)
141 # define WEAKNAME1(x) __##x
143 # define INTERNAL(x) __/**/x/**/_internal
144 # define WEAKNAME(x) __/**/x
147 #ifdef USE_NUMBER_GROUPING
148 /* This file defines a function to check for correct grouping. */
149 # include "grouping.h"
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
;
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
;
176 #ifdef USE_NUMBER_GROUPING
177 /* The thousands character of the current locale. */
179 /* The numeric grouping specification of the current locale,
180 in the format described in <locale.h>. */
181 const char *grouping
;
185 grouping
= _NL_CURRENT (LC_NUMERIC
, GROUPING
);
186 if (*grouping
<= 0 || *grouping
== CHAR_MAX
)
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')
202 if (base
< 0 || base
== 1 || base
> 36)
207 /* Skip white space. */
213 /* Check for a sign. */
219 else if (*s
== L_('+'))
227 if (base
== 16 && s
[0] == L_('0') && TOUPPER (s
[1]) == L_('X'))
230 /* If BASE is zero, figure it out ourselves. */
234 if (TOUPPER (s
[1]) == L_('X'))
245 /* Save the pointer so we can check later if anything happened. */
248 #ifdef USE_NUMBER_GROUPING
251 /* Find the end of the digit string and check its grouping. */
253 for (c
= *end
; c
!= L_('\0'); c
= *++end
)
254 if (c
!= thousands
&& (c
< L_('0') || c
> L_('9'))
255 && (!ISALPHA (c
) || TOUPPER (c
) - L_('A') + 10 >= base
))
260 end
= correctly_grouped_prefix (s
, end
, thousands
, grouping
);
266 cutoff
= ULONG_MAX
/ (unsigned LONG
int) base
;
267 cutlim
= ULONG_MAX
% (unsigned LONG
int) base
;
271 for (c
= *s
; c
!= L_('\0'); c
= *++s
)
275 if (c
>= L_('0') && c
<= L_('9'))
277 else if (ISALPHA (c
))
278 c
= TOUPPER (c
) - L_('A') + 10;
283 /* Check for overflow. */
284 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
288 i
*= (unsigned LONG
int) base
;
293 /* Check if anything actually happened. */
297 /* Store in ENDPTR the address of one character
298 past the last character we converted. */
300 *endptr
= (STRING_TYPE
*) s
;
303 /* Check for a value that is within the range of
304 `unsigned LONG int', but outside the range of `LONG int'. */
307 ? -((unsigned LONG
int) (LONG_MIN
+ 1)) + 1
308 : (unsigned LONG
int) LONG_MAX
))
318 return negative
? LONG_MIN
: LONG_MAX
;
322 /* Return the result of the appropriate sign. */
323 return (negative
? -i
: i
);
326 /* We must handle a special case here: the base is 0 or 16 and the
327 first two characters and '0' and 'x', but the rest are no
328 hexadecimal digits. This is no error case. We return 0 and
329 ENDPTR points to the `x`. */
331 if (save
- nptr
>= 2 && TOUPPER (save
[-1]) == L_('X')
332 && save
[-2] == L_('0'))
333 *endptr
= (STRING_TYPE
*) &save
[-1];
335 /* There was no number to convert. */
336 *endptr
= (STRING_TYPE
*) nptr
;
341 /* External user entry point. */
344 #if defined (__STDC__) && __STDC__
345 # define __P(args) args
347 # define __P(args) ()
351 INT strtol
__P ((const STRING_TYPE
*nptr
, STRING_TYPE
**endptr
,
356 strtol (nptr
, endptr
, base
)
357 const STRING_TYPE
*nptr
;
358 STRING_TYPE
**endptr
;
361 return INTERNAL (strtol
) (nptr
, endptr
, base
, 0);