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 * 3. 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
37 * @(#)vfscanf.c 8.1 (Berkeley) 6/4/93
38 * $FreeBSD: head/lib/libc/stdio/vfwscanf.c 249808 2013-04-23 13:33:13Z emaste $
41 #include "namespace.h"
52 #include "un-namespace.h"
54 #include "libc_private.h"
56 #include "xlocale_private.h"
58 #define BUF 513 /* Maximum length of numeric string. */
61 * Flags used during conversion.
63 #define LONG 0x01 /* l: long or double */
64 #define LONGDBL 0x02 /* L: long double */
65 #define SHORT 0x04 /* h: short */
66 #define SUPPRESS 0x08 /* *: suppress assignment */
67 #define POINTER 0x10 /* p: void * (as hex) */
68 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
69 #define LONGLONG 0x400 /* ll: long long (+ deprecated q: quad) */
70 #define INTMAXT 0x800 /* j: intmax_t */
71 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
72 #define SIZET 0x2000 /* z: size_t */
73 #define SHORTSHORT 0x4000 /* hh: char */
74 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
77 * The following are used in integral conversions only:
78 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
80 #define SIGNOK 0x40 /* +/- is (still) legal */
81 #define NDIGITS 0x80 /* no digits detected */
82 #define PFXOK 0x100 /* 0x prefix is (still) legal */
83 #define NZDIGITS 0x200 /* no zero digits detected */
84 #define HAVESIGN 0x10000 /* sign detected */
89 #define CT_CHAR 0 /* %c conversion */
90 #define CT_CCL 1 /* %[...] conversion */
91 #define CT_STRING 2 /* %s conversion */
92 #define CT_INT 3 /* %[dioupxX] conversion */
93 #define CT_FLOAT 4 /* %[efgEFG] conversion */
95 #ifndef NO_FLOATING_POINT
96 static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
100 const wchar_t *start; /* character class start */
101 const wchar_t *end; /* character class end */
102 int compl; /* ccl is complemented? */
106 inccl(const struct ccl *ccl, wint_t wi)
110 return (wmemchr(ccl->start, wi, ccl->end - ccl->start)
113 return (wmemchr(ccl->start, wi, ccl->end - ccl->start) != NULL);
118 * Conversion functions are passed a pointer to this object instead of
119 * a real parameter to indicate that the assignment-suppression (*)
120 * flag was specified. We could use a NULL pointer to indicate this,
121 * but that would mask bugs in applications that call scanf() with a
124 static const int suppress;
125 #define SUPPRESS_PTR ((void *)&suppress)
127 static const mbstate_t initial_mbs;
130 * The following conversion functions return the number of characters consumed,
131 * or -1 on input failure. Character class conversion returns 0 on match
136 convert_char(FILE *fp, char * mbp, int width, locale_t locale)
145 while (width-- != 0 && (wi = __fgetwc(fp, locale)) != WEOF) {
146 if (mbp != SUPPRESS_PTR) {
147 nconv = wcrtomb(mbp, wi, &mbs);
148 if (nconv == (size_t)-1)
160 convert_wchar(FILE *fp, wchar_t *wcp, int width, locale_t locale)
166 while (width-- != 0 && (wi = __fgetwc(fp, locale)) != WEOF) {
167 if (wcp != SUPPRESS_PTR)
168 *wcp++ = (wchar_t)wi;
177 convert_ccl(FILE *fp, char * mbp, int width, const struct ccl *ccl,
187 while ((wi = __fgetwc(fp, locale)) != WEOF &&
188 width-- != 0 && inccl(ccl, wi)) {
189 if (mbp != SUPPRESS_PTR) {
190 nconv = wcrtomb(mbp, wi, &mbs);
191 if (nconv == (size_t)-1)
198 __ungetwc(wi, fp, locale);
199 if (mbp != SUPPRESS_PTR)
205 convert_wccl(FILE *fp, wchar_t *wcp, int width, const struct ccl *ccl,
212 if (wcp == SUPPRESS_PTR) {
214 while ((wi = __fgetwc(fp, locale)) != WEOF &&
215 width-- != 0 && inccl(ccl, wi))
218 __ungetwc(wi, fp, locale);
221 while ((wi = __fgetwc(fp, locale)) != WEOF &&
222 width-- != 0 && inccl(ccl, wi))
223 *wcp++ = (wchar_t)wi;
225 __ungetwc(wi, fp, locale);
235 convert_string(FILE *fp, char * mbp, int width, locale_t locale)
244 while ((wi = __fgetwc(fp, locale)) != WEOF && width-- != 0 &&
246 if (mbp != SUPPRESS_PTR) {
247 nconv = wcrtomb(mbp, wi, &mbs);
248 if (nconv == (size_t)-1)
255 __ungetwc(wi, fp, locale);
256 if (mbp != SUPPRESS_PTR)
262 convert_wstring(FILE *fp, wchar_t *wcp, int width, locale_t locale)
269 if (wcp == SUPPRESS_PTR) {
270 while ((wi = __fgetwc(fp, locale)) != WEOF &&
271 width-- != 0 && !iswspace(wi))
274 __ungetwc(wi, fp, locale);
277 while ((wi = __fgetwc(fp, locale)) != WEOF &&
278 width-- != 0 && !iswspace(wi)) {
279 *wcp++ = (wchar_t)wi;
283 __ungetwc(wi, fp, locale);
290 * Read an integer, storing it in buf. The only relevant bit in the
291 * flags argument is PFXOK.
293 * Return 0 on a match failure, and the number of characters read
297 parseint(FILE *fp, wchar_t *buf, int width, int base, int flags,
300 /* `basefix' is used to avoid `if' tests */
301 static const short basefix[17] =
302 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
306 flags |= SIGNOK | NDIGITS | NZDIGITS;
307 for (wcp = buf; width; width--) {
308 c = __fgetwc(fp, locale);
310 * Switch on the character; `goto ok' if we accept it
311 * as a part of number.
316 * The digit 0 is always legal, but is special. For
317 * %i conversions, if no digits (zero or nonzero) have
318 * been scanned (only signs), we will have base==0.
319 * In that case, we should set it to 8 and enable 0x
320 * prefixing. Also, if we have not scanned zero
321 * digits before this, do not turn off prefixing
322 * (someone else will turn it off if we have scanned
323 * any nonzero digits).
330 if (flags & NZDIGITS)
331 flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
333 flags &= ~(SIGNOK|PFXOK|NDIGITS);
336 /* 1 through 7 always legal */
337 case '1': case '2': case '3':
338 case '4': case '5': case '6': case '7':
339 base = basefix[base];
340 flags &= ~(SIGNOK | PFXOK | NDIGITS);
343 /* digits 8 and 9 ok iff decimal or hex */
345 base = basefix[base];
347 break; /* not legal here */
348 flags &= ~(SIGNOK | PFXOK | NDIGITS);
351 /* letters ok iff hex */
352 case 'A': case 'B': case 'C':
353 case 'D': case 'E': case 'F':
354 case 'a': case 'b': case 'c':
355 case 'd': case 'e': case 'f':
356 /* no need to fix base here */
358 break; /* not legal here */
359 flags &= ~(SIGNOK | PFXOK | NDIGITS);
362 /* sign ok only as first character */
364 if (flags & SIGNOK) {
372 * x ok iff flag still set & 2nd char (or 3rd char if
376 if (flags & PFXOK && wcp ==
377 buf + 1 + !!(flags & HAVESIGN)) {
378 base = 16; /* if %i */
386 * If we got here, c is not a legal character for a
387 * number. Stop accumulating digits.
390 __ungetwc(c, fp, locale);
394 * c is legal: store it and look at the next.
399 * If we had only a sign, it is no good; push back the sign.
400 * If the number ends in `x', it was [sign] '0' 'x', so push
401 * back the x and treat it as [sign] '0'.
403 if (flags & NDIGITS) {
405 __ungetwc(*--wcp, fp, locale);
409 if (c == 'x' || c == 'X') {
411 __ungetwc(c, fp, locale);
420 vfwscanf_l(FILE * __restrict fp, locale_t locale,
421 const wchar_t * __restrict fmt, va_list ap)
428 ret = __vfwscanf(fp, locale, fmt, ap);
433 vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
435 return vfwscanf_l(fp, __get_locale(), fmt, ap);
439 * Non-MT-safe version.
442 __vfwscanf(FILE * __restrict fp, locale_t locale,
443 const wchar_t * __restrict fmt, va_list ap)
445 #define GETARG(type) ((flags & SUPPRESS) ? SUPPRESS_PTR : va_arg(ap, type))
446 wint_t c; /* character from format, or conversion */
447 size_t width; /* field width, or 0 */
448 int flags; /* flags as defined above */
449 int nassigned; /* number of fields assigned */
450 int nconversions; /* number of conversions */
451 int nr; /* characters read by the current conversion */
452 int nread; /* number of characters consumed from fp */
453 int base; /* base argument to conversion function */
454 struct ccl ccl; /* character class info */
455 wchar_t buf[BUF]; /* buffer for numeric conversions */
456 wint_t wi; /* handy wint_t */
461 ccl.start = ccl.end = NULL;
467 while ((c = __fgetwc(fp, locale)) != WEOF &&
468 iswspace_l(c, locale))
471 __ungetwc(c, fp, locale);
479 * switch on the format. continue if done;
480 * break once format type is derived.
486 if ((wi = __fgetwc(fp, locale)) == WEOF)
489 __ungetwc(wi, fp, locale);
509 flags |= LONGLONG; /* not quite */
528 case '0': case '1': case '2': case '3': case '4':
529 case '5': case '6': case '7': case '8': case '9':
530 width = width * 10 + c - '0';
560 flags |= PFXOK; /* enable 0x prefixing */
566 #ifndef NO_FLOATING_POINT
567 case 'A': case 'E': case 'F': case 'G':
568 case 'a': case 'e': case 'f': case 'g':
589 while (*fmt != '\0' && *fmt != ']')
605 case 'p': /* pointer format is like hex */
606 flags |= POINTER | PFXOK;
607 c = CT_INT; /* assumes sizeof(uintmax_t) */
608 flags |= UNSIGNED; /* >= sizeof(uintptr_t) */
613 if (flags & SUPPRESS) /* ??? */
615 if (flags & SHORTSHORT)
616 *va_arg(ap, char *) = nread;
617 else if (flags & SHORT)
618 *va_arg(ap, short *) = nread;
619 else if (flags & LONG)
620 *va_arg(ap, long *) = nread;
621 else if (flags & LONGLONG)
622 *va_arg(ap, long long *) = nread;
623 else if (flags & INTMAXT)
624 *va_arg(ap, intmax_t *) = nread;
625 else if (flags & SIZET)
626 *va_arg(ap, size_t *) = nread;
627 else if (flags & PTRDIFFT)
628 *va_arg(ap, ptrdiff_t *) = nread;
630 *va_arg(ap, int *) = nread;
637 * Disgusting backwards compatibility hack. XXX
639 case '\0': /* compat */
644 * Consume leading white space, except for formats
645 * that suppress this.
647 if ((flags & NOSKIP) == 0) {
648 while ((wi = __fgetwc(fp, locale)) != WEOF && iswspace(wi))
652 __ungetwc(wi, fp, locale);
661 /* scan arbitrary characters (sets NOSKIP) */
665 nr = convert_wchar(fp, GETARG(wchar_t *), width,
668 nr = convert_char(fp, GETARG(char *), width,
676 /* scan a (nonempty) character class (sets NOSKIP) */
678 width = (size_t)~0; /* `infinity' */
679 /* take only those things in the class */
681 nr = convert_wccl(fp, GETARG(wchar_t *), width,
684 nr = convert_ccl(fp, GETARG(char *), width,
696 /* like CCL, but zero-length string OK, & no NOSKIP */
700 nr = convert_wstring(fp, GETARG(wchar_t *),
703 nr = convert_string(fp, GETARG(char *), width,
711 /* scan an integer as if by the conversion function */
712 if (width == 0 || width > sizeof(buf) /
714 width = sizeof(buf) / sizeof(*buf) - 1;
716 nr = parseint(fp, buf, width, base, flags, locale);
719 if ((flags & SUPPRESS) == 0) {
723 if ((flags & UNSIGNED) == 0)
724 res = wcstoimax(buf, NULL, base);
726 res = wcstoumax(buf, NULL, base);
728 *va_arg(ap, void **) =
729 (void *)(uintptr_t)res;
730 else if (flags & SHORTSHORT)
731 *va_arg(ap, char *) = res;
732 else if (flags & SHORT)
733 *va_arg(ap, short *) = res;
734 else if (flags & LONG)
735 *va_arg(ap, long *) = res;
736 else if (flags & LONGLONG)
737 *va_arg(ap, long long *) = res;
738 else if (flags & INTMAXT)
739 *va_arg(ap, intmax_t *) = res;
740 else if (flags & PTRDIFFT)
741 *va_arg(ap, ptrdiff_t *) = res;
742 else if (flags & SIZET)
743 *va_arg(ap, size_t *) = res;
745 *va_arg(ap, int *) = res;
749 #ifndef NO_FLOATING_POINT
751 /* scan a floating point number as if by strtod */
752 if (width == 0 || width > sizeof(buf) /
754 width = sizeof(buf) / sizeof(*buf) - 1;
755 nr = parsefloat(fp, buf, buf + width, locale);
758 if ((flags & SUPPRESS) == 0) {
759 if (flags & LONGDBL) {
760 long double res = wcstold(buf, NULL);
761 *va_arg(ap, long double *) = res;
762 } else if (flags & LONG) {
763 double res = wcstod(buf, NULL);
764 *va_arg(ap, double *) = res;
766 float res = wcstof(buf, NULL);
767 *va_arg(ap, float *) = res;
771 #endif /* !NO_FLOATING_POINT */
773 if (!(flags & SUPPRESS))
779 return (nconversions != 0 ? nassigned : EOF);
784 #ifndef NO_FLOATING_POINT
786 parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t locale)
793 S_START, S_GOTSIGN, S_INF, S_NAN, S_DONE, S_MAYBEHEX,
794 S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
798 _Bool gotmantdig = 0, ishex = 0;
801 nconv = mbrtowc(&decpt, localeconv()->decimal_point, MB_CUR_MAX, &mbs);
802 if (nconv == (size_t)-1 || nconv == (size_t)-2)
803 decpt = '.'; /* failsafe */
806 * We set commit = p whenever the string we have read so far
807 * constitutes a valid representation of a floating point
808 * number by itself. At some point, the parse will complete
809 * or fail, and we will ungetc() back to the last commit point.
810 * To ensure that the file offset gets updated properly, it is
811 * always necessary to read at least one character that doesn't
812 * match; thus, we can't short-circuit "infinity" or "nan(...)".
816 for (p = buf; p < end; ) {
817 if ((c = __fgetwc(fp, locale)) == WEOF)
823 if (c == '-' || c == '+')
848 (c != "nfinity"[infnanpos] &&
849 c != "NFINITY"[infnanpos]))
851 if (infnanpos == 1 || infnanpos == 6)
852 commit = p; /* inf or infinity */
858 if (c != 'A' && c != 'a')
862 if (c != 'N' && c != 'n')
875 } else if (!iswalnum(c) && c != '_')
885 if (c == 'X' || c == 'x') {
888 } else { /* we saw a '0', but no 'x' */
893 if ((ishex && iswxdigit(c)) || iswdigit(c))
904 if (((c == 'E' || c == 'e') && !ishex) ||
905 ((c == 'P' || c == 'p') && ishex)) {
910 } else if ((ishex && iswxdigit(c)) || iswdigit(c)) {
918 if (c == '-' || c == '+')
937 __ungetwc(c, fp, locale);
939 __ungetwc(*p, fp, locale);
941 return (commit - buf);