.
[gnulib.git] / lib / strtol.c
1 /* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
2
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)
6    any later version.
7
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.
12
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.  */
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #include <ctype.h>
22 #include <errno.h>
23 #ifndef errno
24 extern int errno;
25 #endif
26
27 #if HAVE_LIMITS_H
28 #include <limits.h>
29 #endif
30
31 #ifndef ULONG_MAX
32 #define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
33 #endif
34
35 #ifndef LONG_MAX
36 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
37 #endif
38
39 #ifndef LONG_MIN
40 #define LONG_MIN (-LONG_MAX - 1)
41 #endif
42
43 #if STDC_HEADERS
44 #include <stddef.h>
45 #include <stdlib.h>
46 #else
47 #define NULL 0
48 #endif
49
50 #ifndef UNSIGNED
51 #define UNSIGNED        0
52 #endif
53
54 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
55    If BASE is 0 the base is determined by the presence of a leading
56    zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
57    If BASE is < 2 or > 36, it is reset to 10.
58    If ENDPTR is not NULL, a pointer to the character after the last
59    one converted is stored in *ENDPTR.  */
60 #if     UNSIGNED
61 unsigned long int
62 #define strtol  strtoul
63 #else
64 long int
65 #endif
66 strtol (nptr, endptr, base)
67      const char *nptr;
68      char **endptr;
69      int base;
70 {
71   int negative;
72   register unsigned long int cutoff;
73   register unsigned int cutlim;
74   register unsigned long int i;
75   register const char *s;
76   register unsigned char c;
77   const char *save;
78   int overflow;
79
80   if (base < 0 || base == 1 || base > 36)
81     base = 10;
82
83   s = nptr;
84
85   /* Skip white space.  */
86   while (isspace (*s))
87     ++s;
88   if (*s == '\0')
89     goto noconv;
90
91   /* Check for a sign.  */
92   if (*s == '-')
93     {
94       negative = 1;
95       ++s;
96     }
97   else if (*s == '+')
98     {
99       negative = 0;
100       ++s;
101     }
102   else
103     negative = 0;
104
105   if (base == 16 && s[0] == '0' && toupper (s[1]) == 'X')
106     s += 2;
107
108   /* If BASE is zero, figure it out ourselves.  */
109   if (base == 0)
110     {
111       if (*s == '0')
112         {
113           if (toupper (s[1]) == 'X')
114             {
115               s += 2;
116               base = 16;
117             }
118           else
119             base = 8;
120         }
121       else
122         base = 10;
123     }
124
125   /* Save the pointer so we can check later if anything happened.  */
126   save = s;
127
128   cutoff = ULONG_MAX / (unsigned long int) base;
129   cutlim = ULONG_MAX % (unsigned long int) base;
130
131   overflow = 0;
132   i = 0;
133   for (c = *s; c != '\0'; c = *++s)
134     {
135       if (isdigit (c))
136         c -= '0';
137       else if (isalpha (c))
138         c = toupper (c) - 'A' + 10;
139       else
140         break;
141       if (c >= base)
142         break;
143       /* Check for overflow.  */
144       if (i > cutoff || (i == cutoff && c > cutlim))
145         overflow = 1;
146       else
147         {
148           i *= (unsigned long int) base;
149           i += c;
150         }
151     }
152
153   /* Check if anything actually happened.  */
154   if (s == save)
155     goto noconv;
156
157   /* Store in ENDPTR the address of one character
158      past the last character we converted.  */
159   if (endptr != NULL)
160     *endptr = (char *) s;
161
162 #if     !UNSIGNED
163   /* Check for a value that is within the range of
164      `unsigned long int', but outside the range of `long int'.  */
165   if (i > (negative ?
166            -(unsigned long int) LONG_MIN : (unsigned long int) LONG_MAX))
167     overflow = 1;
168 #endif
169
170   if (overflow)
171     {
172       errno = ERANGE;
173 #if     UNSIGNED
174       return ULONG_MAX;
175 #else
176       return negative ? LONG_MIN : LONG_MAX;
177 #endif
178     }
179
180   /* Return the result of the appropriate sign.  */
181   return (negative ? -i : i);
182
183 noconv:;
184   /* There was no number to convert.  */
185   if (endptr != NULL)
186     *endptr = (char *) nptr;
187   return 0L;
188 }