aed233fd8fc91ac55d684a77c5b5b6d718aa3366
[platform/upstream/bash.git] / lib / sh / strtol.c
1 /* strtol - Convert string representation of a number into an integer value.
2    Copyright (C) 1997 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify it
5    under the terms of the GNU General Public License as published by the
6    Free Software Foundation; either version 2, or (at your option) any
7    later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 #include <config.h>
19
20 #if !defined (HAVE_STRTOL)
21
22 #include <ctype.h>
23 #include <errno.h>
24
25 #ifndef errno
26 extern int errno;
27 #endif
28
29 #ifndef __set_errno
30 #  define __set_errno(Val) errno = (Val)
31 #endif
32
33 #ifdef HAVE_LIMITS_H
34 #  include <limits.h>
35 #endif
36
37 #include <bashansi.h>
38
39 #ifndef NULL
40 #  define NULL 0
41 #endif
42
43 /* Nonzero if we are defining `strtoul', operating on unsigned integers.  */
44 #ifndef UNSIGNED
45 # define UNSIGNED 0
46 # define RETTYPE long
47 #else
48 # define RETTYPE unsigned long
49 #endif
50
51 /* Determine the name.  */
52 #if UNSIGNED
53 #  define strtol strtoul
54 #endif
55
56 #ifndef CHAR_BIT
57 #  define CHAR_BIT 8
58 #endif
59
60 #ifndef ULONG_MAX
61 #  define ULONG_MAX ((unsigned long) ~(unsigned long) 0)
62 #  define ULONG_MIN ((unsigned long) 0 - ULONG_MAX)
63 #endif
64
65 #ifndef LONG_MAX
66 #  define LONG_MAX ((long) (ULONG_MAX >> 1))
67 #  define LONG_MIN ((long) (0 - LONG_MAX))
68 #endif
69
70 /* Convert NPTR to an `unsigned long int' or `long int' in base BASE.
71    If BASE is 0 the base is determined by the presence of a leading
72    zero, indicating octal or a leading "0x" or "0X", indicating hexadecimal.
73    If BASE is < 2 or > 36, it is reset to 10.
74    If ENDPTR is not NULL, a pointer to the character after the last
75    one converted is stored in *ENDPTR.  */
76
77 RETTYPE
78 strtol (nptr, endptr, base)
79      const char *nptr;
80      char **endptr;
81      int base;
82 {
83   int negative;
84   register unsigned long cutoff, i;
85   register unsigned int cutlim;
86   register const char *s;
87   register unsigned char c;
88   const char *save, *end;
89   int overflow;
90
91   if (base < 0 || base == 1 || base > 36)
92     base = 10;
93
94   save = s = nptr;
95
96   /* Skip white space.  */
97   while (isspace (*s))
98     ++s;
99   if (*s == '\0')
100     goto noconv;
101
102   /* Check for a sign.  */
103   if (*s == '-' || *s == '+')
104     {
105       negative = (*s == '-');
106       ++s;
107     }
108   else
109     negative = 0;
110
111   if (base == 16 && *s == '0' && toupper (s[1]) == 'X')
112     s += 2;
113
114   /* If BASE is zero, figure it out ourselves.  */
115   if (base == 0)
116     if (*s == '0')
117       {
118         if (toupper (s[1]) == 'X')
119           {
120             s += 2;
121             base = 16;
122           }
123         else
124           base = 8;
125       }
126     else
127       base = 10;
128
129   /* Save the pointer so we can check later if anything happened.  */
130   save = s;
131
132   end = NULL;
133
134   cutoff = ULONG_MAX / (unsigned long int) base;
135   cutlim = ULONG_MAX % (unsigned long int) base;
136
137   overflow = 0;
138   i = 0;
139   for (c = *s; c != '\0'; c = *++s)
140     {
141       if (s == end)
142         break;
143
144       if (c >= '0' && c <= '9')
145         c -= '0';
146       else if (isalpha (c))
147         c = toupper (c) - 'A' + 10;
148       else
149         break;
150
151       if ((int) c >= base)
152         break;
153
154       /* Check for overflow.  */
155       if (i > cutoff || (i == cutoff && c > cutlim))
156         overflow = 1;
157       else
158         {
159           i *= (unsigned long int) base;
160           i += c;
161         }
162     }
163
164   /* Check if anything actually happened.  */
165   if (s == save)
166     goto noconv;
167
168   /* Store in ENDPTR the address of one character
169      past the last character we converted.  */
170   if (endptr != NULL)
171     *endptr = (char *) s;
172
173 #if !UNSIGNED
174   /* Check for a value that is within the range of
175      `unsigned long int', but outside the range of `long int'.  */
176   if (overflow == 0
177       && i > (negative
178               ? -((unsigned long) (LONG_MIN + 1)) + 1
179               : (unsigned long) LONG_MAX))
180     overflow = 1;
181 #endif
182
183   if (overflow)
184     {
185       __set_errno (ERANGE);
186 #if UNSIGNED
187       return ULONG_MAX;
188 #else
189       return negative ? LONG_MIN : LONG_MAX;
190 #endif
191     }
192
193   /* Return the result with the appropriate sign.  */
194   return (negative ? -i : i);
195
196 noconv:
197   /* We must handle a special case here: the base is 0 or 16 and the
198      first two characters are '0' and 'x', but the rest are no
199      hexadecimal digits.  This is no error case.  We return 0 and
200      ENDPTR points to the `x'.  */
201   if (endptr != NULL)
202     {
203       if (save - nptr >= 2 && toupper (save[-1]) == 'X' && save[-2] == '0')
204         *endptr = (char *) &save[-1];
205       else
206         /*  There was no number to convert.  */
207         *endptr = (char *) nptr;
208     }
209
210   return 0L;
211 }
212
213 #endif /* !HAVE_STRTOL */