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