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
32 * @(#)vfscanf.c 8.1 (Berkeley) 6/4/93
33 * $FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.17 2009/01/19 06:19:51 das Exp $
34 * $DragonFly: src/lib/libc/stdio/vfwscanf.c,v 1.1 2005/07/25 00:37:41 joerg Exp $
37 #include "namespace.h"
48 #include "un-namespace.h"
50 #include "libc_private.h"
52 #include "priv_stdio.h"
54 #ifndef NO_FLOATING_POINT
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 *);
100 (cclcompl ? (wmemchr(ccls, (_c), ccle - ccls) == NULL) : \
101 (wmemchr(ccls, (_c), ccle - ccls) != NULL))
103 static const mbstate_t initial_mbs
;
109 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
115 ret
= __vfwscanf(fp
, fmt
, ap
);
121 * Non-MT-safe version.
124 __vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
126 wint_t c
; /* character from format, or conversion */
127 size_t width
; /* field width, or 0 */
128 wchar_t *p
; /* points into all kinds of strings */
129 int n
; /* handy integer */
130 int flags
; /* flags as defined above */
131 wchar_t *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 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
137 const wchar_t *ccls
; /* character class start */
138 const wchar_t *ccle
; /* character class end */
139 int cclcompl
; /* ccl is complemented? */
140 wint_t wi
; /* handy wint_t */
141 char *mbp
; /* multibyte string pointer for %c %s %[ */
142 size_t nconv
; /* number of bytes in mb. conversion */
143 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
146 /* `basefix' is used to avoid `if' tests in the integer scanner */
147 static short basefix
[17] =
148 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
159 while ((c
= __fgetwc_unlock(fp
)) != WEOF
&&
171 * switch on the format. continue if done;
172 * break once format type is derived.
178 if ((wi
= __fgetwc_unlock(fp
)) == WEOF
)
201 flags
|= LONGLONG
; /* not quite */
220 case '0': case '1': case '2': case '3': case '4':
221 case '5': case '6': case '7': case '8': case '9':
222 width
= width
* 10 + c
- '0';
252 flags
|= PFXOK
; /* enable 0x prefixing */
258 #ifndef NO_FLOATING_POINT
259 case 'A': case 'E': case 'F': case 'G':
260 case 'a': case 'e': case 'f': case 'g':
281 while (*fmt
!= '\0' && *fmt
!= ']')
297 case 'p': /* pointer format is like hex */
298 flags
|= POINTER
| PFXOK
;
299 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
300 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
306 if (flags
& SUPPRESS
) /* ??? */
308 if (flags
& SHORTSHORT
)
309 *va_arg(ap
, char *) = nread
;
310 else if (flags
& SHORT
)
311 *va_arg(ap
, short *) = nread
;
312 else if (flags
& LONG
)
313 *va_arg(ap
, long *) = nread
;
314 else if (flags
& LONGLONG
)
315 *va_arg(ap
, long long *) = nread
;
316 else if (flags
& INTMAXT
)
317 *va_arg(ap
, intmax_t *) = nread
;
318 else if (flags
& SIZET
)
319 *va_arg(ap
, size_t *) = nread
;
320 else if (flags
& PTRDIFFT
)
321 *va_arg(ap
, ptrdiff_t *) = nread
;
323 *va_arg(ap
, int *) = nread
;
330 * Disgusting backwards compatibility hack. XXX
332 case '\0': /* compat */
337 * Consume leading white space, except for formats
338 * that suppress this.
340 if ((flags
& NOSKIP
) == 0) {
341 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&& iswspace(wi
))
354 /* scan arbitrary characters (sets NOSKIP) */
358 if (!(flags
& SUPPRESS
))
359 p
= va_arg(ap
, wchar_t *);
361 while (width
-- != 0 &&
362 (wi
= __fgetwc_unlock(fp
)) != WEOF
) {
363 if (!(flags
& SUPPRESS
))
370 if (!(flags
& SUPPRESS
))
373 if (!(flags
& SUPPRESS
))
374 mbp
= va_arg(ap
, char *);
378 (wi
= __fgetwc_unlock(fp
)) != WEOF
) {
379 if (width
>= MB_CUR_MAX
&&
380 !(flags
& SUPPRESS
)) {
381 nconv
= wcrtomb(mbp
, wi
, &mbs
);
382 if (nconv
== (size_t)-1)
385 nconv
= wcrtomb(mbbuf
, wi
,
387 if (nconv
== (size_t)-1)
393 if (!(flags
& SUPPRESS
))
397 if (!(flags
& SUPPRESS
))
405 if (!(flags
& SUPPRESS
))
412 /* scan a (nonempty) character class (sets NOSKIP) */
414 width
= (size_t)~0; /* `infinity' */
415 /* take only those things in the class */
416 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
418 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
419 width
-- != 0 && INCCL(wi
))
425 } else if (flags
& LONG
) {
426 p0
= p
= va_arg(ap
, wchar_t *);
427 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
428 width
-- != 0 && INCCL(wi
))
438 if (!(flags
& SUPPRESS
))
439 mbp
= va_arg(ap
, char *);
442 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
443 width
!= 0 && INCCL(wi
)) {
444 if (width
>= MB_CUR_MAX
&&
445 !(flags
& SUPPRESS
)) {
446 nconv
= wcrtomb(mbp
, wi
, &mbs
);
447 if (nconv
== (size_t)-1)
450 nconv
= wcrtomb(mbbuf
, wi
,
452 if (nconv
== (size_t)-1)
456 if (!(flags
& SUPPRESS
))
460 if (!(flags
& SUPPRESS
))
467 if (!(flags
& SUPPRESS
)) {
477 /* like CCL, but zero-length string OK, & no NOSKIP */
480 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
481 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
487 } else if (flags
& LONG
) {
488 p0
= p
= va_arg(ap
, wchar_t *);
489 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
500 if (!(flags
& SUPPRESS
))
501 mbp
= va_arg(ap
, char *);
503 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
506 if (width
>= MB_CUR_MAX
&&
507 !(flags
& SUPPRESS
)) {
508 nconv
= wcrtomb(mbp
, wi
, &mbs
);
509 if (nconv
== (size_t)-1)
512 nconv
= wcrtomb(mbbuf
, wi
,
514 if (nconv
== (size_t)-1)
518 if (!(flags
& SUPPRESS
))
522 if (!(flags
& SUPPRESS
))
529 if (!(flags
& SUPPRESS
)) {
538 /* scan an integer as if by the conversion function */
539 if (width
== 0 || width
> sizeof(buf
) /
541 width
= sizeof(buf
) / sizeof(*buf
) - 1;
542 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
543 for (p
= buf
; width
; width
--) {
544 c
= __fgetwc_unlock(fp
);
546 * Switch on the character; `goto ok'
547 * if we accept it as a part of number.
552 * The digit 0 is always legal, but is
553 * special. For %i conversions, if no
554 * digits (zero or nonzero) have been
555 * scanned (only signs), we will have
556 * base==0. In that case, we should set
557 * it to 8 and enable 0x prefixing.
558 * Also, if we have not scanned zero digits
559 * before this, do not turn off prefixing
560 * (someone else will turn it off if we
561 * have scanned any nonzero digits).
568 if (flags
& NZDIGITS
)
569 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
571 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
574 /* 1 through 7 always legal */
575 case '1': case '2': case '3':
576 case '4': case '5': case '6': case '7':
577 base
= basefix
[base
];
578 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
581 /* digits 8 and 9 ok iff decimal or hex */
583 base
= basefix
[base
];
585 break; /* not legal here */
586 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
589 /* letters ok iff hex */
590 case 'A': case 'B': case 'C':
591 case 'D': case 'E': case 'F':
592 case 'a': case 'b': case 'c':
593 case 'd': case 'e': case 'f':
594 /* no need to fix base here */
596 break; /* not legal here */
597 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
600 /* sign ok only as first character */
602 if (flags
& SIGNOK
) {
610 * x ok iff flag still set & 2nd char (or
611 * 3rd char if we have a sign).
614 if (flags
& PFXOK
&& p
==
615 buf
+ 1 + !!(flags
& HAVESIGN
)) {
616 base
= 16; /* if %i */
624 * If we got here, c is not a legal character
625 * for a number. Stop accumulating digits.
632 * c is legal: store it and look at the next.
637 * If we had only a sign, it is no good; push
638 * back the sign. If the number ends in `x',
639 * it was [sign] '0' 'x', so push back the x
640 * and treat it as [sign] '0'.
642 if (flags
& NDIGITS
) {
648 if (c
== 'x' || c
== 'X') {
652 if ((flags
& SUPPRESS
) == 0) {
656 if ((flags
& UNSIGNED
) == 0)
657 res
= wcstoimax(buf
, NULL
, base
);
659 res
= wcstoumax(buf
, NULL
, base
);
661 *va_arg(ap
, void **) =
662 (void *)(uintptr_t)res
;
663 else if (flags
& SHORTSHORT
)
664 *va_arg(ap
, char *) = res
;
665 else if (flags
& SHORT
)
666 *va_arg(ap
, short *) = res
;
667 else if (flags
& LONG
)
668 *va_arg(ap
, long *) = res
;
669 else if (flags
& LONGLONG
)
670 *va_arg(ap
, long long *) = res
;
671 else if (flags
& INTMAXT
)
672 *va_arg(ap
, intmax_t *) = res
;
673 else if (flags
& PTRDIFFT
)
674 *va_arg(ap
, ptrdiff_t *) = res
;
675 else if (flags
& SIZET
)
676 *va_arg(ap
, size_t *) = res
;
678 *va_arg(ap
, int *) = res
;
685 #ifndef NO_FLOATING_POINT
687 /* scan a floating point number as if by strtod */
688 if (width
== 0 || width
> sizeof(buf
) /
690 width
= sizeof(buf
) / sizeof(*buf
) - 1;
691 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
693 if ((flags
& SUPPRESS
) == 0) {
694 if (flags
& LONGDBL
) {
695 long double res
= wcstold(buf
, &p
);
696 *va_arg(ap
, long double *) = res
;
697 } else if (flags
& LONG
) {
698 double res
= wcstod(buf
, &p
);
699 *va_arg(ap
, double *) = res
;
701 float res
= wcstof(buf
, &p
);
702 *va_arg(ap
, float *) = res
;
709 #endif /* !NO_FLOATING_POINT */
713 return (nconversions
!= 0 ? nassigned
: EOF
);
718 #ifndef NO_FLOATING_POINT
720 parsefloat(FILE *fp
, wchar_t *buf
, wchar_t *end
)
727 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_DONE
, S_MAYBEHEX
,
728 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
732 _Bool gotmantdig
= 0, ishex
= 0;
735 nconv
= mbrtowc(&decpt
, localeconv()->decimal_point
, MB_CUR_MAX
, &mbs
);
736 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
737 decpt
= '.'; /* failsafe */
740 * We set commit = p whenever the string we have read so far
741 * constitutes a valid representation of a floating point
742 * number by itself. At some point, the parse will complete
743 * or fail, and we will ungetc() back to the last commit point.
744 * To ensure that the file offset gets updated properly, it is
745 * always necessary to read at least one character that doesn't
746 * match; thus, we can't short-circuit "infinity" or "nan(...)".
750 for (p
= buf
; p
< end
; ) {
751 if ((c
= __fgetwc_unlock(fp
)) == WEOF
)
757 if (c
== '-' || c
== '+')
782 (c
!= "nfinity"[infnanpos
] &&
783 c
!= "NFINITY"[infnanpos
]))
785 if (infnanpos
== 1 || infnanpos
== 6)
786 commit
= p
; /* inf or infinity */
792 if (c
!= 'A' && c
!= 'a')
796 if (c
!= 'N' && c
!= 'n')
809 } else if (!iswalnum(c
) && c
!= '_')
819 if (c
== 'X' || c
== 'x') {
822 } else { /* we saw a '0', but no 'x' */
827 if ((ishex
&& iswxdigit(c
)) || iswdigit(c
))
838 if (((c
== 'E' || c
== 'e') && !ishex
) ||
839 ((c
== 'P' || c
== 'p') && ishex
)) {
844 } else if ((ishex
&& iswxdigit(c
)) || iswdigit(c
)) {
852 if (c
== '-' || c
== '+')
875 return (commit
- buf
);