strtoll.c revision 1.1 1 1.1 thorpej /* $NetBSD: strtoll.c,v 1.1 2006/10/08 03:14:55 thorpej Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1992, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * Redistribution and use in source and binary forms, with or without
8 1.1 thorpej * modification, are permitted provided that the following conditions
9 1.1 thorpej * are met:
10 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.1 thorpej * notice, this list of conditions and the following disclaimer.
12 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.1 thorpej * documentation and/or other materials provided with the distribution.
15 1.1 thorpej * 3. Neither the name of the University nor the names of its contributors
16 1.1 thorpej * may be used to endorse or promote products derived from this software
17 1.1 thorpej * without specific prior written permission.
18 1.1 thorpej *
19 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 thorpej * SUCH DAMAGE.
30 1.1 thorpej */
31 1.1 thorpej
32 1.1 thorpej #if HAVE_NBTOOL_CONFIG_H
33 1.1 thorpej #include "nbtool_config.h"
34 1.1 thorpej #endif
35 1.1 thorpej
36 1.1 thorpej #if !defined(_KERNEL) && !defined(_STANDALONE)
37 1.1 thorpej #include <sys/cdefs.h>
38 1.1 thorpej #if defined(LIBC_SCCS) && !defined(lint)
39 1.1 thorpej #if 0
40 1.1 thorpej static char sccsid[] = "from: @(#)strtoq.c 8.1 (Berkeley) 6/4/93";
41 1.1 thorpej #else
42 1.1 thorpej __RCSID("$NetBSD: strtoll.c,v 1.1 2006/10/08 03:14:55 thorpej Exp $");
43 1.1 thorpej #endif
44 1.1 thorpej #endif /* LIBC_SCCS and not lint */
45 1.1 thorpej
46 1.1 thorpej #ifdef _LIBC
47 1.1 thorpej #include "namespace.h"
48 1.1 thorpej #endif
49 1.1 thorpej
50 1.1 thorpej #include <assert.h>
51 1.1 thorpej #include <ctype.h>
52 1.1 thorpej #include <errno.h>
53 1.1 thorpej #include <limits.h>
54 1.1 thorpej #include <stdlib.h>
55 1.1 thorpej
56 1.1 thorpej #ifdef _LIBC
57 1.1 thorpej #ifdef __weak_alias
58 1.1 thorpej __weak_alias(strtoll, _strtoll)
59 1.1 thorpej #endif
60 1.1 thorpej #endif
61 1.1 thorpej
62 1.1 thorpej #else /* !_KERNEL && !_STANDALONE */
63 1.1 thorpej #include <sys/param.h>
64 1.1 thorpej #include <lib/libkern/libkern.h>
65 1.1 thorpej #define isspace(x) ((x) == ' ' || (x) == '\t' || (x) == '\n' || (x) == '\r')
66 1.1 thorpej #define isdigit(x) ((x) >= '0' && (x) <= '9')
67 1.1 thorpej #define isalpha(x) (((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
68 1.1 thorpej #define toupper(x) ((x) & ~0x20)
69 1.1 thorpej #endif
70 1.1 thorpej
71 1.1 thorpej #if !HAVE_STRTOLL
72 1.1 thorpej /*
73 1.1 thorpej * Convert a string to a long long integer.
74 1.1 thorpej *
75 1.1 thorpej * Ignores `locale' stuff. Assumes that the upper and lower case
76 1.1 thorpej * alphabets and digits are each contiguous.
77 1.1 thorpej */
78 1.1 thorpej /* LONGLONG */
79 1.1 thorpej long long int
80 1.1 thorpej #ifdef _LIBC
81 1.1 thorpej _strtoll(nptr, endptr, base)
82 1.1 thorpej #else
83 1.1 thorpej strtoll(nptr, endptr, base)
84 1.1 thorpej #endif
85 1.1 thorpej const char *nptr;
86 1.1 thorpej char **endptr;
87 1.1 thorpej int base;
88 1.1 thorpej {
89 1.1 thorpej const char *s;
90 1.1 thorpej /* LONGLONG */
91 1.1 thorpej long long int acc, cutoff;
92 1.1 thorpej int c;
93 1.1 thorpej int neg, any, cutlim;
94 1.1 thorpej
95 1.1 thorpej _DIAGASSERT(nptr != NULL);
96 1.1 thorpej /* endptr may be NULL */
97 1.1 thorpej
98 1.1 thorpej #ifdef __GNUC__
99 1.1 thorpej /* This outrageous construct just to shut up a GCC warning. */
100 1.1 thorpej (void) &acc; (void) &cutoff;
101 1.1 thorpej #endif
102 1.1 thorpej
103 1.1 thorpej /*
104 1.1 thorpej * Skip white space and pick up leading +/- sign if any.
105 1.1 thorpej * If base is 0, allow 0x for hex and 0 for octal, else
106 1.1 thorpej * assume decimal; if base is already 16, allow 0x.
107 1.1 thorpej */
108 1.1 thorpej s = nptr;
109 1.1 thorpej do {
110 1.1 thorpej c = (unsigned char) *s++;
111 1.1 thorpej } while (isspace(c));
112 1.1 thorpej if (c == '-') {
113 1.1 thorpej neg = 1;
114 1.1 thorpej c = *s++;
115 1.1 thorpej } else {
116 1.1 thorpej neg = 0;
117 1.1 thorpej if (c == '+')
118 1.1 thorpej c = *s++;
119 1.1 thorpej }
120 1.1 thorpej if ((base == 0 || base == 16) &&
121 1.1 thorpej c == '0' && (*s == 'x' || *s == 'X')) {
122 1.1 thorpej c = s[1];
123 1.1 thorpej s += 2;
124 1.1 thorpej base = 16;
125 1.1 thorpej }
126 1.1 thorpej if (base == 0)
127 1.1 thorpej base = c == '0' ? 8 : 10;
128 1.1 thorpej
129 1.1 thorpej /*
130 1.1 thorpej * Compute the cutoff value between legal numbers and illegal
131 1.1 thorpej * numbers. That is the largest legal value, divided by the
132 1.1 thorpej * base. An input number that is greater than this value, if
133 1.1 thorpej * followed by a legal input character, is too big. One that
134 1.1 thorpej * is equal to this value may be valid or not; the limit
135 1.1 thorpej * between valid and invalid numbers is then based on the last
136 1.1 thorpej * digit. For instance, if the range for long longs is
137 1.1 thorpej * [-9223372036854775808..9223372036854775807] and the input base
138 1.1 thorpej * is 10, cutoff will be set to 922337203685477580 and cutlim to
139 1.1 thorpej * either 7 (neg==0) or 8 (neg==1), meaning that if we have
140 1.1 thorpej * accumulated a value > 922337203685477580, or equal but the
141 1.1 thorpej * next digit is > 7 (or 8), the number is too big, and we will
142 1.1 thorpej * return a range error.
143 1.1 thorpej *
144 1.1 thorpej * Set any if any `digits' consumed; make it negative to indicate
145 1.1 thorpej * overflow.
146 1.1 thorpej */
147 1.1 thorpej cutoff = neg ? LLONG_MIN : LLONG_MAX;
148 1.1 thorpej cutlim = (int)(cutoff % base);
149 1.1 thorpej cutoff /= base;
150 1.1 thorpej if (neg) {
151 1.1 thorpej if (cutlim > 0) {
152 1.1 thorpej cutlim -= base;
153 1.1 thorpej cutoff += 1;
154 1.1 thorpej }
155 1.1 thorpej cutlim = -cutlim;
156 1.1 thorpej }
157 1.1 thorpej for (acc = 0, any = 0;; c = (unsigned char) *s++) {
158 1.1 thorpej if (isdigit(c))
159 1.1 thorpej c -= '0';
160 1.1 thorpej else if (isalpha(c)) {
161 1.1 thorpej #if defined(_KERNEL) || defined(_STANDALONE)
162 1.1 thorpej c = toupper(c) - 'A' + 10;
163 1.1 thorpej #else
164 1.1 thorpej c -= isupper(c) ? 'A' - 10 : 'a' - 10;
165 1.1 thorpej #endif
166 1.1 thorpej } else
167 1.1 thorpej break;
168 1.1 thorpej if (c >= base)
169 1.1 thorpej break;
170 1.1 thorpej if (any < 0)
171 1.1 thorpej continue;
172 1.1 thorpej if (neg) {
173 1.1 thorpej if (acc < cutoff || (acc == cutoff && c > cutlim)) {
174 1.1 thorpej #if defined(_KERNEL) || defined(_STANDALONE)
175 1.1 thorpej if (endptr)
176 1.1 thorpej *endptr = __UNCONST(nptr);
177 1.1 thorpej return (LLONG_MIN);
178 1.1 thorpej #else
179 1.1 thorpej any = -1;
180 1.1 thorpej acc = LLONG_MIN;
181 1.1 thorpej errno = ERANGE;
182 1.1 thorpej #endif
183 1.1 thorpej } else {
184 1.1 thorpej any = 1;
185 1.1 thorpej acc *= base;
186 1.1 thorpej acc -= c;
187 1.1 thorpej }
188 1.1 thorpej } else {
189 1.1 thorpej if (acc > cutoff || (acc == cutoff && c > cutlim)) {
190 1.1 thorpej #if defined(_KERNEL) || defined(_STANDALONE)
191 1.1 thorpej if (endptr)
192 1.1 thorpej *endptr = __UNCONST(nptr);
193 1.1 thorpej return (LLONG_MAX);
194 1.1 thorpej #else
195 1.1 thorpej any = -1;
196 1.1 thorpej acc = LLONG_MAX;
197 1.1 thorpej errno = ERANGE;
198 1.1 thorpej #endif
199 1.1 thorpej } else {
200 1.1 thorpej any = 1;
201 1.1 thorpej acc *= base;
202 1.1 thorpej acc += c;
203 1.1 thorpej }
204 1.1 thorpej }
205 1.1 thorpej }
206 1.1 thorpej if (endptr != 0)
207 1.1 thorpej *endptr = __UNCONST(any ? s - 1 : nptr);
208 1.1 thorpej return (acc);
209 1.1 thorpej }
210 1.1 thorpej #endif
211