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