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