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