1*5c2921b0SApple OSS Distributions /*
2*5c2921b0SApple OSS Distributions * Copyright (c) 2004-2016 Apple Computer, Inc. All rights reserved.
3*5c2921b0SApple OSS Distributions *
4*5c2921b0SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*5c2921b0SApple OSS Distributions *
6*5c2921b0SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*5c2921b0SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*5c2921b0SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*5c2921b0SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*5c2921b0SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*5c2921b0SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*5c2921b0SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*5c2921b0SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*5c2921b0SApple OSS Distributions *
15*5c2921b0SApple OSS Distributions * Please obtain a copy of the License at
16*5c2921b0SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*5c2921b0SApple OSS Distributions *
18*5c2921b0SApple OSS Distributions * The Original Code and all software distributed under the License are
19*5c2921b0SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*5c2921b0SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*5c2921b0SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*5c2921b0SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*5c2921b0SApple OSS Distributions * Please see the License for the specific language governing rights and
24*5c2921b0SApple OSS Distributions * limitations under the License.
25*5c2921b0SApple OSS Distributions *
26*5c2921b0SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*5c2921b0SApple OSS Distributions */
28*5c2921b0SApple OSS Distributions /*-
29*5c2921b0SApple OSS Distributions * Copyright (c) 1990, 1993
30*5c2921b0SApple OSS Distributions * The Regents of the University of California. All rights reserved.
31*5c2921b0SApple OSS Distributions *
32*5c2921b0SApple OSS Distributions * This code is derived from software contributed to Berkeley by
33*5c2921b0SApple OSS Distributions * Chris Torek.
34*5c2921b0SApple OSS Distributions *
35*5c2921b0SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
36*5c2921b0SApple OSS Distributions * modification, are permitted provided that the following conditions
37*5c2921b0SApple OSS Distributions * are met:
38*5c2921b0SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
39*5c2921b0SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
40*5c2921b0SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
41*5c2921b0SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
42*5c2921b0SApple OSS Distributions * documentation and/or other materials provided with the distribution.
43*5c2921b0SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
44*5c2921b0SApple OSS Distributions * must display the following acknowledgement:
45*5c2921b0SApple OSS Distributions * This product includes software developed by the University of
46*5c2921b0SApple OSS Distributions * California, Berkeley and its contributors.
47*5c2921b0SApple OSS Distributions * 4. Neither the name of the University nor the names of its contributors
48*5c2921b0SApple OSS Distributions * may be used to endorse or promote products derived from this software
49*5c2921b0SApple OSS Distributions * without specific prior written permission.
50*5c2921b0SApple OSS Distributions *
51*5c2921b0SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52*5c2921b0SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53*5c2921b0SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54*5c2921b0SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55*5c2921b0SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56*5c2921b0SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57*5c2921b0SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58*5c2921b0SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59*5c2921b0SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60*5c2921b0SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61*5c2921b0SApple OSS Distributions * SUCH DAMAGE.
62*5c2921b0SApple OSS Distributions */
63*5c2921b0SApple OSS Distributions
64*5c2921b0SApple OSS Distributions #include <stdarg.h>
65*5c2921b0SApple OSS Distributions #include <stddef.h>
66*5c2921b0SApple OSS Distributions #include <string.h>
67*5c2921b0SApple OSS Distributions #include <sys/cdefs.h>
68*5c2921b0SApple OSS Distributions #include <sys/param.h>
69*5c2921b0SApple OSS Distributions
70*5c2921b0SApple OSS Distributions quad_t strtoq(const char *, char **, int);
71*5c2921b0SApple OSS Distributions u_quad_t strtouq(const char *, char **, int);
72*5c2921b0SApple OSS Distributions
73*5c2921b0SApple OSS Distributions static inline int
isspace(char c)74*5c2921b0SApple OSS Distributions isspace(char c)
75*5c2921b0SApple OSS Distributions {
76*5c2921b0SApple OSS Distributions return c == ' ' || c == '\t' || c == '\n' || c == '\12';
77*5c2921b0SApple OSS Distributions }
78*5c2921b0SApple OSS Distributions
79*5c2921b0SApple OSS Distributions #define BUF 32 /* Maximum length of numeric string. */
80*5c2921b0SApple OSS Distributions
81*5c2921b0SApple OSS Distributions /*
82*5c2921b0SApple OSS Distributions * Flags used during conversion.
83*5c2921b0SApple OSS Distributions */
84*5c2921b0SApple OSS Distributions #define LONG 0x01 /* l: long or double */
85*5c2921b0SApple OSS Distributions #define SHORT 0x04 /* h: short */
86*5c2921b0SApple OSS Distributions #define SUPPRESS 0x08 /* *: suppress assignment */
87*5c2921b0SApple OSS Distributions #define POINTER 0x10 /* p: void * (as hex) */
88*5c2921b0SApple OSS Distributions #define NOSKIP 0x20 /* [ or c: do not skip blanks */
89*5c2921b0SApple OSS Distributions #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
90*5c2921b0SApple OSS Distributions #define SHORTSHORT 0x4000 /* hh: char */
91*5c2921b0SApple OSS Distributions #define UNSIGNED 0x8000 /* %[oupxX] conversions */
92*5c2921b0SApple OSS Distributions
93*5c2921b0SApple OSS Distributions /*
94*5c2921b0SApple OSS Distributions * The following are used in numeric conversions only:
95*5c2921b0SApple OSS Distributions * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
96*5c2921b0SApple OSS Distributions * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
97*5c2921b0SApple OSS Distributions */
98*5c2921b0SApple OSS Distributions #define SIGNOK 0x40 /* +/- is (still) legal */
99*5c2921b0SApple OSS Distributions #define NDIGITS 0x80 /* no digits detected */
100*5c2921b0SApple OSS Distributions
101*5c2921b0SApple OSS Distributions #define DPTOK 0x100 /* (float) decimal point is still legal */
102*5c2921b0SApple OSS Distributions #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
103*5c2921b0SApple OSS Distributions
104*5c2921b0SApple OSS Distributions #define PFXOK 0x100 /* 0x prefix is (still) legal */
105*5c2921b0SApple OSS Distributions #define NZDIGITS 0x200 /* no zero digits detected */
106*5c2921b0SApple OSS Distributions
107*5c2921b0SApple OSS Distributions /*
108*5c2921b0SApple OSS Distributions * Conversion types.
109*5c2921b0SApple OSS Distributions */
110*5c2921b0SApple OSS Distributions #define CT_CHAR 0 /* %c conversion */
111*5c2921b0SApple OSS Distributions #define CT_CCL 1 /* %[...] conversion */
112*5c2921b0SApple OSS Distributions #define CT_STRING 2 /* %s conversion */
113*5c2921b0SApple OSS Distributions #define CT_INT 3 /* %[dioupxX] conversion */
114*5c2921b0SApple OSS Distributions
115*5c2921b0SApple OSS Distributions static const u_char *__sccl(char *, const u_char *);
116*5c2921b0SApple OSS Distributions
117*5c2921b0SApple OSS Distributions int sscanf(const char *, const char *, ...);
118*5c2921b0SApple OSS Distributions int vsscanf(const char *, char const *, va_list);
119*5c2921b0SApple OSS Distributions
120*5c2921b0SApple OSS Distributions int
sscanf(const char * ibuf,const char * fmt,...)121*5c2921b0SApple OSS Distributions sscanf(const char *ibuf, const char *fmt, ...)
122*5c2921b0SApple OSS Distributions {
123*5c2921b0SApple OSS Distributions va_list ap;
124*5c2921b0SApple OSS Distributions int ret;
125*5c2921b0SApple OSS Distributions
126*5c2921b0SApple OSS Distributions va_start(ap, fmt);
127*5c2921b0SApple OSS Distributions ret = vsscanf(ibuf, fmt, ap);
128*5c2921b0SApple OSS Distributions va_end(ap);
129*5c2921b0SApple OSS Distributions return ret;
130*5c2921b0SApple OSS Distributions }
131*5c2921b0SApple OSS Distributions
132*5c2921b0SApple OSS Distributions int
vsscanf(const char * inp,char const * fmt0,va_list ap)133*5c2921b0SApple OSS Distributions vsscanf(const char *inp, char const *fmt0, va_list ap)
134*5c2921b0SApple OSS Distributions {
135*5c2921b0SApple OSS Distributions ssize_t inr;
136*5c2921b0SApple OSS Distributions const u_char *fmt = (const u_char *)fmt0;
137*5c2921b0SApple OSS Distributions ssize_t width; /* field width, or 0 */
138*5c2921b0SApple OSS Distributions char *p; /* points into all kinds of strings */
139*5c2921b0SApple OSS Distributions int flags; /* flags as defined above */
140*5c2921b0SApple OSS Distributions char *p0; /* saves original value of p when necessary */
141*5c2921b0SApple OSS Distributions int nassigned = 0; /* number of fields assigned */
142*5c2921b0SApple OSS Distributions int nconversions = 0; /* number of conversions */
143*5c2921b0SApple OSS Distributions int nread = 0; /* number of characters consumed from fp */
144*5c2921b0SApple OSS Distributions int base = 0; /* base argument to conversion function */
145*5c2921b0SApple OSS Distributions char ccltab[256]; /* character class table for %[...] */
146*5c2921b0SApple OSS Distributions char buf[BUF]; /* buffer for numeric conversions */
147*5c2921b0SApple OSS Distributions
148*5c2921b0SApple OSS Distributions /* `basefix' is used to avoid `if' tests in the integer scanner */
149*5c2921b0SApple OSS Distributions static short basefix[17] =
150*5c2921b0SApple OSS Distributions { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
151*5c2921b0SApple OSS Distributions
152*5c2921b0SApple OSS Distributions inr = (ssize_t)strlen(inp);
153*5c2921b0SApple OSS Distributions
154*5c2921b0SApple OSS Distributions for (;;) {
155*5c2921b0SApple OSS Distributions char c = (char)*fmt++; /* character from format, or conversion */
156*5c2921b0SApple OSS Distributions if (c == 0) {
157*5c2921b0SApple OSS Distributions return nassigned;
158*5c2921b0SApple OSS Distributions }
159*5c2921b0SApple OSS Distributions if (isspace(c)) {
160*5c2921b0SApple OSS Distributions while (inr > 0 && isspace(*inp)) {
161*5c2921b0SApple OSS Distributions nread++;
162*5c2921b0SApple OSS Distributions inr--;
163*5c2921b0SApple OSS Distributions inp++;
164*5c2921b0SApple OSS Distributions }
165*5c2921b0SApple OSS Distributions continue;
166*5c2921b0SApple OSS Distributions }
167*5c2921b0SApple OSS Distributions if (c != '%') {
168*5c2921b0SApple OSS Distributions goto literal;
169*5c2921b0SApple OSS Distributions }
170*5c2921b0SApple OSS Distributions width = 0;
171*5c2921b0SApple OSS Distributions flags = 0;
172*5c2921b0SApple OSS Distributions /*
173*5c2921b0SApple OSS Distributions * switch on the format. continue if done;
174*5c2921b0SApple OSS Distributions * break once format type is derived.
175*5c2921b0SApple OSS Distributions */
176*5c2921b0SApple OSS Distributions again:
177*5c2921b0SApple OSS Distributions c = (char)*fmt++;
178*5c2921b0SApple OSS Distributions switch (c) {
179*5c2921b0SApple OSS Distributions case '%':
180*5c2921b0SApple OSS Distributions literal:
181*5c2921b0SApple OSS Distributions if (inr <= 0) {
182*5c2921b0SApple OSS Distributions goto input_failure;
183*5c2921b0SApple OSS Distributions }
184*5c2921b0SApple OSS Distributions if (*inp != c) {
185*5c2921b0SApple OSS Distributions goto match_failure;
186*5c2921b0SApple OSS Distributions }
187*5c2921b0SApple OSS Distributions inr--;
188*5c2921b0SApple OSS Distributions inp++;
189*5c2921b0SApple OSS Distributions nread++;
190*5c2921b0SApple OSS Distributions continue;
191*5c2921b0SApple OSS Distributions
192*5c2921b0SApple OSS Distributions case '*':
193*5c2921b0SApple OSS Distributions flags |= SUPPRESS;
194*5c2921b0SApple OSS Distributions goto again;
195*5c2921b0SApple OSS Distributions case 'l':
196*5c2921b0SApple OSS Distributions if (flags & LONG) {
197*5c2921b0SApple OSS Distributions flags &= ~LONG;
198*5c2921b0SApple OSS Distributions flags |= LONGLONG;
199*5c2921b0SApple OSS Distributions } else {
200*5c2921b0SApple OSS Distributions flags |= LONG;
201*5c2921b0SApple OSS Distributions }
202*5c2921b0SApple OSS Distributions goto again;
203*5c2921b0SApple OSS Distributions case 'q':
204*5c2921b0SApple OSS Distributions flags |= LONGLONG; /* not quite */
205*5c2921b0SApple OSS Distributions goto again;
206*5c2921b0SApple OSS Distributions case 'h':
207*5c2921b0SApple OSS Distributions if (flags & SHORT) {
208*5c2921b0SApple OSS Distributions flags &= ~SHORT;
209*5c2921b0SApple OSS Distributions flags |= SHORTSHORT;
210*5c2921b0SApple OSS Distributions } else {
211*5c2921b0SApple OSS Distributions flags |= SHORT;
212*5c2921b0SApple OSS Distributions }
213*5c2921b0SApple OSS Distributions goto again;
214*5c2921b0SApple OSS Distributions
215*5c2921b0SApple OSS Distributions case '0': case '1': case '2': case '3': case '4':
216*5c2921b0SApple OSS Distributions case '5': case '6': case '7': case '8': case '9':
217*5c2921b0SApple OSS Distributions width = width * 10 + c - '0';
218*5c2921b0SApple OSS Distributions goto again;
219*5c2921b0SApple OSS Distributions
220*5c2921b0SApple OSS Distributions /*
221*5c2921b0SApple OSS Distributions * Conversions.
222*5c2921b0SApple OSS Distributions */
223*5c2921b0SApple OSS Distributions case 'd':
224*5c2921b0SApple OSS Distributions c = CT_INT;
225*5c2921b0SApple OSS Distributions base = 10;
226*5c2921b0SApple OSS Distributions break;
227*5c2921b0SApple OSS Distributions
228*5c2921b0SApple OSS Distributions case 'i':
229*5c2921b0SApple OSS Distributions c = CT_INT;
230*5c2921b0SApple OSS Distributions base = 0;
231*5c2921b0SApple OSS Distributions break;
232*5c2921b0SApple OSS Distributions
233*5c2921b0SApple OSS Distributions case 'o':
234*5c2921b0SApple OSS Distributions c = CT_INT;
235*5c2921b0SApple OSS Distributions flags |= UNSIGNED;
236*5c2921b0SApple OSS Distributions base = 8;
237*5c2921b0SApple OSS Distributions break;
238*5c2921b0SApple OSS Distributions
239*5c2921b0SApple OSS Distributions case 'u':
240*5c2921b0SApple OSS Distributions c = CT_INT;
241*5c2921b0SApple OSS Distributions flags |= UNSIGNED;
242*5c2921b0SApple OSS Distributions base = 10;
243*5c2921b0SApple OSS Distributions break;
244*5c2921b0SApple OSS Distributions
245*5c2921b0SApple OSS Distributions case 'X':
246*5c2921b0SApple OSS Distributions case 'x':
247*5c2921b0SApple OSS Distributions flags |= PFXOK; /* enable 0x prefixing */
248*5c2921b0SApple OSS Distributions c = CT_INT;
249*5c2921b0SApple OSS Distributions flags |= UNSIGNED;
250*5c2921b0SApple OSS Distributions base = 16;
251*5c2921b0SApple OSS Distributions break;
252*5c2921b0SApple OSS Distributions
253*5c2921b0SApple OSS Distributions case 's':
254*5c2921b0SApple OSS Distributions c = CT_STRING;
255*5c2921b0SApple OSS Distributions break;
256*5c2921b0SApple OSS Distributions
257*5c2921b0SApple OSS Distributions case '[':
258*5c2921b0SApple OSS Distributions fmt = __sccl(ccltab, fmt);
259*5c2921b0SApple OSS Distributions flags |= NOSKIP;
260*5c2921b0SApple OSS Distributions c = CT_CCL;
261*5c2921b0SApple OSS Distributions break;
262*5c2921b0SApple OSS Distributions
263*5c2921b0SApple OSS Distributions case 'c':
264*5c2921b0SApple OSS Distributions flags |= NOSKIP;
265*5c2921b0SApple OSS Distributions c = CT_CHAR;
266*5c2921b0SApple OSS Distributions break;
267*5c2921b0SApple OSS Distributions
268*5c2921b0SApple OSS Distributions case 'p': /* pointer format is like hex */
269*5c2921b0SApple OSS Distributions flags |= POINTER | PFXOK;
270*5c2921b0SApple OSS Distributions c = CT_INT;
271*5c2921b0SApple OSS Distributions flags |= UNSIGNED;
272*5c2921b0SApple OSS Distributions base = 16;
273*5c2921b0SApple OSS Distributions break;
274*5c2921b0SApple OSS Distributions
275*5c2921b0SApple OSS Distributions case 'n':
276*5c2921b0SApple OSS Distributions nconversions++;
277*5c2921b0SApple OSS Distributions if (flags & SUPPRESS) { /* ??? */
278*5c2921b0SApple OSS Distributions continue;
279*5c2921b0SApple OSS Distributions }
280*5c2921b0SApple OSS Distributions if (flags & SHORTSHORT) {
281*5c2921b0SApple OSS Distributions *va_arg(ap, char *) = (char)nread;
282*5c2921b0SApple OSS Distributions } else if (flags & SHORT) {
283*5c2921b0SApple OSS Distributions *va_arg(ap, short *) = (short)nread;
284*5c2921b0SApple OSS Distributions } else if (flags & LONG) {
285*5c2921b0SApple OSS Distributions *va_arg(ap, long *) = (long)nread;
286*5c2921b0SApple OSS Distributions } else if (flags & LONGLONG) {
287*5c2921b0SApple OSS Distributions *va_arg(ap, long long *) = (long long)nread;
288*5c2921b0SApple OSS Distributions } else {
289*5c2921b0SApple OSS Distributions *va_arg(ap, int *) = (int)nread;
290*5c2921b0SApple OSS Distributions }
291*5c2921b0SApple OSS Distributions continue;
292*5c2921b0SApple OSS Distributions }
293*5c2921b0SApple OSS Distributions
294*5c2921b0SApple OSS Distributions /*
295*5c2921b0SApple OSS Distributions * We have a conversion that requires input.
296*5c2921b0SApple OSS Distributions */
297*5c2921b0SApple OSS Distributions if (inr <= 0) {
298*5c2921b0SApple OSS Distributions goto input_failure;
299*5c2921b0SApple OSS Distributions }
300*5c2921b0SApple OSS Distributions
301*5c2921b0SApple OSS Distributions /*
302*5c2921b0SApple OSS Distributions * Consume leading white space, except for formats
303*5c2921b0SApple OSS Distributions * that suppress this.
304*5c2921b0SApple OSS Distributions */
305*5c2921b0SApple OSS Distributions if ((flags & NOSKIP) == 0) {
306*5c2921b0SApple OSS Distributions while (isspace(*inp)) {
307*5c2921b0SApple OSS Distributions nread++;
308*5c2921b0SApple OSS Distributions if (--inr > 0) {
309*5c2921b0SApple OSS Distributions inp++;
310*5c2921b0SApple OSS Distributions } else {
311*5c2921b0SApple OSS Distributions goto input_failure;
312*5c2921b0SApple OSS Distributions }
313*5c2921b0SApple OSS Distributions }
314*5c2921b0SApple OSS Distributions /*
315*5c2921b0SApple OSS Distributions * Note that there is at least one character in
316*5c2921b0SApple OSS Distributions * the buffer, so conversions that do not set NOSKIP
317*5c2921b0SApple OSS Distributions * can no longer result in an input failure.
318*5c2921b0SApple OSS Distributions */
319*5c2921b0SApple OSS Distributions }
320*5c2921b0SApple OSS Distributions
321*5c2921b0SApple OSS Distributions /*
322*5c2921b0SApple OSS Distributions * Do the conversion.
323*5c2921b0SApple OSS Distributions */
324*5c2921b0SApple OSS Distributions switch (c) {
325*5c2921b0SApple OSS Distributions case CT_CHAR:
326*5c2921b0SApple OSS Distributions /* scan arbitrary characters (sets NOSKIP) */
327*5c2921b0SApple OSS Distributions if (width == 0) {
328*5c2921b0SApple OSS Distributions width = 1;
329*5c2921b0SApple OSS Distributions }
330*5c2921b0SApple OSS Distributions if (flags & SUPPRESS) {
331*5c2921b0SApple OSS Distributions size_t sum = 0;
332*5c2921b0SApple OSS Distributions for (;;) {
333*5c2921b0SApple OSS Distributions ssize_t n = inr;
334*5c2921b0SApple OSS Distributions if (n < width) {
335*5c2921b0SApple OSS Distributions sum += (size_t)n;
336*5c2921b0SApple OSS Distributions width -= n;
337*5c2921b0SApple OSS Distributions inp += n;
338*5c2921b0SApple OSS Distributions if (sum == 0) {
339*5c2921b0SApple OSS Distributions goto input_failure;
340*5c2921b0SApple OSS Distributions }
341*5c2921b0SApple OSS Distributions break;
342*5c2921b0SApple OSS Distributions } else {
343*5c2921b0SApple OSS Distributions sum += (size_t)width;
344*5c2921b0SApple OSS Distributions inr -= width;
345*5c2921b0SApple OSS Distributions inp += width;
346*5c2921b0SApple OSS Distributions break;
347*5c2921b0SApple OSS Distributions }
348*5c2921b0SApple OSS Distributions }
349*5c2921b0SApple OSS Distributions nread += sum;
350*5c2921b0SApple OSS Distributions } else {
351*5c2921b0SApple OSS Distributions bcopy(inp, va_arg(ap, char *), width);
352*5c2921b0SApple OSS Distributions inr -= width;
353*5c2921b0SApple OSS Distributions inp += width;
354*5c2921b0SApple OSS Distributions nread += width;
355*5c2921b0SApple OSS Distributions nassigned++;
356*5c2921b0SApple OSS Distributions }
357*5c2921b0SApple OSS Distributions nconversions++;
358*5c2921b0SApple OSS Distributions break;
359*5c2921b0SApple OSS Distributions
360*5c2921b0SApple OSS Distributions case CT_CCL: {
361*5c2921b0SApple OSS Distributions /* scan a (nonempty) character class (sets NOSKIP) */
362*5c2921b0SApple OSS Distributions if (width == 0) {
363*5c2921b0SApple OSS Distributions width = SSIZE_MAX; /* `infinity' */
364*5c2921b0SApple OSS Distributions }
365*5c2921b0SApple OSS Distributions /* take only those things in the class */
366*5c2921b0SApple OSS Distributions ptrdiff_t n;
367*5c2921b0SApple OSS Distributions if (flags & SUPPRESS) {
368*5c2921b0SApple OSS Distributions n = 0;
369*5c2921b0SApple OSS Distributions while (ccltab[(unsigned char)*inp]) {
370*5c2921b0SApple OSS Distributions n++;
371*5c2921b0SApple OSS Distributions inr--;
372*5c2921b0SApple OSS Distributions inp++;
373*5c2921b0SApple OSS Distributions if (--width == 0) {
374*5c2921b0SApple OSS Distributions break;
375*5c2921b0SApple OSS Distributions }
376*5c2921b0SApple OSS Distributions if (inr <= 0) {
377*5c2921b0SApple OSS Distributions if (n == 0) {
378*5c2921b0SApple OSS Distributions goto input_failure;
379*5c2921b0SApple OSS Distributions }
380*5c2921b0SApple OSS Distributions break;
381*5c2921b0SApple OSS Distributions }
382*5c2921b0SApple OSS Distributions }
383*5c2921b0SApple OSS Distributions if (n == 0) {
384*5c2921b0SApple OSS Distributions goto match_failure;
385*5c2921b0SApple OSS Distributions }
386*5c2921b0SApple OSS Distributions } else {
387*5c2921b0SApple OSS Distributions p0 = p = va_arg(ap, char *);
388*5c2921b0SApple OSS Distributions while (ccltab[(unsigned char)*inp]) {
389*5c2921b0SApple OSS Distributions inr--;
390*5c2921b0SApple OSS Distributions *p++ = *inp++;
391*5c2921b0SApple OSS Distributions if (--width == 0) {
392*5c2921b0SApple OSS Distributions break;
393*5c2921b0SApple OSS Distributions }
394*5c2921b0SApple OSS Distributions if (inr <= 0) {
395*5c2921b0SApple OSS Distributions if (p == p0) {
396*5c2921b0SApple OSS Distributions goto input_failure;
397*5c2921b0SApple OSS Distributions }
398*5c2921b0SApple OSS Distributions break;
399*5c2921b0SApple OSS Distributions }
400*5c2921b0SApple OSS Distributions }
401*5c2921b0SApple OSS Distributions n = p - p0;
402*5c2921b0SApple OSS Distributions if (n == 0) {
403*5c2921b0SApple OSS Distributions goto match_failure;
404*5c2921b0SApple OSS Distributions }
405*5c2921b0SApple OSS Distributions *p = 0;
406*5c2921b0SApple OSS Distributions nassigned++;
407*5c2921b0SApple OSS Distributions }
408*5c2921b0SApple OSS Distributions nread += n;
409*5c2921b0SApple OSS Distributions nconversions++;
410*5c2921b0SApple OSS Distributions break;
411*5c2921b0SApple OSS Distributions }
412*5c2921b0SApple OSS Distributions
413*5c2921b0SApple OSS Distributions case CT_STRING:
414*5c2921b0SApple OSS Distributions /* like CCL, but zero-length string OK, & no NOSKIP */
415*5c2921b0SApple OSS Distributions if (width == 0) {
416*5c2921b0SApple OSS Distributions width = SSIZE_MAX;
417*5c2921b0SApple OSS Distributions }
418*5c2921b0SApple OSS Distributions if (flags & SUPPRESS) {
419*5c2921b0SApple OSS Distributions size_t n = 0;
420*5c2921b0SApple OSS Distributions while (!isspace(*inp)) {
421*5c2921b0SApple OSS Distributions n++;
422*5c2921b0SApple OSS Distributions inr--;
423*5c2921b0SApple OSS Distributions inp++;
424*5c2921b0SApple OSS Distributions if (--width == 0) {
425*5c2921b0SApple OSS Distributions break;
426*5c2921b0SApple OSS Distributions }
427*5c2921b0SApple OSS Distributions if (inr <= 0) {
428*5c2921b0SApple OSS Distributions break;
429*5c2921b0SApple OSS Distributions }
430*5c2921b0SApple OSS Distributions }
431*5c2921b0SApple OSS Distributions nread += n;
432*5c2921b0SApple OSS Distributions } else {
433*5c2921b0SApple OSS Distributions p0 = p = va_arg(ap, char *);
434*5c2921b0SApple OSS Distributions while (!isspace(*inp)) {
435*5c2921b0SApple OSS Distributions inr--;
436*5c2921b0SApple OSS Distributions *p++ = *inp++;
437*5c2921b0SApple OSS Distributions if (--width == 0) {
438*5c2921b0SApple OSS Distributions break;
439*5c2921b0SApple OSS Distributions }
440*5c2921b0SApple OSS Distributions if (inr <= 0) {
441*5c2921b0SApple OSS Distributions break;
442*5c2921b0SApple OSS Distributions }
443*5c2921b0SApple OSS Distributions }
444*5c2921b0SApple OSS Distributions *p = 0;
445*5c2921b0SApple OSS Distributions nread += p - p0;
446*5c2921b0SApple OSS Distributions nassigned++;
447*5c2921b0SApple OSS Distributions }
448*5c2921b0SApple OSS Distributions nconversions++;
449*5c2921b0SApple OSS Distributions continue;
450*5c2921b0SApple OSS Distributions
451*5c2921b0SApple OSS Distributions case CT_INT:
452*5c2921b0SApple OSS Distributions /* scan an integer as if by the conversion function */
453*5c2921b0SApple OSS Distributions if (width <= 0 || width > (ssize_t)(sizeof(buf) - 1)) {
454*5c2921b0SApple OSS Distributions width = sizeof(buf) - 1;
455*5c2921b0SApple OSS Distributions }
456*5c2921b0SApple OSS Distributions flags |= SIGNOK | NDIGITS | NZDIGITS;
457*5c2921b0SApple OSS Distributions for (p = buf; width; width--) {
458*5c2921b0SApple OSS Distributions c = *inp;
459*5c2921b0SApple OSS Distributions /*
460*5c2921b0SApple OSS Distributions * Switch on the character; `goto ok'
461*5c2921b0SApple OSS Distributions * if we accept it as a part of number.
462*5c2921b0SApple OSS Distributions */
463*5c2921b0SApple OSS Distributions switch (c) {
464*5c2921b0SApple OSS Distributions /*
465*5c2921b0SApple OSS Distributions * The digit 0 is always legal, but is
466*5c2921b0SApple OSS Distributions * special. For %i conversions, if no
467*5c2921b0SApple OSS Distributions * digits (zero or nonzero) have been
468*5c2921b0SApple OSS Distributions * scanned (only signs), we will have
469*5c2921b0SApple OSS Distributions * base==0. In that case, we should set
470*5c2921b0SApple OSS Distributions * it to 8 and enable 0x prefixing.
471*5c2921b0SApple OSS Distributions * Also, if we have not scanned zero digits
472*5c2921b0SApple OSS Distributions * before this, do not turn off prefixing
473*5c2921b0SApple OSS Distributions * (someone else will turn it off if we
474*5c2921b0SApple OSS Distributions * have scanned any nonzero digits).
475*5c2921b0SApple OSS Distributions */
476*5c2921b0SApple OSS Distributions case '0':
477*5c2921b0SApple OSS Distributions if (base == 0) {
478*5c2921b0SApple OSS Distributions base = 8;
479*5c2921b0SApple OSS Distributions flags |= PFXOK;
480*5c2921b0SApple OSS Distributions }
481*5c2921b0SApple OSS Distributions if (flags & NZDIGITS) {
482*5c2921b0SApple OSS Distributions flags &= ~(SIGNOK | NZDIGITS | NDIGITS);
483*5c2921b0SApple OSS Distributions } else {
484*5c2921b0SApple OSS Distributions flags &= ~(SIGNOK | PFXOK | NDIGITS);
485*5c2921b0SApple OSS Distributions }
486*5c2921b0SApple OSS Distributions goto ok;
487*5c2921b0SApple OSS Distributions
488*5c2921b0SApple OSS Distributions /* 1 through 7 always legal */
489*5c2921b0SApple OSS Distributions case '1': case '2': case '3':
490*5c2921b0SApple OSS Distributions case '4': case '5': case '6': case '7':
491*5c2921b0SApple OSS Distributions base = basefix[base];
492*5c2921b0SApple OSS Distributions flags &= ~(SIGNOK | PFXOK | NDIGITS);
493*5c2921b0SApple OSS Distributions goto ok;
494*5c2921b0SApple OSS Distributions
495*5c2921b0SApple OSS Distributions /* digits 8 and 9 ok iff decimal or hex */
496*5c2921b0SApple OSS Distributions case '8': case '9':
497*5c2921b0SApple OSS Distributions base = basefix[base];
498*5c2921b0SApple OSS Distributions if (base <= 8) {
499*5c2921b0SApple OSS Distributions break; /* not legal here */
500*5c2921b0SApple OSS Distributions }
501*5c2921b0SApple OSS Distributions flags &= ~(SIGNOK | PFXOK | NDIGITS);
502*5c2921b0SApple OSS Distributions goto ok;
503*5c2921b0SApple OSS Distributions
504*5c2921b0SApple OSS Distributions /* letters ok iff hex */
505*5c2921b0SApple OSS Distributions case 'A': case 'B': case 'C':
506*5c2921b0SApple OSS Distributions case 'D': case 'E': case 'F':
507*5c2921b0SApple OSS Distributions case 'a': case 'b': case 'c':
508*5c2921b0SApple OSS Distributions case 'd': case 'e': case 'f':
509*5c2921b0SApple OSS Distributions /* no need to fix base here */
510*5c2921b0SApple OSS Distributions if (base <= 10) {
511*5c2921b0SApple OSS Distributions break; /* not legal here */
512*5c2921b0SApple OSS Distributions }
513*5c2921b0SApple OSS Distributions flags &= ~(SIGNOK | PFXOK | NDIGITS);
514*5c2921b0SApple OSS Distributions goto ok;
515*5c2921b0SApple OSS Distributions
516*5c2921b0SApple OSS Distributions /* sign ok only as first character */
517*5c2921b0SApple OSS Distributions case '+': case '-':
518*5c2921b0SApple OSS Distributions if (flags & SIGNOK) {
519*5c2921b0SApple OSS Distributions flags &= ~SIGNOK;
520*5c2921b0SApple OSS Distributions goto ok;
521*5c2921b0SApple OSS Distributions }
522*5c2921b0SApple OSS Distributions break;
523*5c2921b0SApple OSS Distributions
524*5c2921b0SApple OSS Distributions /* x ok iff flag still set & 2nd char */
525*5c2921b0SApple OSS Distributions case 'x': case 'X':
526*5c2921b0SApple OSS Distributions if (flags & PFXOK && p == buf + 1) {
527*5c2921b0SApple OSS Distributions base = 16; /* if %i */
528*5c2921b0SApple OSS Distributions flags &= ~PFXOK;
529*5c2921b0SApple OSS Distributions goto ok;
530*5c2921b0SApple OSS Distributions }
531*5c2921b0SApple OSS Distributions break;
532*5c2921b0SApple OSS Distributions }
533*5c2921b0SApple OSS Distributions
534*5c2921b0SApple OSS Distributions /*
535*5c2921b0SApple OSS Distributions * If we got here, c is not a legal character
536*5c2921b0SApple OSS Distributions * for a number. Stop accumulating digits.
537*5c2921b0SApple OSS Distributions */
538*5c2921b0SApple OSS Distributions break;
539*5c2921b0SApple OSS Distributions ok:
540*5c2921b0SApple OSS Distributions /*
541*5c2921b0SApple OSS Distributions * c is legal: store it and look at the next.
542*5c2921b0SApple OSS Distributions */
543*5c2921b0SApple OSS Distributions *p++ = c;
544*5c2921b0SApple OSS Distributions if (--inr > 0) {
545*5c2921b0SApple OSS Distributions inp++;
546*5c2921b0SApple OSS Distributions } else {
547*5c2921b0SApple OSS Distributions break; /* end of input */
548*5c2921b0SApple OSS Distributions }
549*5c2921b0SApple OSS Distributions }
550*5c2921b0SApple OSS Distributions /*
551*5c2921b0SApple OSS Distributions * If we had only a sign, it is no good; push
552*5c2921b0SApple OSS Distributions * back the sign. If the number ends in `x',
553*5c2921b0SApple OSS Distributions * it was [sign] '0' 'x', so push back the x
554*5c2921b0SApple OSS Distributions * and treat it as [sign] '0'.
555*5c2921b0SApple OSS Distributions */
556*5c2921b0SApple OSS Distributions if (flags & NDIGITS) {
557*5c2921b0SApple OSS Distributions if (p > buf) {
558*5c2921b0SApple OSS Distributions inp--;
559*5c2921b0SApple OSS Distributions inr++;
560*5c2921b0SApple OSS Distributions }
561*5c2921b0SApple OSS Distributions goto match_failure;
562*5c2921b0SApple OSS Distributions }
563*5c2921b0SApple OSS Distributions c = p[-1];
564*5c2921b0SApple OSS Distributions if (c == 'x' || c == 'X') {
565*5c2921b0SApple OSS Distributions --p;
566*5c2921b0SApple OSS Distributions inp--;
567*5c2921b0SApple OSS Distributions inr++;
568*5c2921b0SApple OSS Distributions }
569*5c2921b0SApple OSS Distributions if ((flags & SUPPRESS) == 0) {
570*5c2921b0SApple OSS Distributions u_quad_t res;
571*5c2921b0SApple OSS Distributions
572*5c2921b0SApple OSS Distributions *p = 0;
573*5c2921b0SApple OSS Distributions if ((flags & UNSIGNED) == 0) {
574*5c2921b0SApple OSS Distributions res = (u_quad_t)strtoq(buf, (char **)NULL, base);
575*5c2921b0SApple OSS Distributions } else {
576*5c2921b0SApple OSS Distributions res = strtouq(buf, (char **)NULL, base);
577*5c2921b0SApple OSS Distributions }
578*5c2921b0SApple OSS Distributions if (flags & POINTER) {
579*5c2921b0SApple OSS Distributions *va_arg(ap, void **) =
580*5c2921b0SApple OSS Distributions (void *)(uintptr_t)res;
581*5c2921b0SApple OSS Distributions } else if (flags & SHORTSHORT) {
582*5c2921b0SApple OSS Distributions *va_arg(ap, char *) = (char)res;
583*5c2921b0SApple OSS Distributions } else if (flags & SHORT) {
584*5c2921b0SApple OSS Distributions *va_arg(ap, short *) = (short)res;
585*5c2921b0SApple OSS Distributions } else if (flags & LONG) {
586*5c2921b0SApple OSS Distributions *va_arg(ap, long *) = (long)res;
587*5c2921b0SApple OSS Distributions } else if (flags & LONGLONG) {
588*5c2921b0SApple OSS Distributions *va_arg(ap, long long *) = (long long)res;
589*5c2921b0SApple OSS Distributions } else {
590*5c2921b0SApple OSS Distributions *va_arg(ap, int *) = (int)res;
591*5c2921b0SApple OSS Distributions }
592*5c2921b0SApple OSS Distributions nassigned++;
593*5c2921b0SApple OSS Distributions }
594*5c2921b0SApple OSS Distributions nread += p - buf;
595*5c2921b0SApple OSS Distributions nconversions++;
596*5c2921b0SApple OSS Distributions break;
597*5c2921b0SApple OSS Distributions }
598*5c2921b0SApple OSS Distributions }
599*5c2921b0SApple OSS Distributions input_failure:
600*5c2921b0SApple OSS Distributions return nconversions != 0 ? nassigned : -1;
601*5c2921b0SApple OSS Distributions match_failure:
602*5c2921b0SApple OSS Distributions return nassigned;
603*5c2921b0SApple OSS Distributions }
604*5c2921b0SApple OSS Distributions
605*5c2921b0SApple OSS Distributions /*
606*5c2921b0SApple OSS Distributions * Fill in the given table from the scanset at the given format
607*5c2921b0SApple OSS Distributions * (just after `['). Return a pointer to the character past the
608*5c2921b0SApple OSS Distributions * closing `]'. The table has a 1 wherever characters should be
609*5c2921b0SApple OSS Distributions * considered part of the scanset.
610*5c2921b0SApple OSS Distributions */
611*5c2921b0SApple OSS Distributions static const u_char *
__sccl(char * tab,const u_char * fmt)612*5c2921b0SApple OSS Distributions __sccl(char *tab, const u_char *fmt)
613*5c2921b0SApple OSS Distributions {
614*5c2921b0SApple OSS Distributions char v;
615*5c2921b0SApple OSS Distributions
616*5c2921b0SApple OSS Distributions /* first `clear' the whole table */
617*5c2921b0SApple OSS Distributions int c = *fmt++; /* first char hat => negated scanset */
618*5c2921b0SApple OSS Distributions if (c == '^') {
619*5c2921b0SApple OSS Distributions v = 1; /* default => accept */
620*5c2921b0SApple OSS Distributions c = *fmt++; /* get new first char */
621*5c2921b0SApple OSS Distributions } else {
622*5c2921b0SApple OSS Distributions v = 0; /* default => reject */
623*5c2921b0SApple OSS Distributions }
624*5c2921b0SApple OSS Distributions /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
625*5c2921b0SApple OSS Distributions (void) memset(tab, v, 256);
626*5c2921b0SApple OSS Distributions
627*5c2921b0SApple OSS Distributions if (c == 0) {
628*5c2921b0SApple OSS Distributions return fmt - 1;/* format ended before closing ] */
629*5c2921b0SApple OSS Distributions }
630*5c2921b0SApple OSS Distributions /*
631*5c2921b0SApple OSS Distributions * Now set the entries corresponding to the actual scanset
632*5c2921b0SApple OSS Distributions * to the opposite of the above.
633*5c2921b0SApple OSS Distributions *
634*5c2921b0SApple OSS Distributions * The first character may be ']' (or '-') without being special;
635*5c2921b0SApple OSS Distributions * the last character may be '-'.
636*5c2921b0SApple OSS Distributions */
637*5c2921b0SApple OSS Distributions v = 1 - v;
638*5c2921b0SApple OSS Distributions for (;;) {
639*5c2921b0SApple OSS Distributions int n;
640*5c2921b0SApple OSS Distributions tab[c] = v; /* take character c */
641*5c2921b0SApple OSS Distributions doswitch:
642*5c2921b0SApple OSS Distributions n = *fmt++;
643*5c2921b0SApple OSS Distributions switch (n) {
644*5c2921b0SApple OSS Distributions case 0: /* format ended too soon */
645*5c2921b0SApple OSS Distributions return fmt - 1;
646*5c2921b0SApple OSS Distributions
647*5c2921b0SApple OSS Distributions case '-':
648*5c2921b0SApple OSS Distributions /*
649*5c2921b0SApple OSS Distributions * A scanset of the form
650*5c2921b0SApple OSS Distributions * [01+-]
651*5c2921b0SApple OSS Distributions * is defined as `the digit 0, the digit 1,
652*5c2921b0SApple OSS Distributions * the character +, the character -', but
653*5c2921b0SApple OSS Distributions * the effect of a scanset such as
654*5c2921b0SApple OSS Distributions * [a-zA-Z0-9]
655*5c2921b0SApple OSS Distributions * is implementation defined. The V7 Unix
656*5c2921b0SApple OSS Distributions * scanf treats `a-z' as `the letters a through
657*5c2921b0SApple OSS Distributions * z', but treats `a-a' as `the letter a, the
658*5c2921b0SApple OSS Distributions * character -, and the letter a'.
659*5c2921b0SApple OSS Distributions *
660*5c2921b0SApple OSS Distributions * For compatibility, the `-' is not considerd
661*5c2921b0SApple OSS Distributions * to define a range if the character following
662*5c2921b0SApple OSS Distributions * it is either a close bracket (required by ANSI)
663*5c2921b0SApple OSS Distributions * or is not numerically greater than the character
664*5c2921b0SApple OSS Distributions * we just stored in the table (c).
665*5c2921b0SApple OSS Distributions */
666*5c2921b0SApple OSS Distributions n = *fmt;
667*5c2921b0SApple OSS Distributions if (n == ']' || n < c) {
668*5c2921b0SApple OSS Distributions c = '-';
669*5c2921b0SApple OSS Distributions break; /* resume the for(;;) */
670*5c2921b0SApple OSS Distributions }
671*5c2921b0SApple OSS Distributions fmt++;
672*5c2921b0SApple OSS Distributions /* fill in the range */
673*5c2921b0SApple OSS Distributions do {
674*5c2921b0SApple OSS Distributions tab[++c] = v;
675*5c2921b0SApple OSS Distributions } while (c < n);
676*5c2921b0SApple OSS Distributions c = n;
677*5c2921b0SApple OSS Distributions /*
678*5c2921b0SApple OSS Distributions * Alas, the V7 Unix scanf also treats formats
679*5c2921b0SApple OSS Distributions * such as [a-c-e] as `the letters a through e'.
680*5c2921b0SApple OSS Distributions * This too is permitted by the standard....
681*5c2921b0SApple OSS Distributions */
682*5c2921b0SApple OSS Distributions goto doswitch;
683*5c2921b0SApple OSS Distributions
684*5c2921b0SApple OSS Distributions case ']': /* end of scanset */
685*5c2921b0SApple OSS Distributions return fmt;
686*5c2921b0SApple OSS Distributions
687*5c2921b0SApple OSS Distributions default: /* just another character */
688*5c2921b0SApple OSS Distributions c = n;
689*5c2921b0SApple OSS Distributions break;
690*5c2921b0SApple OSS Distributions }
691*5c2921b0SApple OSS Distributions }
692*5c2921b0SApple OSS Distributions /* NOTREACHED */
693*5c2921b0SApple OSS Distributions }
694