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 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
41 #endif /* LIBC_SCCS and not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
46 #include "namespace.h"
57 #include "un-namespace.h"
59 #include "libc_private.h"
61 #include "xlocale_private.h"
63 #define BUF 513 /* Maximum length of numeric string. */
66 * Flags used during conversion.
68 #define LONG 0x01 /* l: long or double */
69 #define LONGDBL 0x02 /* L: long double */
70 #define SHORT 0x04 /* h: short */
71 #define SUPPRESS 0x08 /* *: suppress assignment */
72 #define POINTER 0x10 /* p: void * (as hex) */
73 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
74 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
75 #define INTMAXT 0x800 /* j: intmax_t */
76 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
77 #define SIZET 0x2000 /* z: size_t */
78 #define SHORTSHORT 0x4000 /* hh: char */
79 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
82 * The following are used in integral conversions only:
83 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
85 #define SIGNOK 0x40 /* +/- is (still) legal */
86 #define NDIGITS 0x80 /* no digits detected */
87 #define PFXOK 0x100 /* 0x prefix is (still) legal */
88 #define NZDIGITS 0x200 /* no zero digits detected */
89 #define HAVESIGN 0x10000 /* sign detected */
94 #define CT_CHAR 0 /* %c conversion */
95 #define CT_CCL 1 /* %[...] conversion */
96 #define CT_STRING 2 /* %s conversion */
97 #define CT_INT 3 /* %[dioupxX] conversion */
98 #define CT_FLOAT 4 /* %[efgEFG] conversion */
100 #ifndef NO_FLOATING_POINT
101 static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
105 (cclcompl ? (wmemchr(ccls, (_c), ccle - ccls) == NULL) : \
106 (wmemchr(ccls, (_c), ccle - ccls) != NULL))
108 static const mbstate_t initial_mbs;
114 vfwscanf_l(FILE * __restrict fp, locale_t locale,
115 const wchar_t * __restrict fmt, va_list ap)
122 ret = __vfwscanf(fp, locale, fmt, ap);
127 vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
129 return vfwscanf_l(fp, __get_locale(), fmt, ap);
133 * Non-MT-safe version.
136 __vfwscanf(FILE * __restrict fp, locale_t locale,
137 const wchar_t * __restrict fmt, va_list ap)
139 wint_t c; /* character from format, or conversion */
140 size_t width; /* field width, or 0 */
141 wchar_t *p; /* points into all kinds of strings */
142 int n; /* handy integer */
143 int flags; /* flags as defined above */
144 wchar_t *p0; /* saves original value of p when necessary */
145 int nassigned; /* number of fields assigned */
146 int nconversions; /* number of conversions */
147 int nread; /* number of characters consumed from fp */
148 int base; /* base argument to conversion function */
149 wchar_t buf[BUF]; /* buffer for numeric conversions */
150 const wchar_t *ccls; /* character class start */
151 const wchar_t *ccle; /* character class end */
152 int cclcompl; /* ccl is complemented? */
153 wint_t wi; /* handy wint_t */
154 char *mbp; /* multibyte string pointer for %c %s %[ */
155 size_t nconv; /* number of bytes in mb. conversion */
156 char mbbuf[MB_LEN_MAX]; /* temporary mb. character buffer */
159 /* `basefix' is used to avoid `if' tests in the integer scanner */
160 static short basefix[17] =
161 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
172 while ((c = __fgetwc(fp, locale)) != WEOF &&
173 iswspace_l(c, locale))
176 __ungetwc(c, fp, locale);
184 * switch on the format. continue if done;
185 * break once format type is derived.
191 if ((wi = __fgetwc(fp, locale)) == WEOF)
194 __ungetwc(wi, fp, locale);
214 flags |= LONGLONG; /* not quite */
233 case '0': case '1': case '2': case '3': case '4':
234 case '5': case '6': case '7': case '8': case '9':
235 width = width * 10 + c - '0';
265 flags |= PFXOK; /* enable 0x prefixing */
271 #ifndef NO_FLOATING_POINT
272 case 'A': case 'E': case 'F': case 'G':
273 case 'a': case 'e': case 'f': case 'g':
294 while (*fmt != '\0' && *fmt != ']')
310 case 'p': /* pointer format is like hex */
311 flags |= POINTER | PFXOK;
312 c = CT_INT; /* assumes sizeof(uintmax_t) */
313 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
319 if (flags & SUPPRESS) /* ??? */
321 if (flags & SHORTSHORT)
322 *va_arg(ap, char *) = nread;
323 else if (flags & SHORT)
324 *va_arg(ap, short *) = nread;
325 else if (flags & LONG)
326 *va_arg(ap, long *) = nread;
327 else if (flags & LONGLONG)
328 *va_arg(ap, long long *) = nread;
329 else if (flags & INTMAXT)
330 *va_arg(ap, intmax_t *) = nread;
331 else if (flags & SIZET)
332 *va_arg(ap, size_t *) = nread;
333 else if (flags & PTRDIFFT)
334 *va_arg(ap, ptrdiff_t *) = nread;
336 *va_arg(ap, int *) = nread;
343 * Disgusting backwards compatibility hack. XXX
345 case '\0': /* compat */
350 * Consume leading white space, except for formats
351 * that suppress this.
353 if ((flags & NOSKIP) == 0) {
354 while ((wi = __fgetwc(fp, locale)) != WEOF && iswspace(wi))
358 __ungetwc(wi, fp, locale);
367 /* scan arbitrary characters (sets NOSKIP) */
371 if (!(flags & SUPPRESS))
372 p = va_arg(ap, wchar_t *);
374 while (width-- != 0 &&
375 (wi = __fgetwc(fp, locale)) != WEOF) {
376 if (!(flags & SUPPRESS))
383 if (!(flags & SUPPRESS))
386 if (!(flags & SUPPRESS))
387 mbp = va_arg(ap, char *);
391 (wi = __fgetwc(fp, locale)) != WEOF) {
392 if (width >= MB_CUR_MAX &&
393 !(flags & SUPPRESS)) {
394 nconv = wcrtomb(mbp, wi, &mbs);
395 if (nconv == (size_t)-1)
398 nconv = wcrtomb(mbbuf, wi,
400 if (nconv == (size_t)-1)
403 __ungetwc(wi, fp, locale);
406 if (!(flags & SUPPRESS))
410 if (!(flags & SUPPRESS))
418 if (!(flags & SUPPRESS))
425 /* scan a (nonempty) character class (sets NOSKIP) */
427 width = (size_t)~0; /* `infinity' */
428 /* take only those things in the class */
429 if ((flags & SUPPRESS) && (flags & LONG)) {
431 while ((wi = __fgetwc(fp, locale)) != WEOF &&
432 width-- != 0 && INCCL(wi))
435 __ungetwc(wi, fp, locale);
438 } else if (flags & LONG) {
439 p0 = p = va_arg(ap, wchar_t *);
440 while ((wi = __fgetwc(fp, locale)) != WEOF &&
441 width-- != 0 && INCCL(wi))
444 __ungetwc(wi, fp, locale);
451 if (!(flags & SUPPRESS))
452 mbp = va_arg(ap, char *);
455 while ((wi = __fgetwc(fp, locale)) != WEOF &&
456 width != 0 && INCCL(wi)) {
457 if (width >= MB_CUR_MAX &&
458 !(flags & SUPPRESS)) {
459 nconv = wcrtomb(mbp, wi, &mbs);
460 if (nconv == (size_t)-1)
463 nconv = wcrtomb(mbbuf, wi,
465 if (nconv == (size_t)-1)
469 if (!(flags & SUPPRESS))
473 if (!(flags & SUPPRESS))
479 __ungetwc(wi, fp, locale);
480 if (!(flags & SUPPRESS)) {
490 /* like CCL, but zero-length string OK, & no NOSKIP */
493 if ((flags & SUPPRESS) && (flags & LONG)) {
494 while ((wi = __fgetwc(fp, locale)) != WEOF &&
499 __ungetwc(wi, fp, locale);
500 } else if (flags & LONG) {
501 p0 = p = va_arg(ap, wchar_t *);
502 while ((wi = __fgetwc(fp, locale)) != WEOF &&
509 __ungetwc(wi, fp, locale);
513 if (!(flags & SUPPRESS))
514 mbp = va_arg(ap, char *);
516 while ((wi = __fgetwc(fp, locale)) != WEOF &&
519 if (width >= MB_CUR_MAX &&
520 !(flags & SUPPRESS)) {
521 nconv = wcrtomb(mbp, wi, &mbs);
522 if (nconv == (size_t)-1)
525 nconv = wcrtomb(mbbuf, wi,
527 if (nconv == (size_t)-1)
531 if (!(flags & SUPPRESS))
535 if (!(flags & SUPPRESS))
541 __ungetwc(wi, fp, locale);
542 if (!(flags & SUPPRESS)) {
551 /* scan an integer as if by the conversion function */
552 if (width == 0 || width > sizeof(buf) /
554 width = sizeof(buf) / sizeof(*buf) - 1;
555 flags |= SIGNOK | NDIGITS | NZDIGITS;
556 for (p = buf; width; width--) {
557 c = __fgetwc(fp, locale);
559 * Switch on the character; `goto ok'
560 * if we accept it as a part of number.
565 * The digit 0 is always legal, but is
566 * special. For %i conversions, if no
567 * digits (zero or nonzero) have been
568 * scanned (only signs), we will have
569 * base==0. In that case, we should set
570 * it to 8 and enable 0x prefixing.
571 * Also, if we have not scanned zero digits
572 * before this, do not turn off prefixing
573 * (someone else will turn it off if we
574 * have scanned any nonzero digits).
581 if (flags & NZDIGITS)
582 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
584 flags &= ~(SIGNOK|PFXOK|NDIGITS);
587 /* 1 through 7 always legal */
588 case '1': case '2': case '3':
589 case '4': case '5': case '6': case '7':
590 base = basefix[base];
591 flags &= ~(SIGNOK | PFXOK | NDIGITS);
594 /* digits 8 and 9 ok iff decimal or hex */
596 base = basefix[base];
598 break; /* not legal here */
599 flags &= ~(SIGNOK | PFXOK | NDIGITS);
602 /* letters ok iff hex */
603 case 'A': case 'B': case 'C':
604 case 'D': case 'E': case 'F':
605 case 'a': case 'b': case 'c':
606 case 'd': case 'e': case 'f':
607 /* no need to fix base here */
609 break; /* not legal here */
610 flags &= ~(SIGNOK | PFXOK | NDIGITS);
613 /* sign ok only as first character */
615 if (flags & SIGNOK) {
623 * x ok iff flag still set & 2nd char (or
624 * 3rd char if we have a sign).
627 if (flags & PFXOK && p ==
628 buf + 1 + !!(flags & HAVESIGN)) {
629 base = 16; /* if %i */
637 * If we got here, c is not a legal character
638 * for a number. Stop accumulating digits.
641 __ungetwc(c, fp, locale);
645 * c is legal: store it and look at the next.
650 * If we had only a sign, it is no good; push
651 * back the sign. If the number ends in `x',
652 * it was [sign] '0' 'x', so push back the x
653 * and treat it as [sign] '0'.
655 if (flags & NDIGITS) {
657 __ungetwc(*--p, fp, locale);
661 if (c == 'x' || c == 'X') {
663 __ungetwc(c, fp, locale);
665 if ((flags & SUPPRESS) == 0) {
669 if ((flags & UNSIGNED) == 0)
670 res = wcstoimax(buf, NULL, base);
672 res = wcstoumax(buf, NULL, base);
674 *va_arg(ap, void **) =
675 (void *)(uintptr_t)res;
676 else if (flags & SHORTSHORT)
677 *va_arg(ap, char *) = res;
678 else if (flags & SHORT)
679 *va_arg(ap, short *) = res;
680 else if (flags & LONG)
681 *va_arg(ap, long *) = res;
682 else if (flags & LONGLONG)
683 *va_arg(ap, long long *) = res;
684 else if (flags & INTMAXT)
685 *va_arg(ap, intmax_t *) = res;
686 else if (flags & PTRDIFFT)
687 *va_arg(ap, ptrdiff_t *) = res;
688 else if (flags & SIZET)
689 *va_arg(ap, size_t *) = res;
691 *va_arg(ap, int *) = res;
698 #ifndef NO_FLOATING_POINT
700 /* scan a floating point number as if by strtod */
701 if (width == 0 || width > sizeof(buf) /
703 width = sizeof(buf) / sizeof(*buf) - 1;
704 if ((width = parsefloat(fp, buf, buf + width, locale)) == 0)
706 if ((flags & SUPPRESS) == 0) {
707 if (flags & LONGDBL) {
708 long double res = wcstold(buf, &p);
709 *va_arg(ap, long double *) = res;
710 } else if (flags & LONG) {
711 double res = wcstod(buf, &p);
712 *va_arg(ap, double *) = res;
714 float res = wcstof(buf, &p);
715 *va_arg(ap, float *) = res;
722 #endif /* !NO_FLOATING_POINT */
726 return (nconversions != 0 ? nassigned : EOF);
731 #ifndef NO_FLOATING_POINT
733 parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t locale)
740 S_START, S_GOTSIGN, S_INF, S_NAN, S_DONE, S_MAYBEHEX,
741 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
745 _Bool gotmantdig = 0, ishex = 0;
748 nconv = mbrtowc(&decpt, localeconv()->decimal_point, MB_CUR_MAX, &mbs);
749 if (nconv == (size_t)-1 || nconv == (size_t)-2)
750 decpt = '.'; /* failsafe */
753 * We set commit = p whenever the string we have read so far
754 * constitutes a valid representation of a floating point
755 * number by itself. At some point, the parse will complete
756 * or fail, and we will ungetc() back to the last commit point.
757 * To ensure that the file offset gets updated properly, it is
758 * always necessary to read at least one character that doesn't
759 * match; thus, we can't short-circuit "infinity" or "nan(...)".
763 for (p = buf; p < end; ) {
764 if ((c = __fgetwc(fp, locale)) == WEOF)
770 if (c == '-' || c == '+')
795 (c != "nfinity"[infnanpos] &&
796 c != "NFINITY"[infnanpos]))
798 if (infnanpos == 1 || infnanpos == 6)
799 commit = p; /* inf or infinity */
805 if (c != 'A' && c != 'a')
809 if (c != 'N' && c != 'n')
822 } else if (!iswalnum(c) && c != '_')
832 if (c == 'X' || c == 'x') {
835 } else { /* we saw a '0', but no 'x' */
840 if ((ishex && iswxdigit(c)) || iswdigit(c))
851 if (((c == 'E' || c == 'e') && !ishex) ||
852 ((c == 'P' || c == 'p') && ishex)) {
857 } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
865 if (c == '-' || c == '+')
884 __ungetwc(c, fp, locale);
886 __ungetwc(*p, fp, locale);
888 return (commit - buf);