1 /* strtol - convert string representation of a number into a long integer value. */
3 /* Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if !defined (HAVE_STRTOL)
25 #include <chartypes.h>
33 # define __set_errno(Val) errno = (Val)
49 /* Nonzero if we are defining `strtoul' or `strtoull', operating on
55 # define INT unsigned LONG int
60 # define strtol strtoull
62 # define strtol strtoul
66 # define strtol strtoll
70 /* If QUAD is defined, we are defining `strtoll' or `strtoull',
71 operating on `long long ints. */
74 # define LONG long long
75 # define STRTOL_LONG_MIN LLONG_MIN
76 # define STRTOL_LONG_MAX LLONG_MAX
77 # define STRTOL_ULONG_MAX ULLONG_MAX
80 # define STRTOL_LONG_MIN LONG_MIN
81 # define STRTOL_LONG_MAX LONG_MAX
82 # define STRTOL_ULONG_MAX ULONG_MAX
85 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
86 If BASE is 0 the base is determined by the presence of a leading
87 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
88 If BASE is < 2 or > 36, it is no longer reset to 10; EINVAL is returned.
89 If ENDPTR is not NULL, a pointer to the character after the last
90 one converted is stored in *ENDPTR. */
93 strtol (nptr, endptr, base)
99 register unsigned LONG int cutoff;
100 register unsigned int cutlim;
101 register unsigned LONG int i;
102 register const char *s;
103 register unsigned char c;
104 const char *save, *end;
107 if (base < 0 || base == 1 || base > 36)
109 __set_errno (EINVAL);
115 /* Skip white space. */
116 while (ISSPACE ((unsigned char)*s))
121 /* Check for a sign. */
122 if (*s == '-' || *s == '+')
124 negative = (*s == '-');
130 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
133 if ((base == 0 || base == 16) && TOUPPER ((unsigned char) s[1]) == 'X')
144 /* Save the pointer so we can check later if anything happened. */
149 cutoff = STRTOL_ULONG_MAX / (unsigned LONG int) base;
150 cutlim = STRTOL_ULONG_MAX % (unsigned LONG int) base;
155 if (sizeof (long int) != sizeof (LONG int))
157 unsigned long int j = 0;
158 unsigned long int jmax = ULONG_MAX / base;
160 for (;c != '\0'; c = *++s)
166 else if (ISALPHA (c))
167 c = TOUPPER (c) - 'A' + 10;
173 /* Note that we never can have an overflow. */
176 /* We have an overflow. Now use the long representation. */
177 i = (unsigned LONG int) j;
181 j = j * (unsigned long int) base + c;
184 i = (unsigned LONG int) j;
187 for (;c != '\0'; c = *++s)
193 else if (ISALPHA (c))
194 c = TOUPPER (c) - 'A' + 10;
199 /* Check for overflow. */
200 if (i > cutoff || (i == cutoff && c > cutlim))
205 i *= (unsigned LONG int) base;
210 /* Check if anything actually happened. */
214 /* Store in ENDPTR the address of one character
215 past the last character we converted. */
217 *endptr = (char *) s;
220 /* Check for a value that is within the range of
221 `unsigned LONG int', but outside the range of `LONG int'. */
224 ? -((unsigned LONG int) (STRTOL_LONG_MIN + 1)) + 1
225 : (unsigned LONG int) STRTOL_LONG_MAX))
231 __set_errno (ERANGE);
233 return STRTOL_ULONG_MAX;
235 return negative ? STRTOL_LONG_MIN : STRTOL_LONG_MAX;
239 /* Return the result of the appropriate sign. */
240 return negative ? -i : i;
243 /* We must handle a special case here: the base is 0 or 16 and the
244 first two characters are '0' and 'x', but the rest are no
245 hexadecimal digits. This is no error case. We return 0 and
246 ENDPTR points to the `x`. */
249 if (save - nptr >= 2 && TOUPPER ((unsigned char) save[-1]) == 'X' && save[-2] == '0')
250 *endptr = (char *) &save[-1];
252 /* There was no number to convert. */
253 *endptr = (char *) nptr;
259 #endif /* !HAVE_STRTOL */