xref: /netbsd-src/common/lib/libc/stdlib/strtoul.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: strtoul.c,v 1.1 2005/12/20 19:28:52 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #if !defined(_KERNEL) && !defined(_STANDALONE)
33 #include <sys/cdefs.h>
34 #if defined(LIBC_SCCS) && !defined(lint)
35 #if 0
36 static char sccsid[] = "@(#)strtoul.c	8.1 (Berkeley) 6/4/93";
37 #else
38 __RCSID("$NetBSD: strtoul.c,v 1.1 2005/12/20 19:28:52 christos Exp $");
39 #endif
40 #endif /* LIBC_SCCS and not lint */
41 
42 #include <assert.h>
43 #include <ctype.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <stdlib.h>
47 #else
48 #include <sys/param.h>
49 #include <lib/libkern/libkern.h>
50 #define isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r')
51 #define isdigit(x) ((x) >= '0' && (x) <= '9')
52 #define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
53 #define toupper(x) ((x) & ~0x20)
54 #endif
55 
56 /*
57  * Convert a string to an unsigned long integer.
58  *
59  * Ignores `locale' stuff.  Assumes that the upper and lower case
60  * alphabets and digits are each contiguous.
61  */
62 unsigned long
63 strtoul(nptr, endptr, base)
64 	const char *nptr;
65 	char **endptr;
66 	int base;
67 {
68 	const char *s;
69 	unsigned long acc, cutoff;
70 	int c;
71 	int neg, any, cutlim;
72 
73 	_DIAGASSERT(nptr != NULL);
74 	/* endptr may be NULL */
75 
76 	/*
77 	 * See strtol for comments as to the logic used.
78 	 */
79 	s = nptr;
80 	do {
81 		c = (unsigned char) *s++;
82 	} while (isspace(c));
83 	if (c == '-') {
84 		neg = 1;
85 		c = *s++;
86 	} else {
87 		neg = 0;
88 		if (c == '+')
89 			c = *s++;
90 	}
91 	if ((base == 0 || base == 16) &&
92 	    c == '0' && (*s == 'x' || *s == 'X')) {
93 		c = s[1];
94 		s += 2;
95 		base = 16;
96 	}
97 	if (base == 0)
98 		base = c == '0' ? 8 : 10;
99 
100 	cutoff = ULONG_MAX / (unsigned long)base;
101 	cutlim = (int)(ULONG_MAX % (unsigned long)base);
102 	for (acc = 0, any = 0;; c = (unsigned char) *s++) {
103 		if (isdigit(c))
104 			c -= '0';
105 		else if (isalpha(c)) {
106 #if defined(_KERNEL) || defined(_STANDALONE)
107 			c = toupper(c) - 'A' + 10;
108 #else
109 			c -= isupper(c) ? 'A' - 10 : 'a' - 10;
110 #endif
111 		} else
112 			break;
113 		if (c >= base)
114 			break;
115 		if (any < 0)
116 			continue;
117 		if (acc > cutoff || (acc == cutoff && c > cutlim)) {
118 #if defined(_KERNEL) || defined(_STANDALONE)
119 			if (endptr)
120 				*endptr = __UNCONST(nptr);
121 			return ULONG_MAX;
122 #else
123 			any = -1;
124 			acc = ULONG_MAX;
125 			errno = ERANGE;
126 #endif
127 		} else {
128 			any = 1;
129 			acc *= (unsigned long)base;
130 			acc += c;
131 		}
132 	}
133 	if (neg && any > 0)
134 		acc = -acc;
135 	if (endptr != NULL)
136 		*endptr = __UNCONST(any ? s - 1 : nptr);
137 	return (acc);
138 }
139