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