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