1*16dce513Schristos /*-
2*16dce513Schristos * Copyright (c) 1990 The Regents of the University of California.
3*16dce513Schristos * All rights reserved.
4*16dce513Schristos *
5*16dce513Schristos * Redistribution and use in source and binary forms, with or without
6*16dce513Schristos * modification, are permitted provided that the following conditions
7*16dce513Schristos * are met:
8*16dce513Schristos * 1. Redistributions of source code must retain the above copyright
9*16dce513Schristos * notice, this list of conditions and the following disclaimer.
10*16dce513Schristos * 2. Redistributions in binary form must reproduce the above copyright
11*16dce513Schristos * notice, this list of conditions and the following disclaimer in the
12*16dce513Schristos * documentation and/or other materials provided with the distribution.
13*16dce513Schristos * 3. [rescinded 22 July 1999]
14*16dce513Schristos * 4. Neither the name of the University nor the names of its contributors
15*16dce513Schristos * may be used to endorse or promote products derived from this software
16*16dce513Schristos * without specific prior written permission.
17*16dce513Schristos *
18*16dce513Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*16dce513Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*16dce513Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*16dce513Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*16dce513Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*16dce513Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*16dce513Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*16dce513Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*16dce513Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*16dce513Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*16dce513Schristos * SUCH DAMAGE.
29*16dce513Schristos */
30*16dce513Schristos
31*16dce513Schristos /*
32*16dce513Schristos
33*16dce513Schristos @deftypefn Supplemental {long int} strtol (const char *@var{string}, @
34*16dce513Schristos char **@var{endptr}, int @var{base})
35*16dce513Schristos @deftypefnx Supplemental {unsigned long int} strtoul (const char *@var{string}, @
36*16dce513Schristos char **@var{endptr}, int @var{base})
37*16dce513Schristos
38*16dce513Schristos The @code{strtol} function converts the string in @var{string} to a
39*16dce513Schristos long integer value according to the given @var{base}, which must be
40*16dce513Schristos between 2 and 36 inclusive, or be the special value 0. If @var{base}
41*16dce513Schristos is 0, @code{strtol} will look for the prefixes @code{0} and @code{0x}
42*16dce513Schristos to indicate bases 8 and 16, respectively, else default to base 10.
43*16dce513Schristos When the base is 16 (either explicitly or implicitly), a prefix of
44*16dce513Schristos @code{0x} is allowed. The handling of @var{endptr} is as that of
45*16dce513Schristos @code{strtod} above. The @code{strtoul} function is the same, except
46*16dce513Schristos that the converted value is unsigned.
47*16dce513Schristos
48*16dce513Schristos @end deftypefn
49*16dce513Schristos
50*16dce513Schristos */
51*16dce513Schristos
52*16dce513Schristos #ifdef HAVE_CONFIG_H
53*16dce513Schristos #include "config.h"
54*16dce513Schristos #endif
55*16dce513Schristos #ifdef HAVE_LIMITS_H
56*16dce513Schristos #include <limits.h>
57*16dce513Schristos #endif
58*16dce513Schristos #ifdef HAVE_SYS_PARAM_H
59*16dce513Schristos #include <sys/param.h>
60*16dce513Schristos #endif
61*16dce513Schristos #include <errno.h>
62*16dce513Schristos #ifdef NEED_DECLARATION_ERRNO
63*16dce513Schristos extern int errno;
64*16dce513Schristos #endif
65*16dce513Schristos #include "safe-ctype.h"
66*16dce513Schristos
67*16dce513Schristos /* FIXME: It'd be nice to configure around these, but the include files are too
68*16dce513Schristos painful. These macros should at least be more portable than hardwired hex
69*16dce513Schristos constants. */
70*16dce513Schristos
71*16dce513Schristos #ifndef ULONG_MAX
72*16dce513Schristos #define ULONG_MAX ((unsigned long)(~0L)) /* 0xFFFFFFFF */
73*16dce513Schristos #endif
74*16dce513Schristos
75*16dce513Schristos #ifndef LONG_MAX
76*16dce513Schristos #define LONG_MAX ((long)(ULONG_MAX >> 1)) /* 0x7FFFFFFF */
77*16dce513Schristos #endif
78*16dce513Schristos
79*16dce513Schristos #ifndef LONG_MIN
80*16dce513Schristos #define LONG_MIN ((long)(~LONG_MAX)) /* 0x80000000 */
81*16dce513Schristos #endif
82*16dce513Schristos
83*16dce513Schristos /*
84*16dce513Schristos * Convert a string to a long integer.
85*16dce513Schristos *
86*16dce513Schristos * Ignores `locale' stuff. Assumes that the upper and lower case
87*16dce513Schristos * alphabets and digits are each contiguous.
88*16dce513Schristos */
89*16dce513Schristos long
strtol(const char * nptr,char ** endptr,register int base)90*16dce513Schristos strtol(const char *nptr, char **endptr, register int base)
91*16dce513Schristos {
92*16dce513Schristos register const char *s = nptr;
93*16dce513Schristos register unsigned long acc;
94*16dce513Schristos register int c;
95*16dce513Schristos register unsigned long cutoff;
96*16dce513Schristos register int neg = 0, any, cutlim;
97*16dce513Schristos
98*16dce513Schristos /*
99*16dce513Schristos * Skip white space and pick up leading +/- sign if any.
100*16dce513Schristos * If base is 0, allow 0x for hex and 0 for octal, else
101*16dce513Schristos * assume decimal; if base is already 16, allow 0x.
102*16dce513Schristos */
103*16dce513Schristos do {
104*16dce513Schristos c = *s++;
105*16dce513Schristos } while (ISSPACE(c));
106*16dce513Schristos if (c == '-') {
107*16dce513Schristos neg = 1;
108*16dce513Schristos c = *s++;
109*16dce513Schristos } else if (c == '+')
110*16dce513Schristos c = *s++;
111*16dce513Schristos if ((base == 0 || base == 16) &&
112*16dce513Schristos c == '0' && (*s == 'x' || *s == 'X')) {
113*16dce513Schristos c = s[1];
114*16dce513Schristos s += 2;
115*16dce513Schristos base = 16;
116*16dce513Schristos }
117*16dce513Schristos if (base == 0)
118*16dce513Schristos base = c == '0' ? 8 : 10;
119*16dce513Schristos
120*16dce513Schristos /*
121*16dce513Schristos * Compute the cutoff value between legal numbers and illegal
122*16dce513Schristos * numbers. That is the largest legal value, divided by the
123*16dce513Schristos * base. An input number that is greater than this value, if
124*16dce513Schristos * followed by a legal input character, is too big. One that
125*16dce513Schristos * is equal to this value may be valid or not; the limit
126*16dce513Schristos * between valid and invalid numbers is then based on the last
127*16dce513Schristos * digit. For instance, if the range for longs is
128*16dce513Schristos * [-2147483648..2147483647] and the input base is 10,
129*16dce513Schristos * cutoff will be set to 214748364 and cutlim to either
130*16dce513Schristos * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
131*16dce513Schristos * a value > 214748364, or equal but the next digit is > 7 (or 8),
132*16dce513Schristos * the number is too big, and we will return a range error.
133*16dce513Schristos *
134*16dce513Schristos * Set any if any `digits' consumed; make it negative to indicate
135*16dce513Schristos * overflow.
136*16dce513Schristos */
137*16dce513Schristos cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
138*16dce513Schristos cutlim = cutoff % (unsigned long)base;
139*16dce513Schristos cutoff /= (unsigned long)base;
140*16dce513Schristos for (acc = 0, any = 0;; c = *s++) {
141*16dce513Schristos if (ISDIGIT(c))
142*16dce513Schristos c -= '0';
143*16dce513Schristos else if (ISALPHA(c))
144*16dce513Schristos c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
145*16dce513Schristos else
146*16dce513Schristos break;
147*16dce513Schristos if (c >= base)
148*16dce513Schristos break;
149*16dce513Schristos if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
150*16dce513Schristos any = -1;
151*16dce513Schristos else {
152*16dce513Schristos any = 1;
153*16dce513Schristos acc *= base;
154*16dce513Schristos acc += c;
155*16dce513Schristos }
156*16dce513Schristos }
157*16dce513Schristos if (any < 0) {
158*16dce513Schristos acc = neg ? LONG_MIN : LONG_MAX;
159*16dce513Schristos errno = ERANGE;
160*16dce513Schristos } else if (neg)
161*16dce513Schristos acc = -acc;
162*16dce513Schristos if (endptr != 0)
163*16dce513Schristos *endptr = (char *) (any ? s - 1 : nptr);
164*16dce513Schristos return (acc);
165*16dce513Schristos }
166