xref: /minix3/lib/libc/stdio/vfscanf.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: vfscanf.c,v 1.45 2013/05/17 12:55:57 joerg 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. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  */
342fe8fb19SBen Gras 
352fe8fb19SBen Gras #include <sys/cdefs.h>
362fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
372fe8fb19SBen Gras #if 0
382fe8fb19SBen Gras static char sccsid[] = "@(#)vfscanf.c	8.1 (Berkeley) 6/4/93";
392fe8fb19SBen Gras __FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.41 2007/01/09 00:28:07 imp Exp $");
402fe8fb19SBen Gras #else
41*84d9c625SLionel Sambuc __RCSID("$NetBSD: vfscanf.c,v 1.45 2013/05/17 12:55:57 joerg Exp $");
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
442fe8fb19SBen Gras 
452fe8fb19SBen Gras #include "namespace.h"
462fe8fb19SBen Gras #include <assert.h>
472fe8fb19SBen Gras #include <ctype.h>
482fe8fb19SBen Gras #include <inttypes.h>
492fe8fb19SBen Gras #include <stdio.h>
502fe8fb19SBen Gras #include <stdlib.h>
512fe8fb19SBen Gras #include <stddef.h>
522fe8fb19SBen Gras #include <stdarg.h>
532fe8fb19SBen Gras #include <string.h>
542fe8fb19SBen Gras #include <wchar.h>
552fe8fb19SBen Gras #include <wctype.h>
562fe8fb19SBen Gras 
572fe8fb19SBen Gras #include "reentrant.h"
582fe8fb19SBen Gras #include "local.h"
592fe8fb19SBen Gras 
602fe8fb19SBen Gras #include <locale.h>
61*84d9c625SLionel Sambuc #include "setlocale_local.h"
622fe8fb19SBen Gras 
632fe8fb19SBen Gras /*
642fe8fb19SBen Gras  * Provide an external name for vfscanf.  Note, we don't use the normal
652fe8fb19SBen Gras  * namespace.h method; stdio routines explicitly use the internal name
662fe8fb19SBen Gras  * __svfscanf.
672fe8fb19SBen Gras  */
682fe8fb19SBen Gras #ifdef __weak_alias
692fe8fb19SBen Gras __weak_alias(vfscanf,__svfscanf)
70*84d9c625SLionel Sambuc __weak_alias(vfscanf_l,__svfscanf_l)
712fe8fb19SBen Gras #endif
722fe8fb19SBen Gras 
732fe8fb19SBen Gras #define	BUF		513	/* Maximum length of numeric string. */
742fe8fb19SBen Gras 
752fe8fb19SBen Gras /*
762fe8fb19SBen Gras  * Flags used during conversion.
772fe8fb19SBen Gras  */
782fe8fb19SBen Gras #define	LONG		0x0001	/* l: long or double */
792fe8fb19SBen Gras #define	LONGDBL		0x0002	/* L: long double */
802fe8fb19SBen Gras #define	SHORT		0x0004	/* h: short */
812fe8fb19SBen Gras #define	SUPPRESS	0x0008	/* *: suppress assignment */
822fe8fb19SBen Gras #define	POINTER		0x0010	/* p: void * (as hex) */
832fe8fb19SBen Gras #define	NOSKIP		0x0020	/* [ or c: do not skip blanks */
842fe8fb19SBen Gras #define	LONGLONG	0x0400	/* ll: long long (+ deprecated q: quad) */
852fe8fb19SBen Gras #define	INTMAXT		0x0800	/* j: intmax_t */
862fe8fb19SBen Gras #define	PTRDIFFT	0x1000	/* t: ptrdiff_t */
872fe8fb19SBen Gras #define	SIZET		0x2000	/* z: size_t */
882fe8fb19SBen Gras #define	SHORTSHORT	0x4000	/* hh: char */
892fe8fb19SBen Gras #define	UNSIGNED	0x8000	/* %[oupxX] conversions */
902fe8fb19SBen Gras 
912fe8fb19SBen Gras /*
922fe8fb19SBen Gras  * The following are used in integral conversions only:
932fe8fb19SBen Gras  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
942fe8fb19SBen Gras  */
952fe8fb19SBen Gras #define	SIGNOK		0x00040	/* +/- is (still) legal */
962fe8fb19SBen Gras #define	NDIGITS		0x00080	/* no digits detected */
972fe8fb19SBen Gras #define	PFXOK		0x00100	/* 0x prefix is (still) legal */
982fe8fb19SBen Gras #define	NZDIGITS	0x00200	/* no zero digits detected */
992fe8fb19SBen Gras #define	HAVESIGN	0x10000	/* sign detected */
1002fe8fb19SBen Gras 
1012fe8fb19SBen Gras /*
1022fe8fb19SBen Gras  * Conversion types.
1032fe8fb19SBen Gras  */
1042fe8fb19SBen Gras #define	CT_CHAR		0	/* %c conversion */
1052fe8fb19SBen Gras #define	CT_CCL		1	/* %[...] conversion */
1062fe8fb19SBen Gras #define	CT_STRING	2	/* %s conversion */
1072fe8fb19SBen Gras #define	CT_INT		3	/* %[dioupxX] conversion */
1082fe8fb19SBen Gras #define	CT_FLOAT	4	/* %[efgEFG] conversion */
1092fe8fb19SBen Gras 
110*84d9c625SLionel Sambuc static const u_char *__sccl(char *, const u_char *, locale_t);
1112fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
112*84d9c625SLionel Sambuc static size_t parsefloat(FILE *, char *, char *, locale_t);
1132fe8fb19SBen Gras #endif
1142fe8fb19SBen Gras 
1152fe8fb19SBen Gras int __scanfdebug = 0;
1162fe8fb19SBen Gras 
1172fe8fb19SBen Gras #define __collate_load_error /*CONSTCOND*/0
1182fe8fb19SBen Gras static int
__collate_range_cmp(int c1,int c2,locale_t loc)119*84d9c625SLionel Sambuc __collate_range_cmp(int c1, int c2, locale_t loc)
1202fe8fb19SBen Gras {
1212fe8fb19SBen Gras 	static char s1[2], s2[2];
1222fe8fb19SBen Gras 
1232fe8fb19SBen Gras 	s1[0] = c1;
1242fe8fb19SBen Gras 	s2[0] = c2;
125*84d9c625SLionel Sambuc 	return strcoll_l(s1, s2, loc);
1262fe8fb19SBen Gras }
1272fe8fb19SBen Gras 
1282fe8fb19SBen Gras 
1292fe8fb19SBen Gras /*
1302fe8fb19SBen Gras  * __svfscanf - MT-safe version
1312fe8fb19SBen Gras  */
1322fe8fb19SBen Gras int
__svfscanf(FILE * fp,char const * fmt0,va_list ap)1332fe8fb19SBen Gras __svfscanf(FILE *fp, char const *fmt0, va_list ap)
1342fe8fb19SBen Gras {
135*84d9c625SLionel Sambuc 	return __svfscanf_l(fp, _current_locale(), fmt0, ap);
136*84d9c625SLionel Sambuc }
137*84d9c625SLionel Sambuc 
138*84d9c625SLionel Sambuc int
__svfscanf_l(FILE * fp,locale_t loc,char const * fmt0,va_list ap)139*84d9c625SLionel Sambuc __svfscanf_l(FILE *fp, locale_t loc, char const *fmt0, va_list ap)
140*84d9c625SLionel Sambuc {
1412fe8fb19SBen Gras 	int ret;
1422fe8fb19SBen Gras 
1432fe8fb19SBen Gras 	FLOCKFILE(fp);
144*84d9c625SLionel Sambuc 	ret = __svfscanf_unlocked_l(fp, loc, fmt0, ap);
1452fe8fb19SBen Gras 	FUNLOCKFILE(fp);
146f14fb602SLionel Sambuc 	return ret;
1472fe8fb19SBen Gras }
1482fe8fb19SBen Gras 
1492fe8fb19SBen Gras #define SCANF_SKIP_SPACE() \
1502fe8fb19SBen Gras do { \
151*84d9c625SLionel Sambuc 	while ((fp->_r > 0 || __srefill(fp) == 0) && isspace_l(*fp->_p, loc)) \
1522fe8fb19SBen Gras 		nread++, fp->_r--, fp->_p++; \
1532fe8fb19SBen Gras } while (/*CONSTCOND*/ 0)
1542fe8fb19SBen Gras 
1552fe8fb19SBen Gras /*
1562fe8fb19SBen Gras  * __svfscanf_unlocked - non-MT-safe version of __svfscanf
1572fe8fb19SBen Gras  */
1582fe8fb19SBen Gras int
__svfscanf_unlocked_l(FILE * fp,locale_t loc,const char * fmt0,va_list ap)159*84d9c625SLionel Sambuc __svfscanf_unlocked_l(FILE *fp, locale_t loc, const char *fmt0, va_list ap)
1602fe8fb19SBen Gras {
1612fe8fb19SBen Gras 	const u_char *fmt = (const u_char *)fmt0;
1622fe8fb19SBen Gras 	int c;			/* character from format, or conversion */
1632fe8fb19SBen Gras 	size_t width;		/* field width, or 0 */
1642fe8fb19SBen Gras 	char *p;		/* points into all kinds of strings */
1652fe8fb19SBen Gras 	size_t n;		/* handy size_t */
1662fe8fb19SBen Gras 	int flags;		/* flags as defined above */
1672fe8fb19SBen Gras 	char *p0;		/* saves original value of p when necessary */
1682fe8fb19SBen Gras 	int nassigned;		/* number of fields assigned */
1692fe8fb19SBen Gras 	int nconversions;	/* number of conversions */
170f14fb602SLionel Sambuc 	size_t nread;		/* number of characters consumed from fp */
1712fe8fb19SBen Gras 	int base;		/* base argument to conversion function */
1722fe8fb19SBen Gras 	char ccltab[256];	/* character class table for %[...] */
1732fe8fb19SBen Gras 	char buf[BUF];		/* buffer for numeric and mb conversions */
1742fe8fb19SBen Gras 	wchar_t *wcp;		/* handy wide-character pointer */
1752fe8fb19SBen Gras 	size_t nconv;		/* length of multibyte sequence converted */
1762fe8fb19SBen Gras 	static const mbstate_t initial;
1772fe8fb19SBen Gras 	mbstate_t mbs;
1782fe8fb19SBen Gras 
1792fe8fb19SBen Gras 	/* `basefix' is used to avoid `if' tests in the integer scanner */
1802fe8fb19SBen Gras 	static const short basefix[17] =
1812fe8fb19SBen Gras 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
1822fe8fb19SBen Gras 
1832fe8fb19SBen Gras 	_DIAGASSERT(fp != NULL);
1842fe8fb19SBen Gras 	_DIAGASSERT(fmt0 != NULL);
1852fe8fb19SBen Gras 
1862fe8fb19SBen Gras 	_SET_ORIENTATION(fp, -1);
1872fe8fb19SBen Gras 
1882fe8fb19SBen Gras 	nassigned = 0;
1892fe8fb19SBen Gras 	nconversions = 0;
1902fe8fb19SBen Gras 	nread = 0;
1912fe8fb19SBen Gras 	base = 0;
1922fe8fb19SBen Gras 	for (;;) {
1932fe8fb19SBen Gras 		c = (unsigned char)*fmt++;
1942fe8fb19SBen Gras 		if (c == 0)
195f14fb602SLionel Sambuc 			return nassigned;
196*84d9c625SLionel Sambuc 		if (isspace_l(c, loc)) {
1972fe8fb19SBen Gras 			while ((fp->_r > 0 || __srefill(fp) == 0) &&
198*84d9c625SLionel Sambuc 			    isspace_l(*fp->_p, loc))
1992fe8fb19SBen Gras 				nread++, fp->_r--, fp->_p++;
2002fe8fb19SBen Gras 			continue;
2012fe8fb19SBen Gras 		}
2022fe8fb19SBen Gras 		if (c != '%')
2032fe8fb19SBen Gras 			goto literal;
2042fe8fb19SBen Gras 		width = 0;
2052fe8fb19SBen Gras 		flags = 0;
2062fe8fb19SBen Gras 		/*
2072fe8fb19SBen Gras 		 * switch on the format.  continue if done;
2082fe8fb19SBen Gras 		 * break once format type is derived.
2092fe8fb19SBen Gras 		 */
2102fe8fb19SBen Gras again:		c = *fmt++;
2112fe8fb19SBen Gras 		switch (c) {
2122fe8fb19SBen Gras 		case '%':
2132fe8fb19SBen Gras 			SCANF_SKIP_SPACE();
2142fe8fb19SBen Gras literal:
2152fe8fb19SBen Gras 			if (fp->_r <= 0 && __srefill(fp))
2162fe8fb19SBen Gras 				goto input_failure;
2172fe8fb19SBen Gras 			if (*fp->_p != c)
2182fe8fb19SBen Gras 				goto match_failure;
2192fe8fb19SBen Gras 			fp->_r--, fp->_p++;
2202fe8fb19SBen Gras 			nread++;
2212fe8fb19SBen Gras 			continue;
2222fe8fb19SBen Gras 
2232fe8fb19SBen Gras 		case '*':
2242fe8fb19SBen Gras 			flags |= SUPPRESS;
2252fe8fb19SBen Gras 			goto again;
2262fe8fb19SBen Gras 		case 'j':
2272fe8fb19SBen Gras 			flags |= INTMAXT;
2282fe8fb19SBen Gras 			goto again;
2292fe8fb19SBen Gras 		case 'l':
2302fe8fb19SBen Gras 			if (flags & LONG) {
2312fe8fb19SBen Gras 				flags &= ~LONG;
2322fe8fb19SBen Gras 				flags |= LONGLONG;
2332fe8fb19SBen Gras 			} else
2342fe8fb19SBen Gras 				flags |= LONG;
2352fe8fb19SBen Gras 			goto again;
2362fe8fb19SBen Gras 		case 'q':
2372fe8fb19SBen Gras 			flags |= LONGLONG;	/* not quite */
2382fe8fb19SBen Gras 			goto again;
2392fe8fb19SBen Gras 		case 't':
2402fe8fb19SBen Gras 			flags |= PTRDIFFT;
2412fe8fb19SBen Gras 			goto again;
2422fe8fb19SBen Gras 		case 'z':
2432fe8fb19SBen Gras 			flags |= SIZET;
2442fe8fb19SBen Gras 			goto again;
2452fe8fb19SBen Gras 		case 'L':
2462fe8fb19SBen Gras 			flags |= LONGDBL;
2472fe8fb19SBen Gras 			goto again;
2482fe8fb19SBen Gras 		case 'h':
2492fe8fb19SBen Gras 			if (flags & SHORT) {
2502fe8fb19SBen Gras 				flags &= ~SHORT;
2512fe8fb19SBen Gras 				flags |= SHORTSHORT;
2522fe8fb19SBen Gras 			} else
2532fe8fb19SBen Gras 				flags |= SHORT;
2542fe8fb19SBen Gras 			goto again;
2552fe8fb19SBen Gras 
2562fe8fb19SBen Gras 		case '0': case '1': case '2': case '3': case '4':
2572fe8fb19SBen Gras 		case '5': case '6': case '7': case '8': case '9':
2582fe8fb19SBen Gras 			width = width * 10 + c - '0';
2592fe8fb19SBen Gras 			goto again;
2602fe8fb19SBen Gras 
2612fe8fb19SBen Gras 		/*
2622fe8fb19SBen Gras 		 * Conversions.
2632fe8fb19SBen Gras 		 */
2642fe8fb19SBen Gras 		case 'd':
2652fe8fb19SBen Gras 			c = CT_INT;
2662fe8fb19SBen Gras 			base = 10;
2672fe8fb19SBen Gras 			break;
2682fe8fb19SBen Gras 
2692fe8fb19SBen Gras 		case 'i':
2702fe8fb19SBen Gras 			c = CT_INT;
2712fe8fb19SBen Gras 			base = 0;
2722fe8fb19SBen Gras 			break;
2732fe8fb19SBen Gras 
2742fe8fb19SBen Gras 		case 'o':
2752fe8fb19SBen Gras 			c = CT_INT;
2762fe8fb19SBen Gras 			flags |= UNSIGNED;
2772fe8fb19SBen Gras 			base = 8;
2782fe8fb19SBen Gras 			break;
2792fe8fb19SBen Gras 
2802fe8fb19SBen Gras 		case 'u':
2812fe8fb19SBen Gras 			c = CT_INT;
2822fe8fb19SBen Gras 			flags |= UNSIGNED;
2832fe8fb19SBen Gras 			base = 10;
2842fe8fb19SBen Gras 			break;
2852fe8fb19SBen Gras 
2862fe8fb19SBen Gras 		case 'X':
2872fe8fb19SBen Gras 		case 'x':
2882fe8fb19SBen Gras 			flags |= PFXOK;	/* enable 0x prefixing */
2892fe8fb19SBen Gras 			c = CT_INT;
2902fe8fb19SBen Gras 			flags |= UNSIGNED;
2912fe8fb19SBen Gras 			base = 16;
2922fe8fb19SBen Gras 			break;
2932fe8fb19SBen Gras 
2942fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
2952fe8fb19SBen Gras 		case 'A': case 'E': case 'F': case 'G':
2962fe8fb19SBen Gras 		case 'a': case 'e': case 'f': case 'g':
2972fe8fb19SBen Gras 			c = CT_FLOAT;
2982fe8fb19SBen Gras 			break;
2992fe8fb19SBen Gras #endif
3002fe8fb19SBen Gras 
3012fe8fb19SBen Gras 		case 'S':
3022fe8fb19SBen Gras 			flags |= LONG;
3032fe8fb19SBen Gras 			/* FALLTHROUGH */
3042fe8fb19SBen Gras 		case 's':
3052fe8fb19SBen Gras 			c = CT_STRING;
3062fe8fb19SBen Gras 			break;
3072fe8fb19SBen Gras 
3082fe8fb19SBen Gras 		case '[':
309*84d9c625SLionel Sambuc 			fmt = __sccl(ccltab, fmt, loc);
3102fe8fb19SBen Gras 			flags |= NOSKIP;
3112fe8fb19SBen Gras 			c = CT_CCL;
3122fe8fb19SBen Gras 			break;
3132fe8fb19SBen Gras 
3142fe8fb19SBen Gras 		case 'C':
3152fe8fb19SBen Gras 			flags |= LONG;
3162fe8fb19SBen Gras 			/* FALLTHROUGH */
3172fe8fb19SBen Gras 		case 'c':
3182fe8fb19SBen Gras 			flags |= NOSKIP;
3192fe8fb19SBen Gras 			c = CT_CHAR;
3202fe8fb19SBen Gras 			break;
3212fe8fb19SBen Gras 
3222fe8fb19SBen Gras 		case 'p':	/* pointer format is like hex */
3232fe8fb19SBen Gras 			flags |= POINTER | PFXOK;
3242fe8fb19SBen Gras 			c = CT_INT;		/* assumes sizeof(uintmax_t) */
3252fe8fb19SBen Gras 			flags |= UNSIGNED;	/*      >= sizeof(uintptr_t) */
3262fe8fb19SBen Gras 			base = 16;
3272fe8fb19SBen Gras 			break;
3282fe8fb19SBen Gras 
3292fe8fb19SBen Gras 		case 'n':
3302fe8fb19SBen Gras 			nconversions++;
3312fe8fb19SBen Gras 			if (flags & SUPPRESS)	/* ??? */
3322fe8fb19SBen Gras 				continue;
3332fe8fb19SBen Gras 			if (flags & SHORTSHORT)
334f14fb602SLionel Sambuc 				*va_arg(ap, char *) = (char)nread;
3352fe8fb19SBen Gras 			else if (flags & SHORT)
336f14fb602SLionel Sambuc 				*va_arg(ap, short *) = (short)nread;
3372fe8fb19SBen Gras 			else if (flags & LONG)
3382fe8fb19SBen Gras 				*va_arg(ap, long *) = nread;
3392fe8fb19SBen Gras 			else if (flags & LONGLONG)
3402fe8fb19SBen Gras 				*va_arg(ap, long long *) = nread;
3412fe8fb19SBen Gras 			else if (flags & INTMAXT)
3422fe8fb19SBen Gras 				*va_arg(ap, intmax_t *) = nread;
3432fe8fb19SBen Gras 			else if (flags & SIZET)
3442fe8fb19SBen Gras 				*va_arg(ap, size_t *) = nread;
3452fe8fb19SBen Gras 			else if (flags & PTRDIFFT)
3462fe8fb19SBen Gras 				*va_arg(ap, ptrdiff_t *) = nread;
3472fe8fb19SBen Gras 			else
348f14fb602SLionel Sambuc 				*va_arg(ap, int *) = (int)nread;
3492fe8fb19SBen Gras 			continue;
3502fe8fb19SBen Gras 
3512fe8fb19SBen Gras 		default:
3522fe8fb19SBen Gras 			goto match_failure;
3532fe8fb19SBen Gras 
3542fe8fb19SBen Gras 		/*
3552fe8fb19SBen Gras 		 * Disgusting backwards compatibility hack.	XXX
3562fe8fb19SBen Gras 		 */
3572fe8fb19SBen Gras 		case '\0':	/* compat */
358f14fb602SLionel Sambuc 			return EOF;
3592fe8fb19SBen Gras 		}
3602fe8fb19SBen Gras 
3612fe8fb19SBen Gras 		/*
3622fe8fb19SBen Gras 		 * We have a conversion that requires input.
3632fe8fb19SBen Gras 		 */
3642fe8fb19SBen Gras 		if (fp->_r <= 0 && __srefill(fp))
3652fe8fb19SBen Gras 			goto input_failure;
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) {
372*84d9c625SLionel Sambuc 			while (isspace_l(*fp->_p, loc)) {
3732fe8fb19SBen Gras 				nread++;
3742fe8fb19SBen Gras 				if (--fp->_r > 0)
3752fe8fb19SBen Gras 					fp->_p++;
3762fe8fb19SBen Gras 				else if (__srefill(fp))
3772fe8fb19SBen Gras 					goto input_failure;
3782fe8fb19SBen Gras 			}
3792fe8fb19SBen Gras 			/*
3802fe8fb19SBen Gras 			 * Note that there is at least one character in
3812fe8fb19SBen Gras 			 * the buffer, so conversions that do not set NOSKIP
3822fe8fb19SBen Gras 			 * ca no longer result in an input failure.
3832fe8fb19SBen Gras 			 */
3842fe8fb19SBen Gras 		}
3852fe8fb19SBen Gras 
3862fe8fb19SBen Gras 		/*
3872fe8fb19SBen Gras 		 * Do the conversion.
3882fe8fb19SBen Gras 		 */
3892fe8fb19SBen Gras 		switch (c) {
3902fe8fb19SBen Gras 
3912fe8fb19SBen Gras 		case CT_CHAR:
3922fe8fb19SBen Gras 			/* scan arbitrary characters (sets NOSKIP) */
3932fe8fb19SBen Gras 			if (width == 0)
3942fe8fb19SBen Gras 				width = 1;
3952fe8fb19SBen Gras 			if (flags & LONG) {
3962fe8fb19SBen Gras 				if ((flags & SUPPRESS) == 0)
3972fe8fb19SBen Gras 					wcp = va_arg(ap, wchar_t *);
3982fe8fb19SBen Gras 				else
3992fe8fb19SBen Gras 					wcp = NULL;
4002fe8fb19SBen Gras 				n = 0;
4012fe8fb19SBen Gras 				while (width != 0) {
402*84d9c625SLionel Sambuc 					if (n == MB_CUR_MAX_L(loc)) {
4032fe8fb19SBen Gras 						fp->_flags |= __SERR;
4042fe8fb19SBen Gras 						goto input_failure;
4052fe8fb19SBen Gras 					}
4062fe8fb19SBen Gras 					buf[n++] = *fp->_p;
4072fe8fb19SBen Gras 					fp->_p++;
4082fe8fb19SBen Gras 					fp->_r--;
4092fe8fb19SBen Gras 					mbs = initial;
410*84d9c625SLionel Sambuc 					nconv = mbrtowc_l(wcp, buf, n, &mbs,
411*84d9c625SLionel Sambuc 					    loc);
4122fe8fb19SBen Gras 					if (nconv == (size_t)-1) {
4132fe8fb19SBen Gras 						fp->_flags |= __SERR;
4142fe8fb19SBen Gras 						goto input_failure;
4152fe8fb19SBen Gras 					}
4162fe8fb19SBen Gras 					if (nconv == 0 && !(flags & SUPPRESS))
4172fe8fb19SBen Gras 						*wcp = L'\0';
4182fe8fb19SBen Gras 					if (nconv != (size_t)-2) {
4192fe8fb19SBen Gras 						nread += n;
4202fe8fb19SBen Gras 						width--;
4212fe8fb19SBen Gras 						if (!(flags & SUPPRESS))
4222fe8fb19SBen Gras 							wcp++;
4232fe8fb19SBen Gras 						n = 0;
4242fe8fb19SBen Gras 					}
4252fe8fb19SBen Gras 					if (fp->_r <= 0 && __srefill(fp)) {
4262fe8fb19SBen Gras 						if (n != 0) {
4272fe8fb19SBen Gras 							fp->_flags |= __SERR;
4282fe8fb19SBen Gras 							goto input_failure;
4292fe8fb19SBen Gras 						}
4302fe8fb19SBen Gras 						break;
4312fe8fb19SBen Gras 					}
4322fe8fb19SBen Gras 				}
4332fe8fb19SBen Gras 				if (!(flags & SUPPRESS))
4342fe8fb19SBen Gras 					nassigned++;
4352fe8fb19SBen Gras 			} else if (flags & SUPPRESS) {
4362fe8fb19SBen Gras 				size_t sum = 0;
4372fe8fb19SBen Gras 				for (;;) {
4382fe8fb19SBen Gras 					if ((n = fp->_r) < width) {
4392fe8fb19SBen Gras 						sum += n;
4402fe8fb19SBen Gras 						width -= n;
4412fe8fb19SBen Gras 						fp->_p += n;
4422fe8fb19SBen Gras 						if (__srefill(fp)) {
4432fe8fb19SBen Gras 							if (sum == 0)
4442fe8fb19SBen Gras 							    goto input_failure;
4452fe8fb19SBen Gras 							break;
4462fe8fb19SBen Gras 						}
4472fe8fb19SBen Gras 					} else {
4482fe8fb19SBen Gras 						sum += width;
449f14fb602SLionel Sambuc 						_DIAGASSERT(__type_fit(int,
450f14fb602SLionel Sambuc 						    fp->_r - width));
451f14fb602SLionel Sambuc 						fp->_r -= (int)width;
4522fe8fb19SBen Gras 						fp->_p += width;
4532fe8fb19SBen Gras 						break;
4542fe8fb19SBen Gras 					}
4552fe8fb19SBen Gras 				}
4562fe8fb19SBen Gras 				nread += sum;
4572fe8fb19SBen Gras 			} else {
4582fe8fb19SBen Gras 				size_t r = fread(va_arg(ap, char *), 1,
4592fe8fb19SBen Gras 				    width, fp);
4602fe8fb19SBen Gras 
4612fe8fb19SBen Gras 				if (r == 0)
4622fe8fb19SBen Gras 					goto input_failure;
4632fe8fb19SBen Gras 				nread += r;
4642fe8fb19SBen Gras 				nassigned++;
4652fe8fb19SBen Gras 			}
4662fe8fb19SBen Gras 			nconversions++;
4672fe8fb19SBen Gras 			break;
4682fe8fb19SBen Gras 
4692fe8fb19SBen Gras 		case CT_CCL:
4702fe8fb19SBen Gras 			/* scan a (nonempty) character class (sets NOSKIP) */
4712fe8fb19SBen Gras 			if (width == 0)
4722fe8fb19SBen Gras 				width = (size_t)~0;	/* `infinity' */
4732fe8fb19SBen Gras 			/* take only those things in the class */
4742fe8fb19SBen Gras 			if (flags & LONG) {
4752fe8fb19SBen Gras 				wchar_t twc;
4762fe8fb19SBen Gras 				int nchars;
4772fe8fb19SBen Gras 
4782fe8fb19SBen Gras 				if ((flags & SUPPRESS) == 0)
4792fe8fb19SBen Gras 					wcp = va_arg(ap, wchar_t *);
4802fe8fb19SBen Gras 				else
4812fe8fb19SBen Gras 					wcp = &twc;
4822fe8fb19SBen Gras 				n = 0;
4832fe8fb19SBen Gras 				nchars = 0;
4842fe8fb19SBen Gras 				while (width != 0) {
485*84d9c625SLionel Sambuc 					if (n == MB_CUR_MAX_L(loc)) {
4862fe8fb19SBen Gras 						fp->_flags |= __SERR;
4872fe8fb19SBen Gras 						goto input_failure;
4882fe8fb19SBen Gras 					}
4892fe8fb19SBen Gras 					buf[n++] = *fp->_p;
4902fe8fb19SBen Gras 					fp->_p++;
4912fe8fb19SBen Gras 					fp->_r--;
4922fe8fb19SBen Gras 					mbs = initial;
493*84d9c625SLionel Sambuc 					nconv = mbrtowc_l(wcp, buf, n, &mbs,
494*84d9c625SLionel Sambuc 					    loc);
4952fe8fb19SBen Gras 					if (nconv == (size_t)-1) {
4962fe8fb19SBen Gras 						fp->_flags |= __SERR;
4972fe8fb19SBen Gras 						goto input_failure;
4982fe8fb19SBen Gras 					}
4992fe8fb19SBen Gras 					if (nconv == 0)
5002fe8fb19SBen Gras 						*wcp = L'\0';
5012fe8fb19SBen Gras 					if (nconv != (size_t)-2) {
502*84d9c625SLionel Sambuc 						if (wctob_l(*wcp, loc) != EOF &&
503*84d9c625SLionel Sambuc 						    !ccltab[wctob_l(*wcp, loc)]) {
5042fe8fb19SBen Gras 							while (n != 0) {
5052fe8fb19SBen Gras 								n--;
5062fe8fb19SBen Gras 								(void)ungetc(buf[n],
5072fe8fb19SBen Gras 								    fp);
5082fe8fb19SBen Gras 							}
5092fe8fb19SBen Gras 							break;
5102fe8fb19SBen Gras 						}
5112fe8fb19SBen Gras 						nread += n;
5122fe8fb19SBen Gras 						width--;
5132fe8fb19SBen Gras 						if (!(flags & SUPPRESS))
5142fe8fb19SBen Gras 							wcp++;
5152fe8fb19SBen Gras 						nchars++;
5162fe8fb19SBen Gras 						n = 0;
5172fe8fb19SBen Gras 					}
5182fe8fb19SBen Gras 					if (fp->_r <= 0 && __srefill(fp)) {
5192fe8fb19SBen Gras 						if (n != 0) {
5202fe8fb19SBen Gras 							fp->_flags |= __SERR;
5212fe8fb19SBen Gras 							goto input_failure;
5222fe8fb19SBen Gras 						}
5232fe8fb19SBen Gras 						break;
5242fe8fb19SBen Gras 					}
5252fe8fb19SBen Gras 				}
5262fe8fb19SBen Gras 				if (n != 0) {
5272fe8fb19SBen Gras 					fp->_flags |= __SERR;
5282fe8fb19SBen Gras 					goto input_failure;
5292fe8fb19SBen Gras 				}
5302fe8fb19SBen Gras 				n = nchars;
5312fe8fb19SBen Gras 				if (n == 0)
5322fe8fb19SBen Gras 					goto match_failure;
5332fe8fb19SBen Gras 				if (!(flags & SUPPRESS)) {
5342fe8fb19SBen Gras 					*wcp = L'\0';
5352fe8fb19SBen Gras 					nassigned++;
5362fe8fb19SBen Gras 				}
5372fe8fb19SBen Gras 			} else if (flags & SUPPRESS) {
5382fe8fb19SBen Gras 				n = 0;
5392fe8fb19SBen Gras 				while (ccltab[*fp->_p]) {
5402fe8fb19SBen Gras 					n++, fp->_r--, fp->_p++;
5412fe8fb19SBen Gras 					if (--width == 0)
5422fe8fb19SBen Gras 						break;
5432fe8fb19SBen Gras 					if (fp->_r <= 0 && __srefill(fp)) {
5442fe8fb19SBen Gras 						if (n == 0)
5452fe8fb19SBen Gras 							goto input_failure;
5462fe8fb19SBen Gras 						break;
5472fe8fb19SBen Gras 					}
5482fe8fb19SBen Gras 				}
5492fe8fb19SBen Gras 				if (n == 0)
5502fe8fb19SBen Gras 					goto match_failure;
5512fe8fb19SBen Gras 			} else {
5522fe8fb19SBen Gras 				p0 = p = va_arg(ap, char *);
5532fe8fb19SBen Gras 				while (ccltab[*fp->_p]) {
5542fe8fb19SBen Gras 					fp->_r--;
5552fe8fb19SBen Gras 					*p++ = *fp->_p++;
5562fe8fb19SBen Gras 					if (--width == 0)
5572fe8fb19SBen Gras 						break;
5582fe8fb19SBen Gras 					if (fp->_r <= 0 && __srefill(fp)) {
5592fe8fb19SBen Gras 						if (p == p0)
5602fe8fb19SBen Gras 							goto input_failure;
5612fe8fb19SBen Gras 						break;
5622fe8fb19SBen Gras 					}
5632fe8fb19SBen Gras 				}
5642fe8fb19SBen Gras 				n = p - p0;
5652fe8fb19SBen Gras 				if (n == 0)
5662fe8fb19SBen Gras 					goto match_failure;
5672fe8fb19SBen Gras 				*p = 0;
5682fe8fb19SBen Gras 				nassigned++;
5692fe8fb19SBen Gras 			}
5702fe8fb19SBen Gras 			nread += n;
5712fe8fb19SBen Gras 			nconversions++;
5722fe8fb19SBen Gras 			break;
5732fe8fb19SBen Gras 
5742fe8fb19SBen Gras 		case CT_STRING:
5752fe8fb19SBen Gras 			/* like CCL, but zero-length string OK, & no NOSKIP */
5762fe8fb19SBen Gras 			if (width == 0)
5772fe8fb19SBen Gras 				width = (size_t)~0;
5782fe8fb19SBen Gras 			if (flags & LONG) {
5792fe8fb19SBen Gras 				wchar_t twc;
5802fe8fb19SBen Gras 
5812fe8fb19SBen Gras 				if ((flags & SUPPRESS) == 0)
5822fe8fb19SBen Gras 					wcp = va_arg(ap, wchar_t *);
5832fe8fb19SBen Gras 				else
5842fe8fb19SBen Gras 					wcp = &twc;
5852fe8fb19SBen Gras 				n = 0;
586*84d9c625SLionel Sambuc 				while (!isspace_l(*fp->_p, loc) && width != 0) {
587*84d9c625SLionel Sambuc 					if (n == MB_CUR_MAX_L(loc)) {
5882fe8fb19SBen Gras 						fp->_flags |= __SERR;
5892fe8fb19SBen Gras 						goto input_failure;
5902fe8fb19SBen Gras 					}
5912fe8fb19SBen Gras 					buf[n++] = *fp->_p;
5922fe8fb19SBen Gras 					fp->_p++;
5932fe8fb19SBen Gras 					fp->_r--;
5942fe8fb19SBen Gras 					mbs = initial;
595*84d9c625SLionel Sambuc 					nconv = mbrtowc_l(wcp, buf, n, &mbs,
596*84d9c625SLionel Sambuc 					    loc);
5972fe8fb19SBen Gras 					if (nconv == (size_t)-1) {
5982fe8fb19SBen Gras 						fp->_flags |= __SERR;
5992fe8fb19SBen Gras 						goto input_failure;
6002fe8fb19SBen Gras 					}
6012fe8fb19SBen Gras 					if (nconv == 0)
6022fe8fb19SBen Gras 						*wcp = L'\0';
6032fe8fb19SBen Gras 					if (nconv != (size_t)-2) {
604*84d9c625SLionel Sambuc 						if (iswspace_l(*wcp, loc)) {
6052fe8fb19SBen Gras 							while (n != 0) {
6062fe8fb19SBen Gras 								n--;
6072fe8fb19SBen Gras 								(void)ungetc(buf[n],
6082fe8fb19SBen Gras 								    fp);
6092fe8fb19SBen Gras 							}
6102fe8fb19SBen Gras 							break;
6112fe8fb19SBen Gras 						}
6122fe8fb19SBen Gras 						nread += n;
6132fe8fb19SBen Gras 						width--;
6142fe8fb19SBen Gras 						if (!(flags & SUPPRESS))
6152fe8fb19SBen Gras 							wcp++;
6162fe8fb19SBen Gras 						n = 0;
6172fe8fb19SBen Gras 					}
6182fe8fb19SBen Gras 					if (fp->_r <= 0 && __srefill(fp)) {
6192fe8fb19SBen Gras 						if (n != 0) {
6202fe8fb19SBen Gras 							fp->_flags |= __SERR;
6212fe8fb19SBen Gras 							goto input_failure;
6222fe8fb19SBen Gras 						}
6232fe8fb19SBen Gras 						break;
6242fe8fb19SBen Gras 					}
6252fe8fb19SBen Gras 				}
6262fe8fb19SBen Gras 				if (!(flags & SUPPRESS)) {
6272fe8fb19SBen Gras 					*wcp = L'\0';
6282fe8fb19SBen Gras 					nassigned++;
6292fe8fb19SBen Gras 				}
6302fe8fb19SBen Gras 			} else if (flags & SUPPRESS) {
6312fe8fb19SBen Gras 				n = 0;
632*84d9c625SLionel Sambuc 				while (!isspace_l(*fp->_p, loc)) {
6332fe8fb19SBen Gras 					n++, fp->_r--, fp->_p++;
6342fe8fb19SBen Gras 					if (--width == 0)
6352fe8fb19SBen Gras 						break;
6362fe8fb19SBen Gras 					if (fp->_r <= 0 && __srefill(fp))
6372fe8fb19SBen Gras 						break;
6382fe8fb19SBen Gras 				}
6392fe8fb19SBen Gras 				nread += n;
6402fe8fb19SBen Gras 			} else {
6412fe8fb19SBen Gras 				p0 = p = va_arg(ap, char *);
642*84d9c625SLionel Sambuc 				while (!isspace_l(*fp->_p, loc)) {
6432fe8fb19SBen Gras 					fp->_r--;
6442fe8fb19SBen Gras 					*p++ = *fp->_p++;
6452fe8fb19SBen Gras 					if (--width == 0)
6462fe8fb19SBen Gras 						break;
6472fe8fb19SBen Gras 					if (fp->_r <= 0 && __srefill(fp))
6482fe8fb19SBen Gras 						break;
6492fe8fb19SBen Gras 				}
6502fe8fb19SBen Gras 				*p = 0;
6512fe8fb19SBen Gras 				nread += p - p0;
6522fe8fb19SBen Gras 				nassigned++;
6532fe8fb19SBen Gras 			}
6542fe8fb19SBen Gras 			nconversions++;
6552fe8fb19SBen Gras 			continue;
6562fe8fb19SBen Gras 
6572fe8fb19SBen Gras 		case CT_INT:
6582fe8fb19SBen Gras 			/* scan an integer as if by the conversion function */
6592fe8fb19SBen Gras #ifdef hardway
6602fe8fb19SBen Gras 			if (width == 0 || width > sizeof(buf) - 1)
6612fe8fb19SBen Gras 				width = sizeof(buf) - 1;
6622fe8fb19SBen Gras #else
6632fe8fb19SBen Gras 			/* size_t is unsigned, hence this optimisation */
6642fe8fb19SBen Gras 			if (--width > sizeof(buf) - 2)
6652fe8fb19SBen Gras 				width = sizeof(buf) - 2;
6662fe8fb19SBen Gras 			width++;
6672fe8fb19SBen Gras #endif
6682fe8fb19SBen Gras 			flags |= SIGNOK | NDIGITS | NZDIGITS;
6692fe8fb19SBen Gras 			for (p = buf; width; width--) {
6702fe8fb19SBen Gras 				c = *fp->_p;
6712fe8fb19SBen Gras 				/*
6722fe8fb19SBen Gras 				 * Switch on the character; `goto ok'
6732fe8fb19SBen Gras 				 * if we accept it as a part of number.
6742fe8fb19SBen Gras 				 */
6752fe8fb19SBen Gras 				switch (c) {
6762fe8fb19SBen Gras 
6772fe8fb19SBen Gras 				/*
6782fe8fb19SBen Gras 				 * The digit 0 is always legal, but is
6792fe8fb19SBen Gras 				 * special.  For %i conversions, if no
6802fe8fb19SBen Gras 				 * digits (zero or nonzero) have been
6812fe8fb19SBen Gras 				 * scanned (only signs), we will have
6822fe8fb19SBen Gras 				 * base==0.  In that case, we should set
6832fe8fb19SBen Gras 				 * it to 8 and enable 0x prefixing.
6842fe8fb19SBen Gras 				 * Also, if we have not scanned zero digits
6852fe8fb19SBen Gras 				 * before this, do not turn off prefixing
6862fe8fb19SBen Gras 				 * (someone else will turn it off if we
6872fe8fb19SBen Gras 				 * have scanned any nonzero digits).
6882fe8fb19SBen Gras 				 */
6892fe8fb19SBen Gras 				case '0':
6902fe8fb19SBen Gras 					if (base == 0) {
6912fe8fb19SBen Gras 						base = 8;
6922fe8fb19SBen Gras 						flags |= PFXOK;
6932fe8fb19SBen Gras 					}
6942fe8fb19SBen Gras 					if (flags & NZDIGITS)
6952fe8fb19SBen Gras 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
6962fe8fb19SBen Gras 					else
6972fe8fb19SBen Gras 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
6982fe8fb19SBen Gras 					goto ok;
6992fe8fb19SBen Gras 
7002fe8fb19SBen Gras 				/* 1 through 7 always legal */
7012fe8fb19SBen Gras 				case '1': case '2': case '3':
7022fe8fb19SBen Gras 				case '4': case '5': case '6': case '7':
7032fe8fb19SBen Gras 					base = basefix[base];
7042fe8fb19SBen Gras 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
7052fe8fb19SBen Gras 					goto ok;
7062fe8fb19SBen Gras 
7072fe8fb19SBen Gras 				/* digits 8 and 9 ok iff decimal or hex */
7082fe8fb19SBen Gras 				case '8': case '9':
7092fe8fb19SBen Gras 					base = basefix[base];
7102fe8fb19SBen Gras 					if (base <= 8)
7112fe8fb19SBen Gras 						break;	/* not legal here */
7122fe8fb19SBen Gras 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
7132fe8fb19SBen Gras 					goto ok;
7142fe8fb19SBen Gras 
7152fe8fb19SBen Gras 				/* letters ok iff hex */
7162fe8fb19SBen Gras 				case 'A': case 'B': case 'C':
7172fe8fb19SBen Gras 				case 'D': case 'E': case 'F':
7182fe8fb19SBen Gras 				case 'a': case 'b': case 'c':
7192fe8fb19SBen Gras 				case 'd': case 'e': case 'f':
7202fe8fb19SBen Gras 					/* no need to fix base here */
7212fe8fb19SBen Gras 					if (base <= 10)
7222fe8fb19SBen Gras 						break;	/* not legal here */
7232fe8fb19SBen Gras 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
7242fe8fb19SBen Gras 					goto ok;
7252fe8fb19SBen Gras 
7262fe8fb19SBen Gras 				/* sign ok only as first character */
7272fe8fb19SBen Gras 				case '+': case '-':
7282fe8fb19SBen Gras 					if (flags & SIGNOK) {
7292fe8fb19SBen Gras 						flags &= ~SIGNOK;
7302fe8fb19SBen Gras 						flags |= HAVESIGN;
7312fe8fb19SBen Gras 						goto ok;
7322fe8fb19SBen Gras 					}
7332fe8fb19SBen Gras 					break;
7342fe8fb19SBen Gras 
7352fe8fb19SBen Gras 				/*
7362fe8fb19SBen Gras 				 * x ok iff flag still set & 2nd char (or
7372fe8fb19SBen Gras 				 * 3rd char if we have a sign).
7382fe8fb19SBen Gras 				 */
7392fe8fb19SBen Gras 				case 'x': case 'X':
7402fe8fb19SBen Gras 					if (flags & PFXOK && p ==
7412fe8fb19SBen Gras 					    buf + 1 + !!(flags & HAVESIGN)) {
7422fe8fb19SBen Gras 						base = 16;	/* if %i */
7432fe8fb19SBen Gras 						flags &= ~PFXOK;
7442fe8fb19SBen Gras 						goto ok;
7452fe8fb19SBen Gras 					}
7462fe8fb19SBen Gras 					break;
7472fe8fb19SBen Gras 				}
7482fe8fb19SBen Gras 
7492fe8fb19SBen Gras 				/*
7502fe8fb19SBen Gras 				 * If we got here, c is not a legal character
7512fe8fb19SBen Gras 				 * for a number.  Stop accumulating digits.
7522fe8fb19SBen Gras 				 */
7532fe8fb19SBen Gras 				break;
7542fe8fb19SBen Gras 		ok:
7552fe8fb19SBen Gras 				/*
7562fe8fb19SBen Gras 				 * c is legal: store it and look at the next.
7572fe8fb19SBen Gras 				 */
7582fe8fb19SBen Gras 				*p++ = c;
7592fe8fb19SBen Gras 				if (--fp->_r > 0)
7602fe8fb19SBen Gras 					fp->_p++;
7612fe8fb19SBen Gras 				else if (__srefill(fp))
7622fe8fb19SBen Gras 					break;		/* EOF */
7632fe8fb19SBen Gras 			}
7642fe8fb19SBen Gras 			/*
7652fe8fb19SBen Gras 			 * If we had only a sign, it is no good; push
7662fe8fb19SBen Gras 			 * back the sign.  If the number ends in `x',
7672fe8fb19SBen Gras 			 * it was [sign] '0' 'x', so push back the x
7682fe8fb19SBen Gras 			 * and treat it as [sign] '0'.
7692fe8fb19SBen Gras 			 */
7702fe8fb19SBen Gras 			if (flags & NDIGITS) {
7712fe8fb19SBen Gras 				if (p > buf)
7722fe8fb19SBen Gras 					(void)ungetc(*(u_char *)--p, fp);
7732fe8fb19SBen Gras 				goto match_failure;
7742fe8fb19SBen Gras 			}
7752fe8fb19SBen Gras 			c = ((u_char *)p)[-1];
7762fe8fb19SBen Gras 			if (c == 'x' || c == 'X') {
7772fe8fb19SBen Gras 				--p;
7782fe8fb19SBen Gras 				(void)ungetc(c, fp);
7792fe8fb19SBen Gras 			}
7802fe8fb19SBen Gras 			if ((flags & SUPPRESS) == 0) {
7812fe8fb19SBen Gras 				uintmax_t res;
7822fe8fb19SBen Gras 
7832fe8fb19SBen Gras 				*p = 0;
7842fe8fb19SBen Gras 				if ((flags & UNSIGNED) == 0)
785*84d9c625SLionel Sambuc 				    res = strtoimax_l(buf, (char **)NULL, base,
786*84d9c625SLionel Sambuc 				        loc);
7872fe8fb19SBen Gras 				else
788*84d9c625SLionel Sambuc 				    res = strtoumax_l(buf, (char **)NULL, base,
789*84d9c625SLionel Sambuc 				        loc);
7902fe8fb19SBen Gras 				if (flags & POINTER)
7912fe8fb19SBen Gras 					*va_arg(ap, void **) =
7922fe8fb19SBen Gras 							(void *)(uintptr_t)res;
7932fe8fb19SBen Gras 				else if (flags & SHORTSHORT)
7942fe8fb19SBen Gras 					*va_arg(ap, char *) = (char)res;
7952fe8fb19SBen Gras 				else if (flags & SHORT)
7962fe8fb19SBen Gras 					*va_arg(ap, short *) = (short)res;
7972fe8fb19SBen Gras 				else if (flags & LONG)
7982fe8fb19SBen Gras 					*va_arg(ap, long *) = (long)res;
7992fe8fb19SBen Gras 				else if (flags & LONGLONG)
8002fe8fb19SBen Gras 					*va_arg(ap, long long *) = res;
8012fe8fb19SBen Gras 				else if (flags & INTMAXT)
8022fe8fb19SBen Gras 					*va_arg(ap, intmax_t *) = res;
8032fe8fb19SBen Gras 				else if (flags & PTRDIFFT)
8042fe8fb19SBen Gras 					*va_arg(ap, ptrdiff_t *) =
8052fe8fb19SBen Gras 					    (ptrdiff_t)res;
8062fe8fb19SBen Gras 				else if (flags & SIZET)
8072fe8fb19SBen Gras 					*va_arg(ap, size_t *) = (size_t)res;
8082fe8fb19SBen Gras 				else
8092fe8fb19SBen Gras 					*va_arg(ap, int *) = (int)res;
8102fe8fb19SBen Gras 				nassigned++;
8112fe8fb19SBen Gras 			}
8122fe8fb19SBen Gras 			nread += p - buf;
8132fe8fb19SBen Gras 			nconversions++;
8142fe8fb19SBen Gras 			break;
8152fe8fb19SBen Gras 
8162fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
8172fe8fb19SBen Gras 		case CT_FLOAT:
8182fe8fb19SBen Gras 			/* scan a floating point number as if by strtod */
8192fe8fb19SBen Gras 			if (width == 0 || width > sizeof(buf) - 1)
8202fe8fb19SBen Gras 				width = sizeof(buf) - 1;
821*84d9c625SLionel Sambuc 			if ((width = parsefloat(fp, buf, buf + width, loc)) == 0)
8222fe8fb19SBen Gras 				goto match_failure;
8232fe8fb19SBen Gras 			if ((flags & SUPPRESS) == 0) {
8242fe8fb19SBen Gras 				if (flags & LONGDBL) {
825*84d9c625SLionel Sambuc 					long double res = strtold_l(buf, &p,
826*84d9c625SLionel Sambuc 					    loc);
8272fe8fb19SBen Gras 					*va_arg(ap, long double *) = res;
8282fe8fb19SBen Gras 				} else if (flags & LONG) {
829*84d9c625SLionel Sambuc 					double res = strtod_l(buf, &p, loc);
8302fe8fb19SBen Gras 					*va_arg(ap, double *) = res;
8312fe8fb19SBen Gras 				} else {
832*84d9c625SLionel Sambuc 					float res = strtof_l(buf, &p, loc);
8332fe8fb19SBen Gras 					*va_arg(ap, float *) = res;
8342fe8fb19SBen Gras 				}
8352fe8fb19SBen Gras 				if (__scanfdebug && (size_t)(p - buf) != width)
8362fe8fb19SBen Gras 					abort();
8372fe8fb19SBen Gras 				nassigned++;
8382fe8fb19SBen Gras 			}
8392fe8fb19SBen Gras 			nread += width;
8402fe8fb19SBen Gras 			nconversions++;
8412fe8fb19SBen Gras 			break;
8422fe8fb19SBen Gras #endif /* !NO_FLOATING_POINT */
8432fe8fb19SBen Gras 		}
8442fe8fb19SBen Gras 	}
8452fe8fb19SBen Gras input_failure:
846f14fb602SLionel Sambuc 	return nconversions != 0 ? nassigned : EOF;
8472fe8fb19SBen Gras match_failure:
848f14fb602SLionel Sambuc 	return nassigned;
8492fe8fb19SBen Gras }
8502fe8fb19SBen Gras 
8512fe8fb19SBen Gras /*
8522fe8fb19SBen Gras  * Fill in the given table from the scanset at the given format
8532fe8fb19SBen Gras  * (just after `[').  Return a pointer to the character past the
8542fe8fb19SBen Gras  * closing `]'.  The table has a 1 wherever characters should be
8552fe8fb19SBen Gras  * considered part of the scanset.
8562fe8fb19SBen Gras  */
8572fe8fb19SBen Gras static const u_char *
__sccl(char * tab,const u_char * fmt,locale_t loc)858*84d9c625SLionel Sambuc __sccl(char *tab, const u_char *fmt, locale_t loc)
8592fe8fb19SBen Gras {
8602fe8fb19SBen Gras 	int c, n, v, i;
8612fe8fb19SBen Gras 
8622fe8fb19SBen Gras 	_DIAGASSERT(tab != NULL);
8632fe8fb19SBen Gras 	_DIAGASSERT(fmt != NULL);
8642fe8fb19SBen Gras 	/* first `clear' the whole table */
8652fe8fb19SBen Gras 	c = *fmt++;		/* first char hat => negated scanset */
8662fe8fb19SBen Gras 	if (c == '^') {
8672fe8fb19SBen Gras 		v = 1;		/* default => accept */
8682fe8fb19SBen Gras 		c = *fmt++;	/* get new first char */
8692fe8fb19SBen Gras 	} else
8702fe8fb19SBen Gras 		v = 0;		/* default => reject */
8712fe8fb19SBen Gras 
8722fe8fb19SBen Gras 	/* XXX: Will not work if sizeof(tab*) > sizeof(char) */
8732fe8fb19SBen Gras 	(void)memset(tab, v, 256);
8742fe8fb19SBen Gras 
8752fe8fb19SBen Gras 	if (c == 0)
876f14fb602SLionel Sambuc 		return fmt - 1;/* format ended before closing ] */
8772fe8fb19SBen Gras 
8782fe8fb19SBen Gras 	/*
8792fe8fb19SBen Gras 	 * Now set the entries corresponding to the actual scanset
8802fe8fb19SBen Gras 	 * to the opposite of the above.
8812fe8fb19SBen Gras 	 *
8822fe8fb19SBen Gras 	 * The first character may be ']' (or '-') without being special;
8832fe8fb19SBen Gras 	 * the last character may be '-'.
8842fe8fb19SBen Gras 	 */
8852fe8fb19SBen Gras 	v = 1 - v;
8862fe8fb19SBen Gras 	for (;;) {
8872fe8fb19SBen Gras 		tab[c] = v;		/* take character c */
8882fe8fb19SBen Gras doswitch:
8892fe8fb19SBen Gras 		n = *fmt++;		/* and examine the next */
8902fe8fb19SBen Gras 		switch (n) {
8912fe8fb19SBen Gras 
8922fe8fb19SBen Gras 		case 0:			/* format ended too soon */
893f14fb602SLionel Sambuc 			return fmt - 1;
8942fe8fb19SBen Gras 
8952fe8fb19SBen Gras 		case '-':
8962fe8fb19SBen Gras 			/*
8972fe8fb19SBen Gras 			 * A scanset of the form
8982fe8fb19SBen Gras 			 *	[01+-]
8992fe8fb19SBen Gras 			 * is defined as `the digit 0, the digit 1,
9002fe8fb19SBen Gras 			 * the character +, the character -', but
9012fe8fb19SBen Gras 			 * the effect of a scanset such as
9022fe8fb19SBen Gras 			 *	[a-zA-Z0-9]
9032fe8fb19SBen Gras 			 * is implementation defined.  The V7 Unix
9042fe8fb19SBen Gras 			 * scanf treats `a-z' as `the letters a through
9052fe8fb19SBen Gras 			 * z', but treats `a-a' as `the letter a, the
9062fe8fb19SBen Gras 			 * character -, and the letter a'.
9072fe8fb19SBen Gras 			 *
9082fe8fb19SBen Gras 			 * For compatibility, the `-' is not considerd
9092fe8fb19SBen Gras 			 * to define a range if the character following
9102fe8fb19SBen Gras 			 * it is either a close bracket (required by ANSI)
9112fe8fb19SBen Gras 			 * or is not numerically greater than the character
9122fe8fb19SBen Gras 			 * we just stored in the table (c).
9132fe8fb19SBen Gras 			 */
9142fe8fb19SBen Gras 			n = *fmt;
9152fe8fb19SBen Gras 			if (n == ']' || (__collate_load_error ? n < c :
916*84d9c625SLionel Sambuc 			    __collate_range_cmp(n, c, loc) < 0)) {
9172fe8fb19SBen Gras 				c = '-';
9182fe8fb19SBen Gras 				break;	/* resume the for(;;) */
9192fe8fb19SBen Gras 			}
9202fe8fb19SBen Gras 			fmt++;
9212fe8fb19SBen Gras 			/* fill in the range */
9222fe8fb19SBen Gras 			if (__collate_load_error) {
9232fe8fb19SBen Gras 				do
9242fe8fb19SBen Gras 					tab[++c] = v;
9252fe8fb19SBen Gras 				while (c < n);
9262fe8fb19SBen Gras 			} else {
9272fe8fb19SBen Gras 				for (i = 0; i < 256; i ++)
928*84d9c625SLionel Sambuc 					if (__collate_range_cmp(c, i, loc) < 0 &&
929*84d9c625SLionel Sambuc 					    __collate_range_cmp(i, n, loc) <= 0)
9302fe8fb19SBen Gras 						tab[i] = v;
9312fe8fb19SBen Gras 			}
9322fe8fb19SBen Gras #if 1	/* XXX another disgusting compatibility hack */
9332fe8fb19SBen Gras 			c = n;
9342fe8fb19SBen Gras 			/*
9352fe8fb19SBen Gras 			 * Alas, the V7 Unix scanf also treats formats
9362fe8fb19SBen Gras 			 * such as [a-c-e] as `the letters a through e'.
9372fe8fb19SBen Gras 			 * This too is permitted by the standard....
9382fe8fb19SBen Gras 			 */
9392fe8fb19SBen Gras 			goto doswitch;
9402fe8fb19SBen Gras #else
9412fe8fb19SBen Gras 			c = *fmt++;
9422fe8fb19SBen Gras 			if (c == 0)
943f14fb602SLionel Sambuc 				return fmt - 1;
9442fe8fb19SBen Gras 			if (c == ']')
945f14fb602SLionel Sambuc 				return fmt;
9462fe8fb19SBen Gras #endif
9472fe8fb19SBen Gras 
9482fe8fb19SBen Gras 		case ']':		/* end of scanset */
949f14fb602SLionel Sambuc 			return fmt;
9502fe8fb19SBen Gras 
9512fe8fb19SBen Gras 		default:		/* just another character */
9522fe8fb19SBen Gras 			c = n;
9532fe8fb19SBen Gras 			break;
9542fe8fb19SBen Gras 		}
9552fe8fb19SBen Gras 	}
9562fe8fb19SBen Gras 	/* NOTREACHED */
9572fe8fb19SBen Gras }
9582fe8fb19SBen Gras 
9592fe8fb19SBen Gras #ifndef NO_FLOATING_POINT
960f14fb602SLionel Sambuc static size_t
parsefloat(FILE * fp,char * buf,char * end,locale_t loc)961*84d9c625SLionel Sambuc parsefloat(FILE *fp, char *buf, char *end, locale_t loc)
9622fe8fb19SBen Gras {
9632fe8fb19SBen Gras 	char *commit, *p;
9642fe8fb19SBen Gras 	int infnanpos = 0;
9652fe8fb19SBen Gras 	enum {
9662fe8fb19SBen Gras 		S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
9672fe8fb19SBen Gras 		S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
9682fe8fb19SBen Gras 	} state = S_START;
9692fe8fb19SBen Gras 	unsigned char c;
970*84d9c625SLionel Sambuc 	char decpt = *localeconv_l(loc)->decimal_point;
9712fe8fb19SBen Gras 	_Bool gotmantdig = 0, ishex = 0;
9722fe8fb19SBen Gras 
9732fe8fb19SBen Gras 	/*
9742fe8fb19SBen Gras 	 * We set commit = p whenever the string we have read so far
9752fe8fb19SBen Gras 	 * constitutes a valid representation of a floating point
9762fe8fb19SBen Gras 	 * number by itself.  At some point, the parse will complete
9772fe8fb19SBen Gras 	 * or fail, and we will ungetc() back to the last commit point.
9782fe8fb19SBen Gras 	 * To ensure that the file offset gets updated properly, it is
9792fe8fb19SBen Gras 	 * always necessary to read at least one character that doesn't
9802fe8fb19SBen Gras 	 * match; thus, we can't short-circuit "infinity" or "nan(...)".
9812fe8fb19SBen Gras 	 */
9822fe8fb19SBen Gras 	commit = buf - 1;
9832fe8fb19SBen Gras 	for (p = buf; p < end; ) {
9842fe8fb19SBen Gras 		c = *fp->_p;
9852fe8fb19SBen Gras reswitch:
9862fe8fb19SBen Gras 		switch (state) {
9872fe8fb19SBen Gras 		case S_START:
9882fe8fb19SBen Gras 			state = S_GOTSIGN;
9892fe8fb19SBen Gras 			if (c == '-' || c == '+')
9902fe8fb19SBen Gras 				break;
9912fe8fb19SBen Gras 			else
9922fe8fb19SBen Gras 				goto reswitch;
9932fe8fb19SBen Gras 		case S_GOTSIGN:
9942fe8fb19SBen Gras 			switch (c) {
9952fe8fb19SBen Gras 			case '0':
9962fe8fb19SBen Gras 				state = S_MAYBEHEX;
9972fe8fb19SBen Gras 				commit = p;
9982fe8fb19SBen Gras 				break;
9992fe8fb19SBen Gras 			case 'I':
10002fe8fb19SBen Gras 			case 'i':
10012fe8fb19SBen Gras 				state = S_INF;
10022fe8fb19SBen Gras 				break;
10032fe8fb19SBen Gras 			case 'N':
10042fe8fb19SBen Gras 			case 'n':
10052fe8fb19SBen Gras 				state = S_NAN;
10062fe8fb19SBen Gras 				break;
10072fe8fb19SBen Gras 			default:
10082fe8fb19SBen Gras 				state = S_DIGITS;
10092fe8fb19SBen Gras 				goto reswitch;
10102fe8fb19SBen Gras 			}
10112fe8fb19SBen Gras 			break;
10122fe8fb19SBen Gras 		case S_INF:
10132fe8fb19SBen Gras 			if (infnanpos > 6 ||
10142fe8fb19SBen Gras 			    (c != "nfinity"[infnanpos] &&
10152fe8fb19SBen Gras 			     c != "NFINITY"[infnanpos]))
10162fe8fb19SBen Gras 				goto parsedone;
10172fe8fb19SBen Gras 			if (infnanpos == 1 || infnanpos == 6)
10182fe8fb19SBen Gras 				commit = p;	/* inf or infinity */
10192fe8fb19SBen Gras 			infnanpos++;
10202fe8fb19SBen Gras 			break;
10212fe8fb19SBen Gras 		case S_NAN:
10222fe8fb19SBen Gras 			switch (infnanpos) {
10232fe8fb19SBen Gras 			case -1:	/* XXX kludge to deal with nan(...) */
10242fe8fb19SBen Gras 				goto parsedone;
10252fe8fb19SBen Gras 			case 0:
10262fe8fb19SBen Gras 				if (c != 'A' && c != 'a')
10272fe8fb19SBen Gras 					goto parsedone;
10282fe8fb19SBen Gras 				break;
10292fe8fb19SBen Gras 			case 1:
10302fe8fb19SBen Gras 				if (c != 'N' && c != 'n')
10312fe8fb19SBen Gras 					goto parsedone;
10322fe8fb19SBen Gras 				else
10332fe8fb19SBen Gras 					commit = p;
10342fe8fb19SBen Gras 				break;
10352fe8fb19SBen Gras 			case 2:
10362fe8fb19SBen Gras 				if (c != '(')
10372fe8fb19SBen Gras 					goto parsedone;
10382fe8fb19SBen Gras 				break;
10392fe8fb19SBen Gras 			default:
10402fe8fb19SBen Gras 				if (c == ')') {
10412fe8fb19SBen Gras 					commit = p;
10422fe8fb19SBen Gras 					infnanpos = -2;
1043*84d9c625SLionel Sambuc 				} else if (!isalnum_l(c, loc) && c != '_')
10442fe8fb19SBen Gras 					goto parsedone;
10452fe8fb19SBen Gras 				break;
10462fe8fb19SBen Gras 			}
10472fe8fb19SBen Gras 			infnanpos++;
10482fe8fb19SBen Gras 			break;
10492fe8fb19SBen Gras 		case S_MAYBEHEX:
10502fe8fb19SBen Gras 			state = S_DIGITS;
10512fe8fb19SBen Gras 			if (c == 'X' || c == 'x') {
10522fe8fb19SBen Gras 				ishex = 1;
10532fe8fb19SBen Gras 				break;
10542fe8fb19SBen Gras 			} else {	/* we saw a '0', but no 'x' */
10552fe8fb19SBen Gras 				gotmantdig = 1;
10562fe8fb19SBen Gras 				goto reswitch;
10572fe8fb19SBen Gras 			}
10582fe8fb19SBen Gras 		case S_DIGITS:
1059*84d9c625SLionel Sambuc 			if ((ishex && isxdigit_l(c, loc)) || isdigit_l(c, loc))
10602fe8fb19SBen Gras 				gotmantdig = 1;
10612fe8fb19SBen Gras 			else {
10622fe8fb19SBen Gras 				state = S_FRAC;
10632fe8fb19SBen Gras 				if (c != decpt)
10642fe8fb19SBen Gras 					goto reswitch;
10652fe8fb19SBen Gras 			}
10662fe8fb19SBen Gras 			if (gotmantdig)
10672fe8fb19SBen Gras 				commit = p;
10682fe8fb19SBen Gras 			break;
10692fe8fb19SBen Gras 		case S_FRAC:
10702fe8fb19SBen Gras 			if (((c == 'E' || c == 'e') && !ishex) ||
10712fe8fb19SBen Gras 			    ((c == 'P' || c == 'p') && ishex)) {
10722fe8fb19SBen Gras 				if (!gotmantdig)
10732fe8fb19SBen Gras 					goto parsedone;
10742fe8fb19SBen Gras 				else
10752fe8fb19SBen Gras 					state = S_EXP;
1076*84d9c625SLionel Sambuc 			} else if ((ishex && isxdigit_l(c, loc)) || isdigit_l(c, loc)) {
10772fe8fb19SBen Gras 				commit = p;
10782fe8fb19SBen Gras 				gotmantdig = 1;
10792fe8fb19SBen Gras 			} else
10802fe8fb19SBen Gras 				goto parsedone;
10812fe8fb19SBen Gras 			break;
10822fe8fb19SBen Gras 		case S_EXP:
10832fe8fb19SBen Gras 			state = S_EXPDIGITS;
10842fe8fb19SBen Gras 			if (c == '-' || c == '+')
10852fe8fb19SBen Gras 				break;
10862fe8fb19SBen Gras 			else
10872fe8fb19SBen Gras 				goto reswitch;
10882fe8fb19SBen Gras 		case S_EXPDIGITS:
1089*84d9c625SLionel Sambuc 			if (isdigit_l(c, loc))
10902fe8fb19SBen Gras 				commit = p;
10912fe8fb19SBen Gras 			else
10922fe8fb19SBen Gras 				goto parsedone;
10932fe8fb19SBen Gras 			break;
10942fe8fb19SBen Gras 		default:
10952fe8fb19SBen Gras 			abort();
10962fe8fb19SBen Gras 		}
10972fe8fb19SBen Gras 		*p++ = c;
10982fe8fb19SBen Gras 		if (--fp->_r > 0)
10992fe8fb19SBen Gras 			fp->_p++;
11002fe8fb19SBen Gras 		else if (__srefill(fp))
11012fe8fb19SBen Gras 			break;	/* EOF */
11022fe8fb19SBen Gras 	}
11032fe8fb19SBen Gras 
11042fe8fb19SBen Gras parsedone:
11052fe8fb19SBen Gras 	while (commit < --p)
11062fe8fb19SBen Gras 		(void)ungetc(*(u_char *)p, fp);
11072fe8fb19SBen Gras 	*++commit = '\0';
1108f14fb602SLionel Sambuc 	return commit - buf;
11092fe8fb19SBen Gras }
11102fe8fb19SBen Gras #endif
1111