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: /repoman/r/ncvs/src/lib/libc/stdio/vfscanf.c,v 1.35 2004/01/31 23:16:09 das Exp $
38 * $DragonFly: src/lib/libc/stdio/vfscanf.c,v 1.7 2005/04/21 16:36:35 joerg Exp $
41 #include "namespace.h"
42 #include <sys/types.h>
48 #include "un-namespace.h"
51 #include "libc_private.h"
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 *);
104 * __vfscanf MT-safe version
107 __vfscanf(FILE *fp, char const *fmt0, va_list ap)
112 ret = __svfscanf(fp, fmt0, ap);
118 * __svfscanf - non-MT-safe version of __vfscanf
121 __svfscanf(FILE *fp, char const *fmt0, va_list ap)
123 u_char *fmt = (u_char *)fmt0;
124 int c; /* character from format, or conversion */
125 size_t width; /* field width, or 0 */
126 char *p; /* points into all kinds of strings */
127 int n; /* handy integer */
128 int flags; /* flags as defined above */
129 char *p0; /* saves original value of p when necessary */
130 int nassigned; /* number of fields assigned */
131 int nconversions; /* number of conversions */
132 int nread; /* number of characters consumed from fp */
133 int base; /* base argument to strtoq/strtouq */
134 u_quad_t(*ccfn)(); /* conversion function (strtoq/strtouq) */
135 char ccltab[256]; /* character class table for %[...] */
136 char buf[BUF]; /* buffer for numeric conversions */
138 /* `basefix' is used to avoid `if' tests in the integer scanner */
139 static short basefix[17] =
140 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
141 #ifdef FLOATING_POINT
142 char decimal_point = localeconv()->decimal_point[0];
148 base = 0; /* XXX just to keep gcc happy */
149 ccfn = NULL; /* XXX just to keep gcc happy */
155 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p))
156 nread++, fp->_r--, fp->_p++;
164 * switch on the format. continue if done;
165 * break once format type is derived.
171 if (fp->_r <= 0 && __srefill(fp))
195 case '0': case '1': case '2': case '3': case '4':
196 case '5': case '6': case '7': case '8': case '9':
197 width = width * 10 + c - '0';
202 * Those marked `compat' are for 4.[123]BSD compatibility.
204 * (According to ANSI, E and X formats are supposed
205 * to the same as e and x. Sorry about that.)
207 case 'D': /* compat */
212 ccfn = (u_quad_t (*)())strtoq;
218 ccfn = (u_quad_t (*)())strtoq;
222 case 'O': /* compat */
237 case 'X': /* compat XXX */
241 flags |= PFXOK; /* enable 0x prefixing */
247 #ifdef FLOATING_POINT
248 case 'E': /* compat XXX */
249 case 'F': /* compat */
252 case 'e': case 'f': case 'g':
262 fmt = __sccl(ccltab, fmt);
272 case 'p': /* pointer format is like hex */
273 flags |= POINTER | PFXOK;
281 if (flags & SUPPRESS) /* ??? */
284 *va_arg(ap, short *) = nread;
285 else if (flags & LONG)
286 *va_arg(ap, long *) = nread;
287 else if (flags & QUAD)
288 *va_arg(ap, quad_t *) = nread;
290 *va_arg(ap, int *) = nread;
294 * Disgusting backwards compatibility hacks. XXX
296 case '\0': /* compat */
299 default: /* compat */
303 ccfn = (u_quad_t (*)())strtoq;
309 * We have a conversion that requires input.
311 if (fp->_r <= 0 && __srefill(fp))
315 * Consume leading white space, except for formats
316 * that suppress this.
318 if ((flags & NOSKIP) == 0) {
319 while (isspace(*fp->_p)) {
323 else if (__srefill(fp))
327 * Note that there is at least one character in
328 * the buffer, so conversions that do not set NOSKIP
329 * ca no longer result in an input failure.
339 /* scan arbitrary characters (sets NOSKIP) */
342 if (flags & SUPPRESS) {
345 if ((n = fp->_r) < width) {
363 size_t r = fread((void *)va_arg(ap, char *), 1,
375 /* scan a (nonempty) character class (sets NOSKIP) */
377 width = (size_t)~0; /* `infinity' */
378 /* take only those things in the class */
379 if (flags & SUPPRESS) {
381 while (ccltab[*fp->_p]) {
382 n++, fp->_r--, fp->_p++;
385 if (fp->_r <= 0 && __srefill(fp)) {
394 p0 = p = va_arg(ap, char *);
395 while (ccltab[*fp->_p]) {
400 if (fp->_r <= 0 && __srefill(fp)) {
417 /* like CCL, but zero-length string OK, & no NOSKIP */
420 if (flags & SUPPRESS) {
422 while (!isspace(*fp->_p)) {
423 n++, fp->_r--, fp->_p++;
426 if (fp->_r <= 0 && __srefill(fp))
431 p0 = p = va_arg(ap, char *);
432 while (!isspace(*fp->_p)) {
437 if (fp->_r <= 0 && __srefill(fp))
448 /* scan an integer as if by strtoq/strtouq */
450 if (width == 0 || width > sizeof(buf) - 1)
451 width = sizeof(buf) - 1;
453 /* size_t is unsigned, hence this optimisation */
454 if (--width > sizeof(buf) - 2)
455 width = sizeof(buf) - 2;
458 flags |= SIGNOK | NDIGITS | NZDIGITS;
459 for (p = buf; width; width--) {
462 * Switch on the character; `goto ok'
463 * if we accept it as a part of number.
468 * The digit 0 is always legal, but is
469 * special. For %i conversions, if no
470 * digits (zero or nonzero) have been
471 * scanned (only signs), we will have
472 * base==0. In that case, we should set
473 * it to 8 and enable 0x prefixing.
474 * Also, if we have not scanned zero digits
475 * before this, do not turn off prefixing
476 * (someone else will turn it off if we
477 * have scanned any nonzero digits).
484 if (flags & NZDIGITS)
485 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
487 flags &= ~(SIGNOK|PFXOK|NDIGITS);
490 /* 1 through 7 always legal */
491 case '1': case '2': case '3':
492 case '4': case '5': case '6': case '7':
493 base = basefix[base];
494 flags &= ~(SIGNOK | PFXOK | NDIGITS);
497 /* digits 8 and 9 ok iff decimal or hex */
499 base = basefix[base];
501 break; /* not legal here */
502 flags &= ~(SIGNOK | PFXOK | NDIGITS);
505 /* letters ok iff hex */
506 case 'A': case 'B': case 'C':
507 case 'D': case 'E': case 'F':
508 case 'a': case 'b': case 'c':
509 case 'd': case 'e': case 'f':
510 /* no need to fix base here */
512 break; /* not legal here */
513 flags &= ~(SIGNOK | PFXOK | NDIGITS);
516 /* sign ok only as first character */
518 if (flags & SIGNOK) {
526 * x ok iff flag still set & 2nd char (or
527 * 3rd char if we have a sign).
530 if (flags & PFXOK && p ==
531 buf + 1 + !!(flags & HAVESIGN)) {
532 base = 16; /* if %i */
540 * If we got here, c is not a legal character
541 * for a number. Stop accumulating digits.
546 * c is legal: store it and look at the next.
551 else if (__srefill(fp))
555 * If we had only a sign, it is no good; push
556 * back the sign. If the number ends in `x',
557 * it was [sign] '0' 'x', so push back the x
558 * and treat it as [sign] '0'.
560 if (flags & NDIGITS) {
562 (void)__ungetc(*(u_char *)--p, fp);
565 c = ((u_char *)p)[-1];
566 if (c == 'x' || c == 'X') {
568 (void)__ungetc(c, fp);
570 if ((flags & SUPPRESS) == 0) {
574 res = (*ccfn)(buf, (char **)NULL, base);
576 *va_arg(ap, void **) =
578 else if (flags & SHORT)
579 *va_arg(ap, short *) = res;
580 else if (flags & LONG)
581 *va_arg(ap, long *) = res;
582 else if (flags & QUAD)
583 *va_arg(ap, quad_t *) = res;
585 *va_arg(ap, int *) = res;
592 #ifdef FLOATING_POINT
594 /* scan a floating point number as if by strtod */
596 if (width == 0 || width > sizeof(buf) - 1)
597 width = sizeof(buf) - 1;
599 /* size_t is unsigned, hence this optimisation */
600 if (--width > sizeof(buf) - 2)
601 width = sizeof(buf) - 2;
604 flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
605 for (p = buf; width; width--) {
608 * This code mimicks the integer conversion
609 * code, but is much simpler.
613 case '0': case '1': case '2': case '3':
614 case '4': case '5': case '6': case '7':
616 flags &= ~(SIGNOK | NDIGITS);
620 if (flags & SIGNOK) {
626 /* no exponent without some digits */
627 if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
629 (flags & ~(EXPOK|DPTOK)) |
635 if ((char)c == decimal_point &&
637 flags &= ~(SIGNOK | DPTOK);
647 else if (__srefill(fp))
651 * If no digits, might be missing exponent digits
652 * (just give back the exponent) or might be missing
653 * regular digits, but had sign and/or decimal point.
655 if (flags & NDIGITS) {
657 /* no digits at all */
659 __ungetc(*(u_char *)--p, fp);
662 /* just a bad exponent (e and maybe sign) */
664 if (c != 'e' && c != 'E') {
665 (void)__ungetc(c, fp);/* sign */
668 (void)__ungetc(c, fp);
670 if ((flags & SUPPRESS) == 0) {
674 /* XXX this loses precision for long doubles. */
675 res = strtod(buf, (char **) NULL);
677 *va_arg(ap, long double *) = res;
678 else if (flags & LONG)
679 *va_arg(ap, double *) = res;
681 *va_arg(ap, float *) = res;
687 #endif /* FLOATING_POINT */
691 return (nconversions != 0 ? nassigned : EOF);
697 * Fill in the given table from the scanset at the given format
698 * (just after `['). Return a pointer to the character past the
699 * closing `]'. The table has a 1 wherever characters should be
700 * considered part of the scanset.
709 /* first `clear' the whole table */
710 c = *fmt++; /* first char hat => negated scanset */
712 v = 1; /* default => accept */
713 c = *fmt++; /* get new first char */
715 v = 0; /* default => reject */
717 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
718 (void) memset(tab, v, 256);
721 return (fmt - 1);/* format ended before closing ] */
724 * Now set the entries corresponding to the actual scanset
725 * to the opposite of the above.
727 * The first character may be ']' (or '-') without being special;
728 * the last character may be '-'.
732 tab[c] = v; /* take character c */
734 n = *fmt++; /* and examine the next */
737 case 0: /* format ended too soon */
742 * A scanset of the form
744 * is defined as `the digit 0, the digit 1,
745 * the character +, the character -', but
746 * the effect of a scanset such as
748 * is implementation defined. The V7 Unix
749 * scanf treats `a-z' as `the letters a through
750 * z', but treats `a-a' as `the letter a, the
751 * character -, and the letter a'.
753 * For compatibility, the `-' is not considerd
754 * to define a range if the character following
755 * it is either a close bracket (required by ANSI)
756 * or is not numerically greater than the character
757 * we just stored in the table (c).
761 || (__collate_load_error ? n < c :
762 __collate_range_cmp (n, c) < 0
766 break; /* resume the for(;;) */
769 /* fill in the range */
770 if (__collate_load_error) {
775 for (i = 0; i < 256; i ++)
776 if ( __collate_range_cmp (c, i) < 0
777 && __collate_range_cmp (i, n) <= 0
781 #if 1 /* XXX another disgusting compatibility hack */
784 * Alas, the V7 Unix scanf also treats formats
785 * such as [a-c-e] as `the letters a through e'.
786 * This too is permitted by the standard....
798 case ']': /* end of scanset */
801 default: /* just another character */