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