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