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