xref: /original-bsd/lib/libc/stdlib/strtol.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)strtol.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <limits.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 
17 
18 /*
19  * Convert a string to a long integer.
20  *
21  * Ignores `locale' stuff.  Assumes that the upper and lower case
22  * alphabets and digits are each contiguous.
23  */
24 long
25 strtol(nptr, endptr, base)
26 	const char *nptr;
27 	char **endptr;
28 	register int base;
29 {
30 	register const char *s = nptr;
31 	register unsigned long acc;
32 	register int c;
33 	register unsigned long cutoff;
34 	register int neg = 0, any, cutlim;
35 
36 	/*
37 	 * Skip white space and pick up leading +/- sign if any.
38 	 * If base is 0, allow 0x for hex and 0 for octal, else
39 	 * assume decimal; if base is already 16, allow 0x.
40 	 */
41 	do {
42 		c = *s++;
43 	} while (isspace(c));
44 	if (c == '-') {
45 		neg = 1;
46 		c = *s++;
47 	} else if (c == '+')
48 		c = *s++;
49 	if ((base == 0 || base == 16) &&
50 	    c == '0' && (*s == 'x' || *s == 'X')) {
51 		c = s[1];
52 		s += 2;
53 		base = 16;
54 	}
55 	if (base == 0)
56 		base = c == '0' ? 8 : 10;
57 
58 	/*
59 	 * Compute the cutoff value between legal numbers and illegal
60 	 * numbers.  That is the largest legal value, divided by the
61 	 * base.  An input number that is greater than this value, if
62 	 * followed by a legal input character, is too big.  One that
63 	 * is equal to this value may be valid or not; the limit
64 	 * between valid and invalid numbers is then based on the last
65 	 * digit.  For instance, if the range for longs is
66 	 * [-2147483648..2147483647] and the input base is 10,
67 	 * cutoff will be set to 214748364 and cutlim to either
68 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
69 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
70 	 * the number is too big, and we will return a range error.
71 	 *
72 	 * Set any if any `digits' consumed; make it negative to indicate
73 	 * overflow.
74 	 */
75 	cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
76 	cutlim = cutoff % (unsigned long)base;
77 	cutoff /= (unsigned long)base;
78 	for (acc = 0, any = 0;; c = *s++) {
79 		if (isdigit(c))
80 			c -= '0';
81 		else if (isalpha(c))
82 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
83 		else
84 			break;
85 		if (c >= base)
86 			break;
87 		if (any < 0 || acc > cutoff || acc == cutoff && c > cutlim)
88 			any = -1;
89 		else {
90 			any = 1;
91 			acc *= base;
92 			acc += c;
93 		}
94 	}
95 	if (any < 0) {
96 		acc = neg ? LONG_MIN : LONG_MAX;
97 		errno = ERANGE;
98 	} else if (neg)
99 		acc = -acc;
100 	if (endptr != 0)
101 		*endptr = (char *)(any ? s - 1 : nptr);
102 	return (acc);
103 }
104