2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 #define BUF 513 /* Maximum length of numeric string. */
50 * Flags used during conversion.
52 #define LONG 0x01 /* l: long or double */
53 #define LONGDBL 0x02 /* L: long double; unimplemented */
54 #define SHORT 0x04 /* h: short */
55 #define QUAD 0x08 /* q: quad */
56 #define SUPPRESS 0x10 /* suppress assignment */
57 #define POINTER 0x20 /* weird %p pointer (`fake hex') */
58 #define NOSKIP 0x40 /* do not skip blanks */
61 * The following are used in numeric conversions only:
62 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
63 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
65 #define SIGNOK 0x080 /* +/- is (still) legal */
66 #define NDIGITS 0x100 /* no digits detected */
68 #define DPTOK 0x200 /* (float) decimal point is still legal */
69 #define EXPOK 0x400 /* (float) exponent (e+3, etc) still legal */
71 #define PFXOK 0x200 /* 0x prefix is (still) legal */
72 #define NZDIGITS 0x400 /* no zero digits detected */
77 #define CT_CHAR 0 /* %c conversion */
78 #define CT_CCL 1 /* %[...] conversion */
79 #define CT_STRING 2 /* %s conversion */
80 #define CT_INT 3 /* integer, i.e., strtoll or strtoull */
81 #define CT_FLOAT 4 /* floating, i.e., strtod */
83 #define u_char unsigned char
84 #define u_long unsigned long
86 static u_char
*__sccl(char *tab
, u_char
*fmt
);
92 __svfscanf(fp
, fmt0
, ap
)
97 register u_char
*fmt
= (u_char
*)fmt0
;
98 register int c
; /* character from format, or conversion */
99 register size_t width
; /* field width, or 0 */
100 register char *p
; /* points into all kinds of strings */
101 register int n
; /* handy integer */
102 register int flags
; /* flags as defined above */
103 register char *p0
; /* saves original value of p when necessary */
104 int nassigned
; /* number of fields assigned */
105 int nread
; /* number of characters consumed from fp */
106 int base
; /* base argument to strtoll/strtoull */
107 uint64 (*ccfn
)(); /* conversion function (strtoll/strtoull) */
108 char ccltab
[256]; /* character class table for %[...] */
109 char buf
[BUF
]; /* buffer for numeric conversions */
111 /* `basefix' is used to avoid `if' tests in the integer scanner */
112 static short basefix
[17] =
113 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
117 base
= 0; /* XXX just to keep gcc happy */
118 ccfn
= NULL
; /* XXX just to keep gcc happy */
124 while ((fp
->_r
> 0 || __srefill(fp
) == 0) &&
126 nread
++, fp
->_r
--, fp
->_p
++;
134 * switch on the format. continue if done;
135 * break once format type is derived.
141 if (fp
->_r
<= 0 && __srefill(fp
))
153 flags
|= LONGDBL
| QUAD
;
170 case '0': case '1': case '2': case '3': case '4':
171 case '5': case '6': case '7': case '8': case '9':
172 width
= width
* 10 + c
- '0';
177 * Those marked `compat' are for 4.[123]BSD compatibility.
179 * (According to ANSI, E and X formats are supposed
180 * to the same as e and x. Sorry about that.)
182 case 'D': /* compat */
187 ccfn
= (uint64 (*)())strtoll
;
193 ccfn
= (uint64 (*)())strtoll
;
197 case 'O': /* compat */
214 flags
|= PFXOK
; /* enable 0x prefixing */
220 #ifdef FLOATING_POINT
235 fmt
= __sccl(ccltab
, fmt
);
245 case 'p': /* pointer format is like hex */
246 flags
|= POINTER
| PFXOK
;
253 if (flags
& SUPPRESS
) /* ??? */
256 *va_arg(ap
, short *) = nread
;
257 else if (flags
& LONG
)
258 *va_arg(ap
, long *) = nread
;
260 *va_arg(ap
, int *) = nread
;
264 * Disgusting backwards compatibility hacks. XXX
266 case '\0': /* compat */
269 default: /* compat */
273 ccfn
= (uint64 (*)())strtoll
;
279 * We have a conversion that requires input.
281 if (fp
->_r
<= 0 && __srefill(fp
))
285 * Consume leading white space, except for formats
286 * that suppress this.
288 if ((flags
& NOSKIP
) == 0) {
289 while (isspace(*fp
->_p
)) {
293 else if (__srefill(fp
))
297 * Note that there is at least one character in
298 * the buffer, so conversions that do not set NOSKIP
299 * ca no longer result in an input failure.
309 /* scan arbitrary characters (sets NOSKIP) */
312 if (flags
& SUPPRESS
) {
315 if ((n
= fp
->_r
) < width
) {
333 size_t r
= fread((void *)va_arg(ap
, char *), 1,
344 /* scan a (nonempty) character class (sets NOSKIP) */
346 width
= (size_t)~0; /* `infinity' */
347 /* take only those things in the class */
348 if (flags
& SUPPRESS
) {
350 while (ccltab
[*fp
->_p
]) {
351 n
++, fp
->_r
--, fp
->_p
++;
354 if (fp
->_r
<= 0 && __srefill(fp
)) {
363 p0
= p
= va_arg(ap
, char *);
364 while (ccltab
[*fp
->_p
]) {
369 if (fp
->_r
<= 0 && __srefill(fp
)) {
385 /* like CCL, but zero-length string OK, & no NOSKIP */
388 if (flags
& SUPPRESS
) {
390 while (!isspace(*fp
->_p
)) {
391 n
++, fp
->_r
--, fp
->_p
++;
394 if (fp
->_r
<= 0 && __srefill(fp
))
399 p0
= p
= va_arg(ap
, char *);
400 while (!isspace(*fp
->_p
)) {
405 if (fp
->_r
<= 0 && __srefill(fp
))
415 /* scan an integer as if by strtoll/strtoull */
417 if (width
== 0 || width
> sizeof(buf
) - 1)
418 width
= sizeof(buf
) - 1;
420 /* size_t is unsigned, hence this optimisation */
421 if (--width
> sizeof(buf
) - 2)
422 width
= sizeof(buf
) - 2;
425 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
426 for (p
= buf
; width
; width
--) {
429 * Switch on the character; `goto ok'
430 * if we accept it as a part of number.
435 * The digit 0 is always legal, but is
436 * special. For %i conversions, if no
437 * digits (zero or nonzero) have been
438 * scanned (only signs), we will have
439 * base==0. In that case, we should set
440 * it to 8 and enable 0x prefixing.
441 * Also, if we have not scanned zero digits
442 * before this, do not turn off prefixing
443 * (someone else will turn it off if we
444 * have scanned any nonzero digits).
451 if (flags
& NZDIGITS
)
452 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
454 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
457 /* 1 through 7 always legal */
458 case '1': case '2': case '3':
459 case '4': case '5': case '6': case '7':
460 base
= basefix
[base
];
461 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
464 /* digits 8 and 9 ok iff decimal or hex */
466 base
= basefix
[base
];
468 break; /* not legal here */
469 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
472 /* letters ok iff hex */
473 case 'A': case 'B': case 'C':
474 case 'D': case 'E': case 'F':
475 case 'a': case 'b': case 'c':
476 case 'd': case 'e': case 'f':
477 /* no need to fix base here */
479 break; /* not legal here */
480 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
483 /* sign ok only as first character */
485 if (flags
& SIGNOK
) {
491 /* x ok iff flag still set & 2nd char */
493 if (flags
& PFXOK
&& p
== buf
+ 1) {
494 base
= 16; /* if %i */
502 * If we got here, c is not a legal character
503 * for a number. Stop accumulating digits.
508 * c is legal: store it and look at the next.
513 else if (__srefill(fp
))
517 * If we had only a sign, it is no good; push
518 * back the sign. If the number ends in `x',
519 * it was [sign] '0' 'x', so push back the x
520 * and treat it as [sign] '0'.
522 if (flags
& NDIGITS
) {
524 (void) ungetc(*(u_char
*)--p
, fp
);
527 c
= ((u_char
*)p
)[-1];
528 if (c
== 'x' || c
== 'X') {
530 (void) ungetc(c
, fp
);
532 if ((flags
& SUPPRESS
) == 0) {
536 res
= (*ccfn
)(buf
, (char **)NULL
, base
);
538 *va_arg(ap
, void **) =
540 else if (flags
& QUAD
)
541 *va_arg(ap
, int64
*) = res
;
542 else if (flags
& LONG
)
543 *va_arg(ap
, long *) = res
;
544 else if (flags
& SHORT
)
545 *va_arg(ap
, short *) = res
;
547 *va_arg(ap
, int *) = res
;
553 #ifdef FLOATING_POINT
555 /* scan a floating point number as if by strtod */
557 if (width
== 0 || width
> sizeof(buf
) - 1)
558 width
= sizeof(buf
) - 1;
560 /* size_t is unsigned, hence this optimisation */
561 if (--width
> sizeof(buf
) - 2)
562 width
= sizeof(buf
) - 2;
565 flags
|= SIGNOK
| NDIGITS
| DPTOK
| EXPOK
;
566 for (p
= buf
; width
; width
--) {
569 * This code mimicks the integer conversion
570 * code, but is much simpler.
574 case '0': case '1': case '2': case '3':
575 case '4': case '5': case '6': case '7':
577 flags
&= ~(SIGNOK
| NDIGITS
);
581 if (flags
& SIGNOK
) {
588 flags
&= ~(SIGNOK
| DPTOK
);
593 /* no exponent without some digits */
594 if ((flags
&(NDIGITS
|EXPOK
)) == EXPOK
) {
596 (flags
& ~(EXPOK
|DPTOK
)) |
607 else if (__srefill(fp
))
611 * If no digits, might be missing exponent digits
612 * (just give back the exponent) or might be missing
613 * regular digits, but had sign and/or decimal point.
615 if (flags
& NDIGITS
) {
617 /* no digits at all */
619 ungetc(*(u_char
*)--p
, fp
);
622 /* just a bad exponent (e and maybe sign) */
624 if (c
!= 'e' && c
!= 'E') {
625 (void) ungetc(c
, fp
);/* sign */
628 (void) ungetc(c
, fp
);
630 if ((flags
& SUPPRESS
) == 0) {
634 res
= strtod(buf
, (char **) NULL
);
636 *va_arg(ap
, long double *) = res
;
637 else if (flags
& LONG
)
638 *va_arg(ap
, double *) = res
;
640 *va_arg(ap
, float *) = res
;
645 #endif /* FLOATING_POINT */
649 return (nassigned
? nassigned
: -1);
655 * Fill in the given table from the scanset at the given format
656 * (just after `['). Return a pointer to the character past the
657 * closing `]'. The table has a 1 wherever characters should be
658 * considered part of the scanset.
663 register u_char
*fmt
;
665 register int c
, n
, v
;
667 /* first `clear' the whole table */
668 c
= *fmt
++; /* first char hat => negated scanset */
670 v
= 1; /* default => accept */
671 c
= *fmt
++; /* get new first char */
673 v
= 0; /* default => reject */
674 /* should probably use memset here */
675 for (n
= 0; n
< 256; n
++)
678 return (fmt
- 1);/* format ended before closing ] */
681 * Now set the entries corresponding to the actual scanset
682 * to the opposite of the above.
684 * The first character may be ']' (or '-') without being special;
685 * the last character may be '-'.
689 tab
[c
] = v
; /* take character c */
691 n
= *fmt
++; /* and examine the next */
694 case 0: /* format ended too soon */
699 * A scanset of the form
701 * is defined as `the digit 0, the digit 1,
702 * the character +, the character -', but
703 * the effect of a scanset such as
705 * is implementation defined. The V7 Unix
706 * scanf treats `a-z' as `the letters a through
707 * z', but treats `a-a' as `the letter a, the
708 * character -, and the letter a'.
710 * For compatibility, the `-' is not considerd
711 * to define a range if the character following
712 * it is either a close bracket (required by ANSI)
713 * or is not numerically greater than the character
714 * we just stored in the table (c).
717 if (n
== ']' || n
< c
) {
719 break; /* resume the for(;;) */
722 do { /* fill in the range */
725 #if 1 /* XXX another disgusting compatibility hack */
727 * Alas, the V7 Unix scanf also treats formats
728 * such as [a-c-e] as `the letters a through e'.
729 * This too is permitted by the standard....
741 case ']': /* end of scanset */
744 default: /* just another character */