xref: /minix3/lib/libc/stdio/vfwscanf.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: vfwscanf.c,v 1.12 2014/06/12 22:21:20 justin Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras  * Copyright (c) 1990, 1993
52fe8fb19SBen Gras  *	The Regents of the University of California.  All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
82fe8fb19SBen Gras  * Chris Torek.
92fe8fb19SBen Gras  *
102fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras  * are met:
132fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. All advertising materials mentioning features or use of this software
192fe8fb19SBen Gras  *    must display the following acknowledgement:
202fe8fb19SBen Gras  *	This product includes software developed by the University of
212fe8fb19SBen Gras  *	California, Berkeley and its contributors.
222fe8fb19SBen Gras  * 4. Neither the name of the University nor the names of its contributors
232fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
242fe8fb19SBen Gras  *    without specific prior written permission.
252fe8fb19SBen Gras  *
262fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
272fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
282fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
292fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
302fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
312fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
322fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
332fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
342fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
352fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
362fe8fb19SBen Gras  * SUCH DAMAGE.
372fe8fb19SBen Gras  */
382fe8fb19SBen Gras 
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)ftell.c	8.2 (Berkeley) 5/4/95";
432fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
442fe8fb19SBen Gras #else
45*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: vfwscanf.c,v 1.12 2014/06/12 22:21:20 justin Exp $");
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
482fe8fb19SBen Gras 
492fe8fb19SBen Gras #include "namespace.h"
502fe8fb19SBen Gras #include <ctype.h>
512fe8fb19SBen Gras #include <inttypes.h>
52f14fb602SLionel Sambuc #include <assert.h>
532fe8fb19SBen Gras #include <stdio.h>
542fe8fb19SBen Gras #include <stdlib.h>
552fe8fb19SBen Gras #include <stddef.h>
562fe8fb19SBen Gras #include <stdarg.h>
572fe8fb19SBen Gras #include <string.h>
582fe8fb19SBen Gras #include <limits.h>
592fe8fb19SBen Gras #include <wchar.h>
602fe8fb19SBen Gras #include <wctype.h>
612fe8fb19SBen Gras 
622fe8fb19SBen Gras #include "reentrant.h"
632fe8fb19SBen Gras #include "local.h"
642fe8fb19SBen Gras 
652fe8fb19SBen Gras #include <locale.h>
6684d9c625SLionel Sambuc #include "setlocale_local.h"
672fe8fb19SBen Gras 
682fe8fb19SBen Gras #define	BUF		513	/* Maximum length of numeric string. */
692fe8fb19SBen Gras 
702fe8fb19SBen Gras /*
712fe8fb19SBen Gras  * Flags used during conversion.
722fe8fb19SBen Gras  */
732fe8fb19SBen Gras #define	LONG		0x01	/* l: long or double */
742fe8fb19SBen Gras #define	LONGDBL		0x02	/* L: long double */
752fe8fb19SBen Gras #define	SHORT		0x04	/* h: short */
762fe8fb19SBen Gras #define	SUPPRESS	0x08	/* *: suppress assignment */
772fe8fb19SBen Gras #define	POINTER		0x10	/* p: void * (as hex) */
782fe8fb19SBen Gras #define	NOSKIP		0x20	/* [ or c: do not skip blanks */
792fe8fb19SBen Gras #define	LONGLONG	0x400	/* ll: quad_t (+ deprecated q: quad) */
802fe8fb19SBen Gras #define	INTMAXT		0x800	/* j: intmax_t */
812fe8fb19SBen Gras #define	PTRDIFFT	0x1000	/* t: ptrdiff_t */
822fe8fb19SBen Gras #define	SIZET		0x2000	/* z: size_t */
832fe8fb19SBen Gras #define	SHORTSHORT	0x4000	/* hh: char */
842fe8fb19SBen Gras #define	UNSIGNED	0x8000	/* %[oupxX] conversions */
852fe8fb19SBen Gras 
862fe8fb19SBen Gras /*
872fe8fb19SBen Gras  * The following are used in integral conversions only:
882fe8fb19SBen Gras  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
892fe8fb19SBen Gras  */
902fe8fb19SBen Gras #define	SIGNOK		0x40	/* +/- is (still) legal */
912fe8fb19SBen Gras #define	NDIGITS		0x80	/* no digits detected */
922fe8fb19SBen Gras #define	PFXOK		0x100	/* 0x prefix is (still) legal */
932fe8fb19SBen Gras #define	NZDIGITS	0x200	/* no zero digits detected */
942fe8fb19SBen Gras #define	HAVESIGN	0x10000	/* sign detected */
952fe8fb19SBen Gras 
962fe8fb19SBen Gras /*
972fe8fb19SBen Gras  * Conversion types.
982fe8fb19SBen Gras  */
992fe8fb19SBen Gras #define	CT_CHAR		0	/* %c conversion */
1002fe8fb19SBen Gras #define	CT_CCL		1	/* %[...] conversion */
1012fe8fb19SBen Gras #define	CT_STRING	2	/* %s conversion */
1022fe8fb19SBen Gras #define	CT_INT		3	/* %[dioupxX] conversion */
1032fe8fb19SBen Gras #define	CT_FLOAT	4	/* %[efgEFG] conversion */
1042fe8fb19SBen Gras 
10584d9c625SLionel Sambuc #ifndef NO_FLOATING_POINT
10684d9c625SLionel Sambuc static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
10784d9c625SLionel Sambuc #endif
1082fe8fb19SBen Gras 
1092fe8fb19SBen Gras #define	INCCL(_c)	\
1102fe8fb19SBen Gras 	(cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
1112fe8fb19SBen Gras 	(wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
1122fe8fb19SBen Gras 
1132fe8fb19SBen Gras /*
1142fe8fb19SBen Gras  * MT-safe version.
1152fe8fb19SBen Gras  */
1162fe8fb19SBen Gras int
vfwscanf(FILE * __restrict fp,const wchar_t * __restrict fmt,va_list ap)1172fe8fb19SBen Gras vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
1182fe8fb19SBen Gras {
11984d9c625SLionel Sambuc 	return vfwscanf_l(fp, _current_locale(), fmt, ap);
12084d9c625SLionel Sambuc }
12184d9c625SLionel Sambuc 
12284d9c625SLionel Sambuc int
vfwscanf_l(FILE * __restrict fp,locale_t loc,const wchar_t * __restrict fmt,va_list ap)12384d9c625SLionel Sambuc vfwscanf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
12484d9c625SLionel Sambuc     va_list ap)
12584d9c625SLionel Sambuc {
1262fe8fb19SBen Gras 	int ret;
1272fe8fb19SBen Gras 
1282fe8fb19SBen Gras 	FLOCKFILE(fp);
1292fe8fb19SBen Gras 	_SET_ORIENTATION(fp, 1);
13084d9c625SLionel Sambuc 	ret = __vfwscanf_unlocked_l(fp, loc, fmt, ap);
1312fe8fb19SBen Gras 	FUNLOCKFILE(fp);
132f14fb602SLionel Sambuc 	return ret;
1332fe8fb19SBen Gras }
1342fe8fb19SBen Gras 
1352fe8fb19SBen Gras #define SCANF_SKIP_SPACE() \
1362fe8fb19SBen Gras do { \
1372fe8fb19SBen Gras 	wint_t tc; \
1382fe8fb19SBen Gras  \
13984d9c625SLionel Sambuc 	while ((tc = __fgetwc_unlock(fp)) != WEOF && iswspace_l(tc, loc)) \
1402fe8fb19SBen Gras 		continue; \
1412fe8fb19SBen Gras 	if (tc != WEOF) \
1422fe8fb19SBen Gras 		ungetwc(tc, fp); \
1432fe8fb19SBen Gras } while (/*CONSTCOND*/ 0)
1442fe8fb19SBen Gras 
1452fe8fb19SBen Gras /*
1462fe8fb19SBen Gras  * Non-MT-safe version.
1472fe8fb19SBen Gras  */
1482fe8fb19SBen Gras int
__vfwscanf_unlocked_l(FILE * __restrict fp,locale_t loc,const wchar_t * __restrict fmt,va_list ap)14984d9c625SLionel Sambuc __vfwscanf_unlocked_l(FILE * __restrict fp, locale_t loc,
15084d9c625SLionel Sambuc     const wchar_t * __restrict fmt, va_list ap)
1512fe8fb19SBen Gras {
1522fe8fb19SBen Gras 	wint_t c;		/* character from format, or conversion */
1532fe8fb19SBen Gras 	size_t width;		/* field width, or 0 */
1542fe8fb19SBen Gras 	wchar_t *p;		/* points into all kinds of strings */
1552fe8fb19SBen Gras 	int n;			/* handy integer */
1562fe8fb19SBen Gras 	int flags;		/* flags as defined above */
1572fe8fb19SBen Gras 	wchar_t *p0;		/* saves original value of p when necessary */
1582fe8fb19SBen Gras 	int nassigned;		/* number of fields assigned */
1592fe8fb19SBen Gras 	int nconversions;	/* number of conversions */
160f14fb602SLionel Sambuc 	size_t nread;		/* number of characters consumed from fp */
1612fe8fb19SBen Gras 	int base;		/* base argument to conversion function */
1622fe8fb19SBen Gras 	wchar_t buf[BUF];	/* buffer for numeric conversions */
1632fe8fb19SBen Gras 	const wchar_t *ccls;	/* character class start */
1642fe8fb19SBen Gras 	const wchar_t *ccle;	/* character class end */
1652fe8fb19SBen Gras 	int cclcompl;		/* ccl is complemented? */
1662fe8fb19SBen Gras 	wint_t wi;		/* handy wint_t */
1672fe8fb19SBen Gras 	char *mbp;		/* multibyte string pointer for %c %s %[ */
1682fe8fb19SBen Gras 	size_t nconv;		/* number of bytes in mb. conversion */
1692fe8fb19SBen Gras 	static const mbstate_t initial;
1702fe8fb19SBen Gras 	mbstate_t mbs;
17184d9c625SLionel Sambuc 	char mbbuf[MB_LEN_MAX];	/* temporary mb. character buffer */
1722fe8fb19SBen Gras 	/* `basefix' is used to avoid `if' tests in the integer scanner */
1732fe8fb19SBen Gras 	static short basefix[17] =
1742fe8fb19SBen Gras 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
1752fe8fb19SBen Gras 
1762fe8fb19SBen Gras 	nassigned = 0;
1772fe8fb19SBen Gras 	nconversions = 0;
1782fe8fb19SBen Gras 	nread = 0;
1792fe8fb19SBen Gras 	ccls = ccle = NULL;
1802fe8fb19SBen Gras 	base = 0;
1812fe8fb19SBen Gras 	cclcompl = 0;
1822fe8fb19SBen Gras 	mbp = NULL;
183*0a6a1f1dSLionel Sambuc 	p = NULL; /* XXXgcc */
1842fe8fb19SBen Gras 	for (;;) {
1852fe8fb19SBen Gras 		c = *fmt++;
1862fe8fb19SBen Gras 		if (c == 0)
187f14fb602SLionel Sambuc 			return nassigned;
18884d9c625SLionel Sambuc 		if (iswspace_l(c, loc)) {
1892fe8fb19SBen Gras 			while ((c = __fgetwc_unlock(fp)) != WEOF &&
19084d9c625SLionel Sambuc 			    iswspace_l(c, loc))
1912fe8fb19SBen Gras 				;
1922fe8fb19SBen Gras 			if (c != WEOF)
1932fe8fb19SBen Gras 				ungetwc(c, fp);
1942fe8fb19SBen Gras 			continue;
1952fe8fb19SBen Gras 		}
1962fe8fb19SBen Gras 		if (c != '%')
1972fe8fb19SBen Gras 			goto literal;
1982fe8fb19SBen Gras 		width = 0;
1992fe8fb19SBen Gras 		flags = 0;
2002fe8fb19SBen Gras 		/*
2012fe8fb19SBen Gras 		 * switch on the format.  continue if done;
2022fe8fb19SBen Gras 		 * break once format type is derived.
2032fe8fb19SBen Gras 		 */
2042fe8fb19SBen Gras again:		c = *fmt++;
2052fe8fb19SBen Gras 		switch (c) {
2062fe8fb19SBen Gras 		case '%':
2072fe8fb19SBen Gras 			SCANF_SKIP_SPACE();
2082fe8fb19SBen Gras literal:
2092fe8fb19SBen Gras 			if ((wi = __fgetwc_unlock(fp)) == WEOF)
2102fe8fb19SBen Gras 				goto input_failure;
2112fe8fb19SBen Gras 			if (wi != c) {
2122fe8fb19SBen Gras 				ungetwc(wi, fp);
2132fe8fb19SBen Gras 				goto input_failure;
2142fe8fb19SBen Gras 			}
2152fe8fb19SBen Gras 			nread++;
2162fe8fb19SBen Gras 			continue;
2172fe8fb19SBen Gras 
2182fe8fb19SBen Gras 		case '*':
2192fe8fb19SBen Gras 			flags |= SUPPRESS;
2202fe8fb19SBen Gras 			goto again;
2212fe8fb19SBen Gras 		case 'j':
2222fe8fb19SBen Gras 			flags |= INTMAXT;
2232fe8fb19SBen Gras 			goto again;
2242fe8fb19SBen Gras 		case 'l':
2252fe8fb19SBen Gras 			if (flags & LONG) {
2262fe8fb19SBen Gras 				flags &= ~LONG;
2272fe8fb19SBen Gras 				flags |= LONGLONG;
2282fe8fb19SBen Gras 			} else
2292fe8fb19SBen Gras 				flags |= LONG;
2302fe8fb19SBen Gras 			goto again;
2312fe8fb19SBen Gras 		case 'q':
2322fe8fb19SBen Gras 			flags |= LONGLONG;	/* not quite */
2332fe8fb19SBen Gras 			goto again;
2342fe8fb19SBen Gras 		case 't':
2352fe8fb19SBen Gras 			flags |= PTRDIFFT;
2362fe8fb19SBen Gras 			goto again;
2372fe8fb19SBen Gras 		case 'z':
2382fe8fb19SBen Gras 			flags |= SIZET;
2392fe8fb19SBen Gras 			goto again;
2402fe8fb19SBen Gras 		case 'L':
2412fe8fb19SBen Gras 			flags |= LONGDBL;
2422fe8fb19SBen Gras 			goto again;
2432fe8fb19SBen Gras 		case 'h':
2442fe8fb19SBen Gras 			if (flags & SHORT) {
2452fe8fb19SBen Gras 				flags &= ~SHORT;
2462fe8fb19SBen Gras 				flags |= SHORTSHORT;
2472fe8fb19SBen Gras 			} else
2482fe8fb19SBen Gras 				flags |= SHORT;
2492fe8fb19SBen Gras 			goto again;
2502fe8fb19SBen Gras 
2512fe8fb19SBen Gras 		case '0': case '1': case '2': case '3': case '4':
2522fe8fb19SBen Gras 		case '5': case '6': case '7': case '8': case '9':
2532fe8fb19SBen Gras 			width = width * 10 + c - '0';
2542fe8fb19SBen Gras 			goto again;
2552fe8fb19SBen Gras 
2562fe8fb19SBen Gras 		/*
2572fe8fb19SBen Gras 		 * Conversions.
2582fe8fb19SBen Gras 		 */
2592fe8fb19SBen Gras 		case 'd':
2602fe8fb19SBen Gras 			c = CT_INT;
2612fe8fb19SBen Gras 			base = 10;
2622fe8fb19SBen Gras 			break;
2632fe8fb19SBen Gras 
2642fe8fb19SBen Gras 		case 'i':
2652fe8fb19SBen Gras 			c = CT_INT;
2662fe8fb19SBen Gras 			base = 0;
2672fe8fb19SBen Gras 			break;
2682fe8fb19SBen Gras 
2692fe8fb19SBen Gras 		case 'o':
2702fe8fb19SBen Gras 			c = CT_INT;
2712fe8fb19SBen Gras 			flags |= UNSIGNED;
2722fe8fb19SBen Gras 			base = 8;
2732fe8fb19SBen Gras 			break;
2742fe8fb19SBen Gras 
2752fe8fb19SBen Gras 		case 'u':
2762fe8fb19SBen Gras 			c = CT_INT;
2772fe8fb19SBen Gras 			flags |= UNSIGNED;
2782fe8fb19SBen Gras 			base = 10;
2792fe8fb19SBen Gras 			break;
2802fe8fb19SBen Gras 
2812fe8fb19SBen Gras 		case 'X':
2822fe8fb19SBen Gras 		case 'x':
2832fe8fb19SBen Gras 			flags |= PFXOK;	/* enable 0x prefixing */
2842fe8fb19SBen Gras 			c = CT_INT;
2852fe8fb19SBen Gras 			flags |= UNSIGNED;
2862fe8fb19SBen Gras 			base = 16;
2872fe8fb19SBen Gras 			break;
2882fe8fb19SBen Gras 
2892fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
2902fe8fb19SBen Gras 		case 'A': case 'E': case 'F': case 'G':
2912fe8fb19SBen Gras 		case 'a': case 'e': case 'f': case 'g':
2922fe8fb19SBen Gras 			c = CT_FLOAT;
2932fe8fb19SBen Gras 			break;
2942fe8fb19SBen Gras #endif
2952fe8fb19SBen Gras 
2962fe8fb19SBen Gras 		case 'S':
2972fe8fb19SBen Gras 			flags |= LONG;
2982fe8fb19SBen Gras 			/* FALLTHROUGH */
2992fe8fb19SBen Gras 		case 's':
3002fe8fb19SBen Gras 			c = CT_STRING;
3012fe8fb19SBen Gras 			break;
3022fe8fb19SBen Gras 
3032fe8fb19SBen Gras 		case '[':
3042fe8fb19SBen Gras 			ccls = fmt;
3052fe8fb19SBen Gras 			if (*fmt == '^') {
3062fe8fb19SBen Gras 				cclcompl = 1;
3072fe8fb19SBen Gras 				fmt++;
3082fe8fb19SBen Gras 			} else
3092fe8fb19SBen Gras 				cclcompl = 0;
3102fe8fb19SBen Gras 			if (*fmt == ']')
3112fe8fb19SBen Gras 				fmt++;
3122fe8fb19SBen Gras 			while (*fmt != '\0' && *fmt != ']')
3132fe8fb19SBen Gras 				fmt++;
3142fe8fb19SBen Gras 			ccle = fmt;
3152fe8fb19SBen Gras 			fmt++;
3162fe8fb19SBen Gras 			flags |= NOSKIP;
3172fe8fb19SBen Gras 			c = CT_CCL;
3182fe8fb19SBen Gras 			break;
3192fe8fb19SBen Gras 
3202fe8fb19SBen Gras 		case 'C':
3212fe8fb19SBen Gras 			flags |= LONG;
3222fe8fb19SBen Gras 			/* FALLTHROUGH */
3232fe8fb19SBen Gras 		case 'c':
3242fe8fb19SBen Gras 			flags |= NOSKIP;
3252fe8fb19SBen Gras 			c = CT_CHAR;
3262fe8fb19SBen Gras 			break;
3272fe8fb19SBen Gras 
3282fe8fb19SBen Gras 		case 'p':	/* pointer format is like hex */
3292fe8fb19SBen Gras 			flags |= POINTER | PFXOK;
3302fe8fb19SBen Gras 			c = CT_INT;		/* assumes sizeof(uintmax_t) */
3312fe8fb19SBen Gras 			flags |= UNSIGNED;	/*      >= sizeof(uintptr_t) */
3322fe8fb19SBen Gras 			base = 16;
3332fe8fb19SBen Gras 			break;
3342fe8fb19SBen Gras 
3352fe8fb19SBen Gras 		case 'n':
3362fe8fb19SBen Gras 			nconversions++;
3372fe8fb19SBen Gras 			if (flags & SUPPRESS)	/* ??? */
3382fe8fb19SBen Gras 				continue;
3392fe8fb19SBen Gras 			if (flags & SHORTSHORT)
340f14fb602SLionel Sambuc 				*va_arg(ap, char *) = (char)nread;
3412fe8fb19SBen Gras 			else if (flags & SHORT)
342f14fb602SLionel Sambuc 				*va_arg(ap, short *) = (short)nread;
3432fe8fb19SBen Gras 			else if (flags & LONG)
3442fe8fb19SBen Gras 				*va_arg(ap, long *) = nread;
3452fe8fb19SBen Gras 			else if (flags & LONGLONG)
3462fe8fb19SBen Gras 				*va_arg(ap, quad_t *) = nread;
3472fe8fb19SBen Gras 			else if (flags & INTMAXT)
3482fe8fb19SBen Gras 				*va_arg(ap, intmax_t *) = nread;
3492fe8fb19SBen Gras 			else if (flags & SIZET)
3502fe8fb19SBen Gras 				*va_arg(ap, size_t *) = nread;
3512fe8fb19SBen Gras 			else if (flags & PTRDIFFT)
3522fe8fb19SBen Gras 				*va_arg(ap, ptrdiff_t *) = nread;
3532fe8fb19SBen Gras 			else
354f14fb602SLionel Sambuc 				*va_arg(ap, int *) = (int)nread;
3552fe8fb19SBen Gras 			continue;
3562fe8fb19SBen Gras 
3572fe8fb19SBen Gras 		default:
3582fe8fb19SBen Gras 			goto match_failure;
3592fe8fb19SBen Gras 
3602fe8fb19SBen Gras 		/*
3612fe8fb19SBen Gras 		 * Disgusting backwards compatibility hack.	XXX
3622fe8fb19SBen Gras 		 */
3632fe8fb19SBen Gras 		case '\0':	/* compat */
364f14fb602SLionel Sambuc 			return EOF;
3652fe8fb19SBen Gras 		}
3662fe8fb19SBen Gras 
3672fe8fb19SBen Gras 		/*
3682fe8fb19SBen Gras 		 * Consume leading white space, except for formats
3692fe8fb19SBen Gras 		 * that suppress this.
3702fe8fb19SBen Gras 		 */
3712fe8fb19SBen Gras 		if ((flags & NOSKIP) == 0) {
37284d9c625SLionel Sambuc 			while ((wi = __fgetwc_unlock(fp)) != WEOF &&
37384d9c625SLionel Sambuc 			       iswspace_l(wi, loc))
3742fe8fb19SBen Gras 				nread++;
3752fe8fb19SBen Gras 			if (wi == WEOF)
3762fe8fb19SBen Gras 				goto input_failure;
3772fe8fb19SBen Gras 			ungetwc(wi, fp);
3782fe8fb19SBen Gras 		}
3792fe8fb19SBen Gras 
3802fe8fb19SBen Gras 		/*
3812fe8fb19SBen Gras 		 * Do the conversion.
3822fe8fb19SBen Gras 		 */
3832fe8fb19SBen Gras 		switch (c) {
3842fe8fb19SBen Gras 
3852fe8fb19SBen Gras 		case CT_CHAR:
3862fe8fb19SBen Gras 			/* scan arbitrary characters (sets NOSKIP) */
3872fe8fb19SBen Gras 			if (width == 0)
3882fe8fb19SBen Gras 				width = 1;
3892fe8fb19SBen Gras 			if (flags & LONG) {
3902fe8fb19SBen Gras 				if (!(flags & SUPPRESS))
3912fe8fb19SBen Gras 					p = va_arg(ap, wchar_t *);
3922fe8fb19SBen Gras 				n = 0;
3932fe8fb19SBen Gras 				while (width-- != 0 &&
3942fe8fb19SBen Gras 				    (wi = __fgetwc_unlock(fp)) != WEOF) {
3952fe8fb19SBen Gras 					if (!(flags & SUPPRESS))
3962fe8fb19SBen Gras 						*p++ = (wchar_t)wi;
3972fe8fb19SBen Gras 					n++;
3982fe8fb19SBen Gras 				}
3992fe8fb19SBen Gras 				if (n == 0)
4002fe8fb19SBen Gras 					goto input_failure;
4012fe8fb19SBen Gras 				nread += n;
4022fe8fb19SBen Gras 				if (!(flags & SUPPRESS))
4032fe8fb19SBen Gras 					nassigned++;
4042fe8fb19SBen Gras 			} else {
4052fe8fb19SBen Gras 				if (!(flags & SUPPRESS))
4062fe8fb19SBen Gras 					mbp = va_arg(ap, char *);
4072fe8fb19SBen Gras 				n = 0;
4082fe8fb19SBen Gras 				mbs = initial;
4092fe8fb19SBen Gras 				while (width != 0 &&
4102fe8fb19SBen Gras 				    (wi = __fgetwc_unlock(fp)) != WEOF) {
41184d9c625SLionel Sambuc 					if (width >= MB_CUR_MAX_L(loc) &&
4122fe8fb19SBen Gras 					    !(flags & SUPPRESS)) {
41384d9c625SLionel Sambuc 						nconv = wcrtomb_l(mbp, wi,
41484d9c625SLionel Sambuc 						    &mbs, loc);
4152fe8fb19SBen Gras 						if (nconv == (size_t)-1)
4162fe8fb19SBen Gras 							goto input_failure;
4172fe8fb19SBen Gras 					} else {
41884d9c625SLionel Sambuc 						nconv = wcrtomb_l(mbbuf, wi,
41984d9c625SLionel Sambuc 						    &mbs, loc);
4202fe8fb19SBen Gras 						if (nconv == (size_t)-1)
4212fe8fb19SBen Gras 							goto input_failure;
4222fe8fb19SBen Gras 						if (nconv > width) {
4232fe8fb19SBen Gras 							ungetwc(wi, fp);
4242fe8fb19SBen Gras 							break;
4252fe8fb19SBen Gras 						}
4262fe8fb19SBen Gras 						if (!(flags & SUPPRESS))
4272fe8fb19SBen Gras 							memcpy(mbp, mbbuf,
4282fe8fb19SBen Gras 							    nconv);
4292fe8fb19SBen Gras 					}
4302fe8fb19SBen Gras 					if (!(flags & SUPPRESS))
4312fe8fb19SBen Gras 						mbp += nconv;
4322fe8fb19SBen Gras 					width -= nconv;
4332fe8fb19SBen Gras 					n++;
4342fe8fb19SBen Gras 				}
4352fe8fb19SBen Gras 				if (n == 0)
4362fe8fb19SBen Gras 					goto input_failure;
4372fe8fb19SBen Gras 				nread += n;
4382fe8fb19SBen Gras 				if (!(flags & SUPPRESS))
4392fe8fb19SBen Gras 					nassigned++;
4402fe8fb19SBen Gras 			}
4412fe8fb19SBen Gras 			nconversions++;
4422fe8fb19SBen Gras 			break;
4432fe8fb19SBen Gras 
4442fe8fb19SBen Gras 		case CT_CCL:
4452fe8fb19SBen Gras 			/* scan a (nonempty) character class (sets NOSKIP) */
4462fe8fb19SBen Gras 			if (width == 0)
4472fe8fb19SBen Gras 				width = (size_t)~0;	/* `infinity' */
4482fe8fb19SBen Gras 			/* take only those things in the class */
4492fe8fb19SBen Gras 			if ((flags & SUPPRESS) && (flags & LONG)) {
4502fe8fb19SBen Gras 				n = 0;
4512fe8fb19SBen Gras 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
4522fe8fb19SBen Gras 				    width-- != 0 && INCCL(wi))
4532fe8fb19SBen Gras 					n++;
4542fe8fb19SBen Gras 				if (wi != WEOF)
4552fe8fb19SBen Gras 					ungetwc(wi, fp);
4562fe8fb19SBen Gras 				if (n == 0)
4572fe8fb19SBen Gras 					goto match_failure;
4582fe8fb19SBen Gras 			} else if (flags & LONG) {
4592fe8fb19SBen Gras 				p0 = p = va_arg(ap, wchar_t *);
4602fe8fb19SBen Gras 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
4612fe8fb19SBen Gras 				    width-- != 0 && INCCL(wi))
4622fe8fb19SBen Gras 					*p++ = (wchar_t)wi;
4632fe8fb19SBen Gras 				if (wi != WEOF)
4642fe8fb19SBen Gras 					ungetwc(wi, fp);
465f14fb602SLionel Sambuc 				_DIAGASSERT(__type_fit(int, p - p0));
466f14fb602SLionel Sambuc 				n = (int)(p - p0);
4672fe8fb19SBen Gras 				if (n == 0)
4682fe8fb19SBen Gras 					goto match_failure;
4692fe8fb19SBen Gras 				*p = 0;
4702fe8fb19SBen Gras 				nassigned++;
4712fe8fb19SBen Gras 			} else {
4722fe8fb19SBen Gras 				if (!(flags & SUPPRESS))
4732fe8fb19SBen Gras 					mbp = va_arg(ap, char *);
4742fe8fb19SBen Gras 				n = 0;
4752fe8fb19SBen Gras 				mbs = initial;
4762fe8fb19SBen Gras 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
4772fe8fb19SBen Gras 				    width != 0 && INCCL(wi)) {
47884d9c625SLionel Sambuc 					if (width >= MB_CUR_MAX_L(loc) &&
4792fe8fb19SBen Gras 					   !(flags & SUPPRESS)) {
48084d9c625SLionel Sambuc 						nconv = wcrtomb_l(mbp, wi,
48184d9c625SLionel Sambuc 						    &mbs, loc);
4822fe8fb19SBen Gras 						if (nconv == (size_t)-1)
4832fe8fb19SBen Gras 							goto input_failure;
4842fe8fb19SBen Gras 					} else {
48584d9c625SLionel Sambuc 						nconv = wcrtomb_l(mbbuf, wi,
48684d9c625SLionel Sambuc 						    &mbs, loc);
4872fe8fb19SBen Gras 						if (nconv == (size_t)-1)
4882fe8fb19SBen Gras 							goto input_failure;
4892fe8fb19SBen Gras 						if (nconv > width)
4902fe8fb19SBen Gras 							break;
4912fe8fb19SBen Gras 						if (!(flags & SUPPRESS))
4922fe8fb19SBen Gras 							memcpy(mbp, mbbuf,
4932fe8fb19SBen Gras 							    nconv);
4942fe8fb19SBen Gras 					}
4952fe8fb19SBen Gras 					if (!(flags & SUPPRESS))
4962fe8fb19SBen Gras 						mbp += nconv;
4972fe8fb19SBen Gras 					width -= nconv;
4982fe8fb19SBen Gras 					n++;
4992fe8fb19SBen Gras 				}
5002fe8fb19SBen Gras 				if (wi != WEOF)
5012fe8fb19SBen Gras 					ungetwc(wi, fp);
5022fe8fb19SBen Gras 				if (!(flags & SUPPRESS)) {
5032fe8fb19SBen Gras 					*mbp = 0;
5042fe8fb19SBen Gras 					nassigned++;
5052fe8fb19SBen Gras 				}
5062fe8fb19SBen Gras 			}
5072fe8fb19SBen Gras 			nread += n;
5082fe8fb19SBen Gras 			nconversions++;
5092fe8fb19SBen Gras 			break;
5102fe8fb19SBen Gras 
5112fe8fb19SBen Gras 		case CT_STRING:
5122fe8fb19SBen Gras 			/* like CCL, but zero-length string OK, & no NOSKIP */
5132fe8fb19SBen Gras 			if (width == 0)
5142fe8fb19SBen Gras 				width = (size_t)~0;
5152fe8fb19SBen Gras 			if ((flags & SUPPRESS) && (flags & LONG)) {
5162fe8fb19SBen Gras 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
5172fe8fb19SBen Gras 				    width-- != 0 &&
51884d9c625SLionel Sambuc 				    !iswspace_l(wi, loc))
5192fe8fb19SBen Gras 					nread++;
5202fe8fb19SBen Gras 				if (wi != WEOF)
5212fe8fb19SBen Gras 					ungetwc(wi, fp);
5222fe8fb19SBen Gras 			} else if (flags & LONG) {
5232fe8fb19SBen Gras 				p0 = p = va_arg(ap, wchar_t *);
5242fe8fb19SBen Gras 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
5252fe8fb19SBen Gras 				    width-- != 0 &&
52684d9c625SLionel Sambuc 				    !iswspace_l(wi, loc)) {
5272fe8fb19SBen Gras 					*p++ = (wchar_t)wi;
5282fe8fb19SBen Gras 					nread++;
5292fe8fb19SBen Gras 				}
5302fe8fb19SBen Gras 				if (wi != WEOF)
5312fe8fb19SBen Gras 					ungetwc(wi, fp);
5322fe8fb19SBen Gras 				*p = '\0';
5332fe8fb19SBen Gras 				nassigned++;
5342fe8fb19SBen Gras 			} else {
5352fe8fb19SBen Gras 				if (!(flags & SUPPRESS))
5362fe8fb19SBen Gras 					mbp = va_arg(ap, char *);
5372fe8fb19SBen Gras 				mbs = initial;
5382fe8fb19SBen Gras 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
5392fe8fb19SBen Gras 				    width != 0 &&
54084d9c625SLionel Sambuc 				    !iswspace_l(wi, loc)) {
54184d9c625SLionel Sambuc 					if (width >= MB_CUR_MAX_L(loc) &&
5422fe8fb19SBen Gras 					    !(flags & SUPPRESS)) {
54384d9c625SLionel Sambuc 						nconv = wcrtomb_l(mbp, wi,
54484d9c625SLionel Sambuc 						    &mbs, loc);
5452fe8fb19SBen Gras 						if (nconv == (size_t)-1)
5462fe8fb19SBen Gras 							goto input_failure;
5472fe8fb19SBen Gras 					} else {
54884d9c625SLionel Sambuc 						nconv = wcrtomb_l(mbbuf, wi,
54984d9c625SLionel Sambuc 						    &mbs, loc);
5502fe8fb19SBen Gras 						if (nconv == (size_t)-1)
5512fe8fb19SBen Gras 							goto input_failure;
5522fe8fb19SBen Gras 						if (nconv > width)
5532fe8fb19SBen Gras 							break;
5542fe8fb19SBen Gras 						if (!(flags & SUPPRESS))
5552fe8fb19SBen Gras 							memcpy(mbp, mbbuf,
5562fe8fb19SBen Gras 							    nconv);
5572fe8fb19SBen Gras 					}
5582fe8fb19SBen Gras 					if (!(flags & SUPPRESS))
5592fe8fb19SBen Gras 						mbp += nconv;
5602fe8fb19SBen Gras 					width -= nconv;
5612fe8fb19SBen Gras 					nread++;
5622fe8fb19SBen Gras 				}
5632fe8fb19SBen Gras 				if (wi != WEOF)
5642fe8fb19SBen Gras 					ungetwc(wi, fp);
5652fe8fb19SBen Gras 				if (!(flags & SUPPRESS)) {
5662fe8fb19SBen Gras 					*mbp = 0;
5672fe8fb19SBen Gras 					nassigned++;
5682fe8fb19SBen Gras 				}
5692fe8fb19SBen Gras 			}
5702fe8fb19SBen Gras 			nconversions++;
5712fe8fb19SBen Gras 			continue;
5722fe8fb19SBen Gras 
5732fe8fb19SBen Gras 		case CT_INT:
5742fe8fb19SBen Gras 			/* scan an integer as if by the conversion function */
5752fe8fb19SBen Gras 			if (width == 0 || width > sizeof(buf) /
5762fe8fb19SBen Gras 			    sizeof(*buf) - 1)
5772fe8fb19SBen Gras 				width = sizeof(buf) / sizeof(*buf) - 1;
5782fe8fb19SBen Gras 			flags |= SIGNOK | NDIGITS | NZDIGITS;
5792fe8fb19SBen Gras 			for (p = buf; width; width--) {
5802fe8fb19SBen Gras 				c = __fgetwc_unlock(fp);
5812fe8fb19SBen Gras 				/*
5822fe8fb19SBen Gras 				 * Switch on the character; `goto ok'
5832fe8fb19SBen Gras 				 * if we accept it as a part of number.
5842fe8fb19SBen Gras 				 */
5852fe8fb19SBen Gras 				switch (c) {
5862fe8fb19SBen Gras 
5872fe8fb19SBen Gras 				/*
5882fe8fb19SBen Gras 				 * The digit 0 is always legal, but is
5892fe8fb19SBen Gras 				 * special.  For %i conversions, if no
5902fe8fb19SBen Gras 				 * digits (zero or nonzero) have been
5912fe8fb19SBen Gras 				 * scanned (only signs), we will have
5922fe8fb19SBen Gras 				 * base==0.  In that case, we should set
5932fe8fb19SBen Gras 				 * it to 8 and enable 0x prefixing.
5942fe8fb19SBen Gras 				 * Also, if we have not scanned zero digits
5952fe8fb19SBen Gras 				 * before this, do not turn off prefixing
5962fe8fb19SBen Gras 				 * (someone else will turn it off if we
5972fe8fb19SBen Gras 				 * have scanned any nonzero digits).
5982fe8fb19SBen Gras 				 */
5992fe8fb19SBen Gras 				case '0':
6002fe8fb19SBen Gras 					if (base == 0) {
6012fe8fb19SBen Gras 						base = 8;
6022fe8fb19SBen Gras 						flags |= PFXOK;
6032fe8fb19SBen Gras 					}
6042fe8fb19SBen Gras 					if (flags & NZDIGITS)
6052fe8fb19SBen Gras 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
6062fe8fb19SBen Gras 					else
6072fe8fb19SBen Gras 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
6082fe8fb19SBen Gras 					goto ok;
6092fe8fb19SBen Gras 
6102fe8fb19SBen Gras 				/* 1 through 7 always legal */
6112fe8fb19SBen Gras 				case '1': case '2': case '3':
6122fe8fb19SBen Gras 				case '4': case '5': case '6': case '7':
6132fe8fb19SBen Gras 					base = basefix[base];
6142fe8fb19SBen Gras 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
6152fe8fb19SBen Gras 					goto ok;
6162fe8fb19SBen Gras 
6172fe8fb19SBen Gras 				/* digits 8 and 9 ok iff decimal or hex */
6182fe8fb19SBen Gras 				case '8': case '9':
6192fe8fb19SBen Gras 					base = basefix[base];
6202fe8fb19SBen Gras 					if (base <= 8)
6212fe8fb19SBen Gras 						break;	/* not legal here */
6222fe8fb19SBen Gras 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
6232fe8fb19SBen Gras 					goto ok;
6242fe8fb19SBen Gras 
6252fe8fb19SBen Gras 				/* letters ok iff hex */
6262fe8fb19SBen Gras 				case 'A': case 'B': case 'C':
6272fe8fb19SBen Gras 				case 'D': case 'E': case 'F':
6282fe8fb19SBen Gras 				case 'a': case 'b': case 'c':
6292fe8fb19SBen Gras 				case 'd': case 'e': case 'f':
6302fe8fb19SBen Gras 					/* no need to fix base here */
6312fe8fb19SBen Gras 					if (base <= 10)
6322fe8fb19SBen Gras 						break;	/* not legal here */
6332fe8fb19SBen Gras 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
6342fe8fb19SBen Gras 					goto ok;
6352fe8fb19SBen Gras 
6362fe8fb19SBen Gras 				/* sign ok only as first character */
6372fe8fb19SBen Gras 				case '+': case '-':
6382fe8fb19SBen Gras 					if (flags & SIGNOK) {
6392fe8fb19SBen Gras 						flags &= ~SIGNOK;
6402fe8fb19SBen Gras 						flags |= HAVESIGN;
6412fe8fb19SBen Gras 						goto ok;
6422fe8fb19SBen Gras 					}
6432fe8fb19SBen Gras 					break;
6442fe8fb19SBen Gras 
6452fe8fb19SBen Gras 				/*
6462fe8fb19SBen Gras 				 * x ok iff flag still set & 2nd char (or
6472fe8fb19SBen Gras 				 * 3rd char if we have a sign).
6482fe8fb19SBen Gras 				 */
6492fe8fb19SBen Gras 				case 'x': case 'X':
6502fe8fb19SBen Gras 					if (flags & PFXOK && p ==
6512fe8fb19SBen Gras 					    buf + 1 + !!(flags & HAVESIGN)) {
6522fe8fb19SBen Gras 						base = 16;	/* if %i */
6532fe8fb19SBen Gras 						flags &= ~PFXOK;
6542fe8fb19SBen Gras 						goto ok;
6552fe8fb19SBen Gras 					}
6562fe8fb19SBen Gras 					break;
6572fe8fb19SBen Gras 				}
6582fe8fb19SBen Gras 
6592fe8fb19SBen Gras 				/*
6602fe8fb19SBen Gras 				 * If we got here, c is not a legal character
6612fe8fb19SBen Gras 				 * for a number.  Stop accumulating digits.
6622fe8fb19SBen Gras 				 */
6632fe8fb19SBen Gras 				if (c != WEOF)
6642fe8fb19SBen Gras 					ungetwc(c, fp);
6652fe8fb19SBen Gras 				break;
6662fe8fb19SBen Gras 		ok:
6672fe8fb19SBen Gras 				/*
6682fe8fb19SBen Gras 				 * c is legal: store it and look at the next.
6692fe8fb19SBen Gras 				 */
6702fe8fb19SBen Gras 				*p++ = (wchar_t)c;
6712fe8fb19SBen Gras 			}
6722fe8fb19SBen Gras 			/*
6732fe8fb19SBen Gras 			 * If we had only a sign, it is no good; push
6742fe8fb19SBen Gras 			 * back the sign.  If the number ends in `x',
6752fe8fb19SBen Gras 			 * it was [sign] '0' 'x', so push back the x
6762fe8fb19SBen Gras 			 * and treat it as [sign] '0'.
6772fe8fb19SBen Gras 			 */
6782fe8fb19SBen Gras 			if (flags & NDIGITS) {
6792fe8fb19SBen Gras 				if (p > buf)
6802fe8fb19SBen Gras 					ungetwc(*--p, fp);
6812fe8fb19SBen Gras 				goto match_failure;
6822fe8fb19SBen Gras 			}
6832fe8fb19SBen Gras 			c = p[-1];
6842fe8fb19SBen Gras 			if (c == 'x' || c == 'X') {
6852fe8fb19SBen Gras 				--p;
6862fe8fb19SBen Gras 				ungetwc(c, fp);
6872fe8fb19SBen Gras 			}
6882fe8fb19SBen Gras 			if ((flags & SUPPRESS) == 0) {
6892fe8fb19SBen Gras 				uintmax_t res;
6902fe8fb19SBen Gras 
6912fe8fb19SBen Gras 				*p = 0;
6922fe8fb19SBen Gras 				if ((flags & UNSIGNED) == 0)
69384d9c625SLionel Sambuc 				    res = wcstoimax_l(buf, NULL, base, loc);
6942fe8fb19SBen Gras 				else
69584d9c625SLionel Sambuc 				    res = wcstoumax_l(buf, NULL, base, loc);
6962fe8fb19SBen Gras 				if (flags & POINTER)
6972fe8fb19SBen Gras 					*va_arg(ap, void **) =
6982fe8fb19SBen Gras 							(void *)(uintptr_t)res;
6992fe8fb19SBen Gras 				else if (flags & SHORTSHORT)
7002fe8fb19SBen Gras 					*va_arg(ap, char *) = (char)res;
7012fe8fb19SBen Gras 				else if (flags & SHORT)
7022fe8fb19SBen Gras 					*va_arg(ap, short *) = (short)res;
7032fe8fb19SBen Gras 				else if (flags & LONG)
7042fe8fb19SBen Gras 					*va_arg(ap, long *) = (long)res;
7052fe8fb19SBen Gras 				else if (flags & LONGLONG)
7062fe8fb19SBen Gras 					*va_arg(ap, quad_t *) = res;
7072fe8fb19SBen Gras 				else if (flags & INTMAXT)
7082fe8fb19SBen Gras 					*va_arg(ap, intmax_t *) = res;
7092fe8fb19SBen Gras 				else if (flags & PTRDIFFT)
7102fe8fb19SBen Gras 					*va_arg(ap, ptrdiff_t *) = (ptrdiff_t)res;
7112fe8fb19SBen Gras 				else if (flags & SIZET)
7122fe8fb19SBen Gras 					*va_arg(ap, size_t *) = (size_t)res;
7132fe8fb19SBen Gras 				else
7142fe8fb19SBen Gras 					*va_arg(ap, int *) = (int)res;
7152fe8fb19SBen Gras 				nassigned++;
7162fe8fb19SBen Gras 			}
717f14fb602SLionel Sambuc 			_DIAGASSERT(__type_fit(int, p - buf));
718f14fb602SLionel Sambuc 			nread += (int)(p - buf);
7192fe8fb19SBen Gras 			nconversions++;
7202fe8fb19SBen Gras 			break;
7212fe8fb19SBen Gras 
7222fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
7232fe8fb19SBen Gras 		case CT_FLOAT:
7242fe8fb19SBen Gras 			/* scan a floating point number as if by strtod */
7252fe8fb19SBen Gras 			if (width == 0 || width > sizeof(buf) /
7262fe8fb19SBen Gras 			    sizeof(*buf) - 1)
7272fe8fb19SBen Gras 				width = sizeof(buf) / sizeof(*buf) - 1;
72884d9c625SLionel Sambuc 			if ((width = parsefloat(fp, buf, buf + width, loc)) == 0)
7292fe8fb19SBen Gras 				goto match_failure;
7302fe8fb19SBen Gras 			if ((flags & SUPPRESS) == 0) {
7312fe8fb19SBen Gras 				if (flags & LONGDBL) {
73284d9c625SLionel Sambuc 					long double res = wcstold_l(buf, &p,
73384d9c625SLionel Sambuc 					    loc);
7342fe8fb19SBen Gras 					*va_arg(ap, long double *) = res;
7352fe8fb19SBen Gras 				} else
7362fe8fb19SBen Gras 				if (flags & LONG) {
73784d9c625SLionel Sambuc 					double res = wcstod_l(buf, &p, loc);
7382fe8fb19SBen Gras 					*va_arg(ap, double *) = res;
7392fe8fb19SBen Gras 				} else {
74084d9c625SLionel Sambuc 					float res = wcstof_l(buf, &p, loc);
7412fe8fb19SBen Gras 					*va_arg(ap, float *) = res;
7422fe8fb19SBen Gras 				}
7432fe8fb19SBen Gras #ifdef DEBUG
7442fe8fb19SBen Gras 				if (p - buf != (ptrdiff_t)width)
7452fe8fb19SBen Gras 					abort();
7462fe8fb19SBen Gras #endif
7472fe8fb19SBen Gras 				nassigned++;
7482fe8fb19SBen Gras 			}
7492fe8fb19SBen Gras 			nread += width;
7502fe8fb19SBen Gras 			nconversions++;
7512fe8fb19SBen Gras 			break;
7522fe8fb19SBen Gras #endif /* !NO_FLOATING_POINT */
7532fe8fb19SBen Gras 		}
7542fe8fb19SBen Gras 	}
7552fe8fb19SBen Gras input_failure:
756f14fb602SLionel Sambuc 	return nconversions != 0 ? nassigned : EOF;
7572fe8fb19SBen Gras match_failure:
758f14fb602SLionel Sambuc 	return nassigned;
7592fe8fb19SBen Gras }
7602fe8fb19SBen Gras 
7612fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
7622fe8fb19SBen Gras static int
parsefloat(FILE * fp,wchar_t * buf,wchar_t * end,locale_t loc)76384d9c625SLionel Sambuc parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t loc)
7642fe8fb19SBen Gras {
7652fe8fb19SBen Gras 	wchar_t *commit, *p;
7662fe8fb19SBen Gras 	int infnanpos = 0;
7672fe8fb19SBen Gras 	enum {
7682fe8fb19SBen Gras 		S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
7692fe8fb19SBen Gras 		S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
7702fe8fb19SBen Gras 	} state = S_START;
7712fe8fb19SBen Gras 	wchar_t c;
77284d9c625SLionel Sambuc 	wchar_t decpt = (wchar_t)(unsigned char)*localeconv_l(loc)->decimal_point;
7732fe8fb19SBen Gras 	int gotmantdig = 0, ishex = 0;
7742fe8fb19SBen Gras 
7752fe8fb19SBen Gras 	/*
7762fe8fb19SBen Gras 	 * We set commit = p whenever the string we have read so far
7772fe8fb19SBen Gras 	 * constitutes a valid representation of a floating point
7782fe8fb19SBen Gras 	 * number by itself.  At some point, the parse will complete
7792fe8fb19SBen Gras 	 * or fail, and we will ungetc() back to the last commit point.
7802fe8fb19SBen Gras 	 * To ensure that the file offset gets updated properly, it is
7812fe8fb19SBen Gras 	 * always necessary to read at least one character that doesn't
7822fe8fb19SBen Gras 	 * match; thus, we can't short-circuit "infinity" or "nan(...)".
7832fe8fb19SBen Gras 	 */
7842fe8fb19SBen Gras 	commit = buf - 1;
7852fe8fb19SBen Gras 	c = WEOF;
7862fe8fb19SBen Gras 	for (p = buf; p < end; ) {
7872fe8fb19SBen Gras 		if ((c = __fgetwc_unlock(fp)) == WEOF)
7882fe8fb19SBen Gras 			break;
7892fe8fb19SBen Gras reswitch:
7902fe8fb19SBen Gras 		switch (state) {
7912fe8fb19SBen Gras 		case S_START:
7922fe8fb19SBen Gras 			state = S_GOTSIGN;
7932fe8fb19SBen Gras 			if (c == '-' || c == '+')
7942fe8fb19SBen Gras 				break;
7952fe8fb19SBen Gras 			else
7962fe8fb19SBen Gras 				goto reswitch;
7972fe8fb19SBen Gras 		case S_GOTSIGN:
7982fe8fb19SBen Gras 			switch (c) {
7992fe8fb19SBen Gras 			case '0':
8002fe8fb19SBen Gras 				state = S_MAYBEHEX;
8012fe8fb19SBen Gras 				commit = p;
8022fe8fb19SBen Gras 				break;
8032fe8fb19SBen Gras 			case 'I':
8042fe8fb19SBen Gras 			case 'i':
8052fe8fb19SBen Gras 				state = S_INF;
8062fe8fb19SBen Gras 				break;
8072fe8fb19SBen Gras 			case 'N':
8082fe8fb19SBen Gras 			case 'n':
8092fe8fb19SBen Gras 				state = S_NAN;
8102fe8fb19SBen Gras 				break;
8112fe8fb19SBen Gras 			default:
8122fe8fb19SBen Gras 				state = S_DIGITS;
8132fe8fb19SBen Gras 				goto reswitch;
8142fe8fb19SBen Gras 			}
8152fe8fb19SBen Gras 			break;
8162fe8fb19SBen Gras 		case S_INF:
8172fe8fb19SBen Gras 			if (infnanpos > 6 ||
8182fe8fb19SBen Gras 			    (c != "nfinity"[infnanpos] &&
8192fe8fb19SBen Gras 			     c != "NFINITY"[infnanpos]))
8202fe8fb19SBen Gras 				goto parsedone;
8212fe8fb19SBen Gras 			if (infnanpos == 1 || infnanpos == 6)
8222fe8fb19SBen Gras 				commit = p;	/* inf or infinity */
8232fe8fb19SBen Gras 			infnanpos++;
8242fe8fb19SBen Gras 			break;
8252fe8fb19SBen Gras 		case S_NAN:
8262fe8fb19SBen Gras 			switch (infnanpos) {
8272fe8fb19SBen Gras 			case -1:	/* XXX kludge to deal with nan(...) */
8282fe8fb19SBen Gras 				goto parsedone;
8292fe8fb19SBen Gras 			case 0:
8302fe8fb19SBen Gras 				if (c != 'A' && c != 'a')
8312fe8fb19SBen Gras 					goto parsedone;
8322fe8fb19SBen Gras 				break;
8332fe8fb19SBen Gras 			case 1:
8342fe8fb19SBen Gras 				if (c != 'N' && c != 'n')
8352fe8fb19SBen Gras 					goto parsedone;
8362fe8fb19SBen Gras 				else
8372fe8fb19SBen Gras 					commit = p;
8382fe8fb19SBen Gras 				break;
8392fe8fb19SBen Gras 			case 2:
8402fe8fb19SBen Gras 				if (c != '(')
8412fe8fb19SBen Gras 					goto parsedone;
8422fe8fb19SBen Gras 				break;
8432fe8fb19SBen Gras 			default:
8442fe8fb19SBen Gras 				if (c == ')') {
8452fe8fb19SBen Gras 					commit = p;
8462fe8fb19SBen Gras 					infnanpos = -2;
84784d9c625SLionel Sambuc 				} else if (!iswalnum_l(c, loc) && c != '_')
8482fe8fb19SBen Gras 					goto parsedone;
8492fe8fb19SBen Gras 				break;
8502fe8fb19SBen Gras 			}
8512fe8fb19SBen Gras 			infnanpos++;
8522fe8fb19SBen Gras 			break;
8532fe8fb19SBen Gras 		case S_MAYBEHEX:
8542fe8fb19SBen Gras 			state = S_DIGITS;
8552fe8fb19SBen Gras 			if (c == 'X' || c == 'x') {
8562fe8fb19SBen Gras 				ishex = 1;
8572fe8fb19SBen Gras 				break;
8582fe8fb19SBen Gras 			} else {	/* we saw a '0', but no 'x' */
8592fe8fb19SBen Gras 				gotmantdig = 1;
8602fe8fb19SBen Gras 				goto reswitch;
8612fe8fb19SBen Gras 			}
8622fe8fb19SBen Gras 		case S_DIGITS:
86384d9c625SLionel Sambuc 			if ((ishex && iswxdigit_l(c, loc)) ||
86484d9c625SLionel Sambuc 			    iswdigit_l(c, loc))
8652fe8fb19SBen Gras 				gotmantdig = 1;
8662fe8fb19SBen Gras 			else {
8672fe8fb19SBen Gras 				state = S_FRAC;
8682fe8fb19SBen Gras 				if (c != decpt)
8692fe8fb19SBen Gras 					goto reswitch;
8702fe8fb19SBen Gras 			}
8712fe8fb19SBen Gras 			if (gotmantdig)
8722fe8fb19SBen Gras 				commit = p;
8732fe8fb19SBen Gras 			break;
8742fe8fb19SBen Gras 		case S_FRAC:
8752fe8fb19SBen Gras 			if (((c == 'E' || c == 'e') && !ishex) ||
8762fe8fb19SBen Gras 			    ((c == 'P' || c == 'p') && ishex)) {
8772fe8fb19SBen Gras 				if (!gotmantdig)
8782fe8fb19SBen Gras 					goto parsedone;
8792fe8fb19SBen Gras 				else
8802fe8fb19SBen Gras 					state = S_EXP;
88184d9c625SLionel Sambuc 			} else if ((ishex && iswxdigit_l(c, loc)) ||
88284d9c625SLionel Sambuc 			    iswdigit_l(c, loc)) {
8832fe8fb19SBen Gras 				commit = p;
8842fe8fb19SBen Gras 				gotmantdig = 1;
8852fe8fb19SBen Gras 			} else
8862fe8fb19SBen Gras 				goto parsedone;
8872fe8fb19SBen Gras 			break;
8882fe8fb19SBen Gras 		case S_EXP:
8892fe8fb19SBen Gras 			state = S_EXPDIGITS;
8902fe8fb19SBen Gras 			if (c == '-' || c == '+')
8912fe8fb19SBen Gras 				break;
8922fe8fb19SBen Gras 			else
8932fe8fb19SBen Gras 				goto reswitch;
8942fe8fb19SBen Gras 		case S_EXPDIGITS:
89584d9c625SLionel Sambuc 			if (iswdigit_l(c, loc))
8962fe8fb19SBen Gras 				commit = p;
8972fe8fb19SBen Gras 			else
8982fe8fb19SBen Gras 				goto parsedone;
8992fe8fb19SBen Gras 			break;
9002fe8fb19SBen Gras 		default:
9012fe8fb19SBen Gras 			abort();
9022fe8fb19SBen Gras 		}
9032fe8fb19SBen Gras 		*p++ = c;
9042fe8fb19SBen Gras 		c = WEOF;
9052fe8fb19SBen Gras 	}
9062fe8fb19SBen Gras 
9072fe8fb19SBen Gras parsedone:
9082fe8fb19SBen Gras 	if (c != WEOF)
9092fe8fb19SBen Gras 		ungetwc(c, fp);
9102fe8fb19SBen Gras 	while (commit < --p)
9112fe8fb19SBen Gras 		ungetwc(*p, fp);
9122fe8fb19SBen Gras 	*++commit = '\0';
913f14fb602SLionel Sambuc 	_DIAGASSERT(__type_fit(int, commit - buf));
914f14fb602SLionel Sambuc 	return (int)(commit - buf);
9152fe8fb19SBen Gras }
9162fe8fb19SBen Gras #endif
917