1 /* $NetBSD: vfwscanf.c,v 1.5 2009/01/11 02:46:29 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.5 2009/01/11 02:46:29 christos Exp $");
47 #endif /* LIBC_SCCS and not lint */
49 #include "namespace.h"
61 #include "reentrant.h"
64 #ifndef NO_FLOATING_POINT
68 #define BUF 513 /* Maximum length of numeric string. */
71 * Flags used during conversion.
73 #define LONG 0x01 /* l: long or double */
74 #define LONGDBL 0x02 /* L: long double */
75 #define SHORT 0x04 /* h: short */
76 #define SUPPRESS 0x08 /* *: suppress assignment */
77 #define POINTER 0x10 /* p: void * (as hex) */
78 #define NOSKIP 0x20 /* [ or c: do not skip blanks */
79 #define LONGLONG 0x400 /* ll: quad_t (+ deprecated q: quad) */
80 #define INTMAXT 0x800 /* j: intmax_t */
81 #define PTRDIFFT 0x1000 /* t: ptrdiff_t */
82 #define SIZET 0x2000 /* z: size_t */
83 #define SHORTSHORT 0x4000 /* hh: char */
84 #define UNSIGNED 0x8000 /* %[oupxX] conversions */
87 * The following are used in integral conversions only:
88 * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
90 #define SIGNOK 0x40 /* +/- is (still) legal */
91 #define NDIGITS 0x80 /* no digits detected */
92 #define PFXOK 0x100 /* 0x prefix is (still) legal */
93 #define NZDIGITS 0x200 /* no zero digits detected */
94 #define HAVESIGN 0x10000 /* sign detected */
99 #define CT_CHAR 0 /* %c conversion */
100 #define CT_CCL 1 /* %[...] conversion */
101 #define CT_STRING 2 /* %s conversion */
102 #define CT_INT 3 /* %[dioupxX] conversion */
103 #define CT_FLOAT 4 /* %[efgEFG] conversion */
105 static int parsefloat(FILE *, wchar_t *, wchar_t *);
108 (cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
109 (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
115 vfwscanf(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
120 _SET_ORIENTATION(fp
, 1);
121 ret
= __vfwscanf_unlocked(fp
, fmt
, ap
);
126 #define SCANF_SKIP_SPACE() \
130 while ((tc = __fgetwc_unlock(fp)) != WEOF && iswspace(tc)) \
134 } while (/*CONSTCOND*/ 0)
137 * Non-MT-safe version.
140 __vfwscanf_unlocked(FILE * __restrict fp
, const wchar_t * __restrict fmt
, va_list ap
)
142 wint_t c
; /* character from format, or conversion */
143 size_t width
; /* field width, or 0 */
144 wchar_t *p
; /* points into all kinds of strings */
145 int n
; /* handy integer */
146 int flags
; /* flags as defined above */
147 wchar_t *p0
; /* saves original value of p when necessary */
148 int nassigned
; /* number of fields assigned */
149 int nconversions
; /* number of conversions */
150 int nread
; /* number of characters consumed from fp */
151 int base
; /* base argument to conversion function */
152 wchar_t buf
[BUF
]; /* buffer for numeric conversions */
153 const wchar_t *ccls
; /* character class start */
154 const wchar_t *ccle
; /* character class end */
155 int cclcompl
; /* ccl is complemented? */
156 wint_t wi
; /* handy wint_t */
157 char *mbp
; /* multibyte string pointer for %c %s %[ */
158 size_t nconv
; /* number of bytes in mb. conversion */
159 char mbbuf
[MB_LEN_MAX
]; /* temporary mb. character buffer */
160 static const mbstate_t initial
;
163 /* `basefix' is used to avoid `if' tests in the integer scanner */
164 static short basefix
[17] =
165 { 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
179 while ((c
= __fgetwc_unlock(fp
)) != WEOF
&&
191 * switch on the format. continue if done;
192 * break once format type is derived.
199 if ((wi
= __fgetwc_unlock(fp
)) == WEOF
)
222 flags
|= LONGLONG
; /* not quite */
241 case '0': case '1': case '2': case '3': case '4':
242 case '5': case '6': case '7': case '8': case '9':
243 width
= width
* 10 + c
- '0';
273 flags
|= PFXOK
; /* enable 0x prefixing */
279 #ifndef NO_FLOATING_POINT
280 case 'A': case 'E': case 'F': case 'G':
281 case 'a': case 'e': case 'f': case 'g':
302 while (*fmt
!= '\0' && *fmt
!= ']')
318 case 'p': /* pointer format is like hex */
319 flags
|= POINTER
| PFXOK
;
320 c
= CT_INT
; /* assumes sizeof(uintmax_t) */
321 flags
|= UNSIGNED
; /* >= sizeof(uintptr_t) */
327 if (flags
& SUPPRESS
) /* ??? */
329 if (flags
& SHORTSHORT
)
330 *va_arg(ap
, char *) = nread
;
331 else if (flags
& SHORT
)
332 *va_arg(ap
, short *) = nread
;
333 else if (flags
& LONG
)
334 *va_arg(ap
, long *) = nread
;
335 else if (flags
& LONGLONG
)
336 *va_arg(ap
, quad_t
*) = nread
;
337 else if (flags
& INTMAXT
)
338 *va_arg(ap
, intmax_t *) = nread
;
339 else if (flags
& SIZET
)
340 *va_arg(ap
, size_t *) = nread
;
341 else if (flags
& PTRDIFFT
)
342 *va_arg(ap
, ptrdiff_t *) = nread
;
344 *va_arg(ap
, int *) = nread
;
351 * Disgusting backwards compatibility hack. XXX
353 case '\0': /* compat */
358 * Consume leading white space, except for formats
359 * that suppress this.
361 if ((flags
& NOSKIP
) == 0) {
362 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&& iswspace(wi
))
375 /* scan arbitrary characters (sets NOSKIP) */
379 if (!(flags
& SUPPRESS
))
380 p
= va_arg(ap
, wchar_t *);
382 while (width
-- != 0 &&
383 (wi
= __fgetwc_unlock(fp
)) != WEOF
) {
384 if (!(flags
& SUPPRESS
))
391 if (!(flags
& SUPPRESS
))
394 if (!(flags
& SUPPRESS
))
395 mbp
= va_arg(ap
, char *);
399 (wi
= __fgetwc_unlock(fp
)) != WEOF
) {
400 if (width
>= MB_CUR_MAX
&&
401 !(flags
& SUPPRESS
)) {
402 nconv
= wcrtomb(mbp
, wi
, &mbs
);
403 if (nconv
== (size_t)-1)
406 nconv
= wcrtomb(mbbuf
, wi
,
408 if (nconv
== (size_t)-1)
414 if (!(flags
& SUPPRESS
))
418 if (!(flags
& SUPPRESS
))
426 if (!(flags
& SUPPRESS
))
433 /* scan a (nonempty) character class (sets NOSKIP) */
435 width
= (size_t)~0; /* `infinity' */
436 /* take only those things in the class */
437 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
439 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
440 width
-- != 0 && INCCL(wi
))
446 } else if (flags
& LONG
) {
447 p0
= p
= va_arg(ap
, wchar_t *);
448 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
449 width
-- != 0 && INCCL(wi
))
459 if (!(flags
& SUPPRESS
))
460 mbp
= va_arg(ap
, char *);
463 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
464 width
!= 0 && INCCL(wi
)) {
465 if (width
>= MB_CUR_MAX
&&
466 !(flags
& SUPPRESS
)) {
467 nconv
= wcrtomb(mbp
, wi
, &mbs
);
468 if (nconv
== (size_t)-1)
471 nconv
= wcrtomb(mbbuf
, wi
,
473 if (nconv
== (size_t)-1)
477 if (!(flags
& SUPPRESS
))
481 if (!(flags
& SUPPRESS
))
488 if (!(flags
& SUPPRESS
)) {
498 /* like CCL, but zero-length string OK, & no NOSKIP */
501 if ((flags
& SUPPRESS
) && (flags
& LONG
)) {
502 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
508 } else if (flags
& LONG
) {
509 p0
= p
= va_arg(ap
, wchar_t *);
510 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
521 if (!(flags
& SUPPRESS
))
522 mbp
= va_arg(ap
, char *);
524 while ((wi
= __fgetwc_unlock(fp
)) != WEOF
&&
527 if (width
>= MB_CUR_MAX
&&
528 !(flags
& SUPPRESS
)) {
529 nconv
= wcrtomb(mbp
, wi
, &mbs
);
530 if (nconv
== (size_t)-1)
533 nconv
= wcrtomb(mbbuf
, wi
,
535 if (nconv
== (size_t)-1)
539 if (!(flags
& SUPPRESS
))
543 if (!(flags
& SUPPRESS
))
550 if (!(flags
& SUPPRESS
)) {
559 /* scan an integer as if by the conversion function */
560 if (width
== 0 || width
> sizeof(buf
) /
562 width
= sizeof(buf
) / sizeof(*buf
) - 1;
563 flags
|= SIGNOK
| NDIGITS
| NZDIGITS
;
564 for (p
= buf
; width
; width
--) {
565 c
= __fgetwc_unlock(fp
);
567 * Switch on the character; `goto ok'
568 * if we accept it as a part of number.
573 * The digit 0 is always legal, but is
574 * special. For %i conversions, if no
575 * digits (zero or nonzero) have been
576 * scanned (only signs), we will have
577 * base==0. In that case, we should set
578 * it to 8 and enable 0x prefixing.
579 * Also, if we have not scanned zero digits
580 * before this, do not turn off prefixing
581 * (someone else will turn it off if we
582 * have scanned any nonzero digits).
589 if (flags
& NZDIGITS
)
590 flags
&= ~(SIGNOK
|NZDIGITS
|NDIGITS
);
592 flags
&= ~(SIGNOK
|PFXOK
|NDIGITS
);
595 /* 1 through 7 always legal */
596 case '1': case '2': case '3':
597 case '4': case '5': case '6': case '7':
598 base
= basefix
[base
];
599 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
602 /* digits 8 and 9 ok iff decimal or hex */
604 base
= basefix
[base
];
606 break; /* not legal here */
607 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
610 /* letters ok iff hex */
611 case 'A': case 'B': case 'C':
612 case 'D': case 'E': case 'F':
613 case 'a': case 'b': case 'c':
614 case 'd': case 'e': case 'f':
615 /* no need to fix base here */
617 break; /* not legal here */
618 flags
&= ~(SIGNOK
| PFXOK
| NDIGITS
);
621 /* sign ok only as first character */
623 if (flags
& SIGNOK
) {
631 * x ok iff flag still set & 2nd char (or
632 * 3rd char if we have a sign).
635 if (flags
& PFXOK
&& p
==
636 buf
+ 1 + !!(flags
& HAVESIGN
)) {
637 base
= 16; /* if %i */
645 * If we got here, c is not a legal character
646 * for a number. Stop accumulating digits.
653 * c is legal: store it and look at the next.
658 * If we had only a sign, it is no good; push
659 * back the sign. If the number ends in `x',
660 * it was [sign] '0' 'x', so push back the x
661 * and treat it as [sign] '0'.
663 if (flags
& NDIGITS
) {
669 if (c
== 'x' || c
== 'X') {
673 if ((flags
& SUPPRESS
) == 0) {
677 if ((flags
& UNSIGNED
) == 0)
678 res
= wcstoimax(buf
, NULL
, base
);
680 res
= wcstoumax(buf
, NULL
, base
);
682 *va_arg(ap
, void **) =
683 (void *)(uintptr_t)res
;
684 else if (flags
& SHORTSHORT
)
685 *va_arg(ap
, char *) = (char)res
;
686 else if (flags
& SHORT
)
687 *va_arg(ap
, short *) = (short)res
;
688 else if (flags
& LONG
)
689 *va_arg(ap
, long *) = (long)res
;
690 else if (flags
& LONGLONG
)
691 *va_arg(ap
, quad_t
*) = res
;
692 else if (flags
& INTMAXT
)
693 *va_arg(ap
, intmax_t *) = res
;
694 else if (flags
& PTRDIFFT
)
695 *va_arg(ap
, ptrdiff_t *) = (ptrdiff_t)res
;
696 else if (flags
& SIZET
)
697 *va_arg(ap
, size_t *) = (size_t)res
;
699 *va_arg(ap
, int *) = (int)res
;
706 #ifndef NO_FLOATING_POINT
708 /* scan a floating point number as if by strtod */
709 if (width
== 0 || width
> sizeof(buf
) /
711 width
= sizeof(buf
) / sizeof(*buf
) - 1;
712 if ((width
= parsefloat(fp
, buf
, buf
+ width
)) == 0)
714 if ((flags
& SUPPRESS
) == 0) {
715 if (flags
& LONGDBL
) {
716 long double res
= wcstold(buf
, &p
);
717 *va_arg(ap
, long double *) = res
;
720 double res
= wcstod(buf
, &p
);
721 *va_arg(ap
, double *) = res
;
723 float res
= wcstof(buf
, &p
);
724 *va_arg(ap
, float *) = res
;
727 if (p
- buf
!= (ptrdiff_t)width
)
735 #endif /* !NO_FLOATING_POINT */
739 return (nconversions
!= 0 ? nassigned
: EOF
);
744 #ifndef NO_FLOATING_POINT
746 parsefloat(FILE *fp
, wchar_t *buf
, wchar_t *end
)
751 S_START
, S_GOTSIGN
, S_INF
, S_NAN
, S_MAYBEHEX
,
752 S_DIGITS
, S_FRAC
, S_EXP
, S_EXPDIGITS
755 wchar_t decpt
= (wchar_t)(unsigned char)*localeconv()->decimal_point
;
756 int gotmantdig
= 0, ishex
= 0;
759 * We set commit = p whenever the string we have read so far
760 * constitutes a valid representation of a floating point
761 * number by itself. At some point, the parse will complete
762 * or fail, and we will ungetc() back to the last commit point.
763 * To ensure that the file offset gets updated properly, it is
764 * always necessary to read at least one character that doesn't
765 * match; thus, we can't short-circuit "infinity" or "nan(...)".
769 for (p
= buf
; p
< end
; ) {
770 if ((c
= __fgetwc_unlock(fp
)) == WEOF
)
776 if (c
== '-' || c
== '+')
801 (c
!= "nfinity"[infnanpos
] &&
802 c
!= "NFINITY"[infnanpos
]))
804 if (infnanpos
== 1 || infnanpos
== 6)
805 commit
= p
; /* inf or infinity */
810 case -1: /* XXX kludge to deal with nan(...) */
813 if (c
!= 'A' && c
!= 'a')
817 if (c
!= 'N' && c
!= 'n')
830 } else if (!iswalnum(c
) && c
!= '_')
838 if (c
== 'X' || c
== 'x') {
841 } else { /* we saw a '0', but no 'x' */
846 if ((ishex
&& iswxdigit(c
)) || iswdigit(c
))
857 if (((c
== 'E' || c
== 'e') && !ishex
) ||
858 ((c
== 'P' || c
== 'p') && ishex
)) {
863 } else if ((ishex
&& iswxdigit(c
)) || iswdigit(c
)) {
871 if (c
== '-' || c
== '+')
894 return (commit
- buf
);