1*699cd480SApple OSS Distributions /*
2*699cd480SApple OSS Distributions * Copyright (c) 1998-2016 Apple Inc. All rights reserved.
3*699cd480SApple OSS Distributions *
4*699cd480SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*699cd480SApple OSS Distributions *
6*699cd480SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*699cd480SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*699cd480SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*699cd480SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*699cd480SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*699cd480SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*699cd480SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*699cd480SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*699cd480SApple OSS Distributions *
15*699cd480SApple OSS Distributions * Please obtain a copy of the License at
16*699cd480SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*699cd480SApple OSS Distributions *
18*699cd480SApple OSS Distributions * The Original Code and all software distributed under the License are
19*699cd480SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*699cd480SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*699cd480SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*699cd480SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*699cd480SApple OSS Distributions * Please see the License for the specific language governing rights and
24*699cd480SApple OSS Distributions * limitations under the License.
25*699cd480SApple OSS Distributions *
26*699cd480SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*699cd480SApple OSS Distributions */
28*699cd480SApple OSS Distributions
29*699cd480SApple OSS Distributions /*
30*699cd480SApple OSS Distributions * Copyright (c) 1995 NeXT Computer, Inc. All rights reserved.
31*699cd480SApple OSS Distributions *
32*699cd480SApple OSS Distributions * strol.c - The functions strtol() & strtoul() are exported as public API
33*699cd480SApple OSS Distributions * via the header file ~driverkit/generalFuncs.h
34*699cd480SApple OSS Distributions *
35*699cd480SApple OSS Distributions * HISTORY
36*699cd480SApple OSS Distributions * 25-Oct-1995 Dean Reece at NeXT
37*699cd480SApple OSS Distributions * Created based on BSD4.4's strtol.c & strtoul.c.
38*699cd480SApple OSS Distributions * Removed dependency on _ctype_ by static versions of isupper()...
39*699cd480SApple OSS Distributions * Added support for "0b101..." binary constants.
40*699cd480SApple OSS Distributions * Commented out references to errno.
41*699cd480SApple OSS Distributions */
42*699cd480SApple OSS Distributions
43*699cd480SApple OSS Distributions /*
44*699cd480SApple OSS Distributions * Copyright (c) 1990, 1993
45*699cd480SApple OSS Distributions * The Regents of the University of California. All rights reserved.
46*699cd480SApple OSS Distributions *
47*699cd480SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
48*699cd480SApple OSS Distributions * modification, are permitted provided that the following conditions
49*699cd480SApple OSS Distributions * are met:
50*699cd480SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
51*699cd480SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
52*699cd480SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
53*699cd480SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
54*699cd480SApple OSS Distributions * documentation and/or other materials provided with the distribution.
55*699cd480SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
56*699cd480SApple OSS Distributions * must display the following acknowledgement:
57*699cd480SApple OSS Distributions * This product includes software developed by the University of
58*699cd480SApple OSS Distributions * California, Berkeley and its contributors.
59*699cd480SApple OSS Distributions * 4. Neither the name of the University nor the names of its contributors
60*699cd480SApple OSS Distributions * may be used to endorse or promote products derived from this software
61*699cd480SApple OSS Distributions * without specific prior written permission.
62*699cd480SApple OSS Distributions *
63*699cd480SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64*699cd480SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65*699cd480SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66*699cd480SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67*699cd480SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68*699cd480SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69*699cd480SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70*699cd480SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71*699cd480SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72*699cd480SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73*699cd480SApple OSS Distributions * SUCH DAMAGE.
74*699cd480SApple OSS Distributions */
75*699cd480SApple OSS Distributions
76*699cd480SApple OSS Distributions /*
77*699cd480SApple OSS Distributions #include <string.h>
78*699cd480SApple OSS Distributions #include <stdlib.h>
79*699cd480SApple OSS Distributions #include <limits.h>
80*699cd480SApple OSS Distributions */
81*699cd480SApple OSS Distributions #include <sys/types.h>
82*699cd480SApple OSS Distributions #include <machine/limits.h>
83*699cd480SApple OSS Distributions
84*699cd480SApple OSS Distributions
85*699cd480SApple OSS Distributions long strtol(const char *nptr, char **endptr, int base);
86*699cd480SApple OSS Distributions unsigned long strtoul(const char *nptr, char **endptr, int base);
87*699cd480SApple OSS Distributions quad_t strtoq(const char *nptr, char **endptr, int base);
88*699cd480SApple OSS Distributions u_quad_t strtouq(const char *nptr, char **endptr, int base);
89*699cd480SApple OSS Distributions
90*699cd480SApple OSS Distributions
91*699cd480SApple OSS Distributions typedef int BOOL;
92*699cd480SApple OSS Distributions
93*699cd480SApple OSS Distributions static inline BOOL
isupper(char c)94*699cd480SApple OSS Distributions isupper(char c)
95*699cd480SApple OSS Distributions {
96*699cd480SApple OSS Distributions return c >= 'A' && c <= 'Z';
97*699cd480SApple OSS Distributions }
98*699cd480SApple OSS Distributions
99*699cd480SApple OSS Distributions static inline BOOL
isalpha(char c)100*699cd480SApple OSS Distributions isalpha(char c)
101*699cd480SApple OSS Distributions {
102*699cd480SApple OSS Distributions return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
103*699cd480SApple OSS Distributions }
104*699cd480SApple OSS Distributions
105*699cd480SApple OSS Distributions
106*699cd480SApple OSS Distributions static inline BOOL
isspace(char c)107*699cd480SApple OSS Distributions isspace(char c)
108*699cd480SApple OSS Distributions {
109*699cd480SApple OSS Distributions return c == ' ' || c == '\t' || c == '\n' || c == '\12';
110*699cd480SApple OSS Distributions }
111*699cd480SApple OSS Distributions
112*699cd480SApple OSS Distributions static inline BOOL
isdigit(char c)113*699cd480SApple OSS Distributions isdigit(char c)
114*699cd480SApple OSS Distributions {
115*699cd480SApple OSS Distributions return c >= '0' && c <= '9';
116*699cd480SApple OSS Distributions }
117*699cd480SApple OSS Distributions
118*699cd480SApple OSS Distributions /*
119*699cd480SApple OSS Distributions * Convert a string to a long integer.
120*699cd480SApple OSS Distributions *
121*699cd480SApple OSS Distributions * Ignores `locale' stuff. Assumes that the upper and lower case
122*699cd480SApple OSS Distributions * alphabets and digits are each contiguous.
123*699cd480SApple OSS Distributions */
124*699cd480SApple OSS Distributions long
strtol(const char * nptr,char ** endptr,int base)125*699cd480SApple OSS Distributions strtol(const char *nptr, char **endptr, int base)
126*699cd480SApple OSS Distributions {
127*699cd480SApple OSS Distributions const char *s = nptr;
128*699cd480SApple OSS Distributions unsigned long acc;
129*699cd480SApple OSS Distributions char c;
130*699cd480SApple OSS Distributions unsigned long cutoff;
131*699cd480SApple OSS Distributions int neg = 0, any, cutlim;
132*699cd480SApple OSS Distributions
133*699cd480SApple OSS Distributions /*
134*699cd480SApple OSS Distributions * Skip white space and pick up leading +/- sign if any.
135*699cd480SApple OSS Distributions * If base is 0, allow 0x for hex and 0 for octal, else
136*699cd480SApple OSS Distributions * assume decimal; if base is already 16, allow 0x.
137*699cd480SApple OSS Distributions */
138*699cd480SApple OSS Distributions do {
139*699cd480SApple OSS Distributions c = *s++;
140*699cd480SApple OSS Distributions } while (isspace(c));
141*699cd480SApple OSS Distributions if (c == '-') {
142*699cd480SApple OSS Distributions neg = 1;
143*699cd480SApple OSS Distributions c = *s++;
144*699cd480SApple OSS Distributions } else if (c == '+') {
145*699cd480SApple OSS Distributions c = *s++;
146*699cd480SApple OSS Distributions }
147*699cd480SApple OSS Distributions if ((base == 0 || base == 16) &&
148*699cd480SApple OSS Distributions c == '0' && (*s == 'x' || *s == 'X')) {
149*699cd480SApple OSS Distributions c = s[1];
150*699cd480SApple OSS Distributions s += 2;
151*699cd480SApple OSS Distributions base = 16;
152*699cd480SApple OSS Distributions } else if ((base == 0 || base == 2) &&
153*699cd480SApple OSS Distributions c == '0' && (*s == 'b' || *s == 'B')) {
154*699cd480SApple OSS Distributions c = s[1];
155*699cd480SApple OSS Distributions s += 2;
156*699cd480SApple OSS Distributions base = 2;
157*699cd480SApple OSS Distributions }
158*699cd480SApple OSS Distributions if (base == 0) {
159*699cd480SApple OSS Distributions base = c == '0' ? 8 : 10;
160*699cd480SApple OSS Distributions }
161*699cd480SApple OSS Distributions
162*699cd480SApple OSS Distributions /*
163*699cd480SApple OSS Distributions * Compute the cutoff value between legal numbers and illegal
164*699cd480SApple OSS Distributions * numbers. That is the largest legal value, divided by the
165*699cd480SApple OSS Distributions * base. An input number that is greater than this value, if
166*699cd480SApple OSS Distributions * followed by a legal input character, is too big. One that
167*699cd480SApple OSS Distributions * is equal to this value may be valid or not; the limit
168*699cd480SApple OSS Distributions * between valid and invalid numbers is then based on the last
169*699cd480SApple OSS Distributions * digit. For instance, if the range for longs is
170*699cd480SApple OSS Distributions * [-2147483648..2147483647] and the input base is 10,
171*699cd480SApple OSS Distributions * cutoff will be set to 214748364 and cutlim to either
172*699cd480SApple OSS Distributions * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated
173*699cd480SApple OSS Distributions * a value > 214748364, or equal but the next digit is > 7 (or 8),
174*699cd480SApple OSS Distributions * the number is too big, and we will return a range error.
175*699cd480SApple OSS Distributions *
176*699cd480SApple OSS Distributions * Set any if any `digits' consumed; make it negative to indicate
177*699cd480SApple OSS Distributions * overflow.
178*699cd480SApple OSS Distributions */
179*699cd480SApple OSS Distributions cutoff = neg ? -(unsigned long)LONG_MIN : LONG_MAX;
180*699cd480SApple OSS Distributions cutlim = ((int)(cutoff % (unsigned long)base));
181*699cd480SApple OSS Distributions cutoff /= (unsigned long)base;
182*699cd480SApple OSS Distributions for (acc = 0, any = 0;; c = *s++) {
183*699cd480SApple OSS Distributions if (isdigit(c)) {
184*699cd480SApple OSS Distributions c -= '0';
185*699cd480SApple OSS Distributions } else if (isalpha(c)) {
186*699cd480SApple OSS Distributions c -= isupper(c) ? 'A' - 10 : 'a' - 10;
187*699cd480SApple OSS Distributions } else {
188*699cd480SApple OSS Distributions break;
189*699cd480SApple OSS Distributions }
190*699cd480SApple OSS Distributions if (c >= base) {
191*699cd480SApple OSS Distributions break;
192*699cd480SApple OSS Distributions }
193*699cd480SApple OSS Distributions if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
194*699cd480SApple OSS Distributions any = -1;
195*699cd480SApple OSS Distributions } else {
196*699cd480SApple OSS Distributions any = 1;
197*699cd480SApple OSS Distributions acc *= base;
198*699cd480SApple OSS Distributions acc += c;
199*699cd480SApple OSS Distributions }
200*699cd480SApple OSS Distributions }
201*699cd480SApple OSS Distributions if (any < 0) {
202*699cd480SApple OSS Distributions acc = neg ? LONG_MIN : LONG_MAX;
203*699cd480SApple OSS Distributions // errno = ERANGE;
204*699cd480SApple OSS Distributions } else if (neg) {
205*699cd480SApple OSS Distributions acc = -acc;
206*699cd480SApple OSS Distributions }
207*699cd480SApple OSS Distributions if (endptr != 0) {
208*699cd480SApple OSS Distributions if (any) {
209*699cd480SApple OSS Distributions *endptr = __CAST_AWAY_QUALIFIER(s - 1, const, char *);
210*699cd480SApple OSS Distributions } else {
211*699cd480SApple OSS Distributions *endptr = __CAST_AWAY_QUALIFIER(nptr, const, char *);
212*699cd480SApple OSS Distributions }
213*699cd480SApple OSS Distributions }
214*699cd480SApple OSS Distributions return acc;
215*699cd480SApple OSS Distributions }
216*699cd480SApple OSS Distributions
217*699cd480SApple OSS Distributions unsigned long
strtoul(const char * nptr,char ** endptr,int base)218*699cd480SApple OSS Distributions strtoul(const char *nptr, char **endptr, int base)
219*699cd480SApple OSS Distributions {
220*699cd480SApple OSS Distributions const char *s = nptr;
221*699cd480SApple OSS Distributions unsigned long acc;
222*699cd480SApple OSS Distributions char c;
223*699cd480SApple OSS Distributions unsigned long cutoff;
224*699cd480SApple OSS Distributions int neg = 0, any, cutlim;
225*699cd480SApple OSS Distributions
226*699cd480SApple OSS Distributions /*
227*699cd480SApple OSS Distributions * See strtol for comments as to the logic used.
228*699cd480SApple OSS Distributions */
229*699cd480SApple OSS Distributions do {
230*699cd480SApple OSS Distributions c = *s++;
231*699cd480SApple OSS Distributions } while (isspace(c));
232*699cd480SApple OSS Distributions if (c == '-') {
233*699cd480SApple OSS Distributions neg = 1;
234*699cd480SApple OSS Distributions c = *s++;
235*699cd480SApple OSS Distributions } else if (c == '+') {
236*699cd480SApple OSS Distributions c = *s++;
237*699cd480SApple OSS Distributions }
238*699cd480SApple OSS Distributions if ((base == 0 || base == 16) &&
239*699cd480SApple OSS Distributions c == '0' && (*s == 'x' || *s == 'X')) {
240*699cd480SApple OSS Distributions c = s[1];
241*699cd480SApple OSS Distributions s += 2;
242*699cd480SApple OSS Distributions base = 16;
243*699cd480SApple OSS Distributions } else if ((base == 0 || base == 2) &&
244*699cd480SApple OSS Distributions c == '0' && (*s == 'b' || *s == 'B')) {
245*699cd480SApple OSS Distributions c = s[1];
246*699cd480SApple OSS Distributions s += 2;
247*699cd480SApple OSS Distributions base = 2;
248*699cd480SApple OSS Distributions }
249*699cd480SApple OSS Distributions if (base == 0) {
250*699cd480SApple OSS Distributions base = c == '0' ? 8 : 10;
251*699cd480SApple OSS Distributions }
252*699cd480SApple OSS Distributions cutoff = (unsigned long)ULONG_MAX / (unsigned long)base;
253*699cd480SApple OSS Distributions cutlim = ((int)((unsigned long)ULONG_MAX % (unsigned long)base));
254*699cd480SApple OSS Distributions for (acc = 0, any = 0;; c = *s++) {
255*699cd480SApple OSS Distributions if (isdigit(c)) {
256*699cd480SApple OSS Distributions c -= '0';
257*699cd480SApple OSS Distributions } else if (isalpha(c)) {
258*699cd480SApple OSS Distributions c -= isupper(c) ? 'A' - 10 : 'a' - 10;
259*699cd480SApple OSS Distributions } else {
260*699cd480SApple OSS Distributions break;
261*699cd480SApple OSS Distributions }
262*699cd480SApple OSS Distributions if (c >= base) {
263*699cd480SApple OSS Distributions break;
264*699cd480SApple OSS Distributions }
265*699cd480SApple OSS Distributions if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
266*699cd480SApple OSS Distributions any = -1;
267*699cd480SApple OSS Distributions } else {
268*699cd480SApple OSS Distributions any = 1;
269*699cd480SApple OSS Distributions acc *= base;
270*699cd480SApple OSS Distributions acc += c;
271*699cd480SApple OSS Distributions }
272*699cd480SApple OSS Distributions }
273*699cd480SApple OSS Distributions if (any < 0) {
274*699cd480SApple OSS Distributions acc = ULONG_MAX;
275*699cd480SApple OSS Distributions // errno = ERANGE;
276*699cd480SApple OSS Distributions } else if (neg) {
277*699cd480SApple OSS Distributions acc = -acc;
278*699cd480SApple OSS Distributions }
279*699cd480SApple OSS Distributions if (endptr != 0) {
280*699cd480SApple OSS Distributions if (any) {
281*699cd480SApple OSS Distributions *endptr = __CAST_AWAY_QUALIFIER(s - 1, const, char *);
282*699cd480SApple OSS Distributions } else {
283*699cd480SApple OSS Distributions *endptr = __CAST_AWAY_QUALIFIER(nptr, const, char *);
284*699cd480SApple OSS Distributions }
285*699cd480SApple OSS Distributions }
286*699cd480SApple OSS Distributions
287*699cd480SApple OSS Distributions return acc;
288*699cd480SApple OSS Distributions }
289*699cd480SApple OSS Distributions
290*699cd480SApple OSS Distributions /*
291*699cd480SApple OSS Distributions * Convert a string to a quad integer.
292*699cd480SApple OSS Distributions *
293*699cd480SApple OSS Distributions * Ignores `locale' stuff. Assumes that the upper and lower case
294*699cd480SApple OSS Distributions * alphabets and digits are each contiguous.
295*699cd480SApple OSS Distributions */
296*699cd480SApple OSS Distributions quad_t
strtoq(const char * nptr,char ** endptr,int base)297*699cd480SApple OSS Distributions strtoq(const char *nptr, char **endptr, int base)
298*699cd480SApple OSS Distributions {
299*699cd480SApple OSS Distributions const char *s;
300*699cd480SApple OSS Distributions u_quad_t acc;
301*699cd480SApple OSS Distributions char c;
302*699cd480SApple OSS Distributions u_quad_t qbase, cutoff;
303*699cd480SApple OSS Distributions int neg, any, cutlim;
304*699cd480SApple OSS Distributions
305*699cd480SApple OSS Distributions /*
306*699cd480SApple OSS Distributions * Skip white space and pick up leading +/- sign if any.
307*699cd480SApple OSS Distributions * If base is 0, allow 0x for hex and 0 for octal, else
308*699cd480SApple OSS Distributions * assume decimal; if base is already 16, allow 0x.
309*699cd480SApple OSS Distributions */
310*699cd480SApple OSS Distributions s = nptr;
311*699cd480SApple OSS Distributions do {
312*699cd480SApple OSS Distributions c = *s++;
313*699cd480SApple OSS Distributions } while (isspace(c));
314*699cd480SApple OSS Distributions if (c == '-') {
315*699cd480SApple OSS Distributions neg = 1;
316*699cd480SApple OSS Distributions c = *s++;
317*699cd480SApple OSS Distributions } else {
318*699cd480SApple OSS Distributions neg = 0;
319*699cd480SApple OSS Distributions if (c == '+') {
320*699cd480SApple OSS Distributions c = *s++;
321*699cd480SApple OSS Distributions }
322*699cd480SApple OSS Distributions }
323*699cd480SApple OSS Distributions if ((base == 0 || base == 16) &&
324*699cd480SApple OSS Distributions c == '0' && (*s == 'x' || *s == 'X')) {
325*699cd480SApple OSS Distributions c = s[1];
326*699cd480SApple OSS Distributions s += 2;
327*699cd480SApple OSS Distributions base = 16;
328*699cd480SApple OSS Distributions }
329*699cd480SApple OSS Distributions if (base == 0) {
330*699cd480SApple OSS Distributions base = c == '0' ? 8 : 10;
331*699cd480SApple OSS Distributions }
332*699cd480SApple OSS Distributions
333*699cd480SApple OSS Distributions /*
334*699cd480SApple OSS Distributions * Compute the cutoff value between legal numbers and illegal
335*699cd480SApple OSS Distributions * numbers. That is the largest legal value, divided by the
336*699cd480SApple OSS Distributions * base. An input number that is greater than this value, if
337*699cd480SApple OSS Distributions * followed by a legal input character, is too big. One that
338*699cd480SApple OSS Distributions * is equal to this value may be valid or not; the limit
339*699cd480SApple OSS Distributions * between valid and invalid numbers is then based on the last
340*699cd480SApple OSS Distributions * digit. For instance, if the range for quads is
341*699cd480SApple OSS Distributions * [-9223372036854775808..9223372036854775807] and the input base
342*699cd480SApple OSS Distributions * is 10, cutoff will be set to 922337203685477580 and cutlim to
343*699cd480SApple OSS Distributions * either 7 (neg==0) or 8 (neg==1), meaning that if we have
344*699cd480SApple OSS Distributions * accumulated a value > 922337203685477580, or equal but the
345*699cd480SApple OSS Distributions * next digit is > 7 (or 8), the number is too big, and we will
346*699cd480SApple OSS Distributions * return a range error.
347*699cd480SApple OSS Distributions *
348*699cd480SApple OSS Distributions * Set any if any `digits' consumed; make it negative to indicate
349*699cd480SApple OSS Distributions * overflow.
350*699cd480SApple OSS Distributions */
351*699cd480SApple OSS Distributions qbase = (unsigned)base;
352*699cd480SApple OSS Distributions cutoff = neg ? -(u_quad_t)QUAD_MIN : QUAD_MAX;
353*699cd480SApple OSS Distributions cutlim = ((int)(cutoff % qbase));
354*699cd480SApple OSS Distributions cutoff /= qbase;
355*699cd480SApple OSS Distributions for (acc = 0, any = 0;; c = *s++) {
356*699cd480SApple OSS Distributions if (isdigit(c)) {
357*699cd480SApple OSS Distributions c -= '0';
358*699cd480SApple OSS Distributions } else if (isalpha(c)) {
359*699cd480SApple OSS Distributions c -= isupper(c) ? 'A' - 10 : 'a' - 10;
360*699cd480SApple OSS Distributions } else {
361*699cd480SApple OSS Distributions break;
362*699cd480SApple OSS Distributions }
363*699cd480SApple OSS Distributions if (c >= base) {
364*699cd480SApple OSS Distributions break;
365*699cd480SApple OSS Distributions }
366*699cd480SApple OSS Distributions if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
367*699cd480SApple OSS Distributions any = -1;
368*699cd480SApple OSS Distributions } else {
369*699cd480SApple OSS Distributions any = 1;
370*699cd480SApple OSS Distributions acc *= qbase;
371*699cd480SApple OSS Distributions acc += c;
372*699cd480SApple OSS Distributions }
373*699cd480SApple OSS Distributions }
374*699cd480SApple OSS Distributions if (any < 0) {
375*699cd480SApple OSS Distributions acc = neg ? QUAD_MIN : QUAD_MAX;
376*699cd480SApple OSS Distributions // errno = ERANGE;
377*699cd480SApple OSS Distributions } else if (neg) {
378*699cd480SApple OSS Distributions acc = -acc;
379*699cd480SApple OSS Distributions }
380*699cd480SApple OSS Distributions if (endptr != 0) {
381*699cd480SApple OSS Distributions if (any) {
382*699cd480SApple OSS Distributions *endptr = __CAST_AWAY_QUALIFIER(s - 1, const, char *);
383*699cd480SApple OSS Distributions } else {
384*699cd480SApple OSS Distributions *endptr = __CAST_AWAY_QUALIFIER(nptr, const, char *);
385*699cd480SApple OSS Distributions }
386*699cd480SApple OSS Distributions }
387*699cd480SApple OSS Distributions
388*699cd480SApple OSS Distributions return acc;
389*699cd480SApple OSS Distributions }
390*699cd480SApple OSS Distributions
391*699cd480SApple OSS Distributions
392*699cd480SApple OSS Distributions /*
393*699cd480SApple OSS Distributions * Convert a string to an unsigned quad integer.
394*699cd480SApple OSS Distributions *
395*699cd480SApple OSS Distributions * Ignores `locale' stuff. Assumes that the upper and lower case
396*699cd480SApple OSS Distributions * alphabets and digits are each contiguous.
397*699cd480SApple OSS Distributions */
398*699cd480SApple OSS Distributions u_quad_t
strtouq(const char * nptr,char ** endptr,int base)399*699cd480SApple OSS Distributions strtouq(const char *nptr,
400*699cd480SApple OSS Distributions char **endptr,
401*699cd480SApple OSS Distributions int base)
402*699cd480SApple OSS Distributions {
403*699cd480SApple OSS Distributions const char *s = nptr;
404*699cd480SApple OSS Distributions u_quad_t acc;
405*699cd480SApple OSS Distributions char c;
406*699cd480SApple OSS Distributions u_quad_t qbase, cutoff;
407*699cd480SApple OSS Distributions int neg, any, cutlim;
408*699cd480SApple OSS Distributions
409*699cd480SApple OSS Distributions /*
410*699cd480SApple OSS Distributions * See strtoq for comments as to the logic used.
411*699cd480SApple OSS Distributions */
412*699cd480SApple OSS Distributions s = nptr;
413*699cd480SApple OSS Distributions do {
414*699cd480SApple OSS Distributions c = *s++;
415*699cd480SApple OSS Distributions } while (isspace(c));
416*699cd480SApple OSS Distributions if (c == '-') {
417*699cd480SApple OSS Distributions neg = 1;
418*699cd480SApple OSS Distributions c = *s++;
419*699cd480SApple OSS Distributions } else {
420*699cd480SApple OSS Distributions neg = 0;
421*699cd480SApple OSS Distributions if (c == '+') {
422*699cd480SApple OSS Distributions c = *s++;
423*699cd480SApple OSS Distributions }
424*699cd480SApple OSS Distributions }
425*699cd480SApple OSS Distributions if ((base == 0 || base == 16) &&
426*699cd480SApple OSS Distributions c == '0' && (*s == 'x' || *s == 'X')) {
427*699cd480SApple OSS Distributions c = s[1];
428*699cd480SApple OSS Distributions s += 2;
429*699cd480SApple OSS Distributions base = 16;
430*699cd480SApple OSS Distributions }
431*699cd480SApple OSS Distributions if (base == 0) {
432*699cd480SApple OSS Distributions base = c == '0' ? 8 : 10;
433*699cd480SApple OSS Distributions }
434*699cd480SApple OSS Distributions qbase = (unsigned)base;
435*699cd480SApple OSS Distributions cutoff = (u_quad_t)UQUAD_MAX / qbase;
436*699cd480SApple OSS Distributions cutlim = ((int)((u_quad_t)UQUAD_MAX % qbase));
437*699cd480SApple OSS Distributions for (acc = 0, any = 0;; c = *s++) {
438*699cd480SApple OSS Distributions if (isdigit(c)) {
439*699cd480SApple OSS Distributions c -= '0';
440*699cd480SApple OSS Distributions } else if (isalpha(c)) {
441*699cd480SApple OSS Distributions c -= isupper(c) ? 'A' - 10 : 'a' - 10;
442*699cd480SApple OSS Distributions } else {
443*699cd480SApple OSS Distributions break;
444*699cd480SApple OSS Distributions }
445*699cd480SApple OSS Distributions if (c >= base) {
446*699cd480SApple OSS Distributions break;
447*699cd480SApple OSS Distributions }
448*699cd480SApple OSS Distributions if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
449*699cd480SApple OSS Distributions any = -1;
450*699cd480SApple OSS Distributions } else {
451*699cd480SApple OSS Distributions any = 1;
452*699cd480SApple OSS Distributions acc *= qbase;
453*699cd480SApple OSS Distributions acc += c;
454*699cd480SApple OSS Distributions }
455*699cd480SApple OSS Distributions }
456*699cd480SApple OSS Distributions if (any < 0) {
457*699cd480SApple OSS Distributions acc = UQUAD_MAX;
458*699cd480SApple OSS Distributions // errno = ERANGE;
459*699cd480SApple OSS Distributions } else if (neg) {
460*699cd480SApple OSS Distributions acc = -acc;
461*699cd480SApple OSS Distributions }
462*699cd480SApple OSS Distributions if (endptr != 0) {
463*699cd480SApple OSS Distributions if (any) {
464*699cd480SApple OSS Distributions *endptr = __CAST_AWAY_QUALIFIER(s - 1, const, char *);
465*699cd480SApple OSS Distributions } else {
466*699cd480SApple OSS Distributions *endptr = __CAST_AWAY_QUALIFIER(nptr, const, char *);
467*699cd480SApple OSS Distributions }
468*699cd480SApple OSS Distributions }
469*699cd480SApple OSS Distributions
470*699cd480SApple OSS Distributions return acc;
471*699cd480SApple OSS Distributions }
472