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.4 2004/06/07 20:35:41 hmp Exp $
50 #define FLOATING_POINT
57 #define BUF 513 /* Maximum length of numeric string. */
60 * Flags used during conversion.
62 #define LONG 0x01 /* l: long or double */
63 #define LONGDBL 0x02 /* L: long double */
64 #define SHORT 0x04 /* h: short */
65 #define SUPPRESS 0x08 /* suppress assignment */
66 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
67 #define NOSKIP 0x20 /* do not skip blanks */
71 * The following are used in numeric conversions only:
72 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
73 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
75 #define SIGNOK 0x40 /* +/- is (still) legal */
76 #define NDIGITS 0x80 /* no digits detected */
78 #define DPTOK 0x100 /* (float) decimal point is still legal */
79 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
81 #define PFXOK 0x100 /* 0x prefix is (still) legal */
82 #define NZDIGITS 0x200 /* no zero digits detected */
83 #define HAVESIGN 0x10000 /* sign detected */
88 #define CT_CHAR 0 /* %c conversion */
89 #define CT_CCL 1 /* %[...] conversion */
90 #define CT_STRING 2 /* %s conversion */
91 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
92 #define CT_FLOAT 4 /* floating, i.e., strtod */
94 #define u_char unsigned char
95 #define u_long unsigned long
97 static u_char *__sccl(char *, u_char *);
103 __svfscanf(FILE *fp, char const *fmt0, va_list ap)
105 u_char *fmt = (u_char *)fmt0;
106 int c; /* character from format, or conversion */
107 size_t width; /* field width, or 0 */
108 char *p; /* points into all kinds of strings */
109 int n; /* handy integer */
110 int flags; /* flags as defined above */
111 char *p0; /* saves original value of p when necessary */
112 int nassigned; /* number of fields assigned */
113 int nconversions; /* number of conversions */
114 int nread; /* number of characters consumed from fp */
115 int base; /* base argument to strtoq/strtouq */
116 u_quad_t(*ccfn)(); /* conversion function (strtoq/strtouq) */
117 char ccltab[256]; /* character class table for %[...] */
118 char buf[BUF]; /* buffer for numeric conversions */
120 /* `basefix' is used to avoid `if' tests in the integer scanner */
121 static short basefix[17] =
122 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
123 #ifdef FLOATING_POINT
124 char decimal_point = localeconv()->decimal_point[0];
130 base = 0; /* XXX just to keep gcc happy */
131 ccfn = NULL; /* XXX just to keep gcc happy */
137 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p))
138 nread++, fp->_r--, fp->_p++;
146 * switch on the format. continue if done;
147 * break once format type is derived.
153 if (fp->_r <= 0 && __srefill(fp))
177 case '0': case '1': case '2': case '3': case '4':
178 case '5': case '6': case '7': case '8': case '9':
179 width = width * 10 + c - '0';
184 * Those marked `compat' are for 4.[123]BSD compatibility.
186 * (According to ANSI, E and X formats are supposed
187 * to the same as e and x. Sorry about that.)
189 case 'D': /* compat */
194 ccfn = (u_quad_t (*)())strtoq;
200 ccfn = (u_quad_t (*)())strtoq;
204 case 'O': /* compat */
219 case 'X': /* compat XXX */
223 flags |= PFXOK; /* enable 0x prefixing */
229 #ifdef FLOATING_POINT
230 case 'E': /* compat XXX */
231 case 'F': /* compat */
234 case 'e': case 'f': case 'g':
244 fmt = __sccl(ccltab, fmt);
254 case 'p': /* pointer format is like hex */
255 flags |= POINTER | PFXOK;
263 if (flags & SUPPRESS) /* ??? */
266 *va_arg(ap, short *) = nread;
267 else if (flags & LONG)
268 *va_arg(ap, long *) = nread;
269 else if (flags & QUAD)
270 *va_arg(ap, quad_t *) = nread;
272 *va_arg(ap, int *) = nread;
276 * Disgusting backwards compatibility hacks. XXX
278 case '\0': /* compat */
281 default: /* compat */
285 ccfn = (u_quad_t (*)())strtoq;
291 * We have a conversion that requires input.
293 if (fp->_r <= 0 && __srefill(fp))
297 * Consume leading white space, except for formats
298 * that suppress this.
300 if ((flags & NOSKIP) == 0) {
301 while (isspace(*fp->_p)) {
305 else if (__srefill(fp))
309 * Note that there is at least one character in
310 * the buffer, so conversions that do not set NOSKIP
311 * ca no longer result in an input failure.
321 /* scan arbitrary characters (sets NOSKIP) */
324 if (flags & SUPPRESS) {
327 if ((n = fp->_r) < width) {
345 size_t r = fread((void *)va_arg(ap, char *), 1,
357 /* scan a (nonempty) character class (sets NOSKIP) */
359 width = (size_t)~0; /* `infinity' */
360 /* take only those things in the class */
361 if (flags & SUPPRESS) {
363 while (ccltab[*fp->_p]) {
364 n++, fp->_r--, fp->_p++;
367 if (fp->_r <= 0 && __srefill(fp)) {
376 p0 = p = va_arg(ap, char *);
377 while (ccltab[*fp->_p]) {
382 if (fp->_r <= 0 && __srefill(fp)) {
399 /* like CCL, but zero-length string OK, & no NOSKIP */
402 if (flags & SUPPRESS) {
404 while (!isspace(*fp->_p)) {
405 n++, fp->_r--, fp->_p++;
408 if (fp->_r <= 0 && __srefill(fp))
413 p0 = p = va_arg(ap, char *);
414 while (!isspace(*fp->_p)) {
419 if (fp->_r <= 0 && __srefill(fp))
430 /* scan an integer as if by strtoq/strtouq */
432 if (width == 0 || width > sizeof(buf) - 1)
433 width = sizeof(buf) - 1;
435 /* size_t is unsigned, hence this optimisation */
436 if (--width > sizeof(buf) - 2)
437 width = sizeof(buf) - 2;
440 flags |= SIGNOK | NDIGITS | NZDIGITS;
441 for (p = buf; width; width--) {
444 * Switch on the character; `goto ok'
445 * if we accept it as a part of number.
450 * The digit 0 is always legal, but is
451 * special. For %i conversions, if no
452 * digits (zero or nonzero) have been
453 * scanned (only signs), we will have
454 * base==0. In that case, we should set
455 * it to 8 and enable 0x prefixing.
456 * Also, if we have not scanned zero digits
457 * before this, do not turn off prefixing
458 * (someone else will turn it off if we
459 * have scanned any nonzero digits).
466 if (flags & NZDIGITS)
467 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
469 flags &= ~(SIGNOK|PFXOK|NDIGITS);
472 /* 1 through 7 always legal */
473 case '1': case '2': case '3':
474 case '4': case '5': case '6': case '7':
475 base = basefix[base];
476 flags &= ~(SIGNOK | PFXOK | NDIGITS);
479 /* digits 8 and 9 ok iff decimal or hex */
481 base = basefix[base];
483 break; /* not legal here */
484 flags &= ~(SIGNOK | PFXOK | NDIGITS);
487 /* letters ok iff hex */
488 case 'A': case 'B': case 'C':
489 case 'D': case 'E': case 'F':
490 case 'a': case 'b': case 'c':
491 case 'd': case 'e': case 'f':
492 /* no need to fix base here */
494 break; /* not legal here */
495 flags &= ~(SIGNOK | PFXOK | NDIGITS);
498 /* sign ok only as first character */
500 if (flags & SIGNOK) {
508 * x ok iff flag still set & 2nd char (or
509 * 3rd char if we have a sign).
512 if (flags & PFXOK && p ==
513 buf + 1 + !!(flags & HAVESIGN)) {
514 base = 16; /* if %i */
522 * If we got here, c is not a legal character
523 * for a number. Stop accumulating digits.
528 * c is legal: store it and look at the next.
533 else if (__srefill(fp))
537 * If we had only a sign, it is no good; push
538 * back the sign. If the number ends in `x',
539 * it was [sign] '0' 'x', so push back the x
540 * and treat it as [sign] '0'.
542 if (flags & NDIGITS) {
544 (void) ungetc(*(u_char *)--p, fp);
547 c = ((u_char *)p)[-1];
548 if (c == 'x' || c == 'X') {
550 (void) ungetc(c, fp);
552 if ((flags & SUPPRESS) == 0) {
556 res = (*ccfn)(buf, (char **)NULL, base);
558 *va_arg(ap, void **) =
560 else if (flags & SHORT)
561 *va_arg(ap, short *) = res;
562 else if (flags & LONG)
563 *va_arg(ap, long *) = res;
564 else if (flags & QUAD)
565 *va_arg(ap, quad_t *) = res;
567 *va_arg(ap, int *) = res;
574 #ifdef FLOATING_POINT
576 /* scan a floating point number as if by strtod */
578 if (width == 0 || width > sizeof(buf) - 1)
579 width = sizeof(buf) - 1;
581 /* size_t is unsigned, hence this optimisation */
582 if (--width > sizeof(buf) - 2)
583 width = sizeof(buf) - 2;
586 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
587 for (p = buf; width; width--) {
590 * This code mimicks the integer conversion
591 * code, but is much simpler.
595 case '0': case '1': case '2': case '3':
596 case '4': case '5': case '6': case '7':
598 flags &= ~(SIGNOK | NDIGITS);
602 if (flags & SIGNOK) {
608 /* no exponent without some digits */
609 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
611 (flags & ~(EXPOK|DPTOK)) |
617 if ((char)c == decimal_point &&
619 flags &= ~(SIGNOK | DPTOK);
629 else if (__srefill(fp))
633 * If no digits, might be missing exponent digits
634 * (just give back the exponent) or might be missing
635 * regular digits, but had sign and/or decimal point.
637 if (flags & NDIGITS) {
639 /* no digits at all */
641 ungetc(*(u_char *)--p, fp);
644 /* just a bad exponent (e and maybe sign) */
646 if (c != 'e' && c != 'E') {
647 (void) ungetc(c, fp);/* sign */
650 (void) ungetc(c, fp);
652 if ((flags & SUPPRESS) == 0) {
656 /* XXX this loses precision for long doubles. */
657 res = strtod(buf, (char **) NULL);
659 *va_arg(ap, long double *) = res;
660 else if (flags & LONG)
661 *va_arg(ap, double *) = res;
663 *va_arg(ap, float *) = res;
669 #endif /* FLOATING_POINT */
673 return (nconversions != 0 ? nassigned : EOF);
679 * Fill in the given table from the scanset at the given format
680 * (just after `['). Return a pointer to the character past the
681 * closing `]'. The table has a 1 wherever characters should be
682 * considered part of the scanset.
691 /* first `clear' the whole table */
692 c = *fmt++; /* first char hat => negated scanset */
694 v = 1; /* default => accept */
695 c = *fmt++; /* get new first char */
697 v = 0; /* default => reject */
699 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
700 (void) memset(tab, v, 256);
703 return (fmt - 1);/* format ended before closing ] */
706 * Now set the entries corresponding to the actual scanset
707 * to the opposite of the above.
709 * The first character may be ']' (or '-') without being special;
710 * the last character may be '-'.
714 tab[c] = v; /* take character c */
716 n = *fmt++; /* and examine the next */
719 case 0: /* format ended too soon */
724 * A scanset of the form
726 * is defined as `the digit 0, the digit 1,
727 * the character +, the character -', but
728 * the effect of a scanset such as
730 * is implementation defined. The V7 Unix
731 * scanf treats `a-z' as `the letters a through
732 * z', but treats `a-a' as `the letter a, the
733 * character -, and the letter a'.
735 * For compatibility, the `-' is not considerd
736 * to define a range if the character following
737 * it is either a close bracket (required by ANSI)
738 * or is not numerically greater than the character
739 * we just stored in the table (c).
743 || (__collate_load_error ? n < c :
744 __collate_range_cmp (n, c) < 0
748 break; /* resume the for(;;) */
751 /* fill in the range */
752 if (__collate_load_error) {
757 for (i = 0; i < 256; i ++)
758 if ( __collate_range_cmp (c, i) < 0
759 && __collate_range_cmp (i, n) <= 0
763 #if 1 /* XXX another disgusting compatibility hack */
766 * Alas, the V7 Unix scanf also treats formats
767 * such as [a-c-e] as `the letters a through e'.
768 * This too is permitted by the standard....
780 case ']': /* end of scanset */
783 default: /* just another character */