Home | History | Annotate | Line # | Download | only in stdio
vfscanf.c revision 1.29
      1 /*	$NetBSD: vfscanf.c,v 1.29 2001/12/02 20:53:49 kleink Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Chris Torek.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #if defined(LIBC_SCCS) && !defined(lint)
     41 #if 0
     42 static char sccsid[] = "@(#)vfscanf.c	8.1 (Berkeley) 6/4/93";
     43 #else
     44 __RCSID("$NetBSD: vfscanf.c,v 1.29 2001/12/02 20:53:49 kleink Exp $");
     45 #endif
     46 #endif /* LIBC_SCCS and not lint */
     47 
     48 #include "namespace.h"
     49 
     50 #include <assert.h>
     51 #include <errno.h>
     52 #include <inttypes.h>
     53 #include <stddef.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <ctype.h>
     57 #if __STDC__
     58 #include <stdarg.h>
     59 #else
     60 #include <varargs.h>
     61 #endif
     62 
     63 #include "local.h"
     64 #include "reentrant.h"
     65 
     66 #ifdef FLOATING_POINT
     67 #include "floatio.h"
     68 #endif
     69 
     70 #define	BUF		513	/* Maximum length of numeric string. */
     71 
     72 /*
     73  * Flags used during conversion.
     74  */
     75 #define	LONG		0x0001	/* l: long or double */
     76 #define	LONGDBL		0x0002	/* L: long double; unimplemented */
     77 #define	SHORT		0x0004	/* h: short */
     78 #define	QUAD		0x0008	/* q: quad */
     79 #define	LONGLONG	0x0010	/* ll: long long */
     80 #define	MAXINT		0x0020	/* j: intmax_t */
     81 #define	PTRINT		0x0040	/* t: ptrdiff_t */
     82 #define	SIZEINT		0x0080	/* z: size_t */
     83 #define	SUPPRESS	0x0100	/* suppress assignment */
     84 #define	POINTER		0x0200	/* weird %p pointer (`fake hex') */
     85 #define	NOSKIP		0x0400	/* do not skip blanks */
     86 
     87 /*
     88  * The following are used in numeric conversions only:
     89  * SIGNOK, NDIGITS, DPTOK, and EXPOK are for floating point;
     90  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS are for integral.
     91  */
     92 #define	SIGNOK		0x0800	/* +/- is (still) legal */
     93 #define	NDIGITS		0x1000	/* no digits detected */
     94 
     95 #define	DPTOK		0x2000	/* (float) decimal point is still legal */
     96 #define	EXPOK		0x4000	/* (float) exponent (e+3, etc) still legal */
     97 
     98 #define	PFXOK		0x2000	/* 0x prefix is (still) legal */
     99 #define	NZDIGITS	0x4000	/* no zero digits detected */
    100 
    101 /*
    102  * Conversion types.
    103  */
    104 #define	CT_CHAR		0	/* %c conversion */
    105 #define	CT_CCL		1	/* %[...] conversion */
    106 #define	CT_STRING	2	/* %s conversion */
    107 #define	CT_INT		3	/* integer, i.e., strtoimax or strtoumax */
    108 #define	CT_FLOAT	4	/* floating, i.e., strtod */
    109 
    110 #define u_char unsigned char
    111 #define u_long unsigned long
    112 
    113 static const u_char *__sccl __P((char *, const u_char *));
    114 
    115 /*
    116  * vfscanf
    117  */
    118 int
    119 __svfscanf(fp, fmt0, ap)
    120 	FILE *fp;
    121 	const char *fmt0;
    122 	_BSD_VA_LIST_ ap;
    123 {
    124 	const u_char *fmt = (const u_char *)fmt0;
    125 	int c;		/* character from format, or conversion */
    126 	size_t width;	/* field width, or 0 */
    127 	char *p;	/* points into all kinds of strings */
    128 	int n;		/* handy integer */
    129 	int flags;	/* flags as defined above */
    130 	char *p0;	/* saves original value of p when necessary */
    131 	int nassigned;		/* number of fields assigned */
    132 	int nread;		/* number of characters consumed from fp */
    133 	int base;		/* base argument to strtoimax/strtoumax */
    134 	uintmax_t (*ccfn) __P((const char *, char **, int));
    135 				/* conversion function (strtoimax/strtoumax) */
    136 	char ccltab[256];	/* character class table for %[...] */
    137 	char buf[BUF];		/* buffer for numeric conversions */
    138 
    139 	/* `basefix' is used to avoid `if' tests in the integer scanner */
    140 	static const short basefix[17] =
    141 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
    142 
    143 	_DIAGASSERT(fp != NULL);
    144 	_DIAGASSERT(fmt0 != NULL);
    145 
    146 	FLOCKFILE(fp);
    147 
    148 	nassigned = 0;
    149 	nread = 0;
    150 	base = 0;		/* XXX just to keep gcc happy */
    151 	ccfn = NULL;		/* XXX just to keep gcc happy */
    152 	for (;;) {
    153 		c = *fmt++;
    154 		if (c == 0) {
    155 			FUNLOCKFILE(fp);
    156 			return (nassigned);
    157 		}
    158 		if (isspace(c)) {
    159 			while ((fp->_r > 0 || __srefill(fp) == 0) &&
    160 			    isspace(*fp->_p))
    161 				nread++, fp->_r--, fp->_p++;
    162 			continue;
    163 		}
    164 		if (c != '%')
    165 			goto literal;
    166 		width = 0;
    167 		flags = 0;
    168 		/*
    169 		 * switch on the format.  continue if done;
    170 		 * break once format type is derived.
    171 		 */
    172 again:		c = *fmt++;
    173 		switch (c) {
    174 		case '%':
    175 literal:
    176 			if (fp->_r <= 0 && __srefill(fp))
    177 				goto input_failure;
    178 			if (*fp->_p != c)
    179 				goto match_failure;
    180 			fp->_r--, fp->_p++;
    181 			nread++;
    182 			continue;
    183 
    184 		case '*':
    185 			flags |= SUPPRESS;
    186 			goto again;
    187 		case 'L':
    188 			flags |= LONGDBL;
    189 			goto again;
    190 		case 'h':
    191 			flags |= SHORT;
    192 			goto again;
    193 		case 'j':
    194 			flags |= MAXINT;
    195 			goto again;
    196 		case 'l':
    197 			if (*fmt == 'l') {
    198 				fmt++;
    199 				flags |= LONGLONG;
    200 			} else {
    201 				flags |= LONG;
    202 			}
    203 			goto again;
    204 		case 'q':
    205 			flags |= QUAD;
    206 			goto again;
    207 		case 't':
    208 			flags |= PTRINT;
    209 			goto again;
    210 		case 'z':
    211 			flags |= SIZEINT;
    212 			goto again;
    213 
    214 		case '0': case '1': case '2': case '3': case '4':
    215 		case '5': case '6': case '7': case '8': case '9':
    216 			width = width * 10 + c - '0';
    217 			goto again;
    218 
    219 		/*
    220 		 * Conversions.
    221 		 * Those marked `compat' are for 4.[123]BSD compatibility.
    222 		 *
    223 		 * (According to ANSI, E and X formats are supposed
    224 		 * to the same as e and x.  Sorry about that.)
    225 		 */
    226 		case 'D':	/* compat */
    227 			flags |= LONG;
    228 			/* FALLTHROUGH */
    229 		case 'd':
    230 			c = CT_INT;
    231 			ccfn = (uintmax_t (*) __P((const char *, char **, int)))strtoimax;
    232 			base = 10;
    233 			break;
    234 
    235 		case 'i':
    236 			c = CT_INT;
    237 			ccfn = (uintmax_t (*) __P((const char *, char **, int)))strtoimax;
    238 			base = 0;
    239 			break;
    240 
    241 		case 'O':	/* compat */
    242 			flags |= LONG;
    243 			/* FALLTHROUGH */
    244 		case 'o':
    245 			c = CT_INT;
    246 			ccfn = strtoumax;
    247 			base = 8;
    248 			break;
    249 
    250 		case 'u':
    251 			c = CT_INT;
    252 			ccfn = strtoumax;
    253 			base = 10;
    254 			break;
    255 
    256 		case 'X':
    257 		case 'x':
    258 			flags |= PFXOK;	/* enable 0x prefixing */
    259 			c = CT_INT;
    260 			ccfn = strtoumax;
    261 			base = 16;
    262 			break;
    263 
    264 #ifdef FLOATING_POINT
    265 		case 'E':
    266 		case 'F':
    267 		case 'G':
    268 		case 'e':
    269 		case 'f':
    270 		case 'g':
    271 			c = CT_FLOAT;
    272 			break;
    273 #endif
    274 
    275 		case 's':
    276 			c = CT_STRING;
    277 			break;
    278 
    279 		case '[':
    280 			fmt = __sccl(ccltab, fmt);
    281 			flags |= NOSKIP;
    282 			c = CT_CCL;
    283 			break;
    284 
    285 		case 'c':
    286 			flags |= NOSKIP;
    287 			c = CT_CHAR;
    288 			break;
    289 
    290 		case 'p':	/* pointer format is like hex */
    291 			flags |= POINTER | PFXOK;
    292 			c = CT_INT;
    293 			ccfn = strtoumax;
    294 			base = 16;
    295 			break;
    296 
    297 		case 'n':
    298 			if (flags & SUPPRESS)	/* ??? */
    299 				continue;
    300 			if (flags & SHORT)
    301 				*va_arg(ap, short *) = nread;
    302 			else if (flags & LONG)
    303 				*va_arg(ap, long *) = nread;
    304 			else if (flags & QUAD)
    305 				*va_arg(ap, quad_t *) = nread;
    306 			else if (flags & LONGLONG)
    307 				/* LONGLONG */
    308 				*va_arg(ap, long long int *) = nread;
    309 			else if (flags & SIZEINT)
    310 				*va_arg(ap, ssize_t *) = nread;
    311 			else if (flags & PTRINT)
    312 				*va_arg(ap, ptrdiff_t *) = nread;
    313 			else if (flags & MAXINT)
    314 				*va_arg(ap, intmax_t *) = nread;
    315 			else
    316 				*va_arg(ap, int *) = nread;
    317 			continue;
    318 
    319 		/*
    320 		 * Disgusting backwards compatibility hacks.	XXX
    321 		 */
    322 		case '\0':	/* compat */
    323 			FUNLOCKFILE(fp);
    324 			return (EOF);
    325 
    326 		default:	/* compat */
    327 			if (isupper(c))
    328 				flags |= LONG;
    329 			c = CT_INT;
    330 			ccfn = (uintmax_t (*) __P((const char *, char **, int)))strtoimax;
    331 			base = 10;
    332 			break;
    333 		}
    334 
    335 		/*
    336 		 * We have a conversion that requires input.
    337 		 */
    338 		if (fp->_r <= 0 && __srefill(fp))
    339 			goto input_failure;
    340 
    341 		/*
    342 		 * Consume leading white space, except for formats
    343 		 * that suppress this.
    344 		 */
    345 		if ((flags & NOSKIP) == 0) {
    346 			while (isspace(*fp->_p)) {
    347 				nread++;
    348 				if (--fp->_r > 0)
    349 					fp->_p++;
    350 				else if (__srefill(fp))
    351 					goto input_failure;
    352 			}
    353 			/*
    354 			 * Note that there is at least one character in
    355 			 * the buffer, so conversions that do not set NOSKIP
    356 			 * ca no longer result in an input failure.
    357 			 */
    358 		}
    359 
    360 		/*
    361 		 * Do the conversion.
    362 		 */
    363 		switch (c) {
    364 
    365 		case CT_CHAR:
    366 			/* scan arbitrary characters (sets NOSKIP) */
    367 			if (width == 0)
    368 				width = 1;
    369 			if (flags & SUPPRESS) {
    370 				size_t sum = 0;
    371 				for (;;) {
    372 					if ((n = fp->_r) < width) {
    373 						sum += n;
    374 						width -= n;
    375 						fp->_p += n;
    376 						if (__srefill(fp)) {
    377 							if (sum == 0)
    378 							    goto input_failure;
    379 							break;
    380 						}
    381 					} else {
    382 						sum += width;
    383 						fp->_r -= width;
    384 						fp->_p += width;
    385 						break;
    386 					}
    387 				}
    388 				nread += sum;
    389 			} else {
    390 				size_t r = fread((void *)va_arg(ap, char *), 1,
    391 				    width, fp);
    392 
    393 				if (r == 0)
    394 					goto input_failure;
    395 				nread += r;
    396 				nassigned++;
    397 			}
    398 			break;
    399 
    400 		case CT_CCL:
    401 			/* scan a (nonempty) character class (sets NOSKIP) */
    402 			if (width == 0)
    403 				width = ~0U;	/* `infinity' */
    404 			/* take only those things in the class */
    405 			if (flags & SUPPRESS) {
    406 				n = 0;
    407 				while (ccltab[*fp->_p]) {
    408 					n++, fp->_r--, fp->_p++;
    409 					if (--width == 0)
    410 						break;
    411 					if (fp->_r <= 0 && __srefill(fp)) {
    412 						if (n == 0)
    413 							goto input_failure;
    414 						break;
    415 					}
    416 				}
    417 				if (n == 0)
    418 					goto match_failure;
    419 			} else {
    420 				p0 = p = va_arg(ap, char *);
    421 				while (ccltab[*fp->_p]) {
    422 					fp->_r--;
    423 					*p++ = *fp->_p++;
    424 					if (--width == 0)
    425 						break;
    426 					if (fp->_r <= 0 && __srefill(fp)) {
    427 						if (p == p0)
    428 							goto input_failure;
    429 						break;
    430 					}
    431 				}
    432 				n = p - p0;
    433 				if (n == 0)
    434 					goto match_failure;
    435 				*p = 0;
    436 				nassigned++;
    437 			}
    438 			nread += n;
    439 			break;
    440 
    441 		case CT_STRING:
    442 			/* like CCL, but zero-length string OK, & no NOSKIP */
    443 			if (width == 0)
    444 				width = ~0U;
    445 			if (flags & SUPPRESS) {
    446 				n = 0;
    447 				while (!isspace(*fp->_p)) {
    448 					n++, fp->_r--, fp->_p++;
    449 					if (--width == 0)
    450 						break;
    451 					if (fp->_r <= 0 && __srefill(fp))
    452 						break;
    453 				}
    454 				nread += n;
    455 			} else {
    456 				p0 = p = va_arg(ap, char *);
    457 				while (!isspace(*fp->_p)) {
    458 					fp->_r--;
    459 					*p++ = *fp->_p++;
    460 					if (--width == 0)
    461 						break;
    462 					if (fp->_r <= 0 && __srefill(fp))
    463 						break;
    464 				}
    465 				*p = 0;
    466 				nread += p - p0;
    467 				nassigned++;
    468 			}
    469 			continue;
    470 
    471 		case CT_INT:
    472 			/* scan an integer as if by strtoimax/strtoumax */
    473 #ifdef hardway
    474 			if (width == 0 || width > sizeof(buf) - 1)
    475 				width = sizeof(buf) - 1;
    476 #else
    477 			/* size_t is unsigned, hence this optimisation */
    478 			if (--width > sizeof(buf) - 2)
    479 				width = sizeof(buf) - 2;
    480 			width++;
    481 #endif
    482 			flags |= SIGNOK | NDIGITS | NZDIGITS;
    483 			for (p = buf; width; width--) {
    484 				c = *fp->_p;
    485 				/*
    486 				 * Switch on the character; `goto ok'
    487 				 * if we accept it as a part of number.
    488 				 */
    489 				switch (c) {
    490 
    491 				/*
    492 				 * The digit 0 is always legal, but is
    493 				 * special.  For %i conversions, if no
    494 				 * digits (zero or nonzero) have been
    495 				 * scanned (only signs), we will have
    496 				 * base==0.  In that case, we should set
    497 				 * it to 8 and enable 0x prefixing.
    498 				 * Also, if we have not scanned zero digits
    499 				 * before this, do not turn off prefixing
    500 				 * (someone else will turn it off if we
    501 				 * have scanned any nonzero digits).
    502 				 */
    503 				case '0':
    504 					if (base == 0) {
    505 						base = 8;
    506 						flags |= PFXOK;
    507 					}
    508 					if (flags & NZDIGITS)
    509 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
    510 					else
    511 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
    512 					goto ok;
    513 
    514 				/* 1 through 7 always legal */
    515 				case '1': case '2': case '3':
    516 				case '4': case '5': case '6': case '7':
    517 					base = basefix[base];
    518 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    519 					goto ok;
    520 
    521 				/* digits 8 and 9 ok iff decimal or hex */
    522 				case '8': case '9':
    523 					base = basefix[base];
    524 					if (base <= 8)
    525 						break;	/* not legal here */
    526 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    527 					goto ok;
    528 
    529 				/* letters ok iff hex */
    530 				case 'A': case 'B': case 'C':
    531 				case 'D': case 'E': case 'F':
    532 				case 'a': case 'b': case 'c':
    533 				case 'd': case 'e': case 'f':
    534 					/* no need to fix base here */
    535 					if (base <= 10)
    536 						break;	/* not legal here */
    537 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
    538 					goto ok;
    539 
    540 				/* sign ok only as first character */
    541 				case '+': case '-':
    542 					if (flags & SIGNOK) {
    543 						flags &= ~SIGNOK;
    544 						goto ok;
    545 					}
    546 					break;
    547 
    548 				/* x ok iff flag still set & 2nd char */
    549 				case 'x': case 'X':
    550 					if (flags & PFXOK && p == buf + 1) {
    551 						base = 16;	/* if %i */
    552 						flags &= ~PFXOK;
    553 						goto ok;
    554 					}
    555 					break;
    556 				}
    557 
    558 				/*
    559 				 * If we got here, c is not a legal character
    560 				 * for a number.  Stop accumulating digits.
    561 				 */
    562 				break;
    563 		ok:
    564 				/*
    565 				 * c is legal: store it and look at the next.
    566 				 */
    567 				*p++ = c;
    568 				if (--fp->_r > 0)
    569 					fp->_p++;
    570 				else if (__srefill(fp))
    571 					break;		/* EOF */
    572 			}
    573 			/*
    574 			 * If we had only a sign, it is no good; push
    575 			 * back the sign.  If the number ends in `x',
    576 			 * it was [sign] '0' 'x', so push back the x
    577 			 * and treat it as [sign] '0'.
    578 			 */
    579 			if (flags & NDIGITS) {
    580 				if (p > buf)
    581 					(void) ungetc(*(u_char *)--p, fp);
    582 				goto match_failure;
    583 			}
    584 			c = ((u_char *)p)[-1];
    585 			if (c == 'x' || c == 'X') {
    586 				--p;
    587 				(void) ungetc(c, fp);
    588 			}
    589 			if ((flags & SUPPRESS) == 0) {
    590 				uintmax_t res;
    591 
    592 				*p = 0;
    593 				res = (*ccfn)(buf, (char **)NULL, base);
    594 				if (flags & POINTER)
    595 					*va_arg(ap, void **) =
    596 					    (void *)(long)res;
    597 				else if (flags & MAXINT)
    598 					*va_arg(ap, intmax_t *) = res;
    599 				else if (flags & PTRINT)
    600 					*va_arg(ap, ptrdiff_t *) =
    601 					    (ptrdiff_t)res;
    602 				else if (flags & SIZEINT)
    603 					*va_arg(ap, ssize_t *) = (ssize_t)res;
    604 				else if (flags & LONGLONG)
    605 					/* LONGLONG */
    606 					*va_arg(ap, long long int *) = res;
    607 				else if (flags & QUAD)
    608 					*va_arg(ap, quad_t *) = (quad_t)res;
    609 				else if (flags & LONG)
    610 					*va_arg(ap, long *) = (long)res;
    611 				else if (flags & SHORT)
    612 					*va_arg(ap, short *) = (short)res;
    613 				else
    614 					*va_arg(ap, int *) = (int)res;
    615 				nassigned++;
    616 			}
    617 			nread += p - buf;
    618 			break;
    619 
    620 #ifdef FLOATING_POINT
    621 		case CT_FLOAT:
    622 			/* scan a floating point number as if by strtod */
    623 #ifdef hardway
    624 			if (width == 0 || width > sizeof(buf) - 1)
    625 				width = sizeof(buf) - 1;
    626 #else
    627 			/* size_t is unsigned, hence this optimisation */
    628 			if (--width > sizeof(buf) - 2)
    629 				width = sizeof(buf) - 2;
    630 			width++;
    631 #endif
    632 			flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
    633 			for (p = buf; width; width--) {
    634 				c = *fp->_p;
    635 				/*
    636 				 * This code mimicks the integer conversion
    637 				 * code, but is much simpler.
    638 				 */
    639 				switch (c) {
    640 
    641 				case '0': case '1': case '2': case '3':
    642 				case '4': case '5': case '6': case '7':
    643 				case '8': case '9':
    644 					flags &= ~(SIGNOK | NDIGITS);
    645 					goto fok;
    646 
    647 				case '+': case '-':
    648 					if (flags & SIGNOK) {
    649 						flags &= ~SIGNOK;
    650 						goto fok;
    651 					}
    652 					break;
    653 				case '.':
    654 					if (flags & DPTOK) {
    655 						flags &= ~(SIGNOK | DPTOK);
    656 						goto fok;
    657 					}
    658 					break;
    659 				case 'e': case 'E':
    660 					/* no exponent without some digits */
    661 					if ((flags&(NDIGITS|EXPOK)) == EXPOK) {
    662 						flags =
    663 						    (flags & ~(EXPOK|DPTOK)) |
    664 						    SIGNOK | NDIGITS;
    665 						goto fok;
    666 					}
    667 					break;
    668 				}
    669 				break;
    670 		fok:
    671 				*p++ = c;
    672 				if (--fp->_r > 0)
    673 					fp->_p++;
    674 				else if (__srefill(fp))
    675 					break;	/* EOF */
    676 			}
    677 			/*
    678 			 * If no digits, might be missing exponent digits
    679 			 * (just give back the exponent) or might be missing
    680 			 * regular digits, but had sign and/or decimal point.
    681 			 */
    682 			if (flags & NDIGITS) {
    683 				if (flags & EXPOK) {
    684 					/* no digits at all */
    685 					while (p > buf)
    686 						ungetc(*(u_char *)--p, fp);
    687 					goto match_failure;
    688 				}
    689 				/* just a bad exponent (e and maybe sign) */
    690 				c = *(u_char *)--p;
    691 				if (c != 'e' && c != 'E') {
    692 					(void) ungetc(c, fp);/* sign */
    693 					c = *(u_char *)--p;
    694 				}
    695 				(void) ungetc(c, fp);
    696 			}
    697 			if ((flags & SUPPRESS) == 0) {
    698 				double res;
    699 
    700 				*p = 0;
    701 				res = strtod(buf, (char **) NULL);
    702 				if (flags & LONGDBL)
    703 					*va_arg(ap, long double *) = res;
    704 				else if (flags & LONG)
    705 					*va_arg(ap, double *) = res;
    706 				else
    707 					*va_arg(ap, float *) = res;
    708 				nassigned++;
    709 			}
    710 			nread += p - buf;
    711 			break;
    712 #endif /* FLOATING_POINT */
    713 		}
    714 	}
    715 input_failure:
    716 	FUNLOCKFILE(fp);
    717 	return (nassigned ? nassigned : EOF);
    718 match_failure:
    719 	FUNLOCKFILE(fp);
    720 	return (nassigned);
    721 }
    722 
    723 /*
    724  * Fill in the given table from the scanset at the given format
    725  * (just after `[').  Return a pointer to the character past the
    726  * closing `]'.  The table has a 1 wherever characters should be
    727  * considered part of the scanset.
    728  */
    729 static const u_char *
    730 __sccl(tab, fmt)
    731 	char *tab;
    732 	const u_char *fmt;
    733 {
    734 	int c, n, v;
    735 
    736 	_DIAGASSERT(tab != NULL);
    737 	_DIAGASSERT(fmt != NULL);
    738 
    739 	/* first `clear' the whole table */
    740 	c = *fmt++;		/* first char hat => negated scanset */
    741 	if (c == '^') {
    742 		v = 1;		/* default => accept */
    743 		c = *fmt++;	/* get new first char */
    744 	} else
    745 		v = 0;		/* default => reject */
    746 	/* should probably use memset here */
    747 	for (n = 0; n < 256; n++)
    748 		tab[n] = v;
    749 	if (c == 0)
    750 		return (fmt - 1);/* format ended before closing ] */
    751 
    752 	/*
    753 	 * Now set the entries corresponding to the actual scanset
    754 	 * to the opposite of the above.
    755 	 *
    756 	 * The first character may be ']' (or '-') without being special;
    757 	 * the last character may be '-'.
    758 	 */
    759 	v = 1 - v;
    760 	for (;;) {
    761 		tab[c] = v;		/* take character c */
    762 doswitch:
    763 		n = *fmt++;		/* and examine the next */
    764 		switch (n) {
    765 
    766 		case 0:			/* format ended too soon */
    767 			return (fmt - 1);
    768 
    769 		case '-':
    770 			/*
    771 			 * A scanset of the form
    772 			 *	[01+-]
    773 			 * is defined as `the digit 0, the digit 1,
    774 			 * the character +, the character -', but
    775 			 * the effect of a scanset such as
    776 			 *	[a-zA-Z0-9]
    777 			 * is implementation defined.  The V7 Unix
    778 			 * scanf treats `a-z' as `the letters a through
    779 			 * z', but treats `a-a' as `the letter a, the
    780 			 * character -, and the letter a'.
    781 			 *
    782 			 * For compatibility, the `-' is not considerd
    783 			 * to define a range if the character following
    784 			 * it is either a close bracket (required by ANSI)
    785 			 * or is not numerically greater than the character
    786 			 * we just stored in the table (c).
    787 			 */
    788 			n = *fmt;
    789 			if (n == ']' || n < c) {
    790 				c = '-';
    791 				break;	/* resume the for(;;) */
    792 			}
    793 			fmt++;
    794 			do {		/* fill in the range */
    795 				tab[++c] = v;
    796 			} while (c < n);
    797 #if 1	/* XXX another disgusting compatibility hack */
    798 			/*
    799 			 * Alas, the V7 Unix scanf also treats formats
    800 			 * such as [a-c-e] as `the letters a through e'.
    801 			 * This too is permitted by the standard....
    802 			 */
    803 			goto doswitch;
    804 #else
    805 			c = *fmt++;
    806 			if (c == 0)
    807 				return (fmt - 1);
    808 			if (c == ']')
    809 				return (fmt);
    810 			break;
    811 #endif
    812 
    813 		case ']':		/* end of scanset */
    814 			return (fmt);
    815 
    816 		default:		/* just another character */
    817 			c = n;
    818 			break;
    819 		}
    820 	}
    821 	/* NOTREACHED */
    822 }
    823