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
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
41 static const char rcsid[] =
42 "$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.14.2.2 2002/04/17 14:58:23 ache Exp $";
43 #endif /* LIBC_SCCS and not lint */
58 #define FLOATING_POINT
65 #define BUF 513 /* Maximum length of numeric string. */
68 * Flags used during conversion.
70 #define LONG 0x01 /* l: long or double */
71 #define LONGDBL 0x02 /* L: long double */
72 #define SHORT 0x04 /* h: short */
73 #define SUPPRESS 0x08 /* suppress assignment */
74 #define POINTER 0x10 /* weird %p pointer (`fake hex') */
75 #define NOSKIP 0x20 /* do not skip blanks */
79 * The following are used in numeric conversions only:
80 * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
81 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
83 #define SIGNOK 0x40 /* +/- is (still) legal */
84 #define NDIGITS 0x80 /* no digits detected */
86 #define DPTOK 0x100 /* (float) decimal point is still legal */
87 #define EXPOK 0x200 /* (float) exponent (e+3, etc) still legal */
89 #define PFXOK 0x100 /* 0x prefix is (still) legal */
90 #define NZDIGITS 0x200 /* no zero digits detected */
95 #define CT_CHAR 0 /* %c conversion */
96 #define CT_CCL 1 /* %[...] conversion */
97 #define CT_STRING 2 /* %s conversion */
98 #define CT_INT 3 /* integer, i.e., strtoq or strtouq */
99 #define CT_FLOAT 4 /* floating, i.e., strtod */
101 #define u_char unsigned char
102 #define u_long unsigned long
104 static u_char *__sccl(char *, u_char *);
110 __svfscanf(FILE *fp, char const *fmt0, va_list ap)
112 u_char *fmt = (u_char *)fmt0;
113 int c; /* character from format, or conversion */
114 size_t width; /* field width, or 0 */
115 char *p; /* points into all kinds of strings */
116 int n; /* handy integer */
117 int flags; /* flags as defined above */
118 char *p0; /* saves original value of p when necessary */
119 int nassigned; /* number of fields assigned */
120 int nconversions; /* number of conversions */
121 int nread; /* number of characters consumed from fp */
122 int base; /* base argument to strtoq/strtouq */
123 u_quad_t(*ccfn)(); /* conversion function (strtoq/strtouq) */
124 char ccltab[256]; /* character class table for %[...] */
125 char buf[BUF]; /* buffer for numeric conversions */
127 /* `basefix' is used to avoid `if' tests in the integer scanner */
128 static short basefix[17] =
129 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
130 #ifdef FLOATING_POINT
131 char decimal_point = localeconv()->decimal_point[0];
137 base = 0; /* XXX just to keep gcc happy */
138 ccfn = NULL; /* XXX just to keep gcc happy */
144 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p))
145 nread++, fp->_r--, fp->_p++;
153 * switch on the format. continue if done;
154 * break once format type is derived.
160 if (fp->_r <= 0 && __srefill(fp))
184 case '0': case '1': case '2': case '3': case '4':
185 case '5': case '6': case '7': case '8': case '9':
186 width = width * 10 + c - '0';
191 * Those marked `compat' are for 4.[123]BSD compatibility.
193 * (According to ANSI, E and X formats are supposed
194 * to the same as e and x. Sorry about that.)
196 case 'D': /* compat */
201 ccfn = (u_quad_t (*)())strtoq;
207 ccfn = (u_quad_t (*)())strtoq;
211 case 'O': /* compat */
226 case 'X': /* compat XXX */
230 flags |= PFXOK; /* enable 0x prefixing */
236 #ifdef FLOATING_POINT
237 case 'E': /* compat XXX */
238 case 'F': /* compat */
241 case 'e': case 'f': case 'g':
251 fmt = __sccl(ccltab, fmt);
261 case 'p': /* pointer format is like hex */
262 flags |= POINTER | PFXOK;
270 if (flags & SUPPRESS) /* ??? */
273 *va_arg(ap, short *) = nread;
274 else if (flags & LONG)
275 *va_arg(ap, long *) = nread;
276 else if (flags & QUAD)
277 *va_arg(ap, quad_t *) = nread;
279 *va_arg(ap, int *) = nread;
283 * Disgusting backwards compatibility hacks. XXX
285 case '\0': /* compat */
288 default: /* compat */
292 ccfn = (u_quad_t (*)())strtoq;
298 * We have a conversion that requires input.
300 if (fp->_r <= 0 && __srefill(fp))
304 * Consume leading white space, except for formats
305 * that suppress this.
307 if ((flags & NOSKIP) == 0) {
308 while (isspace(*fp->_p)) {
312 else if (__srefill(fp))
316 * Note that there is at least one character in
317 * the buffer, so conversions that do not set NOSKIP
318 * ca no longer result in an input failure.
328 /* scan arbitrary characters (sets NOSKIP) */
331 if (flags & SUPPRESS) {
334 if ((n = fp->_r) < width) {
352 size_t r = fread((void *)va_arg(ap, char *), 1,
364 /* scan a (nonempty) character class (sets NOSKIP) */
366 width = (size_t)~0; /* `infinity' */
367 /* take only those things in the class */
368 if (flags & SUPPRESS) {
370 while (ccltab[*fp->_p]) {
371 n++, fp->_r--, fp->_p++;
374 if (fp->_r <= 0 && __srefill(fp)) {
383 p0 = p = va_arg(ap, char *);
384 while (ccltab[*fp->_p]) {
389 if (fp->_r <= 0 && __srefill(fp)) {
406 /* like CCL, but zero-length string OK, & no NOSKIP */
409 if (flags & SUPPRESS) {
411 while (!isspace(*fp->_p)) {
412 n++, fp->_r--, fp->_p++;
415 if (fp->_r <= 0 && __srefill(fp))
420 p0 = p = va_arg(ap, char *);
421 while (!isspace(*fp->_p)) {
426 if (fp->_r <= 0 && __srefill(fp))
437 /* scan an integer as if by strtoq/strtouq */
439 if (width == 0 || width > sizeof(buf) - 1)
440 width = sizeof(buf) - 1;
442 /* size_t is unsigned, hence this optimisation */
443 if (--width > sizeof(buf) - 2)
444 width = sizeof(buf) - 2;
447 flags |= SIGNOK | NDIGITS | NZDIGITS;
448 for (p = buf; width; width--) {
451 * Switch on the character; `goto ok'
452 * if we accept it as a part of number.
457 * The digit 0 is always legal, but is
458 * special. For %i conversions, if no
459 * digits (zero or nonzero) have been
460 * scanned (only signs), we will have
461 * base==0. In that case, we should set
462 * it to 8 and enable 0x prefixing.
463 * Also, if we have not scanned zero digits
464 * before this, do not turn off prefixing
465 * (someone else will turn it off if we
466 * have scanned any nonzero digits).
473 if (flags & NZDIGITS)
474 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
476 flags &= ~(SIGNOK|PFXOK|NDIGITS);
479 /* 1 through 7 always legal */
480 case '1': case '2': case '3':
481 case '4': case '5': case '6': case '7':
482 base = basefix[base];
483 flags &= ~(SIGNOK | PFXOK | NDIGITS);
486 /* digits 8 and 9 ok iff decimal or hex */
488 base = basefix[base];
490 break; /* not legal here */
491 flags &= ~(SIGNOK | PFXOK | NDIGITS);
494 /* letters ok iff hex */
495 case 'A': case 'B': case 'C':
496 case 'D': case 'E': case 'F':
497 case 'a': case 'b': case 'c':
498 case 'd': case 'e': case 'f':
499 /* no need to fix base here */
501 break; /* not legal here */
502 flags &= ~(SIGNOK | PFXOK | NDIGITS);
505 /* sign ok only as first character */
507 if (flags & SIGNOK) {
513 /* x ok iff flag still set & 2nd char */
515 if (flags & PFXOK && p == buf + 1) {
516 base = 16; /* if %i */
524 * If we got here, c is not a legal character
525 * for a number. Stop accumulating digits.
530 * c is legal: store it and look at the next.
535 else if (__srefill(fp))
539 * If we had only a sign, it is no good; push
540 * back the sign. If the number ends in `x',
541 * it was [sign] '0' 'x', so push back the x
542 * and treat it as [sign] '0'.
544 if (flags & NDIGITS) {
546 (void) ungetc(*(u_char *)--p, fp);
549 c = ((u_char *)p)[-1];
550 if (c == 'x' || c == 'X') {
552 (void) ungetc(c, fp);
554 if ((flags & SUPPRESS) == 0) {
558 res = (*ccfn)(buf, (char **)NULL, base);
560 *va_arg(ap, void **) =
562 else if (flags & SHORT)
563 *va_arg(ap, short *) = res;
564 else if (flags & LONG)
565 *va_arg(ap, long *) = res;
566 else if (flags & QUAD)
567 *va_arg(ap, quad_t *) = res;
569 *va_arg(ap, int *) = res;
576 #ifdef FLOATING_POINT
578 /* scan a floating point number as if by strtod */
580 if (width == 0 || width > sizeof(buf) - 1)
581 width = sizeof(buf) - 1;
583 /* size_t is unsigned, hence this optimisation */
584 if (--width > sizeof(buf) - 2)
585 width = sizeof(buf) - 2;
588 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
589 for (p = buf; width; width--) {
592 * This code mimicks the integer conversion
593 * code, but is much simpler.
597 case '0': case '1': case '2': case '3':
598 case '4': case '5': case '6': case '7':
600 flags &= ~(SIGNOK | NDIGITS);
604 if (flags & SIGNOK) {
610 /* no exponent without some digits */
611 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
613 (flags & ~(EXPOK|DPTOK)) |
619 if ((char)c == decimal_point &&
621 flags &= ~(SIGNOK | DPTOK);
631 else if (__srefill(fp))
635 * If no digits, might be missing exponent digits
636 * (just give back the exponent) or might be missing
637 * regular digits, but had sign and/or decimal point.
639 if (flags & NDIGITS) {
641 /* no digits at all */
643 ungetc(*(u_char *)--p, fp);
646 /* just a bad exponent (e and maybe sign) */
648 if (c != 'e' && c != 'E') {
649 (void) ungetc(c, fp);/* sign */
652 (void) ungetc(c, fp);
654 if ((flags & SUPPRESS) == 0) {
658 /* XXX this loses precision for long doubles. */
659 res = strtod(buf, (char **) NULL);
661 *va_arg(ap, long double *) = res;
662 else if (flags & LONG)
663 *va_arg(ap, double *) = res;
665 *va_arg(ap, float *) = res;
671 #endif /* FLOATING_POINT */
675 return (nconversions != 0 ? nassigned : EOF);
681 * Fill in the given table from the scanset at the given format
682 * (just after `['). Return a pointer to the character past the
683 * closing `]'. The table has a 1 wherever characters should be
684 * considered part of the scanset.
693 /* first `clear' the whole table */
694 c = *fmt++; /* first char hat => negated scanset */
696 v = 1; /* default => accept */
697 c = *fmt++; /* get new first char */
699 v = 0; /* default => reject */
701 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
702 (void) memset(tab, v, 256);
705 return (fmt - 1);/* format ended before closing ] */
708 * Now set the entries corresponding to the actual scanset
709 * to the opposite of the above.
711 * The first character may be ']' (or '-') without being special;
712 * the last character may be '-'.
716 tab[c] = v; /* take character c */
718 n = *fmt++; /* and examine the next */
721 case 0: /* format ended too soon */
726 * A scanset of the form
728 * is defined as `the digit 0, the digit 1,
729 * the character +, the character -', but
730 * the effect of a scanset such as
732 * is implementation defined. The V7 Unix
733 * scanf treats `a-z' as `the letters a through
734 * z', but treats `a-a' as `the letter a, the
735 * character -, and the letter a'.
737 * For compatibility, the `-' is not considerd
738 * to define a range if the character following
739 * it is either a close bracket (required by ANSI)
740 * or is not numerically greater than the character
741 * we just stored in the table (c).
745 || (__collate_load_error ? n < c :
746 __collate_range_cmp (n, c) < 0
750 break; /* resume the for(;;) */
753 /* fill in the range */
754 if (__collate_load_error) {
759 for (i = 0; i < 256; i ++)
760 if ( __collate_range_cmp (c, i) < 0
761 && __collate_range_cmp (i, n) <= 0
765 #if 1 /* XXX another disgusting compatibility hack */
768 * Alas, the V7 Unix scanf also treats formats
769 * such as [a-c-e] as `the letters a through e'.
770 * This too is permitted by the standard....
782 case ']': /* end of scanset */
785 default: /* just another character */