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