1deab4642SAxel Dörfler/*
2deab4642SAxel Dörfler * Copyright (c) 1990, 1993
3deab4642SAxel Dörfler *	The Regents of the University of California.  All rights reserved.
4deab4642SAxel Dörfler *
5deab4642SAxel Dörfler * Redistribution and use in source and binary forms, with or without
6deab4642SAxel Dörfler * modification, are permitted provided that the following conditions
7deab4642SAxel Dörfler * are met:
8deab4642SAxel Dörfler * 1. Redistributions of source code must retain the above copyright
9deab4642SAxel Dörfler *    notice, this list of conditions and the following disclaimer.
10deab4642SAxel Dörfler * 2. Redistributions in binary form must reproduce the above copyright
11deab4642SAxel Dörfler *    notice, this list of conditions and the following disclaimer in the
12deab4642SAxel Dörfler *    documentation and/or other materials provided with the distribution.
13deab4642SAxel Dörfler * 3. All advertising materials mentioning features or use of this software
14deab4642SAxel Dörfler *    must display the following acknowledgement:
15deab4642SAxel Dörfler *	This product includes software developed by the University of
16deab4642SAxel Dörfler *	California, Berkeley and its contributors.
17deab4642SAxel Dörfler * 4. Neither the name of the University nor the names of its contributors
18deab4642SAxel Dörfler *    may be used to endorse or promote products derived from this software
19deab4642SAxel Dörfler *    without specific prior written permission.
20deab4642SAxel Dörfler *
21deab4642SAxel Dörfler * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22deab4642SAxel Dörfler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23deab4642SAxel Dörfler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24deab4642SAxel Dörfler * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25deab4642SAxel Dörfler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26deab4642SAxel Dörfler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27deab4642SAxel Dörfler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28deab4642SAxel Dörfler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29deab4642SAxel Dörfler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30deab4642SAxel Dörfler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31deab4642SAxel Dörfler * SUCH DAMAGE.
32deab4642SAxel Dörfler */
33deab4642SAxel Dörfler
34deab4642SAxel Dörfler
35deab4642SAxel Dörfler#include <limits.h>
36deab4642SAxel Dörfler#include <ctype.h>
37deab4642SAxel Dörfler#include <errno.h>
38deab4642SAxel Dörfler#include <stdlib.h>
39deab4642SAxel Dörfler
40ae901935SOliver Tappe#include <errno_private.h>
41ae901935SOliver Tappe
42deab4642SAxel Dörfler
43deab4642SAxel Dörfler/*
44deab4642SAxel Dörfler * Convert a string to an unsigned long integer.
45deab4642SAxel Dörfler *
46deab4642SAxel Dörfler * Assumes that the upper and lower case
47deab4642SAxel Dörfler * alphabets and digits are each contiguous.
48deab4642SAxel Dörfler */
49deab4642SAxel Dörfler
50deab4642SAxel Dörflerunsigned long
51deab4642SAxel Dörflerstrtoul(const char * __restrict nptr, char ** __restrict endptr, int base)
52deab4642SAxel Dörfler{
53deab4642SAxel Dörfler	const char *s;
54deab4642SAxel Dörfler	unsigned long acc;
55deab4642SAxel Dörfler	char c;
56deab4642SAxel Dörfler	unsigned long cutoff;
57deab4642SAxel Dörfler	int neg, any, cutlim;
58deab4642SAxel Dörfler
59deab4642SAxel Dörfler	/*
60deab4642SAxel Dörfler	 * See strtol for comments as to the logic used.
61deab4642SAxel Dörfler	 */
62deab4642SAxel Dörfler	s = nptr;
63deab4642SAxel Dörfler	do {
64deab4642SAxel Dörfler		c = *s++;
65deab4642SAxel Dörfler	} while (isspace((unsigned char)c));
66deab4642SAxel Dörfler	if (c == '-') {
67deab4642SAxel Dörfler		neg = 1;
68deab4642SAxel Dörfler		c = *s++;
69deab4642SAxel Dörfler	} else {
70deab4642SAxel Dörfler		neg = 0;
71deab4642SAxel Dörfler		if (c == '+')
72deab4642SAxel Dörfler			c = *s++;
73deab4642SAxel Dörfler	}
74deab4642SAxel Dörfler	if ((base == 0 || base == 16) &&
75deab4642SAxel Dörfler	    c == '0' && (*s == 'x' || *s == 'X')) {
76deab4642SAxel Dörfler		c = s[1];
77deab4642SAxel Dörfler		s += 2;
78deab4642SAxel Dörfler		base = 16;
79deab4642SAxel Dörfler	}
80deab4642SAxel Dörfler	if (base == 0)
81deab4642SAxel Dörfler		base = c == '0' ? 8 : 10;
82deab4642SAxel Dörfler	acc = any = 0;
83deab4642SAxel Dörfler	if (base < 2 || base > 36)
84deab4642SAxel Dörfler		goto noconv;
85deab4642SAxel Dörfler
86deab4642SAxel Dörfler	cutoff = ULONG_MAX / base;
87deab4642SAxel Dörfler	cutlim = ULONG_MAX % base;
88deab4642SAxel Dörfler	for ( ; ; c = *s++) {
89deab4642SAxel Dörfler		if (c >= '0' && c <= '9')
90deab4642SAxel Dörfler			c -= '0';
91deab4642SAxel Dörfler		else if (c >= 'A' && c <= 'Z')
92deab4642SAxel Dörfler			c -= 'A' - 10;
93deab4642SAxel Dörfler		else if (c >= 'a' && c <= 'z')
94deab4642SAxel Dörfler			c -= 'a' - 10;
95deab4642SAxel Dörfler		else
96deab4642SAxel Dörfler			break;
97deab4642SAxel Dörfler		if (c >= base)
98deab4642SAxel Dörfler			break;
99deab4642SAxel Dörfler		if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
100deab4642SAxel Dörfler			any = -1;
101deab4642SAxel Dörfler		else {
102deab4642SAxel Dörfler			any = 1;
103deab4642SAxel Dörfler			acc *= base;
104deab4642SAxel Dörfler			acc += c;
105deab4642SAxel Dörfler		}
106deab4642SAxel Dörfler	}
107deab4642SAxel Dörfler	if (any < 0) {
108deab4642SAxel Dörfler		acc = ULONG_MAX;
109ae901935SOliver Tappe		__set_errno(ERANGE);
110deab4642SAxel Dörfler	} else if (!any) {
111deab4642SAxel Dörflernoconv:
112ae901935SOliver Tappe		__set_errno(EINVAL);
113deab4642SAxel Dörfler	} else if (neg)
114deab4642SAxel Dörfler		acc = -acc;
115deab4642SAxel Dörfler	if (endptr != NULL)
116deab4642SAxel Dörfler		*endptr = (char *)(any ? s - 1 : nptr);
117deab4642SAxel Dörfler	return (acc);
118deab4642SAxel Dörfler}
119056f3c62SAxel Dörfler
120056f3c62SAxel Dörfler
121056f3c62SAxel Dörflerunsigned long __strtoul_internal(const char *number, char **_end, int base, int group);
122056f3c62SAxel Dörfler
123056f3c62SAxel Dörflerunsigned long
124056f3c62SAxel Dörfler__strtoul_internal(const char *number, char **_end, int base, int group)
125056f3c62SAxel Dörfler{
126056f3c62SAxel Dörfler	// ToDo: group is currently not supported!
127056f3c62SAxel Dörfler	(void)group;
128056f3c62SAxel Dörfler
129056f3c62SAxel Dörfler	return strtoul(number, _end, base);
130056f3c62SAxel Dörfler}
131056f3c62SAxel Dörfler
132