xref: /openbsd-src/sys/lib/libsa/strtol.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /* $OpenBSD: strtol.c,v 1.3 1998/05/20 00:07:17 art Exp $ */
2 /* Modified strtol() from stdlib */
3 /*-
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/param.h>
37 
38 /*
39  * Convert a string to a long integer.
40  *
41  * Ignores `locale' stuff.  Assumes that the upper and lower case
42  * alphabets and digits are each contiguous.
43  */
44 long
45 strtol(nptr, endptr, base)
46 	const char *nptr;
47 	char **endptr;
48 	register int base;
49 {
50 	register const char *s;
51 	register long acc, cutoff;
52 	register int c;
53 	register int neg, any, cutlim;
54 
55 	/*
56 	 * Skip white space and pick up leading +/- sign if any.
57 	 * If base is 0, allow 0x for hex and 0 for octal, else
58 	 * assume decimal; if base is already 16, allow 0x.
59 	 */
60 	s = nptr;
61 	do {
62 		c = (unsigned char) *s++;
63 	} while (c <= ' ' || c >= 0x7f);
64 	if (c == '-') {
65 		neg = 1;
66 		c = *s++;
67 	} else {
68 		neg = 0;
69 		if (c == '+')
70 			c = *s++;
71 	}
72 	if ((base == 0 || base == 16) &&
73 	    c == '0' && (*s == 'x' || *s == 'X')) {
74 		c = s[1];
75 		s += 2;
76 		base = 16;
77 	}
78 	if (base == 0)
79 		base = c == '0' ? 8 : 10;
80 
81 	/*
82 	 * Compute the cutoff value between legal numbers and illegal
83 	 * numbers.  That is the largest legal value, divided by the
84 	 * base.  An input number that is greater than this value, if
85 	 * followed by a legal input character, is too big.  One that
86 	 * is equal to this value may be valid or not; the limit
87 	 * between valid and invalid numbers is then based on the last
88 	 * digit.  For instance, if the range for longs is
89 	 * [-2147483648..2147483647] and the input base is 10,
90 	 * cutoff will be set to 214748364 and cutlim to either
91 	 * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
92 	 * a value > 214748364, or equal but the next digit is > 7 (or 8),
93 	 * the number is too big, and we will return a range error.
94 	 *
95 	 * Set any if any `digits' consumed; make it negative to indicate
96 	 * overflow.
97 	 */
98 	cutoff = neg ? LONG_MIN : LONG_MAX;
99 	cutlim = cutoff % base;
100 	cutoff /= base;
101 	if (neg) {
102 		if (cutlim > 0) {
103 			cutlim -= base;
104 			cutoff += 1;
105 		}
106 		cutlim = -cutlim;
107 	}
108 	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
109 		if (c >= '0' && c <= '9')
110 			c -= '0';
111 		else if (c >= 'A' && c <= 'Z')
112 			c -= 'A' - 10;
113 		else if (c >= 'a' && c <= 'z')
114 			c -= 'a' - 10;
115 		else
116 			break;
117 		if (c >= base)
118 			break;
119 		if (any < 0)
120 			continue;
121 		if (neg) {
122 			if (acc < cutoff || (acc == cutoff && c > cutlim)) {
123 				any = -1;
124 				acc = LONG_MIN;
125 			} else {
126 				any = 1;
127 				acc *= base;
128 				acc -= c;
129 			}
130 		} else {
131 			if (acc > cutoff || (acc == cutoff && c > cutlim)) {
132 				any = -1;
133 				acc = LONG_MAX;
134 			} else {
135 				any = 1;
136 				acc *= base;
137 				acc += c;
138 			}
139 		}
140 	}
141 	if (endptr != 0)
142 		*endptr = (char *) (any ? s - 1 : nptr);
143 	return (acc);
144 }
145