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