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