1169695Skan/*-
2169695Skan * Copyright (c) 1990 The Regents of the University of California.
3169695Skan * All rights reserved.
4169695Skan *
5169695Skan * Redistribution and use in source and binary forms, with or without
6169695Skan * modification, are permitted provided that the following conditions
7169695Skan * are met:
8169695Skan * 1. Redistributions of source code must retain the above copyright
9169695Skan *    notice, this list of conditions and the following disclaimer.
10169695Skan * 2. Redistributions in binary form must reproduce the above copyright
11169695Skan *    notice, this list of conditions and the following disclaimer in the
12169695Skan *    documentation and/or other materials provided with the distribution.
13169695Skan * 3. [rescinded 22 July 1999]
14169695Skan * 4. Neither the name of the University nor the names of its contributors
15169695Skan *    may be used to endorse or promote products derived from this software
16169695Skan *    without specific prior written permission.
17169695Skan *
18169695Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19169695Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20169695Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21169695Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22169695Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23169695Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24169695Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25169695Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26169695Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27169695Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28169695Skan * SUCH DAMAGE.
29169695Skan */
30169695Skan
31169695Skan/*
32169695Skan
33169695Skan@deftypefn Supplemental {long int} strtol (const char *@var{string}, char **@var{endptr}, int @var{base})
34169695Skan@deftypefnx Supplemental {unsigned long int} strtoul (const char *@var{string}, char **@var{endptr}, int @var{base})
35169695Skan
36169695SkanThe @code{strtol} function converts the string in @var{string} to a
37169695Skanlong integer value according to the given @var{base}, which must be
38169695Skanbetween 2 and 36 inclusive, or be the special value 0.  If @var{base}
39169695Skanis 0, @code{strtol} will look for the prefixes @code{0} and @code{0x}
40169695Skanto indicate bases 8 and 16, respectively, else default to base 10.
41169695SkanWhen the base is 16 (either explicitly or implicitly), a prefix of
42169695Skan@code{0x} is allowed.  The handling of @var{endptr} is as that of
43169695Skan@code{strtod} above.  The @code{strtoul} function is the same, except
44169695Skanthat the converted value is unsigned.
45169695Skan
46169695Skan@end deftypefn
47169695Skan
48169695Skan*/
49169695Skan
50169695Skan#ifdef HAVE_CONFIG_H
51169695Skan#include "config.h"
52169695Skan#endif
53169695Skan#ifdef HAVE_LIMITS_H
54169695Skan#include <limits.h>
55169695Skan#endif
56169695Skan#ifdef HAVE_SYS_PARAM_H
57169695Skan#include <sys/param.h>
58169695Skan#endif
59169695Skan#include <errno.h>
60169695Skan#ifdef NEED_DECLARATION_ERRNO
61169695Skanextern int errno;
62169695Skan#endif
63169695Skan#include "safe-ctype.h"
64169695Skan
65169695Skan/* FIXME: It'd be nice to configure around these, but the include files are too
66169695Skan   painful.  These macros should at least be more portable than hardwired hex
67169695Skan   constants. */
68169695Skan
69169695Skan#ifndef ULONG_MAX
70169695Skan#define	ULONG_MAX	((unsigned long)(~0L))		/* 0xFFFFFFFF */
71169695Skan#endif
72169695Skan
73169695Skan#ifndef LONG_MAX
74169695Skan#define	LONG_MAX	((long)(ULONG_MAX >> 1))	/* 0x7FFFFFFF */
75169695Skan#endif
76169695Skan
77169695Skan#ifndef LONG_MIN
78169695Skan#define	LONG_MIN	((long)(~LONG_MAX))		/* 0x80000000 */
79169695Skan#endif
80169695Skan
81169695Skan/*
82169695Skan * Convert a string to a long integer.
83169695Skan *
84169695Skan * Ignores `locale' stuff.  Assumes that the upper and lower case
85169695Skan * alphabets and digits are each contiguous.
86169695Skan */
87169695Skanlong
88169695Skanstrtol(const char *nptr, char **endptr, register int base)
89169695Skan{
90169695Skan	register const char *s = nptr;
91169695Skan	register unsigned long acc;
92169695Skan	register int c;
93169695Skan	register unsigned long cutoff;
94169695Skan	register int neg = 0, any, cutlim;
95169695Skan
96169695Skan	/*
97169695Skan	 * Skip white space and pick up leading +/- sign if any.
98169695Skan	 * If base is 0, allow 0x for hex and 0 for octal, else
99169695Skan	 * assume decimal; if base is already 16, allow 0x.
100169695Skan	 */
101169695Skan	do {
102169695Skan		c = *s++;
103169695Skan	} while (ISSPACE(c));
104169695Skan	if (c == '-') {
105169695Skan		neg = 1;
106169695Skan		c = *s++;
107169695Skan	} else if (c == '+')
108169695Skan		c = *s++;
109169695Skan	if ((base == 0 || base == 16) &&
110169695Skan	    c == '0' && (*s == 'x' || *s == 'X')) {
111169695Skan		c = s[1];
112169695Skan		s += 2;
113169695Skan		base = 16;
114169695Skan	}
115169695Skan	if (base == 0)
116169695Skan		base = c == '0' ? 8 : 10;
117169695Skan
118169695Skan	/*
119169695Skan	 * Compute the cutoff value between legal numbers and illegal
120169695Skan	 * numbers.  That is the largest legal value, divided by the
121169695Skan	 * base.  An input number that is greater than this value, if
122169695Skan	 * followed by a legal input character, is too big.  One that
123169695Skan	 * is equal to this value may be valid or not; the limit
124169695Skan	 * between valid and invalid numbers is then based on the last
125169695Skan	 * digit.  For instance, if the range for longs is
126169695Skan	 * [-2147483648..2147483647] and the input base is 10,
127169695Skan	 * cutoff will be set to 214748364 and cutlim to either
128169695Skan	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
129169695Skan	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
130169695Skan	 * the number is too big, and we will return a range error.
131169695Skan	 *
132169695Skan	 * Set any if any `digits' consumed; make it negative to indicate
133169695Skan	 * overflow.
134169695Skan	 */
135169695Skan	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
136169695Skan	cutlim = cutoff % (unsigned long)base;
137169695Skan	cutoff /= (unsigned long)base;
138169695Skan	for (acc = 0, any = 0;; c = *s++) {
139169695Skan		if (ISDIGIT(c))
140169695Skan			c -= '0';
141169695Skan		else if (ISALPHA(c))
142169695Skan			c -= ISUPPER(c) ? 'A' - 10 : 'a' - 10;
143169695Skan		else
144169695Skan			break;
145169695Skan		if (c >= base)
146169695Skan			break;
147169695Skan		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
148169695Skan			any = -1;
149169695Skan		else {
150169695Skan			any = 1;
151169695Skan			acc *= base;
152169695Skan			acc += c;
153169695Skan		}
154169695Skan	}
155169695Skan	if (any < 0) {
156169695Skan		acc = neg ? LONG_MIN : LONG_MAX;
157169695Skan		errno = ERANGE;
158169695Skan	} else if (neg)
159169695Skan		acc = -acc;
160169695Skan	if (endptr != 0)
161169695Skan		*endptr = (char *) (any ? s - 1 : nptr);
162169695Skan	return (acc);
163169695Skan}
164