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