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.11 2006/02/18 17:55:52 joerg Exp $
41 #include "namespace.h"
51 #include "un-namespace.h"
54 #include "libc_private.h"
56 #include "priv_stdio.h"
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 /* p: void * (as hex) */
71 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
72 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
73 #define INTMAXT 0x800 /* j: intmax_t */
74 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
75 #define SIZET 0x2000 /* z: size_t */
76 #define SHORTSHORT 0x4000 /* hh: char */
77 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
80 * The following are used in integral conversions only:
81 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
83 #define SIGNOK 0x40 /* +/- is (still) legal */
84 #define NDIGITS 0x80 /* no digits detected */
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 /* %[dioupxX] conversion */
96 #define CT_FLOAT 4 /* %[efgEFG] conversion */
98 static const u_char *__sccl(char *, const u_char *);
99 static int parsefloat(FILE *, char *, char *);
101 int __scanfdebug = 0;
103 __weak_reference(__vfscanf, vfscanf);
106 * __vfscanf - MT-safe version
109 __vfscanf(FILE *fp, const char *fmt0, va_list ap)
114 ret = __svfscanf(fp, fmt0, ap);
120 * __svfscanf - non-MT-safe version of __vfscanf
123 __svfscanf(FILE *fp, const char *fmt0, va_list ap)
125 const u_char *fmt = (const u_char *)fmt0;
126 int c; /* character from format, or conversion */
127 size_t width; /* field width, or 0 */
128 char *p; /* points into all kinds of strings */
129 int n; /* handy integer */
130 int flags; /* flags as defined above */
131 char *p0; /* saves original value of p when necessary */
132 int nassigned; /* number of fields assigned */
133 int nconversions; /* number of conversions */
134 int nread; /* number of characters consumed from fp */
135 int base; /* base argument to conversion function */
136 char ccltab[256]; /* character class table for %[...] */
137 char buf[BUF]; /* buffer for numeric and mb conversions */
138 wchar_t *wcp; /* handy wide character pointer */
139 size_t nconv; /* length of multibyte sequence converted */
140 static const mbstate_t initial;
143 /* `basefix' is used to avoid `if' tests in the integer scanner */
144 static short basefix[17] =
145 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
147 _SET_ORIENTATION(fp, -1);
157 while ((fp->pub._r > 0 || __srefill(fp) == 0) && isspace(*fp->pub._p))
158 nread++, fp->pub._r--, fp->pub._p++;
166 * switch on the format. continue if done;
167 * break once format type is derived.
173 if (fp->pub._r <= 0 && __srefill(fp))
175 if (*fp->pub._p != c)
177 fp->pub._r--, fp->pub._p++;
196 flags |= LONGLONG; /* assume long long == quad */
216 case '0': case '1': case '2': case '3': case '4':
217 case '5': case '6': case '7': case '8': case '9':
218 width = width * 10 + c - '0';
248 flags |= PFXOK; /* enable 0x prefixing */
254 #ifndef NO_FLOATING_POINT
255 case 'A': case 'E': case 'F': case 'G':
256 case 'a': case 'e': case 'f': case 'g':
269 fmt = __sccl(ccltab, fmt);
282 case 'p': /* pointer format is like hex */
283 flags |= POINTER | PFXOK;
284 c = CT_INT; /* assumes sizeof(uintmax_t) */
285 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
291 if (flags & SUPPRESS) /* ??? */
293 if (flags & SHORTSHORT)
294 *va_arg(ap, char *) = nread;
295 else if (flags & SHORT)
296 *va_arg(ap, short *) = nread;
297 else if (flags & LONG)
298 *va_arg(ap, long *) = nread;
299 else if (flags & LONGLONG)
300 *va_arg(ap, long long *) = nread;
301 else if (flags & INTMAXT)
302 *va_arg(ap, intmax_t *) = nread;
303 else if (flags & SIZET)
304 *va_arg(ap, size_t *) = nread;
305 else if (flags & PTRDIFFT)
306 *va_arg(ap, ptrdiff_t *) = nread;
308 *va_arg(ap, int *) = nread;
314 * Disgusting backwards compatibility hack. XXX
316 case '\0': /* compat */
321 * We have a conversion that requires input.
323 if (fp->pub._r <= 0 && __srefill(fp))
327 * Consume leading white space, except for formats
328 * that suppress this.
330 if ((flags & NOSKIP) == 0) {
331 while (isspace(*fp->pub._p)) {
333 if (--fp->pub._r > 0)
335 else if (__srefill(fp))
339 * Note that there is at least one character in
340 * the buffer, so conversions that do not set NOSKIP
341 * ca no longer result in an input failure.
351 /* scan arbitrary characters (sets NOSKIP) */
355 if ((flags & SUPPRESS) == 0)
356 wcp = va_arg(ap, wchar_t *);
361 if (n == MB_CUR_MAX) {
362 fp->pub._flags |= __SERR;
365 buf[n++] = *fp->pub._p;
369 nconv = mbrtowc(wcp, buf, n, &mbs);
370 if (nconv == (size_t)-1) {
371 fp->pub._flags |= __SERR;
374 if (nconv == 0 && !(flags & SUPPRESS))
376 if (nconv != (size_t)-2) {
379 if (!(flags & SUPPRESS))
383 if (fp->pub._r <= 0 && __srefill(fp)) {
385 fp->pub._flags |= __SERR;
391 if ((flags & SUPPRESS) == 0)
393 } else if (flags & SUPPRESS) {
396 if ((n = fp->pub._r) < width) {
414 size_t r = __fread((void *)va_arg(ap, char *),
426 /* scan a (nonempty) character class (sets NOSKIP) */
428 width = (size_t)~0; /* `infinity' */
429 /* take only those things in the class */
434 if ((flags & SUPPRESS) == 0)
435 wcp = va_arg(ap, wchar_t *);
441 if (n == MB_CUR_MAX) {
442 fp->pub._flags |= __SERR;
445 buf[n++] = *fp->pub._p;
449 nconv = mbrtowc(wcp, buf, n, &mbs);
450 if (nconv == (size_t)-1) {
451 fp->pub._flags |= __SERR;
456 if (nconv != (size_t)-2) {
457 if (wctob(*wcp) != EOF &&
458 !ccltab[wctob(*wcp)]) {
468 if (!(flags & SUPPRESS))
473 if (fp->pub._r <= 0 && __srefill(fp)) {
475 fp->pub._flags |= __SERR;
482 fp->pub._flags |= __SERR;
488 if (!(flags & SUPPRESS)) {
492 } else if (flags & SUPPRESS) {
494 while (ccltab[*fp->pub._p]) {
495 n++, fp->pub._r--, fp->pub._p++;
498 if (fp->pub._r <= 0 && __srefill(fp)) {
507 p0 = p = va_arg(ap, char *);
508 while (ccltab[*fp->pub._p]) {
510 *p++ = *fp->pub._p++;
513 if (fp->pub._r <= 0 && __srefill(fp)) {
530 /* like CCL, but zero-length string OK, & no NOSKIP */
536 if ((flags & SUPPRESS) == 0)
537 wcp = va_arg(ap, wchar_t *);
541 while (!isspace(*fp->pub._p) && width != 0) {
542 if (n == MB_CUR_MAX) {
543 fp->pub._flags |= __SERR;
546 buf[n++] = *fp->pub._p;
550 nconv = mbrtowc(wcp, buf, n, &mbs);
551 if (nconv == (size_t)-1) {
552 fp->pub._flags |= __SERR;
557 if (nconv != (size_t)-2) {
558 if (iswspace(*wcp)) {
568 if (!(flags & SUPPRESS))
572 if (fp->pub._r <= 0 && __srefill(fp)) {
574 fp->pub._flags |= __SERR;
580 if (!(flags & SUPPRESS)) {
584 } else if (flags & SUPPRESS) {
586 while (!isspace(*fp->pub._p)) {
587 n++, fp->pub._r--, fp->pub._p++;
590 if (fp->pub._r <= 0 && __srefill(fp))
595 p0 = p = va_arg(ap, char *);
596 while (!isspace(*fp->pub._p)) {
598 *p++ = *fp->pub._p++;
601 if (fp->pub._r <= 0 && __srefill(fp))
612 /* scan an integer as if by the conversion function */
614 if (width == 0 || width > sizeof(buf) - 1)
615 width = sizeof(buf) - 1;
617 /* size_t is unsigned, hence this optimisation */
618 if (--width > sizeof(buf) - 2)
619 width = sizeof(buf) - 2;
622 flags |= SIGNOK | NDIGITS | NZDIGITS;
623 for (p = buf; width; width--) {
626 * Switch on the character; `goto ok'
627 * if we accept it as a part of number.
632 * The digit 0 is always legal, but is
633 * special. For %i conversions, if no
634 * digits (zero or nonzero) have been
635 * scanned (only signs), we will have
636 * base==0. In that case, we should set
637 * it to 8 and enable 0x prefixing.
638 * Also, if we have not scanned zero digits
639 * before this, do not turn off prefixing
640 * (someone else will turn it off if we
641 * have scanned any nonzero digits).
648 if (flags & NZDIGITS)
649 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
651 flags &= ~(SIGNOK|PFXOK|NDIGITS);
654 /* 1 through 7 always legal */
655 case '1': case '2': case '3':
656 case '4': case '5': case '6': case '7':
657 base = basefix[base];
658 flags &= ~(SIGNOK | PFXOK | NDIGITS);
661 /* digits 8 and 9 ok iff decimal or hex */
663 base = basefix[base];
665 break; /* not legal here */
666 flags &= ~(SIGNOK | PFXOK | NDIGITS);
669 /* letters ok iff hex */
670 case 'A': case 'B': case 'C':
671 case 'D': case 'E': case 'F':
672 case 'a': case 'b': case 'c':
673 case 'd': case 'e': case 'f':
674 /* no need to fix base here */
676 break; /* not legal here */
677 flags &= ~(SIGNOK | PFXOK | NDIGITS);
680 /* sign ok only as first character */
682 if (flags & SIGNOK) {
690 * x ok iff flag still set & 2nd char (or
691 * 3rd char if we have a sign).
694 if (flags & PFXOK && p ==
695 buf + 1 + !!(flags & HAVESIGN)) {
696 base = 16; /* if %i */
704 * If we got here, c is not a legal character
705 * for a number. Stop accumulating digits.
710 * c is legal: store it and look at the next.
713 if (--fp->pub._r > 0)
715 else if (__srefill(fp))
719 * If we had only a sign, it is no good; push
720 * back the sign. If the number ends in `x',
721 * it was [sign] '0' 'x', so push back the x
722 * and treat it as [sign] '0'.
724 if (flags & NDIGITS) {
726 __ungetc(*(u_char *)--p, fp);
729 c = ((u_char *)p)[-1];
730 if (c == 'x' || c == 'X') {
734 if ((flags & SUPPRESS) == 0) {
738 if ((flags & UNSIGNED) == 0)
739 res = strtoimax(buf, (char **)NULL, base);
741 res = strtoumax(buf, (char **)NULL, base);
743 *va_arg(ap, void **) =
744 (void *)(uintptr_t)res;
745 else if (flags & SHORTSHORT)
746 *va_arg(ap, char *) = res;
747 else if (flags & SHORT)
748 *va_arg(ap, short *) = res;
749 else if (flags & LONG)
750 *va_arg(ap, long *) = res;
751 else if (flags & LONGLONG)
752 *va_arg(ap, long long *) = res;
753 else if (flags & INTMAXT)
754 *va_arg(ap, intmax_t *) = res;
755 else if (flags & PTRDIFFT)
756 *va_arg(ap, ptrdiff_t *) = res;
757 else if (flags & SIZET)
758 *va_arg(ap, size_t *) = res;
760 *va_arg(ap, int *) = res;
767 #ifndef NO_FLOATING_POINT
769 /* scan a floating point number as if by strtod */
770 if (width == 0 || width > sizeof(buf) - 1)
771 width = sizeof(buf) - 1;
772 if ((width = parsefloat(fp, buf, buf + width)) == 0)
774 if ((flags & SUPPRESS) == 0) {
775 if (flags & LONGDBL) {
776 #if 0 /* XXX no strtold (yet) */
777 long double res = strtold(buf, &p);
778 *va_arg(ap, long double *) = res;
780 double res = strtod(buf, &p);
781 *va_arg(ap, long double *) = res;
783 } else if (flags & LONG) {
784 double res = strtod(buf, &p);
785 *va_arg(ap, double *) = res;
787 #if 0 /* XXX no strtof (yet) */
788 float res = strtof(buf, &p);
789 *va_arg(ap, float *) = res;
791 float res = strtod(buf, &p);
792 *va_arg(ap, float *) = res;
795 if (__scanfdebug && p - buf != width)
802 #endif /* !NO_FLOATING_POINT */
806 return (nconversions != 0 ? nassigned : EOF);
812 * Fill in the given table from the scanset at the given format
813 * (just after `['). Return a pointer to the character past the
814 * closing `]'. The table has a 1 wherever characters should be
815 * considered part of the scanset.
817 static const u_char *
818 __sccl(char *tab, const u_char *fmt)
822 /* first `clear' the whole table */
823 c = *fmt++; /* first char hat => negated scanset */
825 v = 1; /* default => accept */
826 c = *fmt++; /* get new first char */
828 v = 0; /* default => reject */
830 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
834 return (fmt - 1);/* format ended before closing ] */
837 * Now set the entries corresponding to the actual scanset
838 * to the opposite of the above.
840 * The first character may be ']' (or '-') without being special;
841 * the last character may be '-'.
845 tab[c] = v; /* take character c */
847 n = *fmt++; /* and examine the next */
850 case 0: /* format ended too soon */
855 * A scanset of the form
857 * is defined as `the digit 0, the digit 1,
858 * the character +, the character -', but
859 * the effect of a scanset such as
861 * is implementation defined. The V7 Unix
862 * scanf treats `a-z' as `the letters a through
863 * z', but treats `a-a' as `the letter a, the
864 * character -, and the letter a'.
866 * For compatibility, the `-' is not considerd
867 * to define a range if the character following
868 * it is either a close bracket (required by ANSI)
869 * or is not numerically greater than the character
870 * we just stored in the table (c).
874 || (__collate_load_error ? n < c :
875 __collate_range_cmp (n, c) < 0
879 break; /* resume the for(;;) */
882 /* fill in the range */
883 if (__collate_load_error) {
888 for (i = 0; i < 256; i ++)
889 if ( __collate_range_cmp (c, i) < 0
890 && __collate_range_cmp (i, n) <= 0
894 #if 1 /* XXX another disgusting compatibility hack */
897 * Alas, the V7 Unix scanf also treats formats
898 * such as [a-c-e] as `the letters a through e'.
899 * This too is permitted by the standard....
911 case ']': /* end of scanset */
914 default: /* just another character */
922 #ifndef NO_FLOATING_POINT
924 parsefloat(FILE *fp, char *buf, char *end)
929 S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
930 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
933 char decpt = *localeconv()->decimal_point;
934 _Bool gotmantdig = 0, ishex = 0;
937 * We set commit = p whenever the string we have read so far
938 * constitutes a valid representation of a floating point
939 * number by itself. At some point, the parse will complete
940 * or fail, and we will ungetc() back to the last commit point.
941 * To ensure that the file offset gets updated properly, it is
942 * always necessary to read at least one character that doesn't
943 * match; thus, we can't short-circuit "infinity" or "nan(...)".
946 for (p = buf; p < end; ) {
952 if (c == '-' || c == '+')
977 (c != "nfinity"[infnanpos] &&
978 c != "NFINITY"[infnanpos]))
980 if (infnanpos == 1 || infnanpos == 6)
981 commit = p; /* inf or infinity */
986 case -1: /* XXX kludge to deal with nan(...) */
989 if (c != 'A' && c != 'a')
993 if (c != 'N' && c != 'n')
1006 } else if (!isalnum(c) && c != '_')
1014 if (c == 'X' || c == 'x') {
1017 } else { /* we saw a '0', but no 'x' */
1022 if ((ishex && isxdigit(c)) || isdigit(c))
1033 if (((c == 'E' || c == 'e') && !ishex) ||
1034 ((c == 'P' || c == 'p') && ishex)) {
1039 } else if ((ishex && isxdigit(c)) || isdigit(c)) {
1046 state = S_EXPDIGITS;
1047 if (c == '-' || c == '+')
1061 if (--fp->pub._r > 0)
1063 else if (__srefill(fp))
1068 while (commit < --p)
1069 __ungetc(*(u_char *)p, fp);
1071 return (commit - buf);