4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 4. Neither the name of the University nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * From: Id: vfscanf.c,v 1.13 1998/09/25 12:20:27 obrien Exp
54 * From: static char sccsid[] = "@(#)strtol.c 8.1 (Berkeley) 6/4/93";
55 * From: static char sccsid[] = "@(#)strtoul.c 8.1 (Berkeley) 6/4/93";
60 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
61 * Use is subject to license terms.
64 #pragma ident "%Z%%M% %I% %E% SMI"
66 #include <sys/types.h>
67 #include <sys/systm.h>
68 #include <sys/ctype.h>
69 #include <sys/sunddi.h>
70 #include <util/sscanf.h>
72 #define BUF 32 /* Maximum length of numeric string. */
75 * Flags used during conversion.
77 #define LONG 0x01 /* l: long or double */
78 #define SHORT 0x04 /* h: short */
79 #define SUPPRESS 0x08 /* suppress assignment */
80 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
81 #define NOSKIP 0x20 /* do not skip blanks */
84 * The following are used in numeric conversions only:
85 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
86 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
88 #define SIGNOK 0x40 /* +/- is (still) legal */
89 #define NDIGITS 0x80 /* no digits detected */
91 #define DPTOK 0x100 /* (float) decimal point is still legal */
92 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
94 #define PFXOK 0x100 /* 0x prefix is (still) legal */
95 #define NZDIGITS 0x200 /* no zero digits detected */
100 #define CT_CHAR 0 /* %c conversion */
101 #define CT_CCL 1 /* %[...] conversion */
102 #define CT_STRING 2 /* %s conversion */
103 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
105 static const uchar_t
*set_ccl(char *, const uchar_t
*);
107 #define isspace(ch) (((ch) == ' ') || ((ch) == '\r') || ((ch) == '\n') || \
108 ((ch) == '\t') || ((ch) == '\f'))
111 vsscanf(const char *inp
, char const *fmt0
, va_list ap
)
114 const uchar_t
*fmt
= (const uchar_t
*)fmt0
;
115 int c
; /* character from format, or conversion */
116 size_t width
; /* field width, or 0 */
117 char *p
; /* points into all kinds of strings */
118 int n
; /* handy integer */
119 int flags
; /* flags as defined above */
120 char *p0
; /* saves original value of p when necessary */
121 int nassigned
; /* number of fields assigned */
122 int nconversions
; /* number of conversions */
123 int nread
; /* number of characters consumed from fp */
124 int base
; /* base argument to strtoq/strtouq */
125 int sconv
; /* do signed conversion */
126 char ccltab
[256]; /* character class table for %[...] */
127 char buf
[BUF
]; /* buffer for numeric conversions */
129 /* `basefix' is used to avoid `if' tests in the integer scanner */
130 static short basefix
[17] =
131 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
145 while (inr
> 0 && isspace(*inp
))
146 nread
++, inr
--, inp
++;
154 * switch on the format. continue if done;
155 * break once format type is derived.
179 case '0': case '1': case '2': case '3': case '4':
180 case '5': case '6': case '7': case '8': case '9':
181 width
= width
* 10 + c
- '0';
211 flags
|= PFXOK
; /* enable 0x prefixing */
221 fmt
= set_ccl(ccltab
, fmt
);
231 case 'p': /* pointer format is like hex */
232 flags
|= POINTER
| PFXOK
;
239 if (flags
& SUPPRESS
) /* ??? */
242 *va_arg(ap
, short *) = (short)nread
;
243 else if (flags
& LONG
)
244 *va_arg(ap
, long *) = (long)nread
;
246 *va_arg(ap
, int *) = nread
;
251 * We have a conversion that requires input.
257 * Consume leading white space, except for formats
258 * that suppress this.
260 if ((flags
& NOSKIP
) == 0) {
261 while (isspace(*inp
)) {
269 * Note that there is at least one character in
270 * the buffer, so conversions that do not set NOSKIP
271 * can no longer result in an input failure.
281 /* scan arbitrary characters (sets NOSKIP) */
284 if (flags
& SUPPRESS
) {
287 if ((n
= inr
) < width
) {
300 bcopy(inp
, va_arg(ap
, char *), width
);
310 /* scan a (nonempty) character class (sets NOSKIP) */
312 width
= (size_t)~0; /* `infinity' */
313 /* take only those things in the class */
314 if (flags
& SUPPRESS
) {
316 while (ccltab
[(unsigned char)*inp
]) {
329 p0
= p
= va_arg(ap
, char *);
330 while (ccltab
[(unsigned char)*inp
]) {
352 /* like CCL, but zero-length string OK, & no NOSKIP */
355 if (flags
& SUPPRESS
) {
357 while (!isspace(*inp
)) {
366 p0
= p
= va_arg(ap
, char *);
367 while (!isspace(*inp
)) {
383 /* scan an integer as if by strtoq/strtouq */
384 /* size_t is unsigned, hence this optimisation */
385 if (--width
> sizeof (buf
) - 2)
386 width
= sizeof (buf
) - 2;
388 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
389 for (p
= buf
; width
; width
--) {
392 * Switch on the character; `goto ok'
393 * if we accept it as a part of number.
398 * The digit 0 is always legal, but is
399 * special. For %i conversions, if no
400 * digits (zero or nonzero) have been
401 * scanned (only signs), we will have
402 * base==0. In that case, we should set
403 * it to 8 and enable 0x prefixing.
404 * Also, if we have not scanned zero digits
405 * before this, do not turn off prefixing
406 * (someone else will turn it off if we
407 * have scanned any nonzero digits).
414 if (flags
& NZDIGITS
)
415 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
417 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
420 /* 1 through 7 always legal */
421 case '1': case '2': case '3':
422 case '4': case '5': case '6': case '7':
423 base
= basefix
[base
];
424 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
427 /* digits 8 and 9 ok iff decimal or hex */
429 base
= basefix
[base
];
431 break; /* not legal here */
432 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
435 /* letters ok iff hex */
436 case 'A': case 'B': case 'C':
437 case 'D': case 'E': case 'F':
438 case 'a': case 'b': case 'c':
439 case 'd': case 'e': case 'f':
440 /* no need to fix base here */
442 break; /* not legal here */
443 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
446 /* sign ok only as first character */
448 if (flags
& SIGNOK
) {
454 /* x ok iff flag still set & 2nd char */
456 if (flags
& PFXOK
&& p
== buf
+ 1) {
457 base
= 16; /* if %i */
465 * If we got here, c is not a legal character
466 * for a number. Stop accumulating digits.
471 * c is legal: store it and look at the next.
477 break; /* end of input */
480 * If we had only a sign, it is no good; push
481 * back the sign. If the number ends in `x',
482 * it was [sign] '0' 'x', so push back the x
483 * and treat it as [sign] '0'.
485 if (flags
& NDIGITS
) {
492 c
= ((uchar_t
*)p
)[-1];
493 if (c
== 'x' || c
== 'X') {
498 if ((flags
& SUPPRESS
) == 0) {
503 (void) ddi_strtol(buf
, (char **)NULL
,
504 base
, (long *)(&res
));
506 (void) ddi_strtoul(buf
, (char **)NULL
,
509 *va_arg(ap
, void **) =
510 (void *)(uintptr_t)res
;
511 else if (flags
& SHORT
)
512 *va_arg(ap
, short *) = (short)res
;
513 else if (flags
& LONG
)
514 *va_arg(ap
, long *) = (long)res
;
516 *va_arg(ap
, int *) = (int)res
;
526 return (nconversions
!= 0 ? nassigned
: -1);
532 * Fill in the given table from the scanset at the given format
533 * (just after `['). Return a pointer to the character past the
534 * closing `]'. The table has a 1 wherever characters should be
535 * considered part of the scanset.
537 static const uchar_t
*
538 set_ccl(char *tab
, const uchar_t
*fmt
)
542 /* first `clear' the whole table */
543 c
= *fmt
++; /* first char hat => negated scanset */
545 v
= 1; /* default => accept */
546 c
= *fmt
++; /* get new first char */
548 v
= 0; /* default => reject */
550 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
551 for (n
= 0; n
< 256; n
++)
552 tab
[n
] = v
; /* memset(tab, v, 256) */
555 return (fmt
- 1); /* format ended before closing ] */
558 * Now set the entries corresponding to the actual scanset
559 * to the opposite of the above.
561 * The first character may be ']' (or '-') without being special;
562 * the last character may be '-'.
566 tab
[c
] = v
; /* take character c */
568 n
= *fmt
++; /* and examine the next */
571 case 0: /* format ended too soon */
576 * A scanset of the form
578 * is defined as `the digit 0, the digit 1,
579 * the character +, the character -', but
580 * the effect of a scanset such as
582 * is implementation defined. The V7 Unix
583 * scanf treats `a-z' as `the letters a through
584 * z', but treats `a-a' as `the letter a, the
585 * character -, and the letter a'.
587 * For compatibility, the `-' is not considerd
588 * to define a range if the character following
589 * it is either a close bracket (required by ANSI)
590 * or is not numerically greater than the character
591 * we just stored in the table (c).
594 if (n
== ']' || n
< c
) {
596 break; /* resume the for(;;) */
599 /* fill in the range */
605 * Alas, the V7 Unix scanf also treats formats
606 * such as [a-c-e] as `the letters a through e'.
607 * This too is permitted by the standard....
612 case ']': /* end of scanset */
615 default: /* just another character */
624 sscanf(const char *ibuf
, const char *fmt
, ...)
630 ret
= vsscanf(ibuf
, fmt
, ap
);