1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
29 #define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
33 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
37 #define LONG_MIN (-LONG_MAX - 1)
52 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
53 If BASE is 0 the base is determined by the presence of a leading
54 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
55 If BASE is < 2 or > 36, it is reset to 10.
56 If ENDPTR is not NULL, a pointer to the character after the last
57 one converted is stored in *ENDPTR. */
60 #define strtol strtoul
64 strtol (nptr
, endptr
, base
)
70 register unsigned long int cutoff
;
71 register unsigned int cutlim
;
72 register unsigned long int i
;
73 register const char *s
;
74 register unsigned char c
;
78 if (base
< 0 || base
== 1 || base
> 36)
83 /* Skip white space. */
89 /* Check for a sign. */
103 if (base
== 16 && s
[0] == '0' && toupper (s
[1]) == 'X')
106 /* If BASE is zero, figure it out ourselves. */
111 if (toupper (s
[1]) == 'X')
123 /* Save the pointer so we can check later if anything happened. */
126 cutoff
= ULONG_MAX
/ (unsigned long int) base
;
127 cutlim
= ULONG_MAX
% (unsigned long int) base
;
131 for (c
= *s
; c
!= '\0'; c
= *++s
)
135 else if (isalpha (c
))
136 c
= toupper (c
) - 'A' + 10;
141 /* Check for overflow. */
142 if (i
> cutoff
|| (i
== cutoff
&& c
> cutlim
))
146 i
*= (unsigned long int) base
;
151 /* Check if anything actually happened. */
155 /* Store in ENDPTR the address of one character
156 past the last character we converted. */
158 *endptr
= (char *) s
;
161 /* Check for a value that is within the range of
162 `unsigned long int', but outside the range of `long int'. */
164 -(unsigned long int) LONG_MIN
: (unsigned long int) LONG_MAX
))
174 return negative
? LONG_MIN
: LONG_MAX
;
178 /* Return the result of the appropriate sign. */
179 return (negative
? -i
: i
);
182 /* There was no number to convert. */
184 *endptr
= (char *) nptr
;