1 /* $NetBSD: vfwscanf.c,v 1.8 2012/03/15 18:22:30 christos Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
40 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid
[] = "@(#)ftell.c 8.2 (Berkeley) 5/4/95";
43 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
45 __RCSID("$NetBSD: vfwscanf.c,v 1.8 2012/03/15 18:22:30 christos Exp $");
47 #endif /* LIBC_SCCS and not lint */
49 #include "namespace.h"
62 #include "reentrant.h"
65 #ifndef NO_FLOATING_POINT
69 #define BUF 513 /* Maximum length of numeric string. */
72 * Flags used during conversion.
74 #define LONG 0x01 /* l: long or double */
75 #define LONGDBL 0x02 /* L: long double */
76 #define SHORT 0x04 /* h: short */
77 #define SUPPRESS 0x08 /* *: suppress assignment */
78 #define POINTER 0x10 /* p: void * (as hex) */
79 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
80 #define LONGLONG 0x400 /* ll: quad_t (+ deprecated q: quad) */
81 #define INTMAXT 0x800 /* j: intmax_t */
82 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
83 #define SIZET 0x2000 /* z: size_t */
84 #define SHORTSHORT 0x4000 /* hh: char */
85 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
88 * The following are used in integral conversions only:
89 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
91 #define SIGNOK 0x40 /* +/- is (still) legal */
92 #define NDIGITS 0x80 /* no digits detected */
93 #define PFXOK 0x100 /* 0x prefix is (still) legal */
94 #define NZDIGITS 0x200 /* no zero digits detected */
95 #define HAVESIGN 0x10000 /* sign detected */
100 #define CT_CHAR 0 /* %c conversion */
101 #define CT_CCL 1 /* %[...] conversion */
102 #define CT_STRING 2 /* %s conversion */
103 #define CT_INT 3 /* %[dioupxX] conversion */
104 #define CT_FLOAT 4 /* %[efgEFG] conversion */
106 static int parsefloat(FILE *, wchar_t *, wchar_t *);
109 (cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
110 (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
116 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
121 _SET_ORIENTATION(fp
, 1);
122 ret
= __vfwscanf_unlocked(fp
, fmt
, ap
);
127 #define SCANF_SKIP_SPACE() \
131 while ((tc = __fgetwc_unlock(fp)) != WEOF && iswspace(tc)) \
135 } while (/*CONSTCOND*/ 0)
138 * Non-MT-safe version.
141 __vfwscanf_unlocked(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
143 wint_t c
; /* character from format, or conversion */
144 size_t width
; /* field width, or 0 */
145 wchar_t *p
; /* points into all kinds of strings */
146 int n
; /* handy integer */
147 int flags
; /* flags as defined above */
148 wchar_t *p0
; /* saves original value of p when necessary */
149 int nassigned
; /* number of fields assigned */
150 int nconversions
; /* number of conversions */
151 size_t nread
; /* number of characters consumed from fp */
152 int base
; /* base argument to conversion function */
153 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
154 const wchar_t *ccls
; /* character class start */
155 const wchar_t *ccle
; /* character class end */
156 int cclcompl
; /* ccl is complemented? */
157 wint_t wi
; /* handy wint_t */
158 char *mbp
; /* multibyte string pointer for %c %s %[ */
159 size_t nconv
; /* number of bytes in mb. conversion */
160 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
161 static const mbstate_t initial
;
164 /* `basefix' is used to avoid `if' tests in the integer scanner */
165 static short basefix
[17] =
166 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
180 while ((c
= __fgetwc_unlock(fp
)) != WEOF
&&
192 * switch on the format. continue if done;
193 * break once format type is derived.
200 if ((wi
= __fgetwc_unlock(fp
)) == WEOF
)
223 flags
|= LONGLONG
; /* not quite */
242 case '0': case '1': case '2': case '3': case '4':
243 case '5': case '6': case '7': case '8': case '9':
244 width
= width
* 10 + c
- '0';
274 flags
|= PFXOK
; /* enable 0x prefixing */
280 #ifndef NO_FLOATING_POINT
281 case 'A': case 'E': case 'F': case 'G':
282 case 'a': case 'e': case 'f': case 'g':
303 while (*fmt
!= '\0' && *fmt
!= ']')
319 case 'p': /* pointer format is like hex */
320 flags
|= POINTER
| PFXOK
;
321 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
322 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
328 if (flags
& SUPPRESS
) /* ??? */
330 if (flags
& SHORTSHORT
)
331 *va_arg(ap
, char *) = (char)nread
;
332 else if (flags
& SHORT
)
333 *va_arg(ap
, short *) = (short)nread
;
334 else if (flags
& LONG
)
335 *va_arg(ap
, long *) = nread
;
336 else if (flags
& LONGLONG
)
337 *va_arg(ap
, quad_t
*) = nread
;
338 else if (flags
& INTMAXT
)
339 *va_arg(ap
, intmax_t *) = nread
;
340 else if (flags
& SIZET
)
341 *va_arg(ap
, size_t *) = nread
;
342 else if (flags
& PTRDIFFT
)
343 *va_arg(ap
, ptrdiff_t *) = nread
;
345 *va_arg(ap
, int *) = (int)nread
;
352 * Disgusting backwards compatibility hack. XXX
354 case '\0': /* compat */
359 * Consume leading white space, except for formats
360 * that suppress this.
362 if ((flags
& NOSKIP
) == 0) {
363 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&& iswspace(wi
))
376 /* scan arbitrary characters (sets NOSKIP) */
380 if (!(flags
& SUPPRESS
))
381 p
= va_arg(ap
, wchar_t *);
383 while (width
-- != 0 &&
384 (wi
= __fgetwc_unlock(fp
)) != WEOF
) {
385 if (!(flags
& SUPPRESS
))
392 if (!(flags
& SUPPRESS
))
395 if (!(flags
& SUPPRESS
))
396 mbp
= va_arg(ap
, char *);
400 (wi
= __fgetwc_unlock(fp
)) != WEOF
) {
401 if (width
>= MB_CUR_MAX
&&
402 !(flags
& SUPPRESS
)) {
403 nconv
= wcrtomb(mbp
, wi
, &mbs
);
404 if (nconv
== (size_t)-1)
407 nconv
= wcrtomb(mbbuf
, wi
,
409 if (nconv
== (size_t)-1)
415 if (!(flags
& SUPPRESS
))
419 if (!(flags
& SUPPRESS
))
427 if (!(flags
& SUPPRESS
))
434 /* scan a (nonempty) character class (sets NOSKIP) */
436 width
= (size_t)~0; /* `infinity' */
437 /* take only those things in the class */
438 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
440 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
441 width
-- != 0 && INCCL(wi
))
447 } else if (flags
& LONG
) {
448 p0
= p
= va_arg(ap
, wchar_t *);
449 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
450 width
-- != 0 && INCCL(wi
))
454 _DIAGASSERT(__type_fit(int, p
- p0
));
461 if (!(flags
& SUPPRESS
))
462 mbp
= va_arg(ap
, char *);
465 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
466 width
!= 0 && INCCL(wi
)) {
467 if (width
>= MB_CUR_MAX
&&
468 !(flags
& SUPPRESS
)) {
469 nconv
= wcrtomb(mbp
, wi
, &mbs
);
470 if (nconv
== (size_t)-1)
473 nconv
= wcrtomb(mbbuf
, wi
,
475 if (nconv
== (size_t)-1)
479 if (!(flags
& SUPPRESS
))
483 if (!(flags
& SUPPRESS
))
490 if (!(flags
& SUPPRESS
)) {
500 /* like CCL, but zero-length string OK, & no NOSKIP */
503 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
504 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
510 } else if (flags
& LONG
) {
511 p0
= p
= va_arg(ap
, wchar_t *);
512 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
523 if (!(flags
& SUPPRESS
))
524 mbp
= va_arg(ap
, char *);
526 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
529 if (width
>= MB_CUR_MAX
&&
530 !(flags
& SUPPRESS
)) {
531 nconv
= wcrtomb(mbp
, wi
, &mbs
);
532 if (nconv
== (size_t)-1)
535 nconv
= wcrtomb(mbbuf
, wi
,
537 if (nconv
== (size_t)-1)
541 if (!(flags
& SUPPRESS
))
545 if (!(flags
& SUPPRESS
))
552 if (!(flags
& SUPPRESS
)) {
561 /* scan an integer as if by the conversion function */
562 if (width
== 0 || width
> sizeof(buf
) /
564 width
= sizeof(buf
) / sizeof(*buf
) - 1;
565 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
566 for (p
= buf
; width
; width
--) {
567 c
= __fgetwc_unlock(fp
);
569 * Switch on the character; `goto ok'
570 * if we accept it as a part of number.
575 * The digit 0 is always legal, but is
576 * special. For %i conversions, if no
577 * digits (zero or nonzero) have been
578 * scanned (only signs), we will have
579 * base==0. In that case, we should set
580 * it to 8 and enable 0x prefixing.
581 * Also, if we have not scanned zero digits
582 * before this, do not turn off prefixing
583 * (someone else will turn it off if we
584 * have scanned any nonzero digits).
591 if (flags
& NZDIGITS
)
592 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
594 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
597 /* 1 through 7 always legal */
598 case '1': case '2': case '3':
599 case '4': case '5': case '6': case '7':
600 base
= basefix
[base
];
601 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
604 /* digits 8 and 9 ok iff decimal or hex */
606 base
= basefix
[base
];
608 break; /* not legal here */
609 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
612 /* letters ok iff hex */
613 case 'A': case 'B': case 'C':
614 case 'D': case 'E': case 'F':
615 case 'a': case 'b': case 'c':
616 case 'd': case 'e': case 'f':
617 /* no need to fix base here */
619 break; /* not legal here */
620 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
623 /* sign ok only as first character */
625 if (flags
& SIGNOK
) {
633 * x ok iff flag still set & 2nd char (or
634 * 3rd char if we have a sign).
637 if (flags
& PFXOK
&& p
==
638 buf
+ 1 + !!(flags
& HAVESIGN
)) {
639 base
= 16; /* if %i */
647 * If we got here, c is not a legal character
648 * for a number. Stop accumulating digits.
655 * c is legal: store it and look at the next.
660 * If we had only a sign, it is no good; push
661 * back the sign. If the number ends in `x',
662 * it was [sign] '0' 'x', so push back the x
663 * and treat it as [sign] '0'.
665 if (flags
& NDIGITS
) {
671 if (c
== 'x' || c
== 'X') {
675 if ((flags
& SUPPRESS
) == 0) {
679 if ((flags
& UNSIGNED
) == 0)
680 res
= wcstoimax(buf
, NULL
, base
);
682 res
= wcstoumax(buf
, NULL
, base
);
684 *va_arg(ap
, void **) =
685 (void *)(uintptr_t)res
;
686 else if (flags
& SHORTSHORT
)
687 *va_arg(ap
, char *) = (char)res
;
688 else if (flags
& SHORT
)
689 *va_arg(ap
, short *) = (short)res
;
690 else if (flags
& LONG
)
691 *va_arg(ap
, long *) = (long)res
;
692 else if (flags
& LONGLONG
)
693 *va_arg(ap
, quad_t
*) = res
;
694 else if (flags
& INTMAXT
)
695 *va_arg(ap
, intmax_t *) = res
;
696 else if (flags
& PTRDIFFT
)
697 *va_arg(ap
, ptrdiff_t *) = (ptrdiff_t)res
;
698 else if (flags
& SIZET
)
699 *va_arg(ap
, size_t *) = (size_t)res
;
701 *va_arg(ap
, int *) = (int)res
;
704 _DIAGASSERT(__type_fit(int, p
- buf
));
705 nread
+= (int)(p
- buf
);
709 #ifndef NO_FLOATING_POINT
711 /* scan a floating point number as if by strtod */
712 if (width
== 0 || width
> sizeof(buf
) /
714 width
= sizeof(buf
) / sizeof(*buf
) - 1;
715 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
717 if ((flags
& SUPPRESS
) == 0) {
718 if (flags
& LONGDBL
) {
719 long double res
= wcstold(buf
, &p
);
720 *va_arg(ap
, long double *) = res
;
723 double res
= wcstod(buf
, &p
);
724 *va_arg(ap
, double *) = res
;
726 float res
= wcstof(buf
, &p
);
727 *va_arg(ap
, float *) = res
;
730 if (p
- buf
!= (ptrdiff_t)width
)
738 #endif /* !NO_FLOATING_POINT */
742 return nconversions
!= 0 ? nassigned
: EOF
;
747 #ifndef NO_FLOATING_POINT
749 parsefloat(FILE *fp
, wchar_t *buf
, wchar_t *end
)
754 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
755 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
758 wchar_t decpt
= (wchar_t)(unsigned char)*localeconv()->decimal_point
;
759 int gotmantdig
= 0, ishex
= 0;
762 * We set commit = p whenever the string we have read so far
763 * constitutes a valid representation of a floating point
764 * number by itself. At some point, the parse will complete
765 * or fail, and we will ungetc() back to the last commit point.
766 * To ensure that the file offset gets updated properly, it is
767 * always necessary to read at least one character that doesn't
768 * match; thus, we can't short-circuit "infinity" or "nan(...)".
772 for (p
= buf
; p
< end
; ) {
773 if ((c
= __fgetwc_unlock(fp
)) == WEOF
)
779 if (c
== '-' || c
== '+')
804 (c
!= "nfinity"[infnanpos
] &&
805 c
!= "NFINITY"[infnanpos
]))
807 if (infnanpos
== 1 || infnanpos
== 6)
808 commit
= p
; /* inf or infinity */
813 case -1: /* XXX kludge to deal with nan(...) */
816 if (c
!= 'A' && c
!= 'a')
820 if (c
!= 'N' && c
!= 'n')
833 } else if (!iswalnum(c
) && c
!= '_')
841 if (c
== 'X' || c
== 'x') {
844 } else { /* we saw a '0', but no 'x' */
849 if ((ishex
&& iswxdigit(c
)) || iswdigit(c
))
860 if (((c
== 'E' || c
== 'e') && !ishex
) ||
861 ((c
== 'P' || c
== 'p') && ishex
)) {
866 } else if ((ishex
&& iswxdigit(c
)) || iswdigit(c
)) {
874 if (c
== '-' || c
== '+')
897 _DIAGASSERT(__type_fit(int, commit
- buf
));
898 return (int)(commit
- buf
);