1 /* Copyright (C) 1991 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 1, or (at your option)
9 The GNU C Library 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 the GNU C Library; see the file COPYING. If not, write to
16 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
29 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
30 If BASE is 0 the base is determined by the presence of a leading
31 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
32 If BASE is < 2 or > 36, it is reset to 10.
33 If ENDPTR is not NULL, a pointer to the character after the last
34 one converted is stored in *ENDPTR. */
37 #define strtol strtoul
41 DEFUN(strtol, (nptr, endptr, base),
42 CONST char *nptr AND char **endptr AND int base)
45 register unsigned long int cutoff;
46 register unsigned int cutlim;
47 register unsigned long int i;
48 register CONST char *s;
49 register unsigned char c;
53 if (base < 0 || base == 1 || base > 36)
58 /* Skip white space. */
64 /* Check for a sign. */
78 if (base == 16 && s[0] == '0' && toupper(s[1]) == 'X')
81 /* If BASE is zero, figure it out ourselves. */
85 if (toupper(s[1]) == 'X')
96 /* Save the pointer so we can check later if anything happened. */
99 cutoff = ULONG_MAX / (unsigned long int) base;
100 cutlim = ULONG_MAX % (unsigned long int) base;
104 for (c = *s; c != '\0'; c = *++s)
109 c = toupper(c) - 'A' + 10;
114 /* Check for overflow. */
115 if (i > cutoff || (i == cutoff && c > cutlim))
119 i *= (unsigned long int) base;
124 /* Check if anything actually happened. */
128 /* Store in ENDPTR the address of one character
129 past the last character we converted. */
131 *endptr = (char *) s;
134 /* Check for a value that is within the range of
135 `unsigned long int', but outside the range of `long int'. */
136 if (i > (unsigned long int) (sign > 0 ? LONG_MAX : - LONG_MAX))
146 return sign > 0 ? LONG_MAX : LONG_MIN;
150 /* Return the result of the appropriate sign. */
154 /* There was no number to convert. */
156 *endptr = (char *) nptr;