xref: /csrg-svn/lib/libc/stdlib/strtol.c (revision 61180)
142115Sbostic /*-
2*61180Sbostic  * Copyright (c) 1990, 1993
3*61180Sbostic  *	The Regents of the University of California.  All rights reserved.
435278Sbostic  *
542115Sbostic  * %sccs.include.redist.c%
635278Sbostic  */
735278Sbostic 
835278Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*61180Sbostic static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 06/04/93";
1035278Sbostic #endif /* LIBC_SCCS and not lint */
1135278Sbostic 
1242115Sbostic #include <limits.h>
1335278Sbostic #include <ctype.h>
1442115Sbostic #include <errno.h>
1542179Sbostic #include <stdlib.h>
1635278Sbostic 
1742179Sbostic 
1842115Sbostic /*
1942115Sbostic  * Convert a string to a long integer.
2042115Sbostic  *
2142115Sbostic  * Ignores `locale' stuff.  Assumes that the upper and lower case
2242115Sbostic  * alphabets and digits are each contiguous.
2342115Sbostic  */
2435278Sbostic long
strtol(nptr,endptr,base)2542115Sbostic strtol(nptr, endptr, base)
2646599Sdonn 	const char *nptr;
2746599Sdonn 	char **endptr;
2835278Sbostic 	register int base;
2935278Sbostic {
3046599Sdonn 	register const char *s = nptr;
3142115Sbostic 	register unsigned long acc;
3235278Sbostic 	register int c;
3342115Sbostic 	register unsigned long cutoff;
3442115Sbostic 	register int neg = 0, any, cutlim;
3535278Sbostic 
3642115Sbostic 	/*
3742115Sbostic 	 * Skip white space and pick up leading +/- sign if any.
3842115Sbostic 	 * If base is 0, allow 0x for hex and 0 for octal, else
3942115Sbostic 	 * assume decimal; if base is already 16, allow 0x.
4042115Sbostic 	 */
4142115Sbostic 	do {
4242115Sbostic 		c = *s++;
4342115Sbostic 	} while (isspace(c));
4442115Sbostic 	if (c == '-') {
4542115Sbostic 		neg = 1;
4642115Sbostic 		c = *s++;
4742115Sbostic 	} else if (c == '+')
4842115Sbostic 		c = *s++;
4942115Sbostic 	if ((base == 0 || base == 16) &&
5042115Sbostic 	    c == '0' && (*s == 'x' || *s == 'X')) {
5142115Sbostic 		c = s[1];
5242115Sbostic 		s += 2;
5342115Sbostic 		base = 16;
5435278Sbostic 	}
5542115Sbostic 	if (base == 0)
5642115Sbostic 		base = c == '0' ? 8 : 10;
5735278Sbostic 
5835278Sbostic 	/*
5942115Sbostic 	 * Compute the cutoff value between legal numbers and illegal
6042115Sbostic 	 * numbers.  That is the largest legal value, divided by the
6142115Sbostic 	 * base.  An input number that is greater than this value, if
6242115Sbostic 	 * followed by a legal input character, is too big.  One that
6342115Sbostic 	 * is equal to this value may be valid or not; the limit
6442115Sbostic 	 * between valid and invalid numbers is then based on the last
6542115Sbostic 	 * digit.  For instance, if the range for longs is
6642115Sbostic 	 * [-2147483648..2147483647] and the input base is 10,
6742115Sbostic 	 * cutoff will be set to 214748364 and cutlim to either
6842115Sbostic 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
6942115Sbostic 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
7042115Sbostic 	 * the number is too big, and we will return a range error.
7142115Sbostic 	 *
7242115Sbostic 	 * Set any if any `digits' consumed; make it negative to indicate
7342115Sbostic 	 * overflow.
7435278Sbostic 	 */
7542115Sbostic 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
7642115Sbostic 	cutlim = cutoff % (unsigned long)base;
7742115Sbostic 	cutoff /= (unsigned long)base;
7842115Sbostic 	for (acc = 0, any = 0;; c = *s++) {
7935278Sbostic 		if (isdigit(c))
8035278Sbostic 			c -= '0';
8142115Sbostic 		else if (isalpha(c))
8242115Sbostic 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
8342115Sbostic 		else
8442115Sbostic 			break;
8542115Sbostic 		if (c >= base)
8642115Sbostic 			break;
8742115Sbostic 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
8842115Sbostic 			any = -1;
8935278Sbostic 		else {
9042115Sbostic 			any = 1;
9142115Sbostic 			acc *= base;
9242115Sbostic 			acc += c;
9335278Sbostic 		}
9435278Sbostic 	}
9542115Sbostic 	if (any < 0) {
9642115Sbostic 		acc = neg ? LONG_MIN : LONG_MAX;
9742115Sbostic 		errno = ERANGE;
9842115Sbostic 	} else if (neg)
9942115Sbostic 		acc = -acc;
10042115Sbostic 	if (endptr != 0)
10158776Storek 		*endptr = (char *)(any ? s - 1 : nptr);
10242115Sbostic 	return (acc);
10335278Sbostic }
104