138451Smsmith/*-
238451Smsmith * Copyright (c) 1990, 1993
338451Smsmith *	The Regents of the University of California.  All rights reserved.
438451Smsmith *
538451Smsmith * Redistribution and use in source and binary forms, with or without
638451Smsmith * modification, are permitted provided that the following conditions
738451Smsmith * are met:
838451Smsmith * 1. Redistributions of source code must retain the above copyright
938451Smsmith *    notice, this list of conditions and the following disclaimer.
1038451Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1138451Smsmith *    notice, this list of conditions and the following disclaimer in the
1238451Smsmith *    documentation and/or other materials provided with the distribution.
1338451Smsmith * 4. Neither the name of the University nor the names of its contributors
1438451Smsmith *    may be used to endorse or promote products derived from this software
1538451Smsmith *    without specific prior written permission.
1638451Smsmith *
1738451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1838451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1938451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2038451Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2138451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2238451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2338451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2438451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2538451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2638451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2738451Smsmith * SUCH DAMAGE.
2838451Smsmith */
2938451Smsmith
3084221Sdillon#include <sys/cdefs.h>
3184221Sdillon__FBSDID("$FreeBSD$");
3284221Sdillon
3338451Smsmith#if defined(LIBC_SCCS) && !defined(lint)
3438451Smsmithstatic char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
3538451Smsmith#endif /* LIBC_SCCS and not lint */
3638451Smsmith
3738451Smsmith#include "stand.h"
3838451Smsmith#include <limits.h>
3938451Smsmith
4038451Smsmith/*
4138451Smsmith * Convert a string to a long integer.
4238451Smsmith *
4338451Smsmith * Ignores `locale' stuff.  Assumes that the upper and lower case
4438451Smsmith * alphabets and digits are each contiguous.
4538451Smsmith */
4638451Smsmithlong
4738451Smsmithstrtol(nptr, endptr, base)
4838451Smsmith	const char *nptr;
4938451Smsmith	char **endptr;
5092913Sobrien	int base;
5138451Smsmith{
5292913Sobrien	const char *s;
5392913Sobrien	unsigned long acc;
5492913Sobrien	unsigned char c;
5592913Sobrien	unsigned long cutoff;
5692913Sobrien	int neg = 0, any, cutlim;
5738451Smsmith
5838451Smsmith	/* Be sensible about NULL strings */
5938451Smsmith	if (nptr == NULL)
6038451Smsmith	    nptr = "";
6138451Smsmith	s = nptr;
6238451Smsmith
6338451Smsmith	/*
6438451Smsmith	 * Skip white space and pick up leading +/- sign if any.
6538451Smsmith	 * If base is 0, allow 0x for hex and 0 for octal, else
6638451Smsmith	 * assume decimal; if base is already 16, allow 0x.
6738451Smsmith	 */
6838451Smsmith	do {
6938451Smsmith		c = *s++;
7038451Smsmith	} while (isspace(c));
7138451Smsmith	if (c == '-') {
7238451Smsmith		neg = 1;
7338451Smsmith		c = *s++;
7438451Smsmith	} else if (c == '+')
7538451Smsmith		c = *s++;
7638451Smsmith	if ((base == 0 || base == 16) &&
7738451Smsmith	    c == '0' && (*s == 'x' || *s == 'X')) {
7838451Smsmith		c = s[1];
7938451Smsmith		s += 2;
8038451Smsmith		base = 16;
8138451Smsmith	}
8238451Smsmith	if (base == 0)
8338451Smsmith		base = c == '0' ? 8 : 10;
8438451Smsmith
8538451Smsmith	/*
8638451Smsmith	 * Compute the cutoff value between legal numbers and illegal
8738451Smsmith	 * numbers.  That is the largest legal value, divided by the
8838451Smsmith	 * base.  An input number that is greater than this value, if
8938451Smsmith	 * followed by a legal input character, is too big.  One that
9038451Smsmith	 * is equal to this value may be valid or not; the limit
9138451Smsmith	 * between valid and invalid numbers is then based on the last
9238451Smsmith	 * digit.  For instance, if the range for longs is
9338451Smsmith	 * [-2147483648..2147483647] and the input base is 10,
9438451Smsmith	 * cutoff will be set to 214748364 and cutlim to either
9538451Smsmith	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
9638451Smsmith	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
9738451Smsmith	 * the number is too big, and we will return a range error.
9838451Smsmith	 *
9938451Smsmith	 * Set any if any `digits' consumed; make it negative to indicate
10038451Smsmith	 * overflow.
10138451Smsmith	 */
10238451Smsmith	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
10338451Smsmith	cutlim = cutoff % (unsigned long)base;
10438451Smsmith	cutoff /= (unsigned long)base;
10538451Smsmith	for (acc = 0, any = 0;; c = *s++) {
10638451Smsmith		if (!isascii(c))
10738451Smsmith			break;
10838451Smsmith		if (isdigit(c))
10938451Smsmith			c -= '0';
11038451Smsmith		else if (isalpha(c))
11138451Smsmith			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
11238451Smsmith		else
11338451Smsmith			break;
11438451Smsmith		if (c >= base)
11538451Smsmith			break;
11638451Smsmith		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
11738451Smsmith			any = -1;
11838451Smsmith		else {
11938451Smsmith			any = 1;
12038451Smsmith			acc *= base;
12138451Smsmith			acc += c;
12238451Smsmith		}
12338451Smsmith	}
12438451Smsmith	if (any < 0) {
12538451Smsmith		acc = neg ? LONG_MIN : LONG_MAX;
12638451Smsmith		errno = ERANGE;
12738451Smsmith	} else if (neg)
12838451Smsmith		acc = -acc;
12938451Smsmith	if (endptr != 0)
13038451Smsmith		*endptr = (char *)(any ? s - 1 : nptr);
13138451Smsmith	return (acc);
13238451Smsmith}
133