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
36 * @(#)vfscanf.c 8.1 (Berkeley) 6/4/93
37 * $FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.14.2.2 2002/04/17 14:58:23 ache Exp $
38 * $DragonFly: src/lib/libc/stdio/vfscanf.c,v 1.3 2004/03/08 07:55:57 hmp Exp $
54 #define FLOATING_POINT
61 #define BUF 513 /* Maximum length of numeric string. */
64 * Flags used during conversion.
66 #define LONG 0x01 /* l: long or double */
67 #define LONGDBL 0x02 /* L: long double */
68 #define SHORT 0x04 /* h: short */
69 #define SUPPRESS 0x08 /* suppress assignment */
70 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
71 #define NOSKIP 0x20 /* do not skip blanks */
75 * The following are used in numeric conversions only:
76 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
77 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
79 #define SIGNOK 0x40 /* +/- is (still) legal */
80 #define NDIGITS 0x80 /* no digits detected */
82 #define DPTOK 0x100 /* (float) decimal point is still legal */
83 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
85 #define PFXOK 0x100 /* 0x prefix is (still) legal */
86 #define NZDIGITS 0x200 /* no zero digits detected */
87 #define HAVESIGN 0x10000 /* sign detected */
92 #define CT_CHAR 0 /* %c conversion */
93 #define CT_CCL 1 /* %[...] conversion */
94 #define CT_STRING 2 /* %s conversion */
95 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
96 #define CT_FLOAT 4 /* floating, i.e., strtod */
98 #define u_char unsigned char
99 #define u_long unsigned long
101 static u_char *__sccl(char *, u_char *);
107 __svfscanf(FILE *fp, char const *fmt0, va_list ap)
109 u_char *fmt = (u_char *)fmt0;
110 int c; /* character from format, or conversion */
111 size_t width; /* field width, or 0 */
112 char *p; /* points into all kinds of strings */
113 int n; /* handy integer */
114 int flags; /* flags as defined above */
115 char *p0; /* saves original value of p when necessary */
116 int nassigned; /* number of fields assigned */
117 int nconversions; /* number of conversions */
118 int nread; /* number of characters consumed from fp */
119 int base; /* base argument to strtoq/strtouq */
120 u_quad_t(*ccfn)(); /* conversion function (strtoq/strtouq) */
121 char ccltab[256]; /* character class table for %[...] */
122 char buf[BUF]; /* buffer for numeric conversions */
124 /* `basefix' is used to avoid `if' tests in the integer scanner */
125 static short basefix[17] =
126 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
127 #ifdef FLOATING_POINT
128 char decimal_point = localeconv()->decimal_point[0];
134 base = 0; /* XXX just to keep gcc happy */
135 ccfn = NULL; /* XXX just to keep gcc happy */
141 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p))
142 nread++, fp->_r--, fp->_p++;
150 * switch on the format. continue if done;
151 * break once format type is derived.
157 if (fp->_r <= 0 && __srefill(fp))
181 case '0': case '1': case '2': case '3': case '4':
182 case '5': case '6': case '7': case '8': case '9':
183 width = width * 10 + c - '0';
188 * Those marked `compat' are for 4.[123]BSD compatibility.
190 * (According to ANSI, E and X formats are supposed
191 * to the same as e and x. Sorry about that.)
193 case 'D': /* compat */
198 ccfn = (u_quad_t (*)())strtoq;
204 ccfn = (u_quad_t (*)())strtoq;
208 case 'O': /* compat */
223 case 'X': /* compat XXX */
227 flags |= PFXOK; /* enable 0x prefixing */
233 #ifdef FLOATING_POINT
234 case 'E': /* compat XXX */
235 case 'F': /* compat */
238 case 'e': case 'f': case 'g':
248 fmt = __sccl(ccltab, fmt);
258 case 'p': /* pointer format is like hex */
259 flags |= POINTER | PFXOK;
267 if (flags & SUPPRESS) /* ??? */
270 *va_arg(ap, short *) = nread;
271 else if (flags & LONG)
272 *va_arg(ap, long *) = nread;
273 else if (flags & QUAD)
274 *va_arg(ap, quad_t *) = nread;
276 *va_arg(ap, int *) = nread;
280 * Disgusting backwards compatibility hacks. XXX
282 case '\0': /* compat */
285 default: /* compat */
289 ccfn = (u_quad_t (*)())strtoq;
295 * We have a conversion that requires input.
297 if (fp->_r <= 0 && __srefill(fp))
301 * Consume leading white space, except for formats
302 * that suppress this.
304 if ((flags & NOSKIP) == 0) {
305 while (isspace(*fp->_p)) {
309 else if (__srefill(fp))
313 * Note that there is at least one character in
314 * the buffer, so conversions that do not set NOSKIP
315 * ca no longer result in an input failure.
325 /* scan arbitrary characters (sets NOSKIP) */
328 if (flags & SUPPRESS) {
331 if ((n = fp->_r) < width) {
349 size_t r = fread((void *)va_arg(ap, char *), 1,
361 /* scan a (nonempty) character class (sets NOSKIP) */
363 width = (size_t)~0; /* `infinity' */
364 /* take only those things in the class */
365 if (flags & SUPPRESS) {
367 while (ccltab[*fp->_p]) {
368 n++, fp->_r--, fp->_p++;
371 if (fp->_r <= 0 && __srefill(fp)) {
380 p0 = p = va_arg(ap, char *);
381 while (ccltab[*fp->_p]) {
386 if (fp->_r <= 0 && __srefill(fp)) {
403 /* like CCL, but zero-length string OK, & no NOSKIP */
406 if (flags & SUPPRESS) {
408 while (!isspace(*fp->_p)) {
409 n++, fp->_r--, fp->_p++;
412 if (fp->_r <= 0 && __srefill(fp))
417 p0 = p = va_arg(ap, char *);
418 while (!isspace(*fp->_p)) {
423 if (fp->_r <= 0 && __srefill(fp))
434 /* scan an integer as if by strtoq/strtouq */
436 if (width == 0 || width > sizeof(buf) - 1)
437 width = sizeof(buf) - 1;
439 /* size_t is unsigned, hence this optimisation */
440 if (--width > sizeof(buf) - 2)
441 width = sizeof(buf) - 2;
444 flags |= SIGNOK | NDIGITS | NZDIGITS;
445 for (p = buf; width; width--) {
448 * Switch on the character; `goto ok'
449 * if we accept it as a part of number.
454 * The digit 0 is always legal, but is
455 * special. For %i conversions, if no
456 * digits (zero or nonzero) have been
457 * scanned (only signs), we will have
458 * base==0. In that case, we should set
459 * it to 8 and enable 0x prefixing.
460 * Also, if we have not scanned zero digits
461 * before this, do not turn off prefixing
462 * (someone else will turn it off if we
463 * have scanned any nonzero digits).
470 if (flags & NZDIGITS)
471 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
473 flags &= ~(SIGNOK|PFXOK|NDIGITS);
476 /* 1 through 7 always legal */
477 case '1': case '2': case '3':
478 case '4': case '5': case '6': case '7':
479 base = basefix[base];
480 flags &= ~(SIGNOK | PFXOK | NDIGITS);
483 /* digits 8 and 9 ok iff decimal or hex */
485 base = basefix[base];
487 break; /* not legal here */
488 flags &= ~(SIGNOK | PFXOK | NDIGITS);
491 /* letters ok iff hex */
492 case 'A': case 'B': case 'C':
493 case 'D': case 'E': case 'F':
494 case 'a': case 'b': case 'c':
495 case 'd': case 'e': case 'f':
496 /* no need to fix base here */
498 break; /* not legal here */
499 flags &= ~(SIGNOK | PFXOK | NDIGITS);
502 /* sign ok only as first character */
504 if (flags & SIGNOK) {
512 * x ok iff flag still set & 2nd char (or
513 * 3rd char if we have a sign).
516 if (flags & PFXOK && p ==
517 buf + 1 + !!(flags & HAVESIGN)) {
518 base = 16; /* if %i */
526 * If we got here, c is not a legal character
527 * for a number. Stop accumulating digits.
532 * c is legal: store it and look at the next.
537 else if (__srefill(fp))
541 * If we had only a sign, it is no good; push
542 * back the sign. If the number ends in `x',
543 * it was [sign] '0' 'x', so push back the x
544 * and treat it as [sign] '0'.
546 if (flags & NDIGITS) {
548 (void) ungetc(*(u_char *)--p, fp);
551 c = ((u_char *)p)[-1];
552 if (c == 'x' || c == 'X') {
554 (void) ungetc(c, fp);
556 if ((flags & SUPPRESS) == 0) {
560 res = (*ccfn)(buf, (char **)NULL, base);
562 *va_arg(ap, void **) =
564 else if (flags & SHORT)
565 *va_arg(ap, short *) = res;
566 else if (flags & LONG)
567 *va_arg(ap, long *) = res;
568 else if (flags & QUAD)
569 *va_arg(ap, quad_t *) = res;
571 *va_arg(ap, int *) = res;
578 #ifdef FLOATING_POINT
580 /* scan a floating point number as if by strtod */
582 if (width == 0 || width > sizeof(buf) - 1)
583 width = sizeof(buf) - 1;
585 /* size_t is unsigned, hence this optimisation */
586 if (--width > sizeof(buf) - 2)
587 width = sizeof(buf) - 2;
590 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
591 for (p = buf; width; width--) {
594 * This code mimicks the integer conversion
595 * code, but is much simpler.
599 case '0': case '1': case '2': case '3':
600 case '4': case '5': case '6': case '7':
602 flags &= ~(SIGNOK | NDIGITS);
606 if (flags & SIGNOK) {
612 /* no exponent without some digits */
613 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
615 (flags & ~(EXPOK|DPTOK)) |
621 if ((char)c == decimal_point &&
623 flags &= ~(SIGNOK | DPTOK);
633 else if (__srefill(fp))
637 * If no digits, might be missing exponent digits
638 * (just give back the exponent) or might be missing
639 * regular digits, but had sign and/or decimal point.
641 if (flags & NDIGITS) {
643 /* no digits at all */
645 ungetc(*(u_char *)--p, fp);
648 /* just a bad exponent (e and maybe sign) */
650 if (c != 'e' && c != 'E') {
651 (void) ungetc(c, fp);/* sign */
654 (void) ungetc(c, fp);
656 if ((flags & SUPPRESS) == 0) {
660 /* XXX this loses precision for long doubles. */
661 res = strtod(buf, (char **) NULL);
663 *va_arg(ap, long double *) = res;
664 else if (flags & LONG)
665 *va_arg(ap, double *) = res;
667 *va_arg(ap, float *) = res;
673 #endif /* FLOATING_POINT */
677 return (nconversions != 0 ? nassigned : EOF);
683 * Fill in the given table from the scanset at the given format
684 * (just after `['). Return a pointer to the character past the
685 * closing `]'. The table has a 1 wherever characters should be
686 * considered part of the scanset.
695 /* first `clear' the whole table */
696 c = *fmt++; /* first char hat => negated scanset */
698 v = 1; /* default => accept */
699 c = *fmt++; /* get new first char */
701 v = 0; /* default => reject */
703 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
704 (void) memset(tab, v, 256);
707 return (fmt - 1);/* format ended before closing ] */
710 * Now set the entries corresponding to the actual scanset
711 * to the opposite of the above.
713 * The first character may be ']' (or '-') without being special;
714 * the last character may be '-'.
718 tab[c] = v; /* take character c */
720 n = *fmt++; /* and examine the next */
723 case 0: /* format ended too soon */
728 * A scanset of the form
730 * is defined as `the digit 0, the digit 1,
731 * the character +, the character -', but
732 * the effect of a scanset such as
734 * is implementation defined. The V7 Unix
735 * scanf treats `a-z' as `the letters a through
736 * z', but treats `a-a' as `the letter a, the
737 * character -, and the letter a'.
739 * For compatibility, the `-' is not considerd
740 * to define a range if the character following
741 * it is either a close bracket (required by ANSI)
742 * or is not numerically greater than the character
743 * we just stored in the table (c).
747 || (__collate_load_error ? n < c :
748 __collate_range_cmp (n, c) < 0
752 break; /* resume the for(;;) */
755 /* fill in the range */
756 if (__collate_load_error) {
761 for (i = 0; i < 256; i ++)
762 if ( __collate_range_cmp (c, i) < 0
763 && __collate_range_cmp (i, n) <= 0
767 #if 1 /* XXX another disgusting compatibility hack */
770 * Alas, the V7 Unix scanf also treats formats
771 * such as [a-c-e] as `the letters a through e'.
772 * This too is permitted by the standard....
784 case ']': /* end of scanset */
787 default: /* just another character */