1 /* Convert string representation of a number into an integer value.
2 Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 #if !defined (HAVE_STRTOL)
24 #include <chartypes.h>
32 # define __set_errno(Val) errno = (Val)
48 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
54 # define INT unsigned LONG int
59 # define strtol strtoull
61 # define strtol strtoul
65 # define strtol strtoll
69 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
70 operating on `long long ints. */
73 # define LONG long long
74 # define STRTOL_LONG_MIN LLONG_MIN
75 # define STRTOL_LONG_MAX LLONG_MAX
76 # define STRTOL_ULONG_MAX ULLONG_MAX
79 # define STRTOL_LONG_MIN LONG_MIN
80 # define STRTOL_LONG_MAX LONG_MAX
81 # define STRTOL_ULONG_MAX ULONG_MAX
84 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
85 If BASE is 0 the base is determined by the presence of a leading
86 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
87 If BASE is < 2 or > 36, it is no longer reset to 10; EINVAL is returned.
88 If ENDPTR is not NULL, a pointer to the character after the last
89 one converted is stored in *ENDPTR. */
92 strtol (nptr, endptr, base)
98 register unsigned LONG int cutoff;
99 register unsigned int cutlim;
100 register unsigned LONG int i;
101 register const char *s;
102 register unsigned char c;
103 const char *save, *end;
106 if (base < 0 || base == 1 || base > 36)
108 __set_errno (EINVAL);
114 /* Skip white space. */
115 while (ISSPACE ((unsigned char)*s))
120 /* Check for a sign. */
121 if (*s == '-' || *s == '+')
123 negative = (*s == '-');
129 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
132 if ((base == 0 || base == 16) && TOUPPER ((unsigned char) s[1]) == 'X')
143 /* Save the pointer so we can check later if anything happened. */
148 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
149 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
154 if (sizeof (long int) != sizeof (LONG int))
156 unsigned long int j = 0;
157 unsigned long int jmax = ULONG_MAX / base;
159 for (;c != '\0'; c = *++s)
165 else if (ISALPHA (c))
166 c = TOUPPER (c) - 'A' + 10;
172 /* Note that we never can have an overflow. */
175 /* We have an overflow. Now use the long representation. */
176 i = (unsigned LONG int) j;
180 j = j * (unsigned long int) base + c;
183 i = (unsigned LONG int) j;
186 for (;c != '\0'; c = *++s)
192 else if (ISALPHA (c))
193 c = TOUPPER (c) - 'A' + 10;
198 /* Check for overflow. */
199 if (i > cutoff || (i == cutoff && c > cutlim))
204 i *= (unsigned LONG int) base;
209 /* Check if anything actually happened. */
213 /* Store in ENDPTR the address of one character
214 past the last character we converted. */
216 *endptr = (char *) s;
219 /* Check for a value that is within the range of
220 `unsigned LONG int', but outside the range of `LONG int'. */
223 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
224 : (unsigned LONG int) STRTOL_LONG_MAX))
230 __set_errno (ERANGE);
232 return STRTOL_ULONG_MAX;
234 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
238 /* Return the result of the appropriate sign. */
239 return negative ? -i : i;
242 /* We must handle a special case here: the base is 0 or 16 and the
243 first two characters are '0' and 'x', but the rest are no
244 hexadecimal digits. This is no error case. We return 0 and
245 ENDPTR points to the `x`. */
248 if (save - nptr >= 2 && TOUPPER ((unsigned char) save[-1]) == 'X' && save[-2] == '0')
249 *endptr = (char *) &save[-1];
251 /* There was no number to convert. */
252 *endptr = (char *) nptr;
258 #endif /* !HAVE_STRTOL */