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