1 /* Copyright (C) 1991, 1992 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
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
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 GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
20 #if defined (CONFIG_BROKETS)
21 /* We use <config.h> instead of "config.h" so that a compilation
22 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
23 (which it would do because it found this file in $srcdir). */
38 #define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
42 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
46 #define LONG_MIN (-LONG_MAX - 1)
61 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
62 If BASE is 0 the base is determined by the presence of a leading
63 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
64 If BASE is < 2 or > 36, it is reset to 10.
65 If ENDPTR is not NULL, a pointer to the character after the last
66 one converted is stored in *ENDPTR. */
69 #define strtol strtoul
73 strtol (nptr, endptr, base)
79 register unsigned long int cutoff;
80 register unsigned int cutlim;
81 register unsigned long int i;
82 register const char *s;
83 register unsigned char c;
87 if (base < 0 || base == 1 || base > 36)
92 /* Skip white space. */
98 /* Check for a sign. */
112 if (base == 16 && s[0] == '0' && toupper (s[1]) == 'X')
115 /* If BASE is zero, figure it out ourselves. */
120 if (toupper (s[1]) == 'X')
132 /* Save the pointer so we can check later if anything happened. */
135 cutoff = ULONG_MAX / (unsigned long int) base;
136 cutlim = ULONG_MAX % (unsigned long int) base;
140 for (c = *s; c != '\0'; c = *++s)
144 else if (isalpha (c))
145 c = toupper (c) - 'A' + 10;
150 /* Check for overflow. */
151 if (i > cutoff || (i == cutoff && c > cutlim))
155 i *= (unsigned long int) base;
160 /* Check if anything actually happened. */
164 /* Store in ENDPTR the address of one character
165 past the last character we converted. */
167 *endptr = (char *) s;
170 /* Check for a value that is within the range of
171 `unsigned long int', but outside the range of `long int'. */
173 -(unsigned long int) LONG_MIN : (unsigned long int) LONG_MAX))
183 return negative ? LONG_MIN : LONG_MAX;
187 /* Return the result of the appropriate sign. */
188 return (negative ? -i : i);
191 /* There was no number to convert. */
193 *endptr = (char *) nptr;