1 /* Copyright (C) 1991, 1992, 1994, 1995 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 2 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 02139, USA. */
20 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
21 If BASE is 0 the base is determined by the presence of a leading
22 zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
23 If BASE is < 2 or > 36, it is reset to 10.
24 If ENDPTR is not NULL, a pointer to the character after the last
25 one converted is stored in *ENDPTR. */
29 int strtol (nptr, endptr, base)
35 register unsigned int cutoff;
36 register unsigned int cutlim;
37 register unsigned int i;
38 register const char *s;
39 register unsigned char c;
40 const char *save, *end;
43 if (base < 0 || base == 1 || base > 36)
48 /* Skip white space. */
49 while (((unsigned char) *s) <= 32 && *s)
54 /* Check for a sign. */
58 } else if (*s == '+') {
64 if ((base == 16 && s[0] == '0' && (s[1] == 'X')) || (s[1] == 'x'))
67 /* If BASE is zero, figure it out ourselves. */
70 if (s[1] == 'X' || s[1] == 'x') {
79 /* Save the pointer so we can check later if anything happened. */
84 cutoff = 0x7FFFFFFF / (unsigned int) base;
85 cutlim = 0x7FFFFFFF % (unsigned int) base;
89 for (c = *s; c != '\0'; c = *++s) {
92 if (c >= '0' && c <= '9')
94 else if (c >= 'A' && c <= 'Z')
96 else if (c >= 'a' && c <= 'z')
102 /* Check for overflow. */
103 if (i > cutoff || (i == cutoff && c > cutlim))
106 i *= (unsigned int) base;
111 /* Check if anything actually happened. */
115 /* Store in ENDPTR the address of one character
116 past the last character we converted. */
118 *endptr = (char *) s;
121 return negative ? (int) 0x80000000 : (int) 0x7FFFFFFF;
123 /* Return the result of the appropriate sign. */
124 return (negative ? -i : i);
127 /* We must handle a special case here: the base is 0 or 16 and the
128 first two characters and '0' and 'x', but the rest are no
129 hexadecimal digits. This is no error case. We return 0 and
130 ENDPTR points to the `x`. */
132 if (save - nptr >= 2 && tolower (save[-1]) == 'x' && save[-2] == '0')
133 *endptr = (char *) &save[-1];
135 /* There was no number to convert. */
136 *endptr = (char *) nptr;