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