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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "namespace.h"
49 #include "un-namespace.h"
52 #include "libc_private.h"
55 #ifndef NO_FLOATING_POINT
59 #define BUF 513 /* Maximum length of numeric string. */
62 * Flags used during conversion.
64 #define LONG 0x01 /* l: long or double */
65 #define LONGDBL 0x02 /* L: long double */
66 #define SHORT 0x04 /* h: short */
67 #define SUPPRESS 0x08 /* *: suppress assignment */
68 #define POINTER 0x10 /* p: void * (as hex) */
69 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
70 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
71 #define INTMAXT 0x800 /* j: intmax_t */
72 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
73 #define SIZET 0x2000 /* z: size_t */
74 #define SHORTSHORT 0x4000 /* hh: char */
75 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
78 * The following are used in integral conversions only:
79 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
81 #define SIGNOK 0x40 /* +/- is (still) legal */
82 #define NDIGITS 0x80 /* no digits detected */
83 #define PFXOK 0x100 /* 0x prefix is (still) legal */
84 #define NZDIGITS 0x200 /* no zero digits detected */
85 #define HAVESIGN 0x10000 /* sign detected */
90 #define CT_CHAR 0 /* %c conversion */
91 #define CT_CCL 1 /* %[...] conversion */
92 #define CT_STRING 2 /* %s conversion */
93 #define CT_INT 3 /* %[dioupxX] conversion */
94 #define CT_FLOAT 4 /* %[efgEFG] conversion */
96 static const u_char *__sccl(char *, const u_char *);
97 #ifndef NO_FLOATING_POINT
98 static int parsefloat(FILE *, char *, char *);
101 int __scanfdebug = 0;
103 __weak_reference(__vfscanf, vfscanf);
106 * __vfscanf - MT-safe version
109 __vfscanf(FILE *fp, char const *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 };
157 while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p))
158 nread++, fp->_r--, fp->_p++;
166 * switch on the format. continue if done;
167 * break once format type is derived.
173 if (fp->_r <= 0 && __srefill(fp))
195 flags |= LONGLONG; /* not quite */
214 case '0': case '1': case '2': case '3': case '4':
215 case '5': case '6': case '7': case '8': case '9':
216 width = width * 10 + c - '0';
246 flags |= PFXOK; /* enable 0x prefixing */
252 #ifndef NO_FLOATING_POINT
253 case 'A': case 'E': case 'F': case 'G':
254 case 'a': case 'e': case 'f': case 'g':
267 fmt = __sccl(ccltab, fmt);
280 case 'p': /* pointer format is like hex */
281 flags |= POINTER | PFXOK;
282 c = CT_INT; /* assumes sizeof(uintmax_t) */
283 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
289 if (flags & SUPPRESS) /* ??? */
291 if (flags & SHORTSHORT)
292 *va_arg(ap, char *) = nread;
293 else if (flags & SHORT)
294 *va_arg(ap, short *) = nread;
295 else if (flags & LONG)
296 *va_arg(ap, long *) = nread;
297 else if (flags & LONGLONG)
298 *va_arg(ap, long long *) = nread;
299 else if (flags & INTMAXT)
300 *va_arg(ap, intmax_t *) = nread;
301 else if (flags & SIZET)
302 *va_arg(ap, size_t *) = nread;
303 else if (flags & PTRDIFFT)
304 *va_arg(ap, ptrdiff_t *) = nread;
306 *va_arg(ap, int *) = nread;
313 * Disgusting backwards compatibility hack. XXX
315 case '\0': /* compat */
320 * We have a conversion that requires input.
322 if (fp->_r <= 0 && __srefill(fp))
326 * Consume leading white space, except for formats
327 * that suppress this.
329 if ((flags & NOSKIP) == 0) {
330 while (isspace(*fp->_p)) {
334 else if (__srefill(fp))
338 * Note that there is at least one character in
339 * the buffer, so conversions that do not set NOSKIP
340 * ca no longer result in an input failure.
350 /* scan arbitrary characters (sets NOSKIP) */
354 if ((flags & SUPPRESS) == 0)
355 wcp = va_arg(ap, wchar_t *);
360 if (n == MB_CUR_MAX) {
361 fp->_flags |= __SERR;
368 nconv = mbrtowc(wcp, buf, n, &mbs);
369 if (nconv == (size_t)-1) {
370 fp->_flags |= __SERR;
373 if (nconv == 0 && !(flags & SUPPRESS))
375 if (nconv != (size_t)-2) {
378 if (!(flags & SUPPRESS))
382 if (fp->_r <= 0 && __srefill(fp)) {
384 fp->_flags |= __SERR;
390 if (!(flags & SUPPRESS))
392 } else if (flags & SUPPRESS) {
395 if ((n = fp->_r) < width) {
413 size_t r = __fread((void *)va_arg(ap, char *), 1,
425 /* scan a (nonempty) character class (sets NOSKIP) */
427 width = (size_t)~0; /* `infinity' */
428 /* take only those things in the class */
433 if ((flags & SUPPRESS) == 0)
434 wcp = va_arg(ap, wchar_t *);
440 if (n == MB_CUR_MAX) {
441 fp->_flags |= __SERR;
448 nconv = mbrtowc(wcp, buf, n, &mbs);
449 if (nconv == (size_t)-1) {
450 fp->_flags |= __SERR;
455 if (nconv != (size_t)-2) {
456 if (wctob(*wcp) != EOF &&
457 !ccltab[wctob(*wcp)]) {
467 if (!(flags & SUPPRESS))
472 if (fp->_r <= 0 && __srefill(fp)) {
474 fp->_flags |= __SERR;
481 fp->_flags |= __SERR;
487 if (!(flags & SUPPRESS)) {
491 } else if (flags & SUPPRESS) {
493 while (ccltab[*fp->_p]) {
494 n++, fp->_r--, fp->_p++;
497 if (fp->_r <= 0 && __srefill(fp)) {
506 p0 = p = va_arg(ap, char *);
507 while (ccltab[*fp->_p]) {
512 if (fp->_r <= 0 && __srefill(fp)) {
529 /* like CCL, but zero-length string OK, & no NOSKIP */
535 if ((flags & SUPPRESS) == 0)
536 wcp = va_arg(ap, wchar_t *);
540 while (!isspace(*fp->_p) && width != 0) {
541 if (n == MB_CUR_MAX) {
542 fp->_flags |= __SERR;
549 nconv = mbrtowc(wcp, buf, n, &mbs);
550 if (nconv == (size_t)-1) {
551 fp->_flags |= __SERR;
556 if (nconv != (size_t)-2) {
557 if (iswspace(*wcp)) {
567 if (!(flags & SUPPRESS))
571 if (fp->_r <= 0 && __srefill(fp)) {
573 fp->_flags |= __SERR;
579 if (!(flags & SUPPRESS)) {
583 } else if (flags & SUPPRESS) {
585 while (!isspace(*fp->_p)) {
586 n++, fp->_r--, fp->_p++;
589 if (fp->_r <= 0 && __srefill(fp))
594 p0 = p = va_arg(ap, char *);
595 while (!isspace(*fp->_p)) {
600 if (fp->_r <= 0 && __srefill(fp))
611 /* scan an integer as if by the conversion function */
613 if (width == 0 || width > sizeof(buf) - 1)
614 width = sizeof(buf) - 1;
616 /* size_t is unsigned, hence this optimisation */
617 if (--width > sizeof(buf) - 2)
618 width = sizeof(buf) - 2;
621 flags |= SIGNOK | NDIGITS | NZDIGITS;
622 for (p = buf; width; width--) {
625 * Switch on the character; `goto ok'
626 * if we accept it as a part of number.
631 * The digit 0 is always legal, but is
632 * special. For %i conversions, if no
633 * digits (zero or nonzero) have been
634 * scanned (only signs), we will have
635 * base==0. In that case, we should set
636 * it to 8 and enable 0x prefixing.
637 * Also, if we have not scanned zero digits
638 * before this, do not turn off prefixing
639 * (someone else will turn it off if we
640 * have scanned any nonzero digits).
647 if (flags & NZDIGITS)
648 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
650 flags &= ~(SIGNOK|PFXOK|NDIGITS);
653 /* 1 through 7 always legal */
654 case '1': case '2': case '3':
655 case '4': case '5': case '6': case '7':
656 base = basefix[base];
657 flags &= ~(SIGNOK | PFXOK | NDIGITS);
660 /* digits 8 and 9 ok iff decimal or hex */
662 base = basefix[base];
664 break; /* not legal here */
665 flags &= ~(SIGNOK | PFXOK | NDIGITS);
668 /* letters ok iff hex */
669 case 'A': case 'B': case 'C':
670 case 'D': case 'E': case 'F':
671 case 'a': case 'b': case 'c':
672 case 'd': case 'e': case 'f':
673 /* no need to fix base here */
675 break; /* not legal here */
676 flags &= ~(SIGNOK | PFXOK | NDIGITS);
679 /* sign ok only as first character */
681 if (flags & SIGNOK) {
689 * x ok iff flag still set & 2nd char (or
690 * 3rd char if we have a sign).
693 if (flags & PFXOK && p ==
694 buf + 1 + !!(flags & HAVESIGN)) {
695 base = 16; /* if %i */
703 * If we got here, c is not a legal character
704 * for a number. Stop accumulating digits.
709 * c is legal: store it and look at the next.
714 else if (__srefill(fp))
718 * If we had only a sign, it is no good; push
719 * back the sign. If the number ends in `x',
720 * it was [sign] '0' 'x', so push back the x
721 * and treat it as [sign] '0'.
723 if (flags & NDIGITS) {
725 (void) __ungetc(*(u_char *)--p, fp);
728 c = ((u_char *)p)[-1];
729 if (c == 'x' || c == 'X') {
731 (void) __ungetc(c, fp);
733 if ((flags & SUPPRESS) == 0) {
737 if ((flags & UNSIGNED) == 0)
738 res = strtoimax(buf, (char **)NULL, base);
740 res = strtoumax(buf, (char **)NULL, base);
742 *va_arg(ap, void **) =
743 (void *)(uintptr_t)res;
744 else if (flags & SHORTSHORT)
745 *va_arg(ap, char *) = res;
746 else if (flags & SHORT)
747 *va_arg(ap, short *) = res;
748 else if (flags & LONG)
749 *va_arg(ap, long *) = res;
750 else if (flags & LONGLONG)
751 *va_arg(ap, long long *) = res;
752 else if (flags & INTMAXT)
753 *va_arg(ap, intmax_t *) = res;
754 else if (flags & PTRDIFFT)
755 *va_arg(ap, ptrdiff_t *) = res;
756 else if (flags & SIZET)
757 *va_arg(ap, size_t *) = res;
759 *va_arg(ap, int *) = res;
766 #ifndef NO_FLOATING_POINT
768 /* scan a floating point number as if by strtod */
769 if (width == 0 || width > sizeof(buf) - 1)
770 width = sizeof(buf) - 1;
771 if ((width = parsefloat(fp, buf, buf + width)) == 0)
773 if ((flags & SUPPRESS) == 0) {
774 if (flags & LONGDBL) {
775 long double res = strtold(buf, &p);
776 *va_arg(ap, long double *) = res;
777 } else if (flags & LONG) {
778 double res = strtod(buf, &p);
779 *va_arg(ap, double *) = res;
781 float res = strtof(buf, &p);
782 *va_arg(ap, float *) = res;
784 if (__scanfdebug && p - buf != width)
791 #endif /* !NO_FLOATING_POINT */
795 return (nconversions != 0 ? nassigned : EOF);
801 * Fill in the given table from the scanset at the given format
802 * (just after `['). Return a pointer to the character past the
803 * closing `]'. The table has a 1 wherever characters should be
804 * considered part of the scanset.
806 static const u_char *
813 /* first `clear' the whole table */
814 c = *fmt++; /* first char hat => negated scanset */
816 v = 1; /* default => accept */
817 c = *fmt++; /* get new first char */
819 v = 0; /* default => reject */
821 /* XXX: Will not work if sizeof(tab*) > sizeof(char) */
822 (void) memset(tab, v, 256);
825 return (fmt - 1);/* format ended before closing ] */
828 * Now set the entries corresponding to the actual scanset
829 * to the opposite of the above.
831 * The first character may be ']' (or '-') without being special;
832 * the last character may be '-'.
836 tab[c] = v; /* take character c */
838 n = *fmt++; /* and examine the next */
841 case 0: /* format ended too soon */
846 * A scanset of the form
848 * is defined as `the digit 0, the digit 1,
849 * the character +, the character -', but
850 * the effect of a scanset such as
852 * is implementation defined. The V7 Unix
853 * scanf treats `a-z' as `the letters a through
854 * z', but treats `a-a' as `the letter a, the
855 * character -, and the letter a'.
857 * For compatibility, the `-' is not considerd
858 * to define a range if the character following
859 * it is either a close bracket (required by ANSI)
860 * or is not numerically greater than the character
861 * we just stored in the table (c).
865 || (__collate_load_error ? n < c :
866 __collate_range_cmp (n, c) < 0
870 break; /* resume the for(;;) */
873 /* fill in the range */
874 if (__collate_load_error) {
879 for (i = 0; i < 256; i ++)
880 if ( __collate_range_cmp (c, i) < 0
881 && __collate_range_cmp (i, n) <= 0
885 #if 1 /* XXX another disgusting compatibility hack */
888 * Alas, the V7 Unix scanf also treats formats
889 * such as [a-c-e] as `the letters a through e'.
890 * This too is permitted by the standard....
902 case ']': /* end of scanset */
905 default: /* just another character */
913 #ifndef NO_FLOATING_POINT
915 parsefloat(FILE *fp, char *buf, char *end)
920 S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
921 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
924 char decpt = *localeconv()->decimal_point;
925 _Bool gotmantdig = 0, ishex = 0;
928 * We set commit = p whenever the string we have read so far
929 * constitutes a valid representation of a floating point
930 * number by itself. At some point, the parse will complete
931 * or fail, and we will ungetc() back to the last commit point.
932 * To ensure that the file offset gets updated properly, it is
933 * always necessary to read at least one character that doesn't
934 * match; thus, we can't short-circuit "infinity" or "nan(...)".
937 for (p = buf; p < end; ) {
943 if (c == '-' || c == '+')
968 (c != "nfinity"[infnanpos] &&
969 c != "NFINITY"[infnanpos]))
971 if (infnanpos == 1 || infnanpos == 6)
972 commit = p; /* inf or infinity */
977 case -1: /* XXX kludge to deal with nan(...) */
980 if (c != 'A' && c != 'a')
984 if (c != 'N' && c != 'n')
997 } else if (!isalnum(c) && c != '_')
1005 if (c == 'X' || c == 'x') {
1008 } else { /* we saw a '0', but no 'x' */
1013 if ((ishex && isxdigit(c)) || isdigit(c))
1024 if (((c == 'E' || c == 'e') && !ishex) ||
1025 ((c == 'P' || c == 'p') && ishex)) {
1030 } else if ((ishex && isxdigit(c)) || isdigit(c)) {
1037 state = S_EXPDIGITS;
1038 if (c == '-' || c == '+')
1054 else if (__srefill(fp))
1059 while (commit < --p)
1060 __ungetc(*(u_char *)p, fp);
1062 return (commit - buf);