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