xref: /openbsd-src/lib/libc/stdlib/strtoul.c (revision 6022c2c21c00ed222314d1a82f05f5778bfe1e32)
1*6022c2c2Smillert /*	$OpenBSD: strtoul.c,v 1.11 2017/07/06 16:23:11 millert Exp $ */
2df930be7Sderaadt /*
3b252b5f6Sschwarze  * 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 an unsigned long integer.
38df930be7Sderaadt  *
39df930be7Sderaadt  * Ignores `locale' stuff.  Assumes that the upper and lower case
40df930be7Sderaadt  * alphabets and digits are each contiguous.
41df930be7Sderaadt  */
42df930be7Sderaadt unsigned long
strtoul(const char * nptr,char ** endptr,int base)43d8bc04e4Spat strtoul(const char *nptr, char **endptr, int base)
44df930be7Sderaadt {
45d8bc04e4Spat 	const char *s;
46d8bc04e4Spat 	unsigned long acc, cutoff;
47d8bc04e4Spat 	int c;
48d8bc04e4Spat 	int neg, any, cutlim;
49df930be7Sderaadt 
50df930be7Sderaadt 	/*
51df930be7Sderaadt 	 * See strtol for comments as to the logic used.
52df930be7Sderaadt 	 */
53b252b5f6Sschwarze 	if (base < 0 || base == 1 || base > 36) {
54b252b5f6Sschwarze 		if (endptr != 0)
55b252b5f6Sschwarze 			*endptr = (char *)nptr;
56b252b5f6Sschwarze 		errno = EINVAL;
57b252b5f6Sschwarze 		return 0;
58b252b5f6Sschwarze 	}
59b252b5f6Sschwarze 
60e3237c4aSderaadt 	s = nptr;
61df930be7Sderaadt 	do {
6266ce1e86Sderaadt 		c = (unsigned char) *s++;
63df930be7Sderaadt 	} while (isspace(c));
64df930be7Sderaadt 	if (c == '-') {
65df930be7Sderaadt 		neg = 1;
66df930be7Sderaadt 		c = *s++;
67e3237c4aSderaadt 	} else {
68e3237c4aSderaadt 		neg = 0;
69e3237c4aSderaadt 		if (c == '+')
70df930be7Sderaadt 			c = *s++;
71e3237c4aSderaadt 	}
72*6022c2c2Smillert 	if ((base == 0 || base == 16) && c == '0' &&
73*6022c2c2Smillert 	    (*s == 'x' || *s == 'X') && isxdigit((unsigned char)s[1])) {
74df930be7Sderaadt 		c = s[1];
75df930be7Sderaadt 		s += 2;
76df930be7Sderaadt 		base = 16;
77df930be7Sderaadt 	}
78df930be7Sderaadt 	if (base == 0)
79df930be7Sderaadt 		base = c == '0' ? 8 : 10;
80e3237c4aSderaadt 
81e3237c4aSderaadt 	cutoff = ULONG_MAX / (unsigned long)base;
82e3237c4aSderaadt 	cutlim = ULONG_MAX % (unsigned long)base;
8366ce1e86Sderaadt 	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
84df930be7Sderaadt 		if (isdigit(c))
85df930be7Sderaadt 			c -= '0';
86df930be7Sderaadt 		else if (isalpha(c))
87df930be7Sderaadt 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
88df930be7Sderaadt 		else
89df930be7Sderaadt 			break;
90df930be7Sderaadt 		if (c >= base)
91df930be7Sderaadt 			break;
92e3237c4aSderaadt 		if (any < 0)
93e3237c4aSderaadt 			continue;
94928cef96Stedu 		if (acc > cutoff || (acc == cutoff && c > cutlim)) {
95df930be7Sderaadt 			any = -1;
96e3237c4aSderaadt 			acc = ULONG_MAX;
97e3237c4aSderaadt 			errno = ERANGE;
98e3237c4aSderaadt 		} else {
99df930be7Sderaadt 			any = 1;
100e3237c4aSderaadt 			acc *= (unsigned long)base;
101df930be7Sderaadt 			acc += c;
102df930be7Sderaadt 		}
103df930be7Sderaadt 	}
104e3237c4aSderaadt 	if (neg && any > 0)
105df930be7Sderaadt 		acc = -acc;
106df930be7Sderaadt 	if (endptr != 0)
107df930be7Sderaadt 		*endptr = (char *) (any ? s - 1 : nptr);
108df930be7Sderaadt 	return (acc);
109df930be7Sderaadt }
1100d943ef0Sguenther DEF_STRONG(strtoul);
111