1 /*	$NetBSD: vfwscanf.c,v 1.12 2014/06/12 22:21:20 justin 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[] = "@(#)ftell.c	8.2 (Berkeley) 5/4/95";
43 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwscanf.c,v 1.12 2004/05/02 20:13:29 obrien Exp $");
44 #else
45 __RCSID("$NetBSD: vfwscanf.c,v 1.12 2014/06/12 22:21:20 justin Exp $");
46 #endif
47 #endif /* LIBC_SCCS and not lint */
48 
49 #include "namespace.h"
50 #include <ctype.h>
51 #include <inttypes.h>
52 #include <assert.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <stddef.h>
56 #include <stdarg.h>
57 #include <string.h>
58 #include <limits.h>
59 #include <wchar.h>
60 #include <wctype.h>
61 
62 #include "reentrant.h"
63 #include "local.h"
64 
65 #include <locale.h>
66 #include "setlocale_local.h"
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: quad_t (+ 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 integral conversions only:
88  * SIGNOK, NDIGITS, PFXOK, and NZDIGITS
89  */
90 #define	SIGNOK		0x40	/* +/- is (still) legal */
91 #define	NDIGITS		0x80	/* no digits detected */
92 #define	PFXOK		0x100	/* 0x prefix is (still) legal */
93 #define	NZDIGITS	0x200	/* no zero digits detected */
94 #define	HAVESIGN	0x10000	/* sign detected */
95 
96 /*
97  * Conversion types.
98  */
99 #define	CT_CHAR		0	/* %c conversion */
100 #define	CT_CCL		1	/* %[...] conversion */
101 #define	CT_STRING	2	/* %s conversion */
102 #define	CT_INT		3	/* %[dioupxX] conversion */
103 #define	CT_FLOAT	4	/* %[efgEFG] conversion */
104 
105 #ifndef NO_FLOATING_POINT
106 static int parsefloat(FILE *, wchar_t *, wchar_t *, locale_t);
107 #endif
108 
109 #define	INCCL(_c)	\
110 	(cclcompl ? (wmemchr(ccls, (_c), (size_t)(ccle - ccls)) == NULL) : \
111 	(wmemchr(ccls, (_c), (size_t)(ccle - ccls)) != NULL))
112 
113 /*
114  * MT-safe version.
115  */
116 int
vfwscanf(FILE * __restrict fp,const wchar_t * __restrict fmt,va_list ap)117 vfwscanf(FILE * __restrict fp, const wchar_t * __restrict fmt, va_list ap)
118 {
119 	return vfwscanf_l(fp, _current_locale(), fmt, ap);
120 }
121 
122 int
vfwscanf_l(FILE * __restrict fp,locale_t loc,const wchar_t * __restrict fmt,va_list ap)123 vfwscanf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt,
124     va_list ap)
125 {
126 	int ret;
127 
128 	FLOCKFILE(fp);
129 	_SET_ORIENTATION(fp, 1);
130 	ret = __vfwscanf_unlocked_l(fp, loc, fmt, ap);
131 	FUNLOCKFILE(fp);
132 	return ret;
133 }
134 
135 #define SCANF_SKIP_SPACE() \
136 do { \
137 	wint_t tc; \
138  \
139 	while ((tc = __fgetwc_unlock(fp)) != WEOF && iswspace_l(tc, loc)) \
140 		continue; \
141 	if (tc != WEOF) \
142 		ungetwc(tc, fp); \
143 } while (/*CONSTCOND*/ 0)
144 
145 /*
146  * Non-MT-safe version.
147  */
148 int
__vfwscanf_unlocked_l(FILE * __restrict fp,locale_t loc,const wchar_t * __restrict fmt,va_list ap)149 __vfwscanf_unlocked_l(FILE * __restrict fp, locale_t loc,
150     const wchar_t * __restrict fmt, va_list ap)
151 {
152 	wint_t c;		/* character from format, or conversion */
153 	size_t width;		/* field width, or 0 */
154 	wchar_t *p;		/* points into all kinds of strings */
155 	int n;			/* handy integer */
156 	int flags;		/* flags as defined above */
157 	wchar_t *p0;		/* saves original value of p when necessary */
158 	int nassigned;		/* number of fields assigned */
159 	int nconversions;	/* number of conversions */
160 	size_t nread;		/* number of characters consumed from fp */
161 	int base;		/* base argument to conversion function */
162 	wchar_t buf[BUF];	/* buffer for numeric conversions */
163 	const wchar_t *ccls;	/* character class start */
164 	const wchar_t *ccle;	/* character class end */
165 	int cclcompl;		/* ccl is complemented? */
166 	wint_t wi;		/* handy wint_t */
167 	char *mbp;		/* multibyte string pointer for %c %s %[ */
168 	size_t nconv;		/* number of bytes in mb. conversion */
169 	static const mbstate_t initial;
170 	mbstate_t mbs;
171 	char mbbuf[MB_LEN_MAX];	/* temporary mb. character buffer */
172 	/* `basefix' is used to avoid `if' tests in the integer scanner */
173 	static short basefix[17] =
174 		{ 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
175 
176 	nassigned = 0;
177 	nconversions = 0;
178 	nread = 0;
179 	ccls = ccle = NULL;
180 	base = 0;
181 	cclcompl = 0;
182 	mbp = NULL;
183 	p = NULL; /* XXXgcc */
184 	for (;;) {
185 		c = *fmt++;
186 		if (c == 0)
187 			return nassigned;
188 		if (iswspace_l(c, loc)) {
189 			while ((c = __fgetwc_unlock(fp)) != WEOF &&
190 			    iswspace_l(c, loc))
191 				;
192 			if (c != WEOF)
193 				ungetwc(c, fp);
194 			continue;
195 		}
196 		if (c != '%')
197 			goto literal;
198 		width = 0;
199 		flags = 0;
200 		/*
201 		 * switch on the format.  continue if done;
202 		 * break once format type is derived.
203 		 */
204 again:		c = *fmt++;
205 		switch (c) {
206 		case '%':
207 			SCANF_SKIP_SPACE();
208 literal:
209 			if ((wi = __fgetwc_unlock(fp)) == WEOF)
210 				goto input_failure;
211 			if (wi != c) {
212 				ungetwc(wi, fp);
213 				goto input_failure;
214 			}
215 			nread++;
216 			continue;
217 
218 		case '*':
219 			flags |= SUPPRESS;
220 			goto again;
221 		case 'j':
222 			flags |= INTMAXT;
223 			goto again;
224 		case 'l':
225 			if (flags & LONG) {
226 				flags &= ~LONG;
227 				flags |= LONGLONG;
228 			} else
229 				flags |= LONG;
230 			goto again;
231 		case 'q':
232 			flags |= LONGLONG;	/* not quite */
233 			goto again;
234 		case 't':
235 			flags |= PTRDIFFT;
236 			goto again;
237 		case 'z':
238 			flags |= SIZET;
239 			goto again;
240 		case 'L':
241 			flags |= LONGDBL;
242 			goto again;
243 		case 'h':
244 			if (flags & SHORT) {
245 				flags &= ~SHORT;
246 				flags |= SHORTSHORT;
247 			} else
248 				flags |= SHORT;
249 			goto again;
250 
251 		case '0': case '1': case '2': case '3': case '4':
252 		case '5': case '6': case '7': case '8': case '9':
253 			width = width * 10 + c - '0';
254 			goto again;
255 
256 		/*
257 		 * Conversions.
258 		 */
259 		case 'd':
260 			c = CT_INT;
261 			base = 10;
262 			break;
263 
264 		case 'i':
265 			c = CT_INT;
266 			base = 0;
267 			break;
268 
269 		case 'o':
270 			c = CT_INT;
271 			flags |= UNSIGNED;
272 			base = 8;
273 			break;
274 
275 		case 'u':
276 			c = CT_INT;
277 			flags |= UNSIGNED;
278 			base = 10;
279 			break;
280 
281 		case 'X':
282 		case 'x':
283 			flags |= PFXOK;	/* enable 0x prefixing */
284 			c = CT_INT;
285 			flags |= UNSIGNED;
286 			base = 16;
287 			break;
288 
289 #ifndef NO_FLOATING_POINT
290 		case 'A': case 'E': case 'F': case 'G':
291 		case 'a': case 'e': case 'f': case 'g':
292 			c = CT_FLOAT;
293 			break;
294 #endif
295 
296 		case 'S':
297 			flags |= LONG;
298 			/* FALLTHROUGH */
299 		case 's':
300 			c = CT_STRING;
301 			break;
302 
303 		case '[':
304 			ccls = fmt;
305 			if (*fmt == '^') {
306 				cclcompl = 1;
307 				fmt++;
308 			} else
309 				cclcompl = 0;
310 			if (*fmt == ']')
311 				fmt++;
312 			while (*fmt != '\0' && *fmt != ']')
313 				fmt++;
314 			ccle = fmt;
315 			fmt++;
316 			flags |= NOSKIP;
317 			c = CT_CCL;
318 			break;
319 
320 		case 'C':
321 			flags |= LONG;
322 			/* FALLTHROUGH */
323 		case 'c':
324 			flags |= NOSKIP;
325 			c = CT_CHAR;
326 			break;
327 
328 		case 'p':	/* pointer format is like hex */
329 			flags |= POINTER | PFXOK;
330 			c = CT_INT;		/* assumes sizeof(uintmax_t) */
331 			flags |= UNSIGNED;	/*      >= sizeof(uintptr_t) */
332 			base = 16;
333 			break;
334 
335 		case 'n':
336 			nconversions++;
337 			if (flags & SUPPRESS)	/* ??? */
338 				continue;
339 			if (flags & SHORTSHORT)
340 				*va_arg(ap, char *) = (char)nread;
341 			else if (flags & SHORT)
342 				*va_arg(ap, short *) = (short)nread;
343 			else if (flags & LONG)
344 				*va_arg(ap, long *) = nread;
345 			else if (flags & LONGLONG)
346 				*va_arg(ap, quad_t *) = nread;
347 			else if (flags & INTMAXT)
348 				*va_arg(ap, intmax_t *) = nread;
349 			else if (flags & SIZET)
350 				*va_arg(ap, size_t *) = nread;
351 			else if (flags & PTRDIFFT)
352 				*va_arg(ap, ptrdiff_t *) = nread;
353 			else
354 				*va_arg(ap, int *) = (int)nread;
355 			continue;
356 
357 		default:
358 			goto match_failure;
359 
360 		/*
361 		 * Disgusting backwards compatibility hack.	XXX
362 		 */
363 		case '\0':	/* compat */
364 			return EOF;
365 		}
366 
367 		/*
368 		 * Consume leading white space, except for formats
369 		 * that suppress this.
370 		 */
371 		if ((flags & NOSKIP) == 0) {
372 			while ((wi = __fgetwc_unlock(fp)) != WEOF &&
373 			       iswspace_l(wi, loc))
374 				nread++;
375 			if (wi == WEOF)
376 				goto input_failure;
377 			ungetwc(wi, fp);
378 		}
379 
380 		/*
381 		 * Do the conversion.
382 		 */
383 		switch (c) {
384 
385 		case CT_CHAR:
386 			/* scan arbitrary characters (sets NOSKIP) */
387 			if (width == 0)
388 				width = 1;
389 			if (flags & LONG) {
390 				if (!(flags & SUPPRESS))
391 					p = va_arg(ap, wchar_t *);
392 				n = 0;
393 				while (width-- != 0 &&
394 				    (wi = __fgetwc_unlock(fp)) != WEOF) {
395 					if (!(flags & SUPPRESS))
396 						*p++ = (wchar_t)wi;
397 					n++;
398 				}
399 				if (n == 0)
400 					goto input_failure;
401 				nread += n;
402 				if (!(flags & SUPPRESS))
403 					nassigned++;
404 			} else {
405 				if (!(flags & SUPPRESS))
406 					mbp = va_arg(ap, char *);
407 				n = 0;
408 				mbs = initial;
409 				while (width != 0 &&
410 				    (wi = __fgetwc_unlock(fp)) != WEOF) {
411 					if (width >= MB_CUR_MAX_L(loc) &&
412 					    !(flags & SUPPRESS)) {
413 						nconv = wcrtomb_l(mbp, wi,
414 						    &mbs, loc);
415 						if (nconv == (size_t)-1)
416 							goto input_failure;
417 					} else {
418 						nconv = wcrtomb_l(mbbuf, wi,
419 						    &mbs, loc);
420 						if (nconv == (size_t)-1)
421 							goto input_failure;
422 						if (nconv > width) {
423 							ungetwc(wi, fp);
424 							break;
425 						}
426 						if (!(flags & SUPPRESS))
427 							memcpy(mbp, mbbuf,
428 							    nconv);
429 					}
430 					if (!(flags & SUPPRESS))
431 						mbp += nconv;
432 					width -= nconv;
433 					n++;
434 				}
435 				if (n == 0)
436 					goto input_failure;
437 				nread += n;
438 				if (!(flags & SUPPRESS))
439 					nassigned++;
440 			}
441 			nconversions++;
442 			break;
443 
444 		case CT_CCL:
445 			/* scan a (nonempty) character class (sets NOSKIP) */
446 			if (width == 0)
447 				width = (size_t)~0;	/* `infinity' */
448 			/* take only those things in the class */
449 			if ((flags & SUPPRESS) && (flags & LONG)) {
450 				n = 0;
451 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
452 				    width-- != 0 && INCCL(wi))
453 					n++;
454 				if (wi != WEOF)
455 					ungetwc(wi, fp);
456 				if (n == 0)
457 					goto match_failure;
458 			} else if (flags & LONG) {
459 				p0 = p = va_arg(ap, wchar_t *);
460 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
461 				    width-- != 0 && INCCL(wi))
462 					*p++ = (wchar_t)wi;
463 				if (wi != WEOF)
464 					ungetwc(wi, fp);
465 				_DIAGASSERT(__type_fit(int, p - p0));
466 				n = (int)(p - p0);
467 				if (n == 0)
468 					goto match_failure;
469 				*p = 0;
470 				nassigned++;
471 			} else {
472 				if (!(flags & SUPPRESS))
473 					mbp = va_arg(ap, char *);
474 				n = 0;
475 				mbs = initial;
476 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
477 				    width != 0 && INCCL(wi)) {
478 					if (width >= MB_CUR_MAX_L(loc) &&
479 					   !(flags & SUPPRESS)) {
480 						nconv = wcrtomb_l(mbp, wi,
481 						    &mbs, loc);
482 						if (nconv == (size_t)-1)
483 							goto input_failure;
484 					} else {
485 						nconv = wcrtomb_l(mbbuf, wi,
486 						    &mbs, loc);
487 						if (nconv == (size_t)-1)
488 							goto input_failure;
489 						if (nconv > width)
490 							break;
491 						if (!(flags & SUPPRESS))
492 							memcpy(mbp, mbbuf,
493 							    nconv);
494 					}
495 					if (!(flags & SUPPRESS))
496 						mbp += nconv;
497 					width -= nconv;
498 					n++;
499 				}
500 				if (wi != WEOF)
501 					ungetwc(wi, fp);
502 				if (!(flags & SUPPRESS)) {
503 					*mbp = 0;
504 					nassigned++;
505 				}
506 			}
507 			nread += n;
508 			nconversions++;
509 			break;
510 
511 		case CT_STRING:
512 			/* like CCL, but zero-length string OK, & no NOSKIP */
513 			if (width == 0)
514 				width = (size_t)~0;
515 			if ((flags & SUPPRESS) && (flags & LONG)) {
516 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
517 				    width-- != 0 &&
518 				    !iswspace_l(wi, loc))
519 					nread++;
520 				if (wi != WEOF)
521 					ungetwc(wi, fp);
522 			} else if (flags & LONG) {
523 				p0 = p = va_arg(ap, wchar_t *);
524 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
525 				    width-- != 0 &&
526 				    !iswspace_l(wi, loc)) {
527 					*p++ = (wchar_t)wi;
528 					nread++;
529 				}
530 				if (wi != WEOF)
531 					ungetwc(wi, fp);
532 				*p = '\0';
533 				nassigned++;
534 			} else {
535 				if (!(flags & SUPPRESS))
536 					mbp = va_arg(ap, char *);
537 				mbs = initial;
538 				while ((wi = __fgetwc_unlock(fp)) != WEOF &&
539 				    width != 0 &&
540 				    !iswspace_l(wi, loc)) {
541 					if (width >= MB_CUR_MAX_L(loc) &&
542 					    !(flags & SUPPRESS)) {
543 						nconv = wcrtomb_l(mbp, wi,
544 						    &mbs, loc);
545 						if (nconv == (size_t)-1)
546 							goto input_failure;
547 					} else {
548 						nconv = wcrtomb_l(mbbuf, wi,
549 						    &mbs, loc);
550 						if (nconv == (size_t)-1)
551 							goto input_failure;
552 						if (nconv > width)
553 							break;
554 						if (!(flags & SUPPRESS))
555 							memcpy(mbp, mbbuf,
556 							    nconv);
557 					}
558 					if (!(flags & SUPPRESS))
559 						mbp += nconv;
560 					width -= nconv;
561 					nread++;
562 				}
563 				if (wi != WEOF)
564 					ungetwc(wi, fp);
565 				if (!(flags & SUPPRESS)) {
566 					*mbp = 0;
567 					nassigned++;
568 				}
569 			}
570 			nconversions++;
571 			continue;
572 
573 		case CT_INT:
574 			/* scan an integer as if by the conversion function */
575 			if (width == 0 || width > sizeof(buf) /
576 			    sizeof(*buf) - 1)
577 				width = sizeof(buf) / sizeof(*buf) - 1;
578 			flags |= SIGNOK | NDIGITS | NZDIGITS;
579 			for (p = buf; width; width--) {
580 				c = __fgetwc_unlock(fp);
581 				/*
582 				 * Switch on the character; `goto ok'
583 				 * if we accept it as a part of number.
584 				 */
585 				switch (c) {
586 
587 				/*
588 				 * The digit 0 is always legal, but is
589 				 * special.  For %i conversions, if no
590 				 * digits (zero or nonzero) have been
591 				 * scanned (only signs), we will have
592 				 * base==0.  In that case, we should set
593 				 * it to 8 and enable 0x prefixing.
594 				 * Also, if we have not scanned zero digits
595 				 * before this, do not turn off prefixing
596 				 * (someone else will turn it off if we
597 				 * have scanned any nonzero digits).
598 				 */
599 				case '0':
600 					if (base == 0) {
601 						base = 8;
602 						flags |= PFXOK;
603 					}
604 					if (flags & NZDIGITS)
605 					    flags &= ~(SIGNOK|NZDIGITS|NDIGITS);
606 					else
607 					    flags &= ~(SIGNOK|PFXOK|NDIGITS);
608 					goto ok;
609 
610 				/* 1 through 7 always legal */
611 				case '1': case '2': case '3':
612 				case '4': case '5': case '6': case '7':
613 					base = basefix[base];
614 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
615 					goto ok;
616 
617 				/* digits 8 and 9 ok iff decimal or hex */
618 				case '8': case '9':
619 					base = basefix[base];
620 					if (base <= 8)
621 						break;	/* not legal here */
622 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
623 					goto ok;
624 
625 				/* letters ok iff hex */
626 				case 'A': case 'B': case 'C':
627 				case 'D': case 'E': case 'F':
628 				case 'a': case 'b': case 'c':
629 				case 'd': case 'e': case 'f':
630 					/* no need to fix base here */
631 					if (base <= 10)
632 						break;	/* not legal here */
633 					flags &= ~(SIGNOK | PFXOK | NDIGITS);
634 					goto ok;
635 
636 				/* sign ok only as first character */
637 				case '+': case '-':
638 					if (flags & SIGNOK) {
639 						flags &= ~SIGNOK;
640 						flags |= HAVESIGN;
641 						goto ok;
642 					}
643 					break;
644 
645 				/*
646 				 * x ok iff flag still set & 2nd char (or
647 				 * 3rd char if we have a sign).
648 				 */
649 				case 'x': case 'X':
650 					if (flags & PFXOK && p ==
651 					    buf + 1 + !!(flags & HAVESIGN)) {
652 						base = 16;	/* if %i */
653 						flags &= ~PFXOK;
654 						goto ok;
655 					}
656 					break;
657 				}
658 
659 				/*
660 				 * If we got here, c is not a legal character
661 				 * for a number.  Stop accumulating digits.
662 				 */
663 				if (c != WEOF)
664 					ungetwc(c, fp);
665 				break;
666 		ok:
667 				/*
668 				 * c is legal: store it and look at the next.
669 				 */
670 				*p++ = (wchar_t)c;
671 			}
672 			/*
673 			 * If we had only a sign, it is no good; push
674 			 * back the sign.  If the number ends in `x',
675 			 * it was [sign] '0' 'x', so push back the x
676 			 * and treat it as [sign] '0'.
677 			 */
678 			if (flags & NDIGITS) {
679 				if (p > buf)
680 					ungetwc(*--p, fp);
681 				goto match_failure;
682 			}
683 			c = p[-1];
684 			if (c == 'x' || c == 'X') {
685 				--p;
686 				ungetwc(c, fp);
687 			}
688 			if ((flags & SUPPRESS) == 0) {
689 				uintmax_t res;
690 
691 				*p = 0;
692 				if ((flags & UNSIGNED) == 0)
693 				    res = wcstoimax_l(buf, NULL, base, loc);
694 				else
695 				    res = wcstoumax_l(buf, NULL, base, loc);
696 				if (flags & POINTER)
697 					*va_arg(ap, void **) =
698 							(void *)(uintptr_t)res;
699 				else if (flags & SHORTSHORT)
700 					*va_arg(ap, char *) = (char)res;
701 				else if (flags & SHORT)
702 					*va_arg(ap, short *) = (short)res;
703 				else if (flags & LONG)
704 					*va_arg(ap, long *) = (long)res;
705 				else if (flags & LONGLONG)
706 					*va_arg(ap, quad_t *) = res;
707 				else if (flags & INTMAXT)
708 					*va_arg(ap, intmax_t *) = res;
709 				else if (flags & PTRDIFFT)
710 					*va_arg(ap, ptrdiff_t *) = (ptrdiff_t)res;
711 				else if (flags & SIZET)
712 					*va_arg(ap, size_t *) = (size_t)res;
713 				else
714 					*va_arg(ap, int *) = (int)res;
715 				nassigned++;
716 			}
717 			_DIAGASSERT(__type_fit(int, p - buf));
718 			nread += (int)(p - buf);
719 			nconversions++;
720 			break;
721 
722 #ifndef NO_FLOATING_POINT
723 		case CT_FLOAT:
724 			/* scan a floating point number as if by strtod */
725 			if (width == 0 || width > sizeof(buf) /
726 			    sizeof(*buf) - 1)
727 				width = sizeof(buf) / sizeof(*buf) - 1;
728 			if ((width = parsefloat(fp, buf, buf + width, loc)) == 0)
729 				goto match_failure;
730 			if ((flags & SUPPRESS) == 0) {
731 				if (flags & LONGDBL) {
732 					long double res = wcstold_l(buf, &p,
733 					    loc);
734 					*va_arg(ap, long double *) = res;
735 				} else
736 				if (flags & LONG) {
737 					double res = wcstod_l(buf, &p, loc);
738 					*va_arg(ap, double *) = res;
739 				} else {
740 					float res = wcstof_l(buf, &p, loc);
741 					*va_arg(ap, float *) = res;
742 				}
743 #ifdef DEBUG
744 				if (p - buf != (ptrdiff_t)width)
745 					abort();
746 #endif
747 				nassigned++;
748 			}
749 			nread += width;
750 			nconversions++;
751 			break;
752 #endif /* !NO_FLOATING_POINT */
753 		}
754 	}
755 input_failure:
756 	return nconversions != 0 ? nassigned : EOF;
757 match_failure:
758 	return nassigned;
759 }
760 
761 #ifndef NO_FLOATING_POINT
762 static int
parsefloat(FILE * fp,wchar_t * buf,wchar_t * end,locale_t loc)763 parsefloat(FILE *fp, wchar_t *buf, wchar_t *end, locale_t loc)
764 {
765 	wchar_t *commit, *p;
766 	int infnanpos = 0;
767 	enum {
768 		S_START, S_GOTSIGN, S_INF, S_NAN, S_MAYBEHEX,
769 		S_DIGITS, S_FRAC, S_EXP, S_EXPDIGITS
770 	} state = S_START;
771 	wchar_t c;
772 	wchar_t decpt = (wchar_t)(unsigned char)*localeconv_l(loc)->decimal_point;
773 	int gotmantdig = 0, ishex = 0;
774 
775 	/*
776 	 * We set commit = p whenever the string we have read so far
777 	 * constitutes a valid representation of a floating point
778 	 * number by itself.  At some point, the parse will complete
779 	 * or fail, and we will ungetc() back to the last commit point.
780 	 * To ensure that the file offset gets updated properly, it is
781 	 * always necessary to read at least one character that doesn't
782 	 * match; thus, we can't short-circuit "infinity" or "nan(...)".
783 	 */
784 	commit = buf - 1;
785 	c = WEOF;
786 	for (p = buf; p < end; ) {
787 		if ((c = __fgetwc_unlock(fp)) == WEOF)
788 			break;
789 reswitch:
790 		switch (state) {
791 		case S_START:
792 			state = S_GOTSIGN;
793 			if (c == '-' || c == '+')
794 				break;
795 			else
796 				goto reswitch;
797 		case S_GOTSIGN:
798 			switch (c) {
799 			case '0':
800 				state = S_MAYBEHEX;
801 				commit = p;
802 				break;
803 			case 'I':
804 			case 'i':
805 				state = S_INF;
806 				break;
807 			case 'N':
808 			case 'n':
809 				state = S_NAN;
810 				break;
811 			default:
812 				state = S_DIGITS;
813 				goto reswitch;
814 			}
815 			break;
816 		case S_INF:
817 			if (infnanpos > 6 ||
818 			    (c != "nfinity"[infnanpos] &&
819 			     c != "NFINITY"[infnanpos]))
820 				goto parsedone;
821 			if (infnanpos == 1 || infnanpos == 6)
822 				commit = p;	/* inf or infinity */
823 			infnanpos++;
824 			break;
825 		case S_NAN:
826 			switch (infnanpos) {
827 			case -1:	/* XXX kludge to deal with nan(...) */
828 				goto parsedone;
829 			case 0:
830 				if (c != 'A' && c != 'a')
831 					goto parsedone;
832 				break;
833 			case 1:
834 				if (c != 'N' && c != 'n')
835 					goto parsedone;
836 				else
837 					commit = p;
838 				break;
839 			case 2:
840 				if (c != '(')
841 					goto parsedone;
842 				break;
843 			default:
844 				if (c == ')') {
845 					commit = p;
846 					infnanpos = -2;
847 				} else if (!iswalnum_l(c, loc) && c != '_')
848 					goto parsedone;
849 				break;
850 			}
851 			infnanpos++;
852 			break;
853 		case S_MAYBEHEX:
854 			state = S_DIGITS;
855 			if (c == 'X' || c == 'x') {
856 				ishex = 1;
857 				break;
858 			} else {	/* we saw a '0', but no 'x' */
859 				gotmantdig = 1;
860 				goto reswitch;
861 			}
862 		case S_DIGITS:
863 			if ((ishex && iswxdigit_l(c, loc)) ||
864 			    iswdigit_l(c, loc))
865 				gotmantdig = 1;
866 			else {
867 				state = S_FRAC;
868 				if (c != decpt)
869 					goto reswitch;
870 			}
871 			if (gotmantdig)
872 				commit = p;
873 			break;
874 		case S_FRAC:
875 			if (((c == 'E' || c == 'e') && !ishex) ||
876 			    ((c == 'P' || c == 'p') && ishex)) {
877 				if (!gotmantdig)
878 					goto parsedone;
879 				else
880 					state = S_EXP;
881 			} else if ((ishex && iswxdigit_l(c, loc)) ||
882 			    iswdigit_l(c, loc)) {
883 				commit = p;
884 				gotmantdig = 1;
885 			} else
886 				goto parsedone;
887 			break;
888 		case S_EXP:
889 			state = S_EXPDIGITS;
890 			if (c == '-' || c == '+')
891 				break;
892 			else
893 				goto reswitch;
894 		case S_EXPDIGITS:
895 			if (iswdigit_l(c, loc))
896 				commit = p;
897 			else
898 				goto parsedone;
899 			break;
900 		default:
901 			abort();
902 		}
903 		*p++ = c;
904 		c = WEOF;
905 	}
906 
907 parsedone:
908 	if (c != WEOF)
909 		ungetwc(c, fp);
910 	while (commit < --p)
911 		ungetwc(*p, fp);
912 	*++commit = '\0';
913 	_DIAGASSERT(__type_fit(int, commit - buf));
914 	return (int)(commit - buf);
915 }
916 #endif
917