1 /* strtol - Convert string representation of a number into an integer value.
2 Copyright (C) 1997 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 #if !defined (HAVE_STRTOL)
30 # define __set_errno(Val) errno = (Val)
43 /* Nonzero if we are defining `strtoul', operating on unsigned integers. */
48 # define RETTYPE unsigned long
51 /* Determine the name. */
53 # define strtol strtoul
61 # define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
62 # define ULONG_MIN ((unsigned long) 0 - ULONG_MAX)
66 # define LONG_MAX ((long) (ULONG_MAX >> 1))
67 # define LONG_MIN ((long) (0 - LONG_MAX))
70 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
71 If BASE is 0 the base is determined by the presence of a leading
72 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
73 If BASE is < 2 or > 36, it is reset to 10.
74 If ENDPTR is not NULL, a pointer to the character after the last
75 one converted is stored in *ENDPTR. */
78 strtol (nptr, endptr, base)
84 register unsigned long cutoff, i;
85 register unsigned int cutlim;
86 register const char *s;
87 register unsigned char c;
88 const char *save, *end;
91 if (base < 0 || base == 1 || base > 36)
96 /* Skip white space. */
102 /* Check for a sign. */
103 if (*s == '-' || *s == '+')
105 negative = (*s == '-');
111 if (base == 16 && *s == '0' && toupper (s[1]) == 'X')
114 /* If BASE is zero, figure it out ourselves. */
118 if (toupper (s[1]) == 'X')
129 /* Save the pointer so we can check later if anything happened. */
134 cutoff = ULONG_MAX / (unsigned long int) base;
135 cutlim = ULONG_MAX % (unsigned long int) base;
139 for (c = *s; c != '\0'; c = *++s)
144 if (c >= '0' && c <= '9')
146 else if (isalpha (c))
147 c = toupper (c) - 'A' + 10;
154 /* Check for overflow. */
155 if (i > cutoff || (i == cutoff && c > cutlim))
159 i *= (unsigned long int) base;
164 /* Check if anything actually happened. */
168 /* Store in ENDPTR the address of one character
169 past the last character we converted. */
171 *endptr = (char *) s;
174 /* Check for a value that is within the range of
175 `unsigned long int', but outside the range of `long int'. */
178 ? -((unsigned long) (LONG_MIN + 1)) + 1
179 : (unsigned long) LONG_MAX))
185 __set_errno (ERANGE);
189 return negative ? LONG_MIN : LONG_MAX;
193 /* Return the result with the appropriate sign. */
194 return (negative ? -i : i);
197 /* We must handle a special case here: the base is 0 or 16 and the
198 first two characters are '0' and 'x', but the rest are no
199 hexadecimal digits. This is no error case. We return 0 and
200 ENDPTR points to the `x'. */
203 if (save - nptr >= 2 && toupper (save[-1]) == 'X' && save[-2] == '0')
204 *endptr = (char *) &save[-1];
206 /* There was no number to convert. */
207 *endptr = (char *) nptr;
213 #endif /* !HAVE_STRTOL */