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