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