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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "namespace.h"
52 #include "un-namespace.h"
54 #include "libc_private.h"
57 #ifndef NO_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 /* 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 #ifndef NO_FLOATING_POINT
99 static int parsefloat(FILE *, wchar_t *, wchar_t *);
102 extern int __scanfdebug;
105 (cclcompl ? (wmemchr(ccls, (_c), ccle - ccls) == NULL) : \
106 (wmemchr(ccls, (_c), ccle - ccls) != NULL))
112 vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
118 ret = __vfwscanf(fp, fmt, ap);
124 * Non-MT-safe version.
127 __vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
129 wint_t c; /* character from format, or conversion */
130 size_t width; /* field width, or 0 */
131 wchar_t *p; /* points into all kinds of strings */
132 int n; /* handy integer */
133 int flags; /* flags as defined above */
134 wchar_t *p0; /* saves original value of p when necessary */
135 int nassigned; /* number of fields assigned */
136 int nconversions; /* number of conversions */
137 int nread; /* number of characters consumed from fp */
138 int base; /* base argument to conversion function */
139 wchar_t buf[BUF]; /* buffer for numeric conversions */
140 const wchar_t *ccls; /* character class start */
141 const wchar_t *ccle; /* character class end */
142 int cclcompl; /* ccl is complemented? */
143 wint_t wi; /* handy wint_t */
144 char *mbp; /* multibyte string pointer for %c %s %[ */
145 size_t nconv; /* number of bytes in mb. conversion */
146 char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
147 static const mbstate_t initial;
150 /* `basefix' is used to avoid `if' tests in the integer scanner */
151 static short basefix[17] =
152 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
163 while ((c = __fgetwc(fp)) != WEOF &&
175 * switch on the format. continue if done;
176 * break once format type is derived.
182 if ((wi = __fgetwc(fp)) == WEOF)
205 flags |= LONGLONG; /* not quite */
224 case '0': case '1': case '2': case '3': case '4':
225 case '5': case '6': case '7': case '8': case '9':
226 width = width * 10 + c - '0';
256 flags |= PFXOK; /* enable 0x prefixing */
262 #ifndef NO_FLOATING_POINT
263 case 'A': case 'E': case 'F': case 'G':
264 case 'a': case 'e': case 'f': case 'g':
285 while (*fmt != '\0' && *fmt != ']')
301 case 'p': /* pointer format is like hex */
302 flags |= POINTER | PFXOK;
303 c = CT_INT; /* assumes sizeof(uintmax_t) */
304 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
310 if (flags & SUPPRESS) /* ??? */
312 if (flags & SHORTSHORT)
313 *va_arg(ap, char *) = nread;
314 else if (flags & SHORT)
315 *va_arg(ap, short *) = nread;
316 else if (flags & LONG)
317 *va_arg(ap, long *) = nread;
318 else if (flags & LONGLONG)
319 *va_arg(ap, long long *) = nread;
320 else if (flags & INTMAXT)
321 *va_arg(ap, intmax_t *) = nread;
322 else if (flags & SIZET)
323 *va_arg(ap, size_t *) = nread;
324 else if (flags & PTRDIFFT)
325 *va_arg(ap, ptrdiff_t *) = nread;
327 *va_arg(ap, int *) = nread;
334 * Disgusting backwards compatibility hack. XXX
336 case '\0': /* compat */
341 * Consume leading white space, except for formats
342 * that suppress this.
344 if ((flags & NOSKIP) == 0) {
345 while ((wi = __fgetwc(fp)) != WEOF && iswspace(wi))
358 /* scan arbitrary characters (sets NOSKIP) */
362 if (!(flags & SUPPRESS))
363 p = va_arg(ap, wchar_t *);
365 while (width-- != 0 &&
366 (wi = __fgetwc(fp)) != WEOF) {
367 if (!(flags & SUPPRESS))
374 if (!(flags & SUPPRESS))
377 if (!(flags & SUPPRESS))
378 mbp = va_arg(ap, char *);
382 (wi = __fgetwc(fp)) != WEOF) {
383 if (width >= MB_CUR_MAX &&
384 !(flags & SUPPRESS)) {
385 nconv = wcrtomb(mbp, wi, &mbs);
386 if (nconv == (size_t)-1)
389 nconv = wcrtomb(mbbuf, wi,
391 if (nconv == (size_t)-1)
397 if (!(flags & SUPPRESS))
401 if (!(flags & SUPPRESS))
409 if (!(flags & SUPPRESS))
416 /* scan a (nonempty) character class (sets NOSKIP) */
418 width = (size_t)~0; /* `infinity' */
419 /* take only those things in the class */
420 if ((flags & SUPPRESS) && (flags & LONG)) {
422 while ((wi = __fgetwc(fp)) != WEOF &&
423 width-- != 0 && INCCL(wi))
429 } else if (flags & LONG) {
430 p0 = p = va_arg(ap, wchar_t *);
431 while ((wi = __fgetwc(fp)) != WEOF &&
432 width-- != 0 && INCCL(wi))
442 if (!(flags & SUPPRESS))
443 mbp = va_arg(ap, char *);
446 while ((wi = __fgetwc(fp)) != WEOF &&
447 width != 0 && INCCL(wi)) {
448 if (width >= MB_CUR_MAX &&
449 !(flags & SUPPRESS)) {
450 nconv = wcrtomb(mbp, wi, &mbs);
451 if (nconv == (size_t)-1)
454 nconv = wcrtomb(mbbuf, wi,
456 if (nconv == (size_t)-1)
460 if (!(flags & SUPPRESS))
464 if (!(flags & SUPPRESS))
471 if (!(flags & SUPPRESS)) {
481 /* like CCL, but zero-length string OK, & no NOSKIP */
484 if ((flags & SUPPRESS) && (flags & LONG)) {
485 while ((wi = __fgetwc(fp)) != WEOF &&
491 } else if (flags & LONG) {
492 p0 = p = va_arg(ap, wchar_t *);
493 while ((wi = __fgetwc(fp)) != WEOF &&
504 if (!(flags & SUPPRESS))
505 mbp = va_arg(ap, char *);
507 while ((wi = __fgetwc(fp)) != WEOF &&
510 if (width >= MB_CUR_MAX &&
511 !(flags & SUPPRESS)) {
512 nconv = wcrtomb(mbp, wi, &mbs);
513 if (nconv == (size_t)-1)
516 nconv = wcrtomb(mbbuf, wi,
518 if (nconv == (size_t)-1)
522 if (!(flags & SUPPRESS))
526 if (!(flags & SUPPRESS))
533 if (!(flags & SUPPRESS)) {
542 /* scan an integer as if by the conversion function */
543 if (width == 0 || width > sizeof(buf) /
545 width = sizeof(buf) / sizeof(*buf) - 1;
546 flags |= SIGNOK | NDIGITS | NZDIGITS;
547 for (p = buf; width; width--) {
550 * Switch on the character; `goto ok'
551 * if we accept it as a part of number.
556 * The digit 0 is always legal, but is
557 * special. For %i conversions, if no
558 * digits (zero or nonzero) have been
559 * scanned (only signs), we will have
560 * base==0. In that case, we should set
561 * it to 8 and enable 0x prefixing.
562 * Also, if we have not scanned zero digits
563 * before this, do not turn off prefixing
564 * (someone else will turn it off if we
565 * have scanned any nonzero digits).
572 if (flags & NZDIGITS)
573 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
575 flags &= ~(SIGNOK|PFXOK|NDIGITS);
578 /* 1 through 7 always legal */
579 case '1': case '2': case '3':
580 case '4': case '5': case '6': case '7':
581 base = basefix[base];
582 flags &= ~(SIGNOK | PFXOK | NDIGITS);
585 /* digits 8 and 9 ok iff decimal or hex */
587 base = basefix[base];
589 break; /* not legal here */
590 flags &= ~(SIGNOK | PFXOK | NDIGITS);
593 /* letters ok iff hex */
594 case 'A': case 'B': case 'C':
595 case 'D': case 'E': case 'F':
596 case 'a': case 'b': case 'c':
597 case 'd': case 'e': case 'f':
598 /* no need to fix base here */
600 break; /* not legal here */
601 flags &= ~(SIGNOK | PFXOK | NDIGITS);
604 /* sign ok only as first character */
606 if (flags & SIGNOK) {
614 * x ok iff flag still set & 2nd char (or
615 * 3rd char if we have a sign).
618 if (flags & PFXOK && p ==
619 buf + 1 + !!(flags & HAVESIGN)) {
620 base = 16; /* if %i */
628 * If we got here, c is not a legal character
629 * for a number. Stop accumulating digits.
636 * c is legal: store it and look at the next.
641 * If we had only a sign, it is no good; push
642 * back the sign. If the number ends in `x',
643 * it was [sign] '0' 'x', so push back the x
644 * and treat it as [sign] '0'.
646 if (flags & NDIGITS) {
652 if (c == 'x' || c == 'X') {
656 if ((flags & SUPPRESS) == 0) {
660 if ((flags & UNSIGNED) == 0)
661 res = wcstoimax(buf, NULL, base);
663 res = wcstoumax(buf, NULL, base);
665 *va_arg(ap, void **) =
666 (void *)(uintptr_t)res;
667 else if (flags & SHORTSHORT)
668 *va_arg(ap, char *) = res;
669 else if (flags & SHORT)
670 *va_arg(ap, short *) = res;
671 else if (flags & LONG)
672 *va_arg(ap, long *) = res;
673 else if (flags & LONGLONG)
674 *va_arg(ap, long long *) = res;
675 else if (flags & INTMAXT)
676 *va_arg(ap, intmax_t *) = res;
677 else if (flags & PTRDIFFT)
678 *va_arg(ap, ptrdiff_t *) = res;
679 else if (flags & SIZET)
680 *va_arg(ap, size_t *) = res;
682 *va_arg(ap, int *) = res;
689 #ifndef NO_FLOATING_POINT
691 /* scan a floating point number as if by strtod */
692 if (width == 0 || width > sizeof(buf) /
694 width = sizeof(buf) / sizeof(*buf) - 1;
695 if ((width = parsefloat(fp, buf, buf + width)) == 0)
697 if ((flags & SUPPRESS) == 0) {
698 if (flags & LONGDBL) {
699 long double res = wcstold(buf, &p);
700 *va_arg(ap, long double *) = res;
701 } else if (flags & LONG) {
702 double res = wcstod(buf, &p);
703 *va_arg(ap, double *) = res;
705 float res = wcstof(buf, &p);
706 *va_arg(ap, float *) = res;
708 if (__scanfdebug && p - buf != width)
715 #endif /* !NO_FLOATING_POINT */
719 return (nconversions != 0 ? nassigned : EOF);
724 #ifndef NO_FLOATING_POINT
726 parsefloat(FILE *fp, wchar_t *buf, wchar_t *end)
731 S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
732 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
735 wchar_t decpt = (wchar_t)(unsigned char)*localeconv()->decimal_point;
736 _Bool gotmantdig = 0, ishex = 0;
739 * We set commit = p whenever the string we have read so far
740 * constitutes a valid representation of a floating point
741 * number by itself. At some point, the parse will complete
742 * or fail, and we will ungetc() back to the last commit point.
743 * To ensure that the file offset gets updated properly, it is
744 * always necessary to read at least one character that doesn't
745 * match; thus, we can't short-circuit "infinity" or "nan(...)".
749 for (p = buf; p < end; ) {
750 if ((c = __fgetwc(fp)) == WEOF)
756 if (c == '-' || c == '+')
781 (c != "nfinity"[infnanpos] &&
782 c != "NFINITY"[infnanpos]))
784 if (infnanpos == 1 || infnanpos == 6)
785 commit = p; /* inf or infinity */
790 case -1: /* XXX kludge to deal with nan(...) */
793 if (c != 'A' && c != 'a')
797 if (c != 'N' && c != 'n')
810 } else if (!iswalnum(c) && c != '_')
818 if (c == 'X' || c == 'x') {
821 } else { /* we saw a '0', but no 'x' */
826 if ((ishex && iswxdigit(c)) || iswdigit(c))
837 if (((c == 'E' || c == 'e') && !ishex) ||
838 ((c == 'P' || c == 'p') && ishex)) {
843 } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
851 if (c == '-' || c == '+')
874 return (commit - buf);