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