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