strtol.c revision 84221
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 * 3. All advertising materials mentioning features or use of this software
1438451Smsmith *    must display the following acknowledgement:
1538451Smsmith *	This product includes software developed by the University of
1638451Smsmith *	California, Berkeley and its contributors.
1738451Smsmith * 4. Neither the name of the University nor the names of its contributors
1838451Smsmith *    may be used to endorse or promote products derived from this software
1938451Smsmith *    without specific prior written permission.
2038451Smsmith *
2138451Smsmith * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2238451Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2338451Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2438451Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2538451Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2638451Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2738451Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2838451Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2938451Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3038451Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3138451Smsmith * SUCH DAMAGE.
3238451Smsmith */
3338451Smsmith
3484221Sdillon#include <sys/cdefs.h>
3584221Sdillon__FBSDID("$FreeBSD: head/lib/libstand/strtol.c 84221 2001-09-30 22:28:01Z dillon $");
3684221Sdillon
3738451Smsmith#if defined(LIBC_SCCS) && !defined(lint)
3838451Smsmithstatic char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 6/4/93";
3938451Smsmith#endif /* LIBC_SCCS and not lint */
4038451Smsmith
4138451Smsmith#include "stand.h"
4238451Smsmith#include <limits.h>
4338451Smsmith
4438451Smsmith/*
4538451Smsmith * Convert a string to a long integer.
4638451Smsmith *
4738451Smsmith * Ignores `locale' stuff.  Assumes that the upper and lower case
4838451Smsmith * alphabets and digits are each contiguous.
4938451Smsmith */
5038451Smsmithlong
5138451Smsmithstrtol(nptr, endptr, base)
5238451Smsmith	const char *nptr;
5338451Smsmith	char **endptr;
5438451Smsmith	register int base;
5538451Smsmith{
5638451Smsmith	register const char *s;
5738451Smsmith	register unsigned long acc;
5838451Smsmith	register unsigned char c;
5938451Smsmith	register unsigned long cutoff;
6038451Smsmith	register int neg = 0, any, cutlim;
6138451Smsmith
6238451Smsmith	/* Be sensible about NULL strings */
6338451Smsmith	if (nptr == NULL)
6438451Smsmith	    nptr = "";
6538451Smsmith	s = nptr;
6638451Smsmith
6738451Smsmith	/*
6838451Smsmith	 * Skip white space and pick up leading +/- sign if any.
6938451Smsmith	 * If base is 0, allow 0x for hex and 0 for octal, else
7038451Smsmith	 * assume decimal; if base is already 16, allow 0x.
7138451Smsmith	 */
7238451Smsmith	do {
7338451Smsmith		c = *s++;
7438451Smsmith	} while (isspace(c));
7538451Smsmith	if (c == '-') {
7638451Smsmith		neg = 1;
7738451Smsmith		c = *s++;
7838451Smsmith	} else if (c == '+')
7938451Smsmith		c = *s++;
8038451Smsmith	if ((base == 0 || base == 16) &&
8138451Smsmith	    c == '0' && (*s == 'x' || *s == 'X')) {
8238451Smsmith		c = s[1];
8338451Smsmith		s += 2;
8438451Smsmith		base = 16;
8538451Smsmith	}
8638451Smsmith	if (base == 0)
8738451Smsmith		base = c == '0' ? 8 : 10;
8838451Smsmith
8938451Smsmith	/*
9038451Smsmith	 * Compute the cutoff value between legal numbers and illegal
9138451Smsmith	 * numbers.  That is the largest legal value, divided by the
9238451Smsmith	 * base.  An input number that is greater than this value, if
9338451Smsmith	 * followed by a legal input character, is too big.  One that
9438451Smsmith	 * is equal to this value may be valid or not; the limit
9538451Smsmith	 * between valid and invalid numbers is then based on the last
9638451Smsmith	 * digit.  For instance, if the range for longs is
9738451Smsmith	 * [-2147483648..2147483647] and the input base is 10,
9838451Smsmith	 * cutoff will be set to 214748364 and cutlim to either
9938451Smsmith	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
10038451Smsmith	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
10138451Smsmith	 * the number is too big, and we will return a range error.
10238451Smsmith	 *
10338451Smsmith	 * Set any if any `digits' consumed; make it negative to indicate
10438451Smsmith	 * overflow.
10538451Smsmith	 */
10638451Smsmith	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
10738451Smsmith	cutlim = cutoff % (unsigned long)base;
10838451Smsmith	cutoff /= (unsigned long)base;
10938451Smsmith	for (acc = 0, any = 0;; c = *s++) {
11038451Smsmith		if (!isascii(c))
11138451Smsmith			break;
11238451Smsmith		if (isdigit(c))
11338451Smsmith			c -= '0';
11438451Smsmith		else if (isalpha(c))
11538451Smsmith			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
11638451Smsmith		else
11738451Smsmith			break;
11838451Smsmith		if (c >= base)
11938451Smsmith			break;
12038451Smsmith		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
12138451Smsmith			any = -1;
12238451Smsmith		else {
12338451Smsmith			any = 1;
12438451Smsmith			acc *= base;
12538451Smsmith			acc += c;
12638451Smsmith		}
12738451Smsmith	}
12838451Smsmith	if (any < 0) {
12938451Smsmith		acc = neg ? LONG_MIN : LONG_MAX;
13038451Smsmith		errno = ERANGE;
13138451Smsmith	} else if (neg)
13238451Smsmith		acc = -acc;
13338451Smsmith	if (endptr != 0)
13438451Smsmith		*endptr = (char *)(any ? s - 1 : nptr);
13538451Smsmith	return (acc);
13638451Smsmith}
137