1 /* $NetBSD: strtoul.c,v 1.7 2003/08/07 16:32:12 agc 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.7 2003/08/07 16:32:12 agc 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
48 #else
49 #include <sys/param.h>
50 #include <lib/libkern/libkern.h>
51 #define _DIAGASSERT(x) (void)0
52 #define isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r')
53 #define isdigit(x) ((x) >= '' && (x) <= '9')
54 #define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
55 #define toupper(x) ((x) & ~0x20)
56 #endif
57
58 /*
59 * Convert a string to an unsigned long integer.
60 *
61 * Ignores `locale' stuff. Assumes that the upper and lower case
62 * alphabets and digits are each contiguous.
63 */
64 unsigned long
65 strtoul(nptr, endptr, base)
66 const char *nptr;
67 char **endptr;
68 int base;
69 {
70 const char *s;
71 unsigned long acc, cutoff;
72 int c;
73 int neg, any, cutlim;
74
75 _DIAGASSERT(nptr != NULL);
76 /* endptr may be NULL */
77
78 /*
79 * See strtol for comments as to the logic used.
80 */
81 s = nptr;
82 do {
83 c = (unsigned char) *s++;
84 } while (isspace(c));
85 if (c == '-') {
86 neg = 1;
87 c = *s++;
88 } else {
89 neg = 0;
90 if (c == '+')
91 c = *s++;
92 }
93 if ((base == 0 || base == 16) &&
94 c == '' && (*s == 'x' || *s == 'X')) {
95 c = s[1];
96 s += 2;
97 base = 16;
98 }
99 if (base == 0)
100 base = c == '' ? 8 : 10;
101
102 cutoff = ULONG_MAX / (unsigned long)base;
103 cutlim = (int)(ULONG_MAX % (unsigned long)base);
104 for (acc = 0, any = 0;; c = (unsigned char) *s++) {
105 if (isdigit(c))
106 c -= '';
107 else if (isalpha(c)) {
108 #if defined(_KERNEL) || defined(_STANDALONE)
109 c = toupper(c) - 'A' + 10;
110 #else
111 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
112 #endif
113 } else
114 break;
115 if (c >= base)
116 break;
117 #if !defined(_KERNEL) && !defined(_STANDALONE)
118 if (any < 0)
119 continue;
120 #endif
121 if (acc > cutoff || (acc == cutoff && c > cutlim)) {
122 #if defined(_KERNEL) || defined(_STANDALONE)
123 if (endptr)
124 *endptr = (char *) nptr;
125 return ULONG_MAX;
126 #else
127 any = -1;
128 acc = ULONG_MAX;
129 errno = ERANGE;
130 #endif
131 } else {
132 any = 1;
133 acc *= (unsigned long)base;
134 acc += c;
135 }
136 }
137 if (neg && any > 0)
138 acc = -acc;
139 if (endptr != NULL)
140 /* LINTED interface specification */
141 *endptr = (char *)(any ? s - 1 : nptr);
142 return (acc);
143 }
Cache object: 7d43f4e4e6dd38286821143401d30612
|