xref: /freebsd/lib/libc/stdio/vfscanf.c (revision f6a4109212fd8fbabc731f07b2dd5c7e07fbec33)
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 <wchar.h>
52 #include <wctype.h>
53 #include "un-namespace.h"
54 
55 #include "collate.h"
56 #include "libc_private.h"
57 #include "local.h"
58 
59 #define FLOATING_POINT
60 
61 #ifdef FLOATING_POINT
62 #include <locale.h>
63 #endif
64 
65 #define	BUF		513	/* Maximum length of numeric string. */
66 
67 /*
68  * Flags used during conversion.
69  */
70 #define	LONG		0x01	/* l: long or double */
71 #define	LONGDBL		0x02	/* L: long double */
72 #define	SHORT		0x04	/* h: short */
73 #define	SUPPRESS	0x08	/* *: suppress assignment */
74 #define	POINTER		0x10	/* p: void * (as hex) */
75 #define	NOSKIP		0x20	/* [ or c: do not skip blanks */
76 #define	LONGLONG	0x400	/* ll: long long (+ deprecated q: quad) */
77 #define	INTMAXT		0x800	/* j: intmax_t */
78 #define	PTRDIFFT	0x1000	/* t: ptrdiff_t */
79 #define	SIZET		0x2000	/* z: size_t */
80 #define	SHORTSHORT	0x4000	/* hh: char */
81 #define	UNSIGNED	0x8000	/* %[oupxX] conversions */
82 
83 /*
84  * The following are used in integral conversions only:
85  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
86  */
87 #define	SIGNOK		0x40	/* +/- is (still) legal */
88 #define	NDIGITS		0x80	/* no digits detected */
89 #define	PFXOK		0x100	/* 0x prefix is (still) legal */
90 #define	NZDIGITS	0x200	/* no zero digits detected */
91 #define	HAVESIGN	0x10000	/* sign detected */
92 
93 /*
94  * Conversion types.
95  */
96 #define	CT_CHAR		0	/* %c conversion */
97 #define	CT_CCL		1	/* %[...] conversion */
98 #define	CT_STRING	2	/* %s conversion */
99 #define	CT_INT		3	/* %[dioupxX] conversion */
100 #define	CT_FLOAT	4	/* %[efgEFG] conversion */
101 
102 static const u_char *__sccl(char *, const u_char *);
103 static int parsefloat(FILE *, char *, char *);
104 
105 int __scanfdebug = 0;
106 
107 __weak_reference(__vfscanf, vfscanf);
108 
109 /*
110  * __vfscanf - MT-safe version
111  */
112 int
113 __vfscanf(FILE *fp, char const *fmt0, va_list ap)
114 {
115 	int ret;
116 
117 	FLOCKFILE(fp);
118 	ret = __svfscanf(fp, fmt0, ap);
119 	FUNLOCKFILE(fp);
120 	return (ret);
121 }
122 
123 /*
124  * __svfscanf - non-MT-safe version of __vfscanf
125  */
126 int
127 __svfscanf(FILE *fp, const char *fmt0, va_list ap)
128 {
129 	const u_char *fmt = (const u_char *)fmt0;
130 	int c;			/* character from format, or conversion */
131 	size_t width;		/* field width, or 0 */
132 	char *p;		/* points into all kinds of strings */
133 	int n;			/* handy integer */
134 	int flags;		/* flags as defined above */
135 	char *p0;		/* saves original value of p when necessary */
136 	int nassigned;		/* number of fields assigned */
137 	int nconversions;	/* number of conversions */
138 	int nread;		/* number of characters consumed from fp */
139 	int base;		/* base argument to conversion function */
140 	char ccltab[256];	/* character class table for %[...] */
141 	char buf[BUF];		/* buffer for numeric and mb conversions */
142 	wchar_t *wcp;		/* handy wide character pointer */
143 	wchar_t *wcp0;		/* saves original value of wcp */
144 	size_t nconv;		/* length of multibyte sequence converted */
145 
146 	/* `basefix' is used to avoid `if' tests in the integer scanner */
147 	static short basefix[17] =
148 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
149 
150 	ORIENT(fp, -1);
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 'A': case 'E': case 'F': case 'G':
257 		case 'a': 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 & LONG) {
357 				if ((flags & SUPPRESS) == 0)
358 					wcp = va_arg(ap, wchar_t *);
359 				else
360 					wcp = NULL;
361 				n = 0;
362 				while (width != 0) {
363 					if (n == MB_CUR_MAX) {
364 						fp->_flags |= __SERR;
365 						goto input_failure;
366 					}
367 					buf[n++] = *fp->_p;
368 					fp->_p++;
369 					fp->_r--;
370 					nconv = mbrtowc(wcp, buf, n, NULL);
371 					if (nconv == (size_t)-1) {
372 						fp->_flags |= __SERR;
373 						goto input_failure;
374 					}
375 					if (nconv == 0 && !(flags & SUPPRESS))
376 						*wcp = L'\0';
377 					if (nconv != (size_t)-2) {
378 						nread += n;
379 						width--;
380 						if (!(flags & SUPPRESS))
381 							wcp++;
382 						n = 0;
383 					}
384 					if (fp->_r <= 0 && __srefill(fp)) {
385 						if (n != 0) {
386 							fp->_flags |= __SERR;
387 							goto input_failure;
388 						}
389 						break;
390 					}
391 				}
392 				if (!(flags & SUPPRESS))
393 					nassigned++;
394 			} else if (flags & SUPPRESS) {
395 				size_t sum = 0;
396 				for (;;) {
397 					if ((n = fp->_r) < width) {
398 						sum += n;
399 						width -= n;
400 						fp->_p += n;
401 						if (__srefill(fp)) {
402 							if (sum == 0)
403 							    goto input_failure;
404 							break;
405 						}
406 					} else {
407 						sum += width;
408 						fp->_r -= width;
409 						fp->_p += width;
410 						break;
411 					}
412 				}
413 				nread += sum;
414 			} else {
415 				size_t r = fread((void *)va_arg(ap, char *), 1,
416 				    width, fp);
417 
418 				if (r == 0)
419 					goto input_failure;
420 				nread += r;
421 				nassigned++;
422 			}
423 			nconversions++;
424 			break;
425 
426 		case CT_CCL:
427 			/* scan a (nonempty) character class (sets NOSKIP) */
428 			if (width == 0)
429 				width = (size_t)~0;	/* `infinity' */
430 			/* take only those things in the class */
431 			if (flags & LONG) {
432 				wchar_t twc;
433 				int nchars;
434 
435 				if ((flags & SUPPRESS) == 0)
436 					wcp = wcp0 = va_arg(ap, wchar_t *);
437 				else
438 					wcp = wcp0 = &twc;
439 				n = 0;
440 				nchars = 0;
441 				while (width != 0) {
442 					if (n == MB_CUR_MAX) {
443 						fp->_flags |= __SERR;
444 						goto input_failure;
445 					}
446 					buf[n++] = *fp->_p;
447 					fp->_p++;
448 					fp->_r--;
449 					nconv = mbrtowc(wcp, buf, n, NULL);
450 					if (nconv == (size_t)-1) {
451 						fp->_flags |= __SERR;
452 						goto input_failure;
453 					}
454 					if (nconv == 0)
455 						*wcp = L'\0';
456 					if (nconv != (size_t)-2) {
457 						if (wctob(*wcp) != EOF &&
458 						    !ccltab[wctob(*wcp)]) {
459 							while (n != 0) {
460 								n--;
461 								__ungetc(buf[n],
462 								    fp);
463 							}
464 							break;
465 						}
466 						nread += n;
467 						width--;
468 						if (!(flags & SUPPRESS))
469 							wcp++;
470 						nchars++;
471 						n = 0;
472 					}
473 					if (fp->_r <= 0 && __srefill(fp)) {
474 						if (n != 0) {
475 							fp->_flags |= __SERR;
476 							goto input_failure;
477 						}
478 						break;
479 					}
480 				}
481 				if (n != 0) {
482 					fp->_flags |= __SERR;
483 					goto input_failure;
484 				}
485 				n = nchars;
486 				if (n == 0)
487 					goto match_failure;
488 				if (!(flags & SUPPRESS)) {
489 					*wcp = L'\0';
490 					nassigned++;
491 				}
492 			} else if (flags & SUPPRESS) {
493 				n = 0;
494 				while (ccltab[*fp->_p]) {
495 					n++, fp->_r--, fp->_p++;
496 					if (--width == 0)
497 						break;
498 					if (fp->_r <= 0 && __srefill(fp)) {
499 						if (n == 0)
500 							goto input_failure;
501 						break;
502 					}
503 				}
504 				if (n == 0)
505 					goto match_failure;
506 			} else {
507 				p0 = p = va_arg(ap, char *);
508 				while (ccltab[*fp->_p]) {
509 					fp->_r--;
510 					*p++ = *fp->_p++;
511 					if (--width == 0)
512 						break;
513 					if (fp->_r <= 0 && __srefill(fp)) {
514 						if (p == p0)
515 							goto input_failure;
516 						break;
517 					}
518 				}
519 				n = p - p0;
520 				if (n == 0)
521 					goto match_failure;
522 				*p = 0;
523 				nassigned++;
524 			}
525 			nread += n;
526 			nconversions++;
527 			break;
528 
529 		case CT_STRING:
530 			/* like CCL, but zero-length string OK, & no NOSKIP */
531 			if (width == 0)
532 				width = (size_t)~0;
533 			if (flags & LONG) {
534 				wchar_t twc;
535 
536 				if ((flags & SUPPRESS) == 0)
537 					wcp = va_arg(ap, wchar_t *);
538 				else
539 					wcp = &twc;
540 				n = 0;
541 				while (!isspace(*fp->_p) && width != 0) {
542 					if (n == MB_CUR_MAX) {
543 						fp->_flags |= __SERR;
544 						goto input_failure;
545 					}
546 					buf[n++] = *fp->_p;
547 					fp->_p++;
548 					fp->_r--;
549 					nconv = mbrtowc(wcp, buf, n, NULL);
550 					if (nconv == (size_t)-1) {
551 						fp->_flags |= __SERR;
552 						goto input_failure;
553 					}
554 					if (nconv == 0)
555 						*wcp = L'\0';
556 					if (nconv != (size_t)-2) {
557 						if (iswspace(*wcp)) {
558 							while (n != 0) {
559 								n--;
560 								__ungetc(buf[n],
561 								    fp);
562 							}
563 							break;
564 						}
565 						nread += n;
566 						width--;
567 						if (!(flags & SUPPRESS))
568 							wcp++;
569 						n = 0;
570 					}
571 					if (fp->_r <= 0 && __srefill(fp)) {
572 						if (n != 0) {
573 							fp->_flags |= __SERR;
574 							goto input_failure;
575 						}
576 						break;
577 					}
578 				}
579 				if (!(flags & SUPPRESS)) {
580 					*wcp = L'\0';
581 					nassigned++;
582 				}
583 			} else if (flags & SUPPRESS) {
584 				n = 0;
585 				while (!isspace(*fp->_p)) {
586 					n++, fp->_r--, fp->_p++;
587 					if (--width == 0)
588 						break;
589 					if (fp->_r <= 0 && __srefill(fp))
590 						break;
591 				}
592 				nread += n;
593 			} else {
594 				p0 = p = va_arg(ap, char *);
595 				while (!isspace(*fp->_p)) {
596 					fp->_r--;
597 					*p++ = *fp->_p++;
598 					if (--width == 0)
599 						break;
600 					if (fp->_r <= 0 && __srefill(fp))
601 						break;
602 				}
603 				*p = 0;
604 				nread += p - p0;
605 				nassigned++;
606 			}
607 			nconversions++;
608 			continue;
609 
610 		case CT_INT:
611 			/* scan an integer as if by the conversion function */
612 #ifdef hardway
613 			if (width == 0 || width > sizeof(buf) - 1)
614 				width = sizeof(buf) - 1;
615 #else
616 			/* size_t is unsigned, hence this optimisation */
617 			if (--width > sizeof(buf) - 2)
618 				width = sizeof(buf) - 2;
619 			width++;
620 #endif
621 			flags |= SIGNOK | NDIGITS | NZDIGITS;
622 			for (p = buf; width; width--) {
623 				c = *fp->_p;
624 				/*
625 				 * Switch on the character; `goto ok'
626 				 * if we accept it as a part of number.
627 				 */
628 				switch (c) {
629 
630 				/*
631 				 * The digit 0 is always legal, but is
632 				 * special.  For %i conversions, if no
633 				 * digits (zero or nonzero) have been
634 				 * scanned (only signs), we will have
635 				 * base==0.  In that case, we should set
636 				 * it to 8 and enable 0x prefixing.
637 				 * Also, if we have not scanned zero digits
638 				 * before this, do not turn off prefixing
639 				 * (someone else will turn it off if we
640 				 * have scanned any nonzero digits).
641 				 */
642 				case '0':
643 					if (base == 0) {
644 						base = 8;
645 						flags |= PFXOK;
646 					}
647 					if (flags & NZDIGITS)
648 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
649 					else
650 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
651 					goto ok;
652 
653 				/* 1 through 7 always legal */
654 				case '1': case '2': case '3':
655 				case '4': case '5': case '6': case '7':
656 					base = basefix[base];
657 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
658 					goto ok;
659 
660 				/* digits 8 and 9 ok iff decimal or hex */
661 				case '8': case '9':
662 					base = basefix[base];
663 					if (base <= 8)
664 						break;	/* not legal here */
665 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
666 					goto ok;
667 
668 				/* letters ok iff hex */
669 				case 'A': case 'B': case 'C':
670 				case 'D': case 'E': case 'F':
671 				case 'a': case 'b': case 'c':
672 				case 'd': case 'e': case 'f':
673 					/* no need to fix base here */
674 					if (base <= 10)
675 						break;	/* not legal here */
676 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
677 					goto ok;
678 
679 				/* sign ok only as first character */
680 				case '+': case '-':
681 					if (flags & SIGNOK) {
682 						flags &= ~SIGNOK;
683 						flags |= HAVESIGN;
684 						goto ok;
685 					}
686 					break;
687 
688 				/*
689 				 * x ok iff flag still set & 2nd char (or
690 				 * 3rd char if we have a sign).
691 				 */
692 				case 'x': case 'X':
693 					if (flags & PFXOK && p ==
694 					    buf + 1 + !!(flags & HAVESIGN)) {
695 						base = 16;	/* if %i */
696 						flags &= ~PFXOK;
697 						goto ok;
698 					}
699 					break;
700 				}
701 
702 				/*
703 				 * If we got here, c is not a legal character
704 				 * for a number.  Stop accumulating digits.
705 				 */
706 				break;
707 		ok:
708 				/*
709 				 * c is legal: store it and look at the next.
710 				 */
711 				*p++ = c;
712 				if (--fp->_r > 0)
713 					fp->_p++;
714 				else if (__srefill(fp))
715 					break;		/* EOF */
716 			}
717 			/*
718 			 * If we had only a sign, it is no good; push
719 			 * back the sign.  If the number ends in `x',
720 			 * it was [sign] '0' 'x', so push back the x
721 			 * and treat it as [sign] '0'.
722 			 */
723 			if (flags & NDIGITS) {
724 				if (p > buf)
725 					(void) __ungetc(*(u_char *)--p, fp);
726 				goto match_failure;
727 			}
728 			c = ((u_char *)p)[-1];
729 			if (c == 'x' || c == 'X') {
730 				--p;
731 				(void) __ungetc(c, fp);
732 			}
733 			if ((flags & SUPPRESS) == 0) {
734 				uintmax_t res;
735 
736 				*p = 0;
737 				if ((flags & UNSIGNED) == 0)
738 				    res = strtoimax(buf, (char **)NULL, base);
739 				else
740 				    res = strtoumax(buf, (char **)NULL, base);
741 				if (flags & POINTER)
742 					*va_arg(ap, void **) =
743 							(void *)(uintptr_t)res;
744 				else if (flags & SHORTSHORT)
745 					*va_arg(ap, char *) = res;
746 				else if (flags & SHORT)
747 					*va_arg(ap, short *) = res;
748 				else if (flags & LONG)
749 					*va_arg(ap, long *) = res;
750 				else if (flags & LONGLONG)
751 					*va_arg(ap, long long *) = res;
752 				else if (flags & INTMAXT)
753 					*va_arg(ap, intmax_t *) = res;
754 				else if (flags & PTRDIFFT)
755 					*va_arg(ap, ptrdiff_t *) = res;
756 				else if (flags & SIZET)
757 					*va_arg(ap, size_t *) = res;
758 				else
759 					*va_arg(ap, int *) = res;
760 				nassigned++;
761 			}
762 			nread += p - buf;
763 			nconversions++;
764 			break;
765 
766 #ifdef FLOATING_POINT
767 		case CT_FLOAT:
768 			/* scan a floating point number as if by strtod */
769 			if (width == 0 || width > sizeof(buf) - 1)
770 				width = sizeof(buf) - 1;
771 			if ((width = parsefloat(fp, buf, buf + width)) == 0)
772 				goto match_failure;
773 			if ((flags & SUPPRESS) == 0) {
774 				if (flags & LONGDBL) {
775 					long double res = strtold(buf, &p);
776 					*va_arg(ap, long double *) = res;
777 				} else if (flags & LONG) {
778 					double res = strtod(buf, &p);
779 					*va_arg(ap, double *) = res;
780 				} else {
781 					float res = strtof(buf, &p);
782 					*va_arg(ap, float *) = res;
783 				}
784 				if (__scanfdebug && p - buf != width)
785 					abort();
786 				nassigned++;
787 			}
788 			nread += width;
789 			nconversions++;
790 			break;
791 #endif /* FLOATING_POINT */
792 		}
793 	}
794 input_failure:
795 	return (nconversions != 0 ? nassigned : EOF);
796 match_failure:
797 	return (nassigned);
798 }
799 
800 /*
801  * Fill in the given table from the scanset at the given format
802  * (just after `[').  Return a pointer to the character past the
803  * closing `]'.  The table has a 1 wherever characters should be
804  * considered part of the scanset.
805  */
806 static const u_char *
807 __sccl(tab, fmt)
808 	char *tab;
809 	const u_char *fmt;
810 {
811 	int c, n, v, i;
812 
813 	/* first `clear' the whole table */
814 	c = *fmt++;		/* first char hat => negated scanset */
815 	if (c == '^') {
816 		v = 1;		/* default => accept */
817 		c = *fmt++;	/* get new first char */
818 	} else
819 		v = 0;		/* default => reject */
820 
821 	/* XXX: Will not work if sizeof(tab*) > sizeof(char) */
822 	(void) memset(tab, v, 256);
823 
824 	if (c == 0)
825 		return (fmt - 1);/* format ended before closing ] */
826 
827 	/*
828 	 * Now set the entries corresponding to the actual scanset
829 	 * to the opposite of the above.
830 	 *
831 	 * The first character may be ']' (or '-') without being special;
832 	 * the last character may be '-'.
833 	 */
834 	v = 1 - v;
835 	for (;;) {
836 		tab[c] = v;		/* take character c */
837 doswitch:
838 		n = *fmt++;		/* and examine the next */
839 		switch (n) {
840 
841 		case 0:			/* format ended too soon */
842 			return (fmt - 1);
843 
844 		case '-':
845 			/*
846 			 * A scanset of the form
847 			 *	[01+-]
848 			 * is defined as `the digit 0, the digit 1,
849 			 * the character +, the character -', but
850 			 * the effect of a scanset such as
851 			 *	[a-zA-Z0-9]
852 			 * is implementation defined.  The V7 Unix
853 			 * scanf treats `a-z' as `the letters a through
854 			 * z', but treats `a-a' as `the letter a, the
855 			 * character -, and the letter a'.
856 			 *
857 			 * For compatibility, the `-' is not considerd
858 			 * to define a range if the character following
859 			 * it is either a close bracket (required by ANSI)
860 			 * or is not numerically greater than the character
861 			 * we just stored in the table (c).
862 			 */
863 			n = *fmt;
864 			if (n == ']'
865 			    || (__collate_load_error ? n < c :
866 				__collate_range_cmp (n, c) < 0
867 			       )
868 			   ) {
869 				c = '-';
870 				break;	/* resume the for(;;) */
871 			}
872 			fmt++;
873 			/* fill in the range */
874 			if (__collate_load_error) {
875 				do {
876 					tab[++c] = v;
877 				} while (c < n);
878 			} else {
879 				for (i = 0; i < 256; i ++)
880 					if (   __collate_range_cmp (c, i) < 0
881 					    && __collate_range_cmp (i, n) <= 0
882 					   )
883 						tab[i] = v;
884 			}
885 #if 1	/* XXX another disgusting compatibility hack */
886 			c = n;
887 			/*
888 			 * Alas, the V7 Unix scanf also treats formats
889 			 * such as [a-c-e] as `the letters a through e'.
890 			 * This too is permitted by the standard....
891 			 */
892 			goto doswitch;
893 #else
894 			c = *fmt++;
895 			if (c == 0)
896 				return (fmt - 1);
897 			if (c == ']')
898 				return (fmt);
899 #endif
900 			break;
901 
902 		case ']':		/* end of scanset */
903 			return (fmt);
904 
905 		default:		/* just another character */
906 			c = n;
907 			break;
908 		}
909 	}
910 	/* NOTREACHED */
911 }
912 
913 #ifdef FLOATING_POINT
914 static int
915 parsefloat(FILE *fp, char *buf, char *end)
916 {
917 	char *commit, *p;
918 	int infnanpos = 0;
919 	enum {
920 		S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
921 		S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
922 	} state = S_START;
923 	unsigned char c;
924 	char decpt = *localeconv()->decimal_point;
925 	_Bool gotmantdig = 0, ishex = 0;
926 
927 	/*
928 	 * We set commit = p whenever the string we have read so far
929 	 * constitutes a valid representation of a floating point
930 	 * number by itself.  At some point, the parse will complete
931 	 * or fail, and we will ungetc() back to the last commit point.
932 	 * To ensure that the file offset gets updated properly, it is
933 	 * always necessary to read at least one character that doesn't
934 	 * match; thus, we can't short-circuit "infinity" or "nan(...)".
935 	 */
936 	commit = buf - 1;
937 	for (p = buf; p < end; ) {
938 		c = *fp->_p;
939 reswitch:
940 		switch (state) {
941 		case S_START:
942 			state = S_GOTSIGN;
943 			if (c == '-' || c == '+')
944 				break;
945 			else
946 				goto reswitch;
947 		case S_GOTSIGN:
948 			switch (c) {
949 			case '0':
950 				state = S_MAYBEHEX;
951 				commit = p;
952 				break;
953 			case 'I':
954 			case 'i':
955 				state = S_INF;
956 				break;
957 			case 'N':
958 			case 'n':
959 				state = S_NAN;
960 				break;
961 			default:
962 				state = S_DIGITS;
963 				goto reswitch;
964 			}
965 			break;
966 		case S_INF:
967 			if (infnanpos > 6 ||
968 			    (c != "nfinity"[infnanpos] &&
969 			     c != "NFINITY"[infnanpos]))
970 				goto parsedone;
971 			if (infnanpos == 1 || infnanpos == 6)
972 				commit = p;	/* inf or infinity */
973 			infnanpos++;
974 			break;
975 		case S_NAN:
976 			switch (infnanpos) {
977 			case -1:	/* XXX kludge to deal with nan(...) */
978 				goto parsedone;
979 			case 0:
980 				if (c != 'A' && c != 'a')
981 					goto parsedone;
982 				break;
983 			case 1:
984 				if (c != 'N' && c != 'n')
985 					goto parsedone;
986 				else
987 					commit = p;
988 				break;
989 			case 2:
990 				if (c != '(')
991 					goto parsedone;
992 				break;
993 			default:
994 				if (c == ')') {
995 					commit = p;
996 					infnanpos = -2;
997 				} else if (!isalnum(c) && c != '_')
998 					goto parsedone;
999 				break;
1000 			}
1001 			infnanpos++;
1002 			break;
1003 		case S_MAYBEHEX:
1004 			state = S_DIGITS;
1005 			if (c == 'X' || c == 'x') {
1006 				ishex = 1;
1007 				break;
1008 			} else {	/* we saw a '0', but no 'x' */
1009 				gotmantdig = 1;
1010 				goto reswitch;
1011 			}
1012 		case S_DIGITS:
1013 			if ((ishex && isxdigit(c)) || isdigit(c))
1014 				gotmantdig = 1;
1015 			else {
1016 				state = S_FRAC;
1017 				if (c != decpt)
1018 					goto reswitch;
1019 			}
1020 			if (gotmantdig)
1021 				commit = p;
1022 			break;
1023 		case S_FRAC:
1024 			if (((c == 'E' || c == 'e') && !ishex) ||
1025 			    ((c == 'P' || c == 'p') && ishex)) {
1026 				if (!gotmantdig)
1027 					goto parsedone;
1028 				else
1029 					state = S_EXP;
1030 			} else if ((ishex && isxdigit(c)) || isdigit(c)) {
1031 				commit = p;
1032 				gotmantdig = 1;
1033 			} else
1034 				goto parsedone;
1035 			break;
1036 		case S_EXP:
1037 			state = S_EXPDIGITS;
1038 			if (c == '-' || c == '+')
1039 				break;
1040 			else
1041 				goto reswitch;
1042 		case S_EXPDIGITS:
1043 			if (isdigit(c))
1044 				commit = p;
1045 			else
1046 				goto parsedone;
1047 			break;
1048 		default:
1049 			abort();
1050 		}
1051 		*p++ = c;
1052 		if (--fp->_r > 0)
1053 			fp->_p++;
1054 		else if (__srefill(fp))
1055 			break;	/* EOF */
1056 	}
1057 
1058 parsedone:
1059 	while (commit < --p)
1060 		__ungetc(*(u_char *)p, fp);
1061 	*++commit = '\0';
1062 	return (commit - buf);
1063 }
1064 #endif
1065