1 /* Copyright (C) 1991, 1992, 1994 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 Library General Public License as
7 published by the Free Software Foundation; either version 3 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
18 Cambridge, MA 02110-1301, USA. */
30 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
31 If BASE is 0 the base is determined by the presence of a leading
32 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
33 If BASE is < 2 or > 36, it is reset to 10.
34 If ENDPTR is not NULL, a pointer to the character after the last
35 one converted is stored in *ENDPTR. */
38 #define strtol strtoul
42 strtol (nptr, endptr, base)
48 register unsigned long int cutoff;
49 register unsigned int cutlim;
50 register unsigned long int i;
51 register const char *s;
52 register unsigned char c;
56 if (base < 0 || base == 1 || base > 36)
61 /* Skip white space. */
67 /* Check for a sign. */
81 if (base == 16 && s[0] == '0' && toupper (s[1]) == 'X')
84 /* If BASE is zero, figure it out ourselves. */
88 if (toupper (s[1]) == 'X')
99 /* Save the pointer so we can check later if anything happened. */
102 cutoff = ULONG_MAX / (unsigned long int) base;
103 cutlim = ULONG_MAX % (unsigned long int) base;
107 for (c = *s; c != '\0'; c = *++s)
111 else if (isalpha (c))
112 c = toupper (c) - 'A' + 10;
117 /* Check for overflow. */
118 if (i > cutoff || (i == cutoff && c > cutlim))
122 i *= (unsigned long int) base;
127 /* Check if anything actually happened. */
131 /* Store in ENDPTR the address of one character
132 past the last character we converted. */
134 *endptr = (char *) s;
137 /* Check for a value that is within the range of
138 `unsigned long int', but outside the range of `long int'. */
140 -(unsigned long int) LONG_MIN : (unsigned long int) LONG_MAX))
150 return negative ? LONG_MIN : LONG_MAX;
154 /* Return the result of the appropriate sign. */
155 return (negative ? -i : i);
158 /* There was no number to convert. */
160 *endptr = (char *) nptr;