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