Home | History | Annotate | Line # | Download | only in stdio
vfscanf.c revision 1.39.6.2
      1  1.39.6.2  christos /*	$NetBSD: vfscanf.c,v 1.39.6.2 2008/08/28 16:41:22 christos Exp $	*/
      2  1.39.6.2  christos 
      3  1.39.6.2  christos /*-
      4  1.39.6.2  christos  * Copyright (c) 1990, 1993
      5  1.39.6.2  christos  *	The Regents of the University of California.  All rights reserved.
      6  1.39.6.2  christos  *
      7  1.39.6.2  christos  * This code is derived from software contributed to Berkeley by
      8  1.39.6.2  christos  * Chris Torek.
      9  1.39.6.2  christos  *
     10  1.39.6.2  christos  * Redistribution and use in source and binary forms, with or without
     11  1.39.6.2  christos  * modification, are permitted provided that the following conditions
     12  1.39.6.2  christos  * are met:
     13  1.39.6.2  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.39.6.2  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.39.6.2  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.39.6.2  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.39.6.2  christos  *    documentation and/or other materials provided with the distribution.
     18  1.39.6.2  christos  * 3. Neither the name of the University nor the names of its contributors
     19  1.39.6.2  christos  *    may be used to endorse or promote products derived from this software
     20  1.39.6.2  christos  *    without specific prior written permission.
     21  1.39.6.2  christos  *
     22  1.39.6.2  christos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     23  1.39.6.2  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  1.39.6.2  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  1.39.6.2  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  1.39.6.2  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  1.39.6.2  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  1.39.6.2  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  1.39.6.2  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  1.39.6.2  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  1.39.6.2  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  1.39.6.2  christos  * SUCH DAMAGE.
     33  1.39.6.2  christos  */
     34  1.39.6.2  christos 
     35  1.39.6.2  christos #include <sys/cdefs.h>
     36  1.39.6.2  christos #if defined(LIBC_SCCS) && !defined(lint)
     37  1.39.6.2  christos #if 0
     38  1.39.6.2  christos static char sccsid[] = "@(#)vfscanf.c	8.1 (Berkeley) 6/4/93";
     39  1.39.6.2  christos __FBSDID("$FreeBSD: src/lib/libc/stdio/vfscanf.c,v 1.41 2007/01/09 00:28:07 imp Exp $");
     40  1.39.6.2  christos #else
     41  1.39.6.2  christos __RCSID("$NetBSD: vfscanf.c,v 1.39.6.2 2008/08/28 16:41:22 christos Exp $");
     42  1.39.6.2  christos #endif
     43  1.39.6.2  christos #endif /* LIBC_SCCS and not lint */
     44  1.39.6.2  christos 
     45  1.39.6.2  christos #include "namespace.h"
     46  1.39.6.2  christos #include <assert.h>
     47  1.39.6.2  christos #include <ctype.h>
     48  1.39.6.2  christos #include <inttypes.h>
     49  1.39.6.2  christos #include <stdio.h>
     50  1.39.6.2  christos #include <stdlib.h>
     51  1.39.6.2  christos #include <stddef.h>
     52  1.39.6.2  christos #include <stdarg.h>
     53  1.39.6.2  christos #include <string.h>
     54  1.39.6.2  christos #include <wchar.h>
     55  1.39.6.2  christos #include <wctype.h>
     56  1.39.6.2  christos 
     57  1.39.6.2  christos #include "reentrant.h"
     58  1.39.6.2  christos #include "local.h"
     59  1.39.6.2  christos 
     60  1.39.6.2  christos #ifndef NO_FLOATING_POINT
     61  1.39.6.2  christos #include <locale.h>
     62  1.39.6.2  christos #endif
     63  1.39.6.2  christos 
     64  1.39.6.2  christos /*
     65  1.39.6.2  christos  * Provide an external name for vfscanf.  Note, we don't use the normal
     66  1.39.6.2  christos  * namespace.h method; stdio routines explicitly use the internal name
     67  1.39.6.2  christos  * __svfscanf.
     68  1.39.6.2  christos  */
     69  1.39.6.2  christos #ifdef __weak_alias
     70  1.39.6.2  christos __weak_alias(vfscanf,__svfscanf)
     71  1.39.6.2  christos #endif
     72  1.39.6.2  christos 
     73  1.39.6.2  christos #define	BUF		513	/* Maximum length of numeric string. */
     74  1.39.6.2  christos 
     75  1.39.6.2  christos /*
     76  1.39.6.2  christos  * Flags used during conversion.
     77  1.39.6.2  christos  */
     78  1.39.6.2  christos #define	LONG		0x0001	/* l: long or double */
     79  1.39.6.2  christos #define	LONGDBL		0x0002	/* L: long double */
     80  1.39.6.2  christos #define	SHORT		0x0004	/* h: short */
     81  1.39.6.2  christos #define	SUPPRESS	0x0008	/* *: suppress assignment */
     82  1.39.6.2  christos #define	POINTER		0x0010	/* p: void * (as hex) */
     83  1.39.6.2  christos #define	NOSKIP		0x0020	/* [ or c: do not skip blanks */
     84  1.39.6.2  christos #define	LONGLONG	0x0400	/* ll: long long (+ deprecated q: quad) */
     85  1.39.6.2  christos #define	INTMAXT		0x0800	/* j: intmax_t */
     86  1.39.6.2  christos #define	PTRDIFFT	0x1000	/* t: ptrdiff_t */
     87  1.39.6.2  christos #define	SIZET		0x2000	/* z: size_t */
     88  1.39.6.2  christos #define	SHORTSHORT	0x4000	/* hh: char */
     89  1.39.6.2  christos #define	UNSIGNED	0x8000	/* %[oupxX] conversions */
     90  1.39.6.2  christos 
     91  1.39.6.2  christos /*
     92  1.39.6.2  christos  * The following are used in integral conversions only:
     93  1.39.6.2  christos  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
     94  1.39.6.2  christos  */
     95  1.39.6.2  christos #define	SIGNOK		0x00040	/* +/- is (still) legal */
     96  1.39.6.2  christos #define	NDIGITS		0x00080	/* no digits detected */
     97  1.39.6.2  christos #define	PFXOK		0x00100	/* 0x prefix is (still) legal */
     98  1.39.6.2  christos #define	NZDIGITS	0x00200	/* no zero digits detected */
     99  1.39.6.2  christos #define	HAVESIGN	0x10000	/* sign detected */
    100  1.39.6.2  christos 
    101  1.39.6.2  christos /*
    102  1.39.6.2  christos  * Conversion types.
    103  1.39.6.2  christos  */
    104  1.39.6.2  christos #define	CT_CHAR		0	/* %c conversion */
    105  1.39.6.2  christos #define	CT_CCL		1	/* %[...] conversion */
    106  1.39.6.2  christos #define	CT_STRING	2	/* %s conversion */
    107  1.39.6.2  christos #define	CT_INT		3	/* %[dioupxX] conversion */
    108  1.39.6.2  christos #define	CT_FLOAT	4	/* %[efgEFG] conversion */
    109  1.39.6.2  christos 
    110  1.39.6.2  christos static const u_char *__sccl(char *, const u_char *);
    111  1.39.6.2  christos #ifndef NO_FLOATING_POINT
    112  1.39.6.2  christos static int parsefloat(FILE *, char *, char *);
    113  1.39.6.2  christos #endif
    114  1.39.6.2  christos 
    115  1.39.6.2  christos int __scanfdebug = 0;
    116  1.39.6.2  christos 
    117  1.39.6.2  christos #define __collate_load_error /*CONSTCOND*/0
    118  1.39.6.2  christos static int
    119  1.39.6.2  christos __collate_range_cmp(int c1, int c2)
    120  1.39.6.2  christos {
    121  1.39.6.2  christos 	static char s1[2], s2[2];
    122  1.39.6.2  christos 
    123  1.39.6.2  christos 	s1[0] = c1;
    124  1.39.6.2  christos 	s2[0] = c2;
    125  1.39.6.2  christos 	return strcoll(s1, s2);
    126  1.39.6.2  christos }
    127  1.39.6.2  christos 
    128  1.39.6.2  christos 
    129  1.39.6.2  christos /*
    130  1.39.6.2  christos  * __svfscanf - MT-safe version
    131  1.39.6.2  christos  */
    132  1.39.6.2  christos int
    133  1.39.6.2  christos __svfscanf(FILE *fp, char const *fmt0, va_list ap)
    134  1.39.6.2  christos {
    135  1.39.6.2  christos 	int ret;
    136  1.39.6.2  christos 
    137  1.39.6.2  christos 	FLOCKFILE(fp);
    138  1.39.6.2  christos 	ret = __svfscanf_unlocked(fp, fmt0, ap);
    139  1.39.6.2  christos 	FUNLOCKFILE(fp);
    140  1.39.6.2  christos 	return (ret);
    141  1.39.6.2  christos }
    142  1.39.6.2  christos 
    143  1.39.6.2  christos #define SCANF_SKIP_SPACE() \
    144  1.39.6.2  christos do { \
    145  1.39.6.2  christos 	while ((fp->_r > 0 || __srefill(fp) == 0) && isspace(*fp->_p)) \
    146  1.39.6.2  christos 		nread++, fp->_r--, fp->_p++; \
    147  1.39.6.2  christos } while (/*CONSTCOND*/ 0)
    148  1.39.6.2  christos 
    149  1.39.6.2  christos /*
    150  1.39.6.2  christos  * __svfscanf_unlocked - non-MT-safe version of __svfscanf
    151  1.39.6.2  christos  */
    152  1.39.6.2  christos int
    153  1.39.6.2  christos __svfscanf_unlocked(FILE *fp, const char *fmt0, va_list ap)
    154  1.39.6.2  christos {
    155  1.39.6.2  christos 	const u_char *fmt = (const u_char *)fmt0;
    156  1.39.6.2  christos 	int c;			/* character from format, or conversion */
    157  1.39.6.2  christos 	size_t width;		/* field width, or 0 */
    158  1.39.6.2  christos 	char *p;		/* points into all kinds of strings */
    159  1.39.6.2  christos 	size_t n;		/* handy size_t */
    160  1.39.6.2  christos 	int flags;		/* flags as defined above */
    161  1.39.6.2  christos 	char *p0;		/* saves original value of p when necessary */
    162  1.39.6.2  christos 	int nassigned;		/* number of fields assigned */
    163  1.39.6.2  christos 	int nconversions;	/* number of conversions */
    164  1.39.6.2  christos 	int nread;		/* number of characters consumed from fp */
    165  1.39.6.2  christos 	int base;		/* base argument to conversion function */
    166  1.39.6.2  christos 	char ccltab[256];	/* character class table for %[...] */
    167  1.39.6.2  christos 	char buf[BUF];		/* buffer for numeric and mb conversions */
    168  1.39.6.2  christos 	wchar_t *wcp;		/* handy wide character pointer */
    169  1.39.6.2  christos 	size_t nconv;		/* length of multibyte sequence converted */
    170  1.39.6.2  christos 	static const mbstate_t initial;
    171  1.39.6.2  christos 	mbstate_t mbs;
    172  1.39.6.2  christos 
    173  1.39.6.2  christos 	/* `basefix' is used to avoid `if' tests in the integer scanner */
    174  1.39.6.2  christos 	static const short basefix[17] =
    175  1.39.6.2  christos 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    176  1.39.6.2  christos 
    177  1.39.6.2  christos 	_DIAGASSERT(fp != NULL);
    178  1.39.6.2  christos 	_DIAGASSERT(fmt0 != NULL);
    179  1.39.6.2  christos 
    180  1.39.6.2  christos 	_SET_ORIENTATION(fp, -1);
    181  1.39.6.2  christos 
    182  1.39.6.2  christos 	nassigned = 0;
    183  1.39.6.2  christos 	nconversions = 0;
    184  1.39.6.2  christos 	nread = 0;
    185  1.39.6.2  christos 	base = 0;
    186  1.39.6.2  christos 	for (;;) {
    187  1.39.6.2  christos 		c = (unsigned char)*fmt++;
    188  1.39.6.2  christos 		if (c == 0)
    189  1.39.6.2  christos 			return (nassigned);
    190  1.39.6.2  christos 		if (isspace(c)) {
    191  1.39.6.2  christos 			while ((fp->_r > 0 || __srefill(fp) == 0) &&
    192  1.39.6.2  christos 			    isspace(*fp->_p))
    193  1.39.6.2  christos 				nread++, fp->_r--, fp->_p++;
    194  1.39.6.2  christos 			continue;
    195  1.39.6.2  christos 		}
    196  1.39.6.2  christos 		if (c != '%')
    197  1.39.6.2  christos 			goto literal;
    198  1.39.6.2  christos 		width = 0;
    199  1.39.6.2  christos 		flags = 0;
    200  1.39.6.2  christos 		/*
    201  1.39.6.2  christos 		 * switch on the format.  continue if done;
    202  1.39.6.2  christos 		 * break once format type is derived.
    203  1.39.6.2  christos 		 */
    204  1.39.6.2  christos again:		c = *fmt++;
    205  1.39.6.2  christos 		switch (c) {
    206  1.39.6.2  christos 		case '%':
    207  1.39.6.2  christos 			SCANF_SKIP_SPACE();
    208  1.39.6.2  christos literal:
    209  1.39.6.2  christos 			if (fp->_r <= 0 && __srefill(fp))
    210  1.39.6.2  christos 				goto input_failure;
    211  1.39.6.2  christos 			if (*fp->_p != c)
    212  1.39.6.2  christos 				goto match_failure;
    213  1.39.6.2  christos 			fp->_r--, fp->_p++;
    214  1.39.6.2  christos 			nread++;
    215  1.39.6.2  christos 			continue;
    216  1.39.6.2  christos 
    217  1.39.6.2  christos 		case '*':
    218  1.39.6.2  christos 			flags |= SUPPRESS;
    219  1.39.6.2  christos 			goto again;
    220  1.39.6.2  christos 		case 'j':
    221  1.39.6.2  christos 			flags |= INTMAXT;
    222  1.39.6.2  christos 			goto again;
    223  1.39.6.2  christos 		case 'l':
    224  1.39.6.2  christos 			if (flags & LONG) {
    225  1.39.6.2  christos 				flags &= ~LONG;
    226  1.39.6.2  christos 				flags |= LONGLONG;
    227  1.39.6.2  christos 			} else
    228  1.39.6.2  christos 				flags |= LONG;
    229  1.39.6.2  christos 			goto again;
    230  1.39.6.2  christos 		case 'q':
    231  1.39.6.2  christos 			flags |= LONGLONG;	/* not quite */
    232  1.39.6.2  christos 			goto again;
    233  1.39.6.2  christos 		case 't':
    234  1.39.6.2  christos 			flags |= PTRDIFFT;
    235  1.39.6.2  christos 			goto again;
    236  1.39.6.2  christos 		case 'z':
    237  1.39.6.2  christos 			flags |= SIZET;
    238  1.39.6.2  christos 			goto again;
    239  1.39.6.2  christos 		case 'L':
    240  1.39.6.2  christos 			flags |= LONGDBL;
    241  1.39.6.2  christos 			goto again;
    242  1.39.6.2  christos 		case 'h':
    243  1.39.6.2  christos 			if (flags & SHORT) {
    244  1.39.6.2  christos 				flags &= ~SHORT;
    245  1.39.6.2  christos 				flags |= SHORTSHORT;
    246  1.39.6.2  christos 			} else
    247  1.39.6.2  christos 				flags |= SHORT;
    248  1.39.6.2  christos 			goto again;
    249  1.39.6.2  christos 
    250  1.39.6.2  christos 		case '0': case '1': case '2': case '3': case '4':
    251  1.39.6.2  christos 		case '5': case '6': case '7': case '8': case '9':
    252  1.39.6.2  christos 			width = width * 10 + c - '0';
    253  1.39.6.2  christos 			goto again;
    254  1.39.6.2  christos 
    255  1.39.6.2  christos 		/*
    256  1.39.6.2  christos 		 * Conversions.
    257  1.39.6.2  christos 		 */
    258  1.39.6.2  christos 		case 'd':
    259  1.39.6.2  christos 			c = CT_INT;
    260  1.39.6.2  christos 			base = 10;
    261  1.39.6.2  christos 			break;
    262  1.39.6.2  christos 
    263  1.39.6.2  christos 		case 'i':
    264  1.39.6.2  christos 			c = CT_INT;
    265  1.39.6.2  christos 			base = 0;
    266  1.39.6.2  christos 			break;
    267  1.39.6.2  christos 
    268  1.39.6.2  christos 		case 'o':
    269  1.39.6.2  christos 			c = CT_INT;
    270  1.39.6.2  christos 			flags |= UNSIGNED;
    271  1.39.6.2  christos 			base = 8;
    272  1.39.6.2  christos 			break;
    273  1.39.6.2  christos 
    274  1.39.6.2  christos 		case 'u':
    275  1.39.6.2  christos 			c = CT_INT;
    276  1.39.6.2  christos 			flags |= UNSIGNED;
    277  1.39.6.2  christos 			base = 10;
    278  1.39.6.2  christos 			break;
    279  1.39.6.2  christos 
    280  1.39.6.2  christos 		case 'X':
    281  1.39.6.2  christos 		case 'x':
    282  1.39.6.2  christos 			flags |= PFXOK;	/* enable 0x prefixing */
    283  1.39.6.2  christos 			c = CT_INT;
    284  1.39.6.2  christos 			flags |= UNSIGNED;
    285  1.39.6.2  christos 			base = 16;
    286  1.39.6.2  christos 			break;
    287  1.39.6.2  christos 
    288  1.39.6.2  christos #ifndef NO_FLOATING_POINT
    289  1.39.6.2  christos 		case 'A': case 'E': case 'F': case 'G':
    290  1.39.6.2  christos 		case 'a': case 'e': case 'f': case 'g':
    291  1.39.6.2  christos 			c = CT_FLOAT;
    292  1.39.6.2  christos 			break;
    293  1.39.6.2  christos #endif
    294  1.39.6.2  christos 
    295  1.39.6.2  christos 		case 'S':
    296  1.39.6.2  christos 			flags |= LONG;
    297  1.39.6.2  christos 			/* FALLTHROUGH */
    298  1.39.6.2  christos 		case 's':
    299  1.39.6.2  christos 			c = CT_STRING;
    300  1.39.6.2  christos 			break;
    301  1.39.6.2  christos 
    302  1.39.6.2  christos 		case '[':
    303  1.39.6.2  christos 			fmt = __sccl(ccltab, fmt);
    304  1.39.6.2  christos 			flags |= NOSKIP;
    305  1.39.6.2  christos 			c = CT_CCL;
    306  1.39.6.2  christos 			break;
    307  1.39.6.2  christos 
    308  1.39.6.2  christos 		case 'C':
    309  1.39.6.2  christos 			flags |= LONG;
    310  1.39.6.2  christos 			/* FALLTHROUGH */
    311  1.39.6.2  christos 		case 'c':
    312  1.39.6.2  christos 			flags |= NOSKIP;
    313  1.39.6.2  christos 			c = CT_CHAR;
    314  1.39.6.2  christos 			break;
    315  1.39.6.2  christos 
    316  1.39.6.2  christos 		case 'p':	/* pointer format is like hex */
    317  1.39.6.2  christos 			flags |= POINTER | PFXOK;
    318  1.39.6.2  christos 			c = CT_INT;		/* assumes sizeof(uintmax_t) */
    319  1.39.6.2  christos 			flags |= UNSIGNED;	/*      >= sizeof(uintptr_t) */
    320  1.39.6.2  christos 			base = 16;
    321  1.39.6.2  christos 			break;
    322  1.39.6.2  christos 
    323  1.39.6.2  christos 		case 'n':
    324  1.39.6.2  christos 			nconversions++;
    325  1.39.6.2  christos 			if (flags & SUPPRESS)	/* ??? */
    326  1.39.6.2  christos 				continue;
    327  1.39.6.2  christos 			if (flags & SHORTSHORT)
    328  1.39.6.2  christos 				*va_arg(ap, char *) = nread;
    329  1.39.6.2  christos 			else if (flags & SHORT)
    330  1.39.6.2  christos 				*va_arg(ap, short *) = nread;
    331  1.39.6.2  christos 			else if (flags & LONG)
    332  1.39.6.2  christos 				*va_arg(ap, long *) = nread;
    333  1.39.6.2  christos 			else if (flags & LONGLONG)
    334  1.39.6.2  christos 				*va_arg(ap, long long *) = nread;
    335  1.39.6.2  christos 			else if (flags & INTMAXT)
    336  1.39.6.2  christos 				*va_arg(ap, intmax_t *) = nread;
    337  1.39.6.2  christos 			else if (flags & SIZET)
    338  1.39.6.2  christos 				*va_arg(ap, size_t *) = nread;
    339  1.39.6.2  christos 			else if (flags & PTRDIFFT)
    340  1.39.6.2  christos 				*va_arg(ap, ptrdiff_t *) = nread;
    341  1.39.6.2  christos 			else
    342  1.39.6.2  christos 				*va_arg(ap, int *) = nread;
    343  1.39.6.2  christos 			continue;
    344  1.39.6.2  christos 
    345  1.39.6.2  christos 		default:
    346  1.39.6.2  christos 			goto match_failure;
    347  1.39.6.2  christos 
    348  1.39.6.2  christos 		/*
    349  1.39.6.2  christos 		 * Disgusting backwards compatibility hack.	XXX
    350  1.39.6.2  christos 		 */
    351  1.39.6.2  christos 		case '\0':	/* compat */
    352  1.39.6.2  christos 			return (EOF);
    353  1.39.6.2  christos 		}
    354  1.39.6.2  christos 
    355  1.39.6.2  christos 		/*
    356  1.39.6.2  christos 		 * We have a conversion that requires input.
    357  1.39.6.2  christos 		 */
    358  1.39.6.2  christos 		if (fp->_r <= 0 && __srefill(fp))
    359  1.39.6.2  christos 			goto input_failure;
    360  1.39.6.2  christos 
    361  1.39.6.2  christos 		/*
    362  1.39.6.2  christos 		 * Consume leading white space, except for formats
    363  1.39.6.2  christos 		 * that suppress this.
    364  1.39.6.2  christos 		 */
    365  1.39.6.2  christos 		if ((flags & NOSKIP) == 0) {
    366  1.39.6.2  christos 			while (isspace(*fp->_p)) {
    367  1.39.6.2  christos 				nread++;
    368  1.39.6.2  christos 				if (--fp->_r > 0)
    369  1.39.6.2  christos 					fp->_p++;
    370  1.39.6.2  christos 				else if (__srefill(fp))
    371  1.39.6.2  christos 					goto input_failure;
    372  1.39.6.2  christos 			}
    373  1.39.6.2  christos 			/*
    374  1.39.6.2  christos 			 * Note that there is at least one character in
    375  1.39.6.2  christos 			 * the buffer, so conversions that do not set NOSKIP
    376  1.39.6.2  christos 			 * ca no longer result in an input failure.
    377  1.39.6.2  christos 			 */
    378  1.39.6.2  christos 		}
    379  1.39.6.2  christos 
    380  1.39.6.2  christos 		/*
    381  1.39.6.2  christos 		 * Do the conversion.
    382  1.39.6.2  christos 		 */
    383  1.39.6.2  christos 		switch (c) {
    384  1.39.6.2  christos 
    385  1.39.6.2  christos 		case CT_CHAR:
    386  1.39.6.2  christos 			/* scan arbitrary characters (sets NOSKIP) */
    387  1.39.6.2  christos 			if (width == 0)
    388  1.39.6.2  christos 				width = 1;
    389  1.39.6.2  christos 			if (flags & LONG) {
    390  1.39.6.2  christos 				if ((flags & SUPPRESS) == 0)
    391  1.39.6.2  christos 					wcp = va_arg(ap, wchar_t *);
    392  1.39.6.2  christos 				else
    393  1.39.6.2  christos 					wcp = NULL;
    394  1.39.6.2  christos 				n = 0;
    395  1.39.6.2  christos 				while (width != 0) {
    396  1.39.6.2  christos 					if (n == MB_CUR_MAX) {
    397  1.39.6.2  christos 						fp->_flags |= __SERR;
    398  1.39.6.2  christos 						goto input_failure;
    399  1.39.6.2  christos 					}
    400  1.39.6.2  christos 					buf[n++] = *fp->_p;
    401  1.39.6.2  christos 					fp->_p++;
    402  1.39.6.2  christos 					fp->_r--;
    403  1.39.6.2  christos 					mbs = initial;
    404  1.39.6.2  christos 					nconv = mbrtowc(wcp, buf, n, &mbs);
    405  1.39.6.2  christos 					if (nconv == (size_t)-1) {
    406  1.39.6.2  christos 						fp->_flags |= __SERR;
    407  1.39.6.2  christos 						goto input_failure;
    408  1.39.6.2  christos 					}
    409  1.39.6.2  christos 					if (nconv == 0 && !(flags & SUPPRESS))
    410  1.39.6.2  christos 						*wcp = L'\0';
    411  1.39.6.2  christos 					if (nconv != (size_t)-2) {
    412  1.39.6.2  christos 						nread += n;
    413  1.39.6.2  christos 						width--;
    414  1.39.6.2  christos 						if (!(flags & SUPPRESS))
    415  1.39.6.2  christos 							wcp++;
    416  1.39.6.2  christos 						n = 0;
    417  1.39.6.2  christos 					}
    418  1.39.6.2  christos 					if (fp->_r <= 0 && __srefill(fp)) {
    419  1.39.6.2  christos 						if (n != 0) {
    420  1.39.6.2  christos 							fp->_flags |= __SERR;
    421  1.39.6.2  christos 							goto input_failure;
    422  1.39.6.2  christos 						}
    423  1.39.6.2  christos 						break;
    424  1.39.6.2  christos 					}
    425  1.39.6.2  christos 				}
    426  1.39.6.2  christos 				if (!(flags & SUPPRESS))
    427  1.39.6.2  christos 					nassigned++;
    428  1.39.6.2  christos 			} else if (flags & SUPPRESS) {
    429  1.39.6.2  christos 				size_t sum = 0;
    430  1.39.6.2  christos 				for (;;) {
    431  1.39.6.2  christos 					if ((n = fp->_r) < width) {
    432  1.39.6.2  christos 						sum += n;
    433  1.39.6.2  christos 						width -= n;
    434  1.39.6.2  christos 						fp->_p += n;
    435  1.39.6.2  christos 						if (__srefill(fp)) {
    436  1.39.6.2  christos 							if (sum == 0)
    437  1.39.6.2  christos 							    goto input_failure;
    438  1.39.6.2  christos 							break;
    439  1.39.6.2  christos 						}
    440  1.39.6.2  christos 					} else {
    441  1.39.6.2  christos 						sum += width;
    442  1.39.6.2  christos 						fp->_r -= width;
    443  1.39.6.2  christos 						fp->_p += width;
    444  1.39.6.2  christos 						break;
    445  1.39.6.2  christos 					}
    446  1.39.6.2  christos 				}
    447  1.39.6.2  christos 				nread += sum;
    448  1.39.6.2  christos 			} else {
    449  1.39.6.2  christos 				size_t r = fread(va_arg(ap, char *), 1,
    450  1.39.6.2  christos 				    width, fp);
    451  1.39.6.2  christos 
    452  1.39.6.2  christos 				if (r == 0)
    453  1.39.6.2  christos 					goto input_failure;
    454  1.39.6.2  christos 				nread += r;
    455  1.39.6.2  christos 				nassigned++;
    456  1.39.6.2  christos 			}
    457  1.39.6.2  christos 			nconversions++;
    458  1.39.6.2  christos 			break;
    459  1.39.6.2  christos 
    460  1.39.6.2  christos 		case CT_CCL:
    461  1.39.6.2  christos 			/* scan a (nonempty) character class (sets NOSKIP) */
    462  1.39.6.2  christos 			if (width == 0)
    463  1.39.6.2  christos 				width = (size_t)~0;	/* `infinity' */
    464  1.39.6.2  christos 			/* take only those things in the class */
    465  1.39.6.2  christos 			if (flags & LONG) {
    466  1.39.6.2  christos 				wchar_t twc;
    467  1.39.6.2  christos 				int nchars;
    468  1.39.6.2  christos 
    469  1.39.6.2  christos 				if ((flags & SUPPRESS) == 0)
    470  1.39.6.2  christos 					wcp = va_arg(ap, wchar_t *);
    471  1.39.6.2  christos 				else
    472  1.39.6.2  christos 					wcp = &twc;
    473  1.39.6.2  christos 				n = 0;
    474  1.39.6.2  christos 				nchars = 0;
    475  1.39.6.2  christos 				while (width != 0) {
    476  1.39.6.2  christos 					if (n == MB_CUR_MAX) {
    477  1.39.6.2  christos 						fp->_flags |= __SERR;
    478  1.39.6.2  christos 						goto input_failure;
    479  1.39.6.2  christos 					}
    480  1.39.6.2  christos 					buf[n++] = *fp->_p;
    481  1.39.6.2  christos 					fp->_p++;
    482  1.39.6.2  christos 					fp->_r--;
    483  1.39.6.2  christos 					mbs = initial;
    484  1.39.6.2  christos 					nconv = mbrtowc(wcp, buf, n, &mbs);
    485  1.39.6.2  christos 					if (nconv == (size_t)-1) {
    486  1.39.6.2  christos 						fp->_flags |= __SERR;
    487  1.39.6.2  christos 						goto input_failure;
    488  1.39.6.2  christos 					}
    489  1.39.6.2  christos 					if (nconv == 0)
    490  1.39.6.2  christos 						*wcp = L'\0';
    491  1.39.6.2  christos 					if (nconv != (size_t)-2) {
    492  1.39.6.2  christos 						if (wctob(*wcp) != EOF &&
    493  1.39.6.2  christos 						    !ccltab[wctob(*wcp)]) {
    494  1.39.6.2  christos 							while (n != 0) {
    495  1.39.6.2  christos 								n--;
    496  1.39.6.2  christos 								(void)ungetc(buf[n],
    497  1.39.6.2  christos 								    fp);
    498  1.39.6.2  christos 							}
    499  1.39.6.2  christos 							break;
    500  1.39.6.2  christos 						}
    501  1.39.6.2  christos 						nread += n;
    502  1.39.6.2  christos 						width--;
    503  1.39.6.2  christos 						if (!(flags & SUPPRESS))
    504  1.39.6.2  christos 							wcp++;
    505  1.39.6.2  christos 						nchars++;
    506  1.39.6.2  christos 						n = 0;
    507  1.39.6.2  christos 					}
    508  1.39.6.2  christos 					if (fp->_r <= 0 && __srefill(fp)) {
    509  1.39.6.2  christos 						if (n != 0) {
    510  1.39.6.2  christos 							fp->_flags |= __SERR;
    511  1.39.6.2  christos 							goto input_failure;
    512  1.39.6.2  christos 						}
    513  1.39.6.2  christos 						break;
    514  1.39.6.2  christos 					}
    515  1.39.6.2  christos 				}
    516  1.39.6.2  christos 				if (n != 0) {
    517  1.39.6.2  christos 					fp->_flags |= __SERR;
    518  1.39.6.2  christos 					goto input_failure;
    519  1.39.6.2  christos 				}
    520  1.39.6.2  christos 				n = nchars;
    521  1.39.6.2  christos 				if (n == 0)
    522  1.39.6.2  christos 					goto match_failure;
    523  1.39.6.2  christos 				if (!(flags & SUPPRESS)) {
    524  1.39.6.2  christos 					*wcp = L'\0';
    525  1.39.6.2  christos 					nassigned++;
    526  1.39.6.2  christos 				}
    527  1.39.6.2  christos 			} else if (flags & SUPPRESS) {
    528  1.39.6.2  christos 				n = 0;
    529  1.39.6.2  christos 				while (ccltab[*fp->_p]) {
    530  1.39.6.2  christos 					n++, fp->_r--, fp->_p++;
    531  1.39.6.2  christos 					if (--width == 0)
    532  1.39.6.2  christos 						break;
    533  1.39.6.2  christos 					if (fp->_r <= 0 && __srefill(fp)) {
    534  1.39.6.2  christos 						if (n == 0)
    535  1.39.6.2  christos 							goto input_failure;
    536  1.39.6.2  christos 						break;
    537  1.39.6.2  christos 					}
    538  1.39.6.2  christos 				}
    539  1.39.6.2  christos 				if (n == 0)
    540  1.39.6.2  christos 					goto match_failure;
    541  1.39.6.2  christos 			} else {
    542  1.39.6.2  christos 				p0 = p = va_arg(ap, char *);
    543  1.39.6.2  christos 				while (ccltab[*fp->_p]) {
    544  1.39.6.2  christos 					fp->_r--;
    545  1.39.6.2  christos 					*p++ = *fp->_p++;
    546  1.39.6.2  christos 					if (--width == 0)
    547  1.39.6.2  christos 						break;
    548  1.39.6.2  christos 					if (fp->_r <= 0 && __srefill(fp)) {
    549  1.39.6.2  christos 						if (p == p0)
    550  1.39.6.2  christos 							goto input_failure;
    551  1.39.6.2  christos 						break;
    552  1.39.6.2  christos 					}
    553  1.39.6.2  christos 				}
    554  1.39.6.2  christos 				n = p - p0;
    555  1.39.6.2  christos 				if (n == 0)
    556  1.39.6.2  christos 					goto match_failure;
    557  1.39.6.2  christos 				*p = 0;
    558  1.39.6.2  christos 				nassigned++;
    559  1.39.6.2  christos 			}
    560  1.39.6.2  christos 			nread += n;
    561  1.39.6.2  christos 			nconversions++;
    562  1.39.6.2  christos 			break;
    563  1.39.6.2  christos 
    564  1.39.6.2  christos 		case CT_STRING:
    565  1.39.6.2  christos 			/* like CCL, but zero-length string OK, & no NOSKIP */
    566  1.39.6.2  christos 			if (width == 0)
    567  1.39.6.2  christos 				width = (size_t)~0;
    568  1.39.6.2  christos 			if (flags & LONG) {
    569  1.39.6.2  christos 				wchar_t twc;
    570  1.39.6.2  christos 
    571  1.39.6.2  christos 				if ((flags & SUPPRESS) == 0)
    572  1.39.6.2  christos 					wcp = va_arg(ap, wchar_t *);
    573  1.39.6.2  christos 				else
    574  1.39.6.2  christos 					wcp = &twc;
    575  1.39.6.2  christos 				n = 0;
    576  1.39.6.2  christos 				while (!isspace(*fp->_p) && width != 0) {
    577  1.39.6.2  christos 					if (n == MB_CUR_MAX) {
    578  1.39.6.2  christos 						fp->_flags |= __SERR;
    579  1.39.6.2  christos 						goto input_failure;
    580  1.39.6.2  christos 					}
    581  1.39.6.2  christos 					buf[n++] = *fp->_p;
    582  1.39.6.2  christos 					fp->_p++;
    583  1.39.6.2  christos 					fp->_r--;
    584  1.39.6.2  christos 					mbs = initial;
    585  1.39.6.2  christos 					nconv = mbrtowc(wcp, buf, n, &mbs);
    586  1.39.6.2  christos 					if (nconv == (size_t)-1) {
    587  1.39.6.2  christos 						fp->_flags |= __SERR;
    588  1.39.6.2  christos 						goto input_failure;
    589  1.39.6.2  christos 					}
    590  1.39.6.2  christos 					if (nconv == 0)
    591  1.39.6.2  christos 						*wcp = L'\0';
    592  1.39.6.2  christos 					if (nconv != (size_t)-2) {
    593  1.39.6.2  christos 						if (iswspace(*wcp)) {
    594  1.39.6.2  christos 							while (n != 0) {
    595  1.39.6.2  christos 								n--;
    596  1.39.6.2  christos 								(void)ungetc(buf[n],
    597  1.39.6.2  christos 								    fp);
    598  1.39.6.2  christos 							}
    599  1.39.6.2  christos 							break;
    600  1.39.6.2  christos 						}
    601  1.39.6.2  christos 						nread += n;
    602  1.39.6.2  christos 						width--;
    603  1.39.6.2  christos 						if (!(flags & SUPPRESS))
    604  1.39.6.2  christos 							wcp++;
    605  1.39.6.2  christos 						n = 0;
    606  1.39.6.2  christos 					}
    607  1.39.6.2  christos 					if (fp->_r <= 0 && __srefill(fp)) {
    608  1.39.6.2  christos 						if (n != 0) {
    609  1.39.6.2  christos 							fp->_flags |= __SERR;
    610  1.39.6.2  christos 							goto input_failure;
    611  1.39.6.2  christos 						}
    612  1.39.6.2  christos 						break;
    613  1.39.6.2  christos 					}
    614  1.39.6.2  christos 				}
    615  1.39.6.2  christos 				if (!(flags & SUPPRESS)) {
    616  1.39.6.2  christos 					*wcp = L'\0';
    617  1.39.6.2  christos 					nassigned++;
    618  1.39.6.2  christos 				}
    619  1.39.6.2  christos 			} else if (flags & SUPPRESS) {
    620  1.39.6.2  christos 				n = 0;
    621  1.39.6.2  christos 				while (!isspace(*fp->_p)) {
    622  1.39.6.2  christos 					n++, fp->_r--, fp->_p++;
    623  1.39.6.2  christos 					if (--width == 0)
    624  1.39.6.2  christos 						break;
    625  1.39.6.2  christos 					if (fp->_r <= 0 && __srefill(fp))
    626  1.39.6.2  christos 						break;
    627  1.39.6.2  christos 				}
    628  1.39.6.2  christos 				nread += n;
    629  1.39.6.2  christos 			} else {
    630  1.39.6.2  christos 				p0 = p = va_arg(ap, char *);
    631  1.39.6.2  christos 				while (!isspace(*fp->_p)) {
    632  1.39.6.2  christos 					fp->_r--;
    633  1.39.6.2  christos 					*p++ = *fp->_p++;
    634  1.39.6.2  christos 					if (--width == 0)
    635  1.39.6.2  christos 						break;
    636  1.39.6.2  christos 					if (fp->_r <= 0 && __srefill(fp))
    637  1.39.6.2  christos 						break;
    638  1.39.6.2  christos 				}
    639  1.39.6.2  christos 				*p = 0;
    640  1.39.6.2  christos 				nread += p - p0;
    641  1.39.6.2  christos 				nassigned++;
    642  1.39.6.2  christos 			}
    643  1.39.6.2  christos 			nconversions++;
    644  1.39.6.2  christos 			continue;
    645  1.39.6.2  christos 
    646  1.39.6.2  christos 		case CT_INT:
    647  1.39.6.2  christos 			/* scan an integer as if by the conversion function */
    648  1.39.6.2  christos #ifdef hardway
    649  1.39.6.2  christos 			if (width == 0 || width > sizeof(buf) - 1)
    650  1.39.6.2  christos 				width = sizeof(buf) - 1;
    651  1.39.6.2  christos #else
    652  1.39.6.2  christos 			/* size_t is unsigned, hence this optimisation */
    653  1.39.6.2  christos 			if (--width > sizeof(buf) - 2)
    654  1.39.6.2  christos 				width = sizeof(buf) - 2;
    655  1.39.6.2  christos 			width++;
    656  1.39.6.2  christos #endif
    657  1.39.6.2  christos 			flags |= SIGNOK | NDIGITS | NZDIGITS;
    658  1.39.6.2  christos 			for (p = buf; width; width--) {
    659  1.39.6.2  christos 				c = *fp->_p;
    660  1.39.6.2  christos 				/*
    661  1.39.6.2  christos 				 * Switch on the character; `goto ok'
    662  1.39.6.2  christos 				 * if we accept it as a part of number.
    663  1.39.6.2  christos 				 */
    664  1.39.6.2  christos 				switch (c) {
    665  1.39.6.2  christos 
    666  1.39.6.2  christos 				/*
    667  1.39.6.2  christos 				 * The digit 0 is always legal, but is
    668  1.39.6.2  christos 				 * special.  For %i conversions, if no
    669  1.39.6.2  christos 				 * digits (zero or nonzero) have been
    670  1.39.6.2  christos 				 * scanned (only signs), we will have
    671  1.39.6.2  christos 				 * base==0.  In that case, we should set
    672  1.39.6.2  christos 				 * it to 8 and enable 0x prefixing.
    673  1.39.6.2  christos 				 * Also, if we have not scanned zero digits
    674  1.39.6.2  christos 				 * before this, do not turn off prefixing
    675  1.39.6.2  christos 				 * (someone else will turn it off if we
    676  1.39.6.2  christos 				 * have scanned any nonzero digits).
    677  1.39.6.2  christos 				 */
    678  1.39.6.2  christos 				case '0':
    679  1.39.6.2  christos 					if (base == 0) {
    680  1.39.6.2  christos 						base = 8;
    681  1.39.6.2  christos 						flags |= PFXOK;
    682  1.39.6.2  christos 					}
    683  1.39.6.2  christos 					if (flags & NZDIGITS)
    684  1.39.6.2  christos 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
    685  1.39.6.2  christos 					else
    686  1.39.6.2  christos 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
    687  1.39.6.2  christos 					goto ok;
    688  1.39.6.2  christos 
    689  1.39.6.2  christos 				/* 1 through 7 always legal */
    690  1.39.6.2  christos 				case '1': case '2': case '3':
    691  1.39.6.2  christos 				case '4': case '5': case '6': case '7':
    692  1.39.6.2  christos 					base = basefix[base];
    693  1.39.6.2  christos 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    694  1.39.6.2  christos 					goto ok;
    695  1.39.6.2  christos 
    696  1.39.6.2  christos 				/* digits 8 and 9 ok iff decimal or hex */
    697  1.39.6.2  christos 				case '8': case '9':
    698  1.39.6.2  christos 					base = basefix[base];
    699  1.39.6.2  christos 					if (base <= 8)
    700  1.39.6.2  christos 						break;	/* not legal here */
    701  1.39.6.2  christos 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    702  1.39.6.2  christos 					goto ok;
    703  1.39.6.2  christos 
    704  1.39.6.2  christos 				/* letters ok iff hex */
    705  1.39.6.2  christos 				case 'A': case 'B': case 'C':
    706  1.39.6.2  christos 				case 'D': case 'E': case 'F':
    707  1.39.6.2  christos 				case 'a': case 'b': case 'c':
    708  1.39.6.2  christos 				case 'd': case 'e': case 'f':
    709  1.39.6.2  christos 					/* no need to fix base here */
    710  1.39.6.2  christos 					if (base <= 10)
    711  1.39.6.2  christos 						break;	/* not legal here */
    712  1.39.6.2  christos 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    713  1.39.6.2  christos 					goto ok;
    714  1.39.6.2  christos 
    715  1.39.6.2  christos 				/* sign ok only as first character */
    716  1.39.6.2  christos 				case '+': case '-':
    717  1.39.6.2  christos 					if (flags & SIGNOK) {
    718  1.39.6.2  christos 						flags &= ~SIGNOK;
    719  1.39.6.2  christos 						flags |= HAVESIGN;
    720  1.39.6.2  christos 						goto ok;
    721  1.39.6.2  christos 					}
    722  1.39.6.2  christos 					break;
    723  1.39.6.2  christos 
    724  1.39.6.2  christos 				/*
    725  1.39.6.2  christos 				 * x ok iff flag still set & 2nd char (or
    726  1.39.6.2  christos 				 * 3rd char if we have a sign).
    727  1.39.6.2  christos 				 */
    728  1.39.6.2  christos 				case 'x': case 'X':
    729  1.39.6.2  christos 					if (flags & PFXOK && p ==
    730  1.39.6.2  christos 					    buf + 1 + !!(flags & HAVESIGN)) {
    731  1.39.6.2  christos 						base = 16;	/* if %i */
    732  1.39.6.2  christos 						flags &= ~PFXOK;
    733  1.39.6.2  christos 						goto ok;
    734  1.39.6.2  christos 					}
    735  1.39.6.2  christos 					break;
    736  1.39.6.2  christos 				}
    737  1.39.6.2  christos 
    738  1.39.6.2  christos 				/*
    739  1.39.6.2  christos 				 * If we got here, c is not a legal character
    740  1.39.6.2  christos 				 * for a number.  Stop accumulating digits.
    741  1.39.6.2  christos 				 */
    742  1.39.6.2  christos 				break;
    743  1.39.6.2  christos 		ok:
    744  1.39.6.2  christos 				/*
    745  1.39.6.2  christos 				 * c is legal: store it and look at the next.
    746  1.39.6.2  christos 				 */
    747  1.39.6.2  christos 				*p++ = c;
    748  1.39.6.2  christos 				if (--fp->_r > 0)
    749  1.39.6.2  christos 					fp->_p++;
    750  1.39.6.2  christos 				else if (__srefill(fp))
    751  1.39.6.2  christos 					break;		/* EOF */
    752  1.39.6.2  christos 			}
    753  1.39.6.2  christos 			/*
    754  1.39.6.2  christos 			 * If we had only a sign, it is no good; push
    755  1.39.6.2  christos 			 * back the sign.  If the number ends in `x',
    756  1.39.6.2  christos 			 * it was [sign] '0' 'x', so push back the x
    757  1.39.6.2  christos 			 * and treat it as [sign] '0'.
    758  1.39.6.2  christos 			 */
    759  1.39.6.2  christos 			if (flags & NDIGITS) {
    760  1.39.6.2  christos 				if (p > buf)
    761  1.39.6.2  christos 					(void)ungetc(*(u_char *)--p, fp);
    762  1.39.6.2  christos 				goto match_failure;
    763  1.39.6.2  christos 			}
    764  1.39.6.2  christos 			c = ((u_char *)p)[-1];
    765  1.39.6.2  christos 			if (c == 'x' || c == 'X') {
    766  1.39.6.2  christos 				--p;
    767  1.39.6.2  christos 				(void)ungetc(c, fp);
    768  1.39.6.2  christos 			}
    769  1.39.6.2  christos 			if ((flags & SUPPRESS) == 0) {
    770  1.39.6.2  christos 				uintmax_t res;
    771  1.39.6.2  christos 
    772  1.39.6.2  christos 				*p = 0;
    773  1.39.6.2  christos 				if ((flags & UNSIGNED) == 0)
    774  1.39.6.2  christos 				    res = strtoimax(buf, (char **)NULL, base);
    775  1.39.6.2  christos 				else
    776  1.39.6.2  christos 				    res = strtoumax(buf, (char **)NULL, base);
    777  1.39.6.2  christos 				if (flags & POINTER)
    778  1.39.6.2  christos 					*va_arg(ap, void **) =
    779  1.39.6.2  christos 							(void *)(uintptr_t)res;
    780  1.39.6.2  christos 				else if (flags & SHORTSHORT)
    781  1.39.6.2  christos 					*va_arg(ap, char *) = (char)res;
    782  1.39.6.2  christos 				else if (flags & SHORT)
    783  1.39.6.2  christos 					*va_arg(ap, short *) = (short)res;
    784  1.39.6.2  christos 				else if (flags & LONG)
    785  1.39.6.2  christos 					*va_arg(ap, long *) = (long)res;
    786  1.39.6.2  christos 				else if (flags & LONGLONG)
    787  1.39.6.2  christos 					*va_arg(ap, long long *) = res;
    788  1.39.6.2  christos 				else if (flags & INTMAXT)
    789  1.39.6.2  christos 					*va_arg(ap, intmax_t *) = res;
    790  1.39.6.2  christos 				else if (flags & PTRDIFFT)
    791  1.39.6.2  christos 					*va_arg(ap, ptrdiff_t *) =
    792  1.39.6.2  christos 					    (ptrdiff_t)res;
    793  1.39.6.2  christos 				else if (flags & SIZET)
    794  1.39.6.2  christos 					*va_arg(ap, size_t *) = (size_t)res;
    795  1.39.6.2  christos 				else
    796  1.39.6.2  christos 					*va_arg(ap, int *) = (int)res;
    797  1.39.6.2  christos 				nassigned++;
    798  1.39.6.2  christos 			}
    799  1.39.6.2  christos 			nread += p - buf;
    800  1.39.6.2  christos 			nconversions++;
    801  1.39.6.2  christos 			break;
    802  1.39.6.2  christos 
    803  1.39.6.2  christos #ifndef NO_FLOATING_POINT
    804  1.39.6.2  christos 		case CT_FLOAT:
    805  1.39.6.2  christos 			/* scan a floating point number as if by strtod */
    806  1.39.6.2  christos 			if (width == 0 || width > sizeof(buf) - 1)
    807  1.39.6.2  christos 				width = sizeof(buf) - 1;
    808  1.39.6.2  christos 			if ((width = parsefloat(fp, buf, buf + width)) == 0)
    809  1.39.6.2  christos 				goto match_failure;
    810  1.39.6.2  christos 			if ((flags & SUPPRESS) == 0) {
    811  1.39.6.2  christos 				if (flags & LONGDBL) {
    812  1.39.6.2  christos 					long double res = strtold(buf, &p);
    813  1.39.6.2  christos 					*va_arg(ap, long double *) = res;
    814  1.39.6.2  christos 				} else if (flags & LONG) {
    815  1.39.6.2  christos 					double res = strtod(buf, &p);
    816  1.39.6.2  christos 					*va_arg(ap, double *) = res;
    817  1.39.6.2  christos 				} else {
    818  1.39.6.2  christos 					float res = strtof(buf, &p);
    819  1.39.6.2  christos 					*va_arg(ap, float *) = res;
    820  1.39.6.2  christos 				}
    821  1.39.6.2  christos 				if (__scanfdebug && p - buf != width)
    822  1.39.6.2  christos 					abort();
    823  1.39.6.2  christos 				nassigned++;
    824  1.39.6.2  christos 			}
    825  1.39.6.2  christos 			nread += width;
    826  1.39.6.2  christos 			nconversions++;
    827  1.39.6.2  christos 			break;
    828  1.39.6.2  christos #endif /* !NO_FLOATING_POINT */
    829  1.39.6.2  christos 		}
    830  1.39.6.2  christos 	}
    831  1.39.6.2  christos input_failure:
    832  1.39.6.2  christos 	return (nconversions != 0 ? nassigned : EOF);
    833  1.39.6.2  christos match_failure:
    834  1.39.6.2  christos 	return (nassigned);
    835  1.39.6.2  christos }
    836  1.39.6.2  christos 
    837  1.39.6.2  christos /*
    838  1.39.6.2  christos  * Fill in the given table from the scanset at the given format
    839  1.39.6.2  christos  * (just after `[').  Return a pointer to the character past the
    840  1.39.6.2  christos  * closing `]'.  The table has a 1 wherever characters should be
    841  1.39.6.2  christos  * considered part of the scanset.
    842  1.39.6.2  christos  */
    843  1.39.6.2  christos static const u_char *
    844  1.39.6.2  christos __sccl(tab, fmt)
    845  1.39.6.2  christos 	char *tab;
    846  1.39.6.2  christos 	const u_char *fmt;
    847  1.39.6.2  christos {
    848  1.39.6.2  christos 	int c, n, v, i;
    849  1.39.6.2  christos 
    850  1.39.6.2  christos 	_DIAGASSERT(tab != NULL);
    851  1.39.6.2  christos 	_DIAGASSERT(fmt != NULL);
    852  1.39.6.2  christos 	/* first `clear' the whole table */
    853  1.39.6.2  christos 	c = *fmt++;		/* first char hat => negated scanset */
    854  1.39.6.2  christos 	if (c == '^') {
    855  1.39.6.2  christos 		v = 1;		/* default => accept */
    856  1.39.6.2  christos 		c = *fmt++;	/* get new first char */
    857  1.39.6.2  christos 	} else
    858  1.39.6.2  christos 		v = 0;		/* default => reject */
    859  1.39.6.2  christos 
    860  1.39.6.2  christos 	/* XXX: Will not work if sizeof(tab*) > sizeof(char) */
    861  1.39.6.2  christos 	(void)memset(tab, v, 256);
    862  1.39.6.2  christos 
    863  1.39.6.2  christos 	if (c == 0)
    864  1.39.6.2  christos 		return (fmt - 1);/* format ended before closing ] */
    865  1.39.6.2  christos 
    866  1.39.6.2  christos 	/*
    867  1.39.6.2  christos 	 * Now set the entries corresponding to the actual scanset
    868  1.39.6.2  christos 	 * to the opposite of the above.
    869  1.39.6.2  christos 	 *
    870  1.39.6.2  christos 	 * The first character may be ']' (or '-') without being special;
    871  1.39.6.2  christos 	 * the last character may be '-'.
    872  1.39.6.2  christos 	 */
    873  1.39.6.2  christos 	v = 1 - v;
    874  1.39.6.2  christos 	for (;;) {
    875  1.39.6.2  christos 		tab[c] = v;		/* take character c */
    876  1.39.6.2  christos doswitch:
    877  1.39.6.2  christos 		n = *fmt++;		/* and examine the next */
    878  1.39.6.2  christos 		switch (n) {
    879  1.39.6.2  christos 
    880  1.39.6.2  christos 		case 0:			/* format ended too soon */
    881  1.39.6.2  christos 			return (fmt - 1);
    882  1.39.6.2  christos 
    883  1.39.6.2  christos 		case '-':
    884  1.39.6.2  christos 			/*
    885  1.39.6.2  christos 			 * A scanset of the form
    886  1.39.6.2  christos 			 *	[01+-]
    887  1.39.6.2  christos 			 * is defined as `the digit 0, the digit 1,
    888  1.39.6.2  christos 			 * the character +, the character -', but
    889  1.39.6.2  christos 			 * the effect of a scanset such as
    890  1.39.6.2  christos 			 *	[a-zA-Z0-9]
    891  1.39.6.2  christos 			 * is implementation defined.  The V7 Unix
    892  1.39.6.2  christos 			 * scanf treats `a-z' as `the letters a through
    893  1.39.6.2  christos 			 * z', but treats `a-a' as `the letter a, the
    894  1.39.6.2  christos 			 * character -, and the letter a'.
    895  1.39.6.2  christos 			 *
    896  1.39.6.2  christos 			 * For compatibility, the `-' is not considerd
    897  1.39.6.2  christos 			 * to define a range if the character following
    898  1.39.6.2  christos 			 * it is either a close bracket (required by ANSI)
    899  1.39.6.2  christos 			 * or is not numerically greater than the character
    900  1.39.6.2  christos 			 * we just stored in the table (c).
    901  1.39.6.2  christos 			 */
    902  1.39.6.2  christos 			n = *fmt;
    903  1.39.6.2  christos 			if (n == ']' || (__collate_load_error ? n < c :
    904  1.39.6.2  christos 			    __collate_range_cmp(n, c) < 0)) {
    905  1.39.6.2  christos 				c = '-';
    906  1.39.6.2  christos 				break;	/* resume the for(;;) */
    907  1.39.6.2  christos 			}
    908  1.39.6.2  christos 			fmt++;
    909  1.39.6.2  christos 			/* fill in the range */
    910  1.39.6.2  christos 			if (__collate_load_error) {
    911  1.39.6.2  christos 				do
    912  1.39.6.2  christos 					tab[++c] = v;
    913  1.39.6.2  christos 				while (c < n);
    914  1.39.6.2  christos 			} else {
    915  1.39.6.2  christos 				for (i = 0; i < 256; i ++)
    916  1.39.6.2  christos 					if (__collate_range_cmp(c, i) < 0 &&
    917  1.39.6.2  christos 					    __collate_range_cmp(i, n) <= 0)
    918  1.39.6.2  christos 						tab[i] = v;
    919  1.39.6.2  christos 			}
    920  1.39.6.2  christos #if 1	/* XXX another disgusting compatibility hack */
    921  1.39.6.2  christos 			c = n;
    922  1.39.6.2  christos 			/*
    923  1.39.6.2  christos 			 * Alas, the V7 Unix scanf also treats formats
    924  1.39.6.2  christos 			 * such as [a-c-e] as `the letters a through e'.
    925  1.39.6.2  christos 			 * This too is permitted by the standard....
    926  1.39.6.2  christos 			 */
    927  1.39.6.2  christos 			goto doswitch;
    928  1.39.6.2  christos #else
    929  1.39.6.2  christos 			c = *fmt++;
    930  1.39.6.2  christos 			if (c == 0)
    931  1.39.6.2  christos 				return (fmt - 1);
    932  1.39.6.2  christos 			if (c == ']')
    933  1.39.6.2  christos 				return (fmt);
    934  1.39.6.2  christos #endif
    935  1.39.6.2  christos 
    936  1.39.6.2  christos 		case ']':		/* end of scanset */
    937  1.39.6.2  christos 			return (fmt);
    938  1.39.6.2  christos 
    939  1.39.6.2  christos 		default:		/* just another character */
    940  1.39.6.2  christos 			c = n;
    941  1.39.6.2  christos 			break;
    942  1.39.6.2  christos 		}
    943  1.39.6.2  christos 	}
    944  1.39.6.2  christos 	/* NOTREACHED */
    945  1.39.6.2  christos }
    946  1.39.6.2  christos 
    947  1.39.6.2  christos #ifndef NO_FLOATING_POINT
    948  1.39.6.2  christos static int
    949  1.39.6.2  christos parsefloat(FILE *fp, char *buf, char *end)
    950  1.39.6.2  christos {
    951  1.39.6.2  christos 	char *commit, *p;
    952  1.39.6.2  christos 	int infnanpos = 0;
    953  1.39.6.2  christos 	enum {
    954  1.39.6.2  christos 		S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
    955  1.39.6.2  christos 		S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
    956  1.39.6.2  christos 	} state = S_START;
    957  1.39.6.2  christos 	unsigned char c;
    958  1.39.6.2  christos 	char decpt = *localeconv()->decimal_point;
    959  1.39.6.2  christos 	_Bool gotmantdig = 0, ishex = 0;
    960  1.39.6.2  christos 
    961  1.39.6.2  christos 	/*
    962  1.39.6.2  christos 	 * We set commit = p whenever the string we have read so far
    963  1.39.6.2  christos 	 * constitutes a valid representation of a floating point
    964  1.39.6.2  christos 	 * number by itself.  At some point, the parse will complete
    965  1.39.6.2  christos 	 * or fail, and we will ungetc() back to the last commit point.
    966  1.39.6.2  christos 	 * To ensure that the file offset gets updated properly, it is
    967  1.39.6.2  christos 	 * always necessary to read at least one character that doesn't
    968  1.39.6.2  christos 	 * match; thus, we can't short-circuit "infinity" or "nan(...)".
    969  1.39.6.2  christos 	 */
    970  1.39.6.2  christos 	commit = buf - 1;
    971  1.39.6.2  christos 	for (p = buf; p < end; ) {
    972  1.39.6.2  christos 		c = *fp->_p;
    973  1.39.6.2  christos reswitch:
    974  1.39.6.2  christos 		switch (state) {
    975  1.39.6.2  christos 		case S_START:
    976  1.39.6.2  christos 			state = S_GOTSIGN;
    977  1.39.6.2  christos 			if (c == '-' || c == '+')
    978  1.39.6.2  christos 				break;
    979  1.39.6.2  christos 			else
    980  1.39.6.2  christos 				goto reswitch;
    981  1.39.6.2  christos 		case S_GOTSIGN:
    982  1.39.6.2  christos 			switch (c) {
    983  1.39.6.2  christos 			case '0':
    984  1.39.6.2  christos 				state = S_MAYBEHEX;
    985  1.39.6.2  christos 				commit = p;
    986  1.39.6.2  christos 				break;
    987  1.39.6.2  christos 			case 'I':
    988  1.39.6.2  christos 			case 'i':
    989  1.39.6.2  christos 				state = S_INF;
    990  1.39.6.2  christos 				break;
    991  1.39.6.2  christos 			case 'N':
    992  1.39.6.2  christos 			case 'n':
    993  1.39.6.2  christos 				state = S_NAN;
    994  1.39.6.2  christos 				break;
    995  1.39.6.2  christos 			default:
    996  1.39.6.2  christos 				state = S_DIGITS;
    997  1.39.6.2  christos 				goto reswitch;
    998  1.39.6.2  christos 			}
    999  1.39.6.2  christos 			break;
   1000  1.39.6.2  christos 		case S_INF:
   1001  1.39.6.2  christos 			if (infnanpos > 6 ||
   1002  1.39.6.2  christos 			    (c != "nfinity"[infnanpos] &&
   1003  1.39.6.2  christos 			     c != "NFINITY"[infnanpos]))
   1004  1.39.6.2  christos 				goto parsedone;
   1005  1.39.6.2  christos 			if (infnanpos == 1 || infnanpos == 6)
   1006  1.39.6.2  christos 				commit = p;	/* inf or infinity */
   1007  1.39.6.2  christos 			infnanpos++;
   1008  1.39.6.2  christos 			break;
   1009  1.39.6.2  christos 		case S_NAN:
   1010  1.39.6.2  christos 			switch (infnanpos) {
   1011  1.39.6.2  christos 			case -1:	/* XXX kludge to deal with nan(...) */
   1012  1.39.6.2  christos 				goto parsedone;
   1013  1.39.6.2  christos 			case 0:
   1014  1.39.6.2  christos 				if (c != 'A' && c != 'a')
   1015  1.39.6.2  christos 					goto parsedone;
   1016  1.39.6.2  christos 				break;
   1017  1.39.6.2  christos 			case 1:
   1018  1.39.6.2  christos 				if (c != 'N' && c != 'n')
   1019  1.39.6.2  christos 					goto parsedone;
   1020  1.39.6.2  christos 				else
   1021  1.39.6.2  christos 					commit = p;
   1022  1.39.6.2  christos 				break;
   1023  1.39.6.2  christos 			case 2:
   1024  1.39.6.2  christos 				if (c != '(')
   1025  1.39.6.2  christos 					goto parsedone;
   1026  1.39.6.2  christos 				break;
   1027  1.39.6.2  christos 			default:
   1028  1.39.6.2  christos 				if (c == ')') {
   1029  1.39.6.2  christos 					commit = p;
   1030  1.39.6.2  christos 					infnanpos = -2;
   1031  1.39.6.2  christos 				} else if (!isalnum(c) && c != '_')
   1032  1.39.6.2  christos 					goto parsedone;
   1033  1.39.6.2  christos 				break;
   1034  1.39.6.2  christos 			}
   1035  1.39.6.2  christos 			infnanpos++;
   1036  1.39.6.2  christos 			break;
   1037  1.39.6.2  christos 		case S_MAYBEHEX:
   1038  1.39.6.2  christos 			state = S_DIGITS;
   1039  1.39.6.2  christos 			if (c == 'X' || c == 'x') {
   1040  1.39.6.2  christos 				ishex = 1;
   1041  1.39.6.2  christos 				break;
   1042  1.39.6.2  christos 			} else {	/* we saw a '0', but no 'x' */
   1043  1.39.6.2  christos 				gotmantdig = 1;
   1044  1.39.6.2  christos 				goto reswitch;
   1045  1.39.6.2  christos 			}
   1046  1.39.6.2  christos 		case S_DIGITS:
   1047  1.39.6.2  christos 			if ((ishex && isxdigit(c)) || isdigit(c))
   1048  1.39.6.2  christos 				gotmantdig = 1;
   1049  1.39.6.2  christos 			else {
   1050  1.39.6.2  christos 				state = S_FRAC;
   1051  1.39.6.2  christos 				if (c != decpt)
   1052  1.39.6.2  christos 					goto reswitch;
   1053  1.39.6.2  christos 			}
   1054  1.39.6.2  christos 			if (gotmantdig)
   1055  1.39.6.2  christos 				commit = p;
   1056  1.39.6.2  christos 			break;
   1057  1.39.6.2  christos 		case S_FRAC:
   1058  1.39.6.2  christos 			if (((c == 'E' || c == 'e') && !ishex) ||
   1059  1.39.6.2  christos 			    ((c == 'P' || c == 'p') && ishex)) {
   1060  1.39.6.2  christos 				if (!gotmantdig)
   1061  1.39.6.2  christos 					goto parsedone;
   1062  1.39.6.2  christos 				else
   1063  1.39.6.2  christos 					state = S_EXP;
   1064  1.39.6.2  christos 			} else if ((ishex && isxdigit(c)) || isdigit(c)) {
   1065  1.39.6.2  christos 				commit = p;
   1066  1.39.6.2  christos 				gotmantdig = 1;
   1067  1.39.6.2  christos 			} else
   1068  1.39.6.2  christos 				goto parsedone;
   1069  1.39.6.2  christos 			break;
   1070  1.39.6.2  christos 		case S_EXP:
   1071  1.39.6.2  christos 			state = S_EXPDIGITS;
   1072  1.39.6.2  christos 			if (c == '-' || c == '+')
   1073  1.39.6.2  christos 				break;
   1074  1.39.6.2  christos 			else
   1075  1.39.6.2  christos 				goto reswitch;
   1076  1.39.6.2  christos 		case S_EXPDIGITS:
   1077  1.39.6.2  christos 			if (isdigit(c))
   1078  1.39.6.2  christos 				commit = p;
   1079  1.39.6.2  christos 			else
   1080  1.39.6.2  christos 				goto parsedone;
   1081  1.39.6.2  christos 			break;
   1082  1.39.6.2  christos 		default:
   1083  1.39.6.2  christos 			abort();
   1084  1.39.6.2  christos 		}
   1085  1.39.6.2  christos 		*p++ = c;
   1086  1.39.6.2  christos 		if (--fp->_r > 0)
   1087  1.39.6.2  christos 			fp->_p++;
   1088  1.39.6.2  christos 		else if (__srefill(fp))
   1089  1.39.6.2  christos 			break;	/* EOF */
   1090  1.39.6.2  christos 	}
   1091  1.39.6.2  christos 
   1092  1.39.6.2  christos parsedone:
   1093  1.39.6.2  christos 	while (commit < --p)
   1094  1.39.6.2  christos 		(void)ungetc(*(u_char *)p, fp);
   1095  1.39.6.2  christos 	*++commit = '\0';
   1096  1.39.6.2  christos 	return (commit - buf);
   1097  1.39.6.2  christos }
   1098  1.39.6.2  christos #endif
   1099