xref: /freebsd/lib/libc/stdio/vfwprintf.c (revision aed23bc4ceaab17b4717b9f4dab300cd372f801b)
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 #include <sys/cdefs.h>
38 #if 0
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid[] = "@(#)vfprintf.c	8.1 (Berkeley) 6/4/93";
41 #endif /* LIBC_SCCS and not lint */
42 __FBSDID("FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.45 2002/09/06 11:23:55 tjr Exp ");
43 #endif
44 __FBSDID("$FreeBSD$");
45 
46 /*
47  * Actual wprintf innards.
48  *
49  * Avoid making gratuitous changes to this source file; it should be kept
50  * as close as possible to vfprintf.c for ease of maintenance.
51  */
52 
53 #include "namespace.h"
54 #include <sys/types.h>
55 
56 #include <ctype.h>
57 #include <limits.h>
58 #include <locale.h>
59 #include <stdarg.h>
60 #include <stddef.h>
61 #include <stdint.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <wchar.h>
66 #include <wctype.h>
67 #include "un-namespace.h"
68 
69 #include "libc_private.h"
70 #include "local.h"
71 #include "fvwrite.h"
72 
73 /* Define FLOATING_POINT to get floating point. */
74 #define	FLOATING_POINT
75 
76 union arg {
77 	int	intarg;
78 	u_int	uintarg;
79 	long	longarg;
80 	u_long	ulongarg;
81 	long long longlongarg;
82 	unsigned long long ulonglongarg;
83 	ptrdiff_t ptrdiffarg;
84 	size_t	sizearg;
85 	intmax_t intmaxarg;
86 	uintmax_t uintmaxarg;
87 	void	*pvoidarg;
88 	char	*pchararg;
89 	signed char *pschararg;
90 	short	*pshortarg;
91 	int	*pintarg;
92 	long	*plongarg;
93 	long long *plonglongarg;
94 	ptrdiff_t *pptrdiffarg;
95 	size_t	*psizearg;
96 	intmax_t *pintmaxarg;
97 #ifdef FLOATING_POINT
98 	double	doublearg;
99 	long double longdoublearg;
100 #endif
101 	wint_t	wintarg;
102 	wchar_t	*pwchararg;
103 };
104 
105 /*
106  * Type ids for argument type table.
107  */
108 enum typeid {
109 	T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
110 	T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
111 	T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
112 	T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
113 	T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
114 };
115 
116 static int	__sbprintf(FILE *, const wchar_t *, va_list);
117 static wchar_t	*__ujtoa(uintmax_t, wchar_t *, int, int, wchar_t *, int, char,
118 		    const char *);
119 static wchar_t	*__ultoa(u_long, wchar_t *, int, int, wchar_t *, int, char,
120 		    const char *);
121 static wchar_t	*__mbsconv(char *, int);
122 static void	__find_arguments(const wchar_t *, va_list, union arg **);
123 static void	__grow_type_table(int, enum typeid **, int *);
124 
125 /*
126  * Helper function for `fprintf to unbuffered unix file': creates a
127  * temporary buffer.  We only work on write-only files; this avoids
128  * worries about ungetc buffers and so forth.
129  */
130 static int
131 __sbprintf(FILE *fp, const wchar_t *fmt, va_list ap)
132 {
133 	int ret;
134 	FILE fake;
135 	unsigned char buf[BUFSIZ];
136 
137 	/* copy the important variables */
138 	fake._flags = fp->_flags & ~__SNBF;
139 	fake._file = fp->_file;
140 	fake._cookie = fp->_cookie;
141 	fake._write = fp->_write;
142 	fake._extra = fp->_extra;
143 
144 	/* set up the buffer */
145 	fake._bf._base = fake._p = buf;
146 	fake._bf._size = fake._w = sizeof(buf);
147 	fake._lbfsize = 0;	/* not actually used, but Just In Case */
148 
149 	/* do the work, then copy any error status */
150 	ret = __vfwprintf(&fake, fmt, ap);
151 	if (ret >= 0 && __fflush(&fake))
152 		ret = WEOF;
153 	if (fake._flags & __SERR)
154 		fp->_flags |= __SERR;
155 	return (ret);
156 }
157 
158 /*
159  * Macros for converting digits to letters and vice versa
160  */
161 #define	to_digit(c)	((c) - '0')
162 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
163 #define	to_char(n)	((n) + '0')
164 
165 /*
166  * Convert an unsigned long to ASCII for printf purposes, returning
167  * a pointer to the first character of the string representation.
168  * Octal numbers can be forced to have a leading zero; hex numbers
169  * use the given digits.
170  */
171 static wchar_t *
172 __ultoa(u_long val, wchar_t *endp, int base, int octzero, wchar_t *xdigs,
173 	int needgrp, char thousep, const char *grp)
174 {
175 	wchar_t *cp = endp;
176 	long sval;
177 	int ndig;
178 
179 	/*
180 	 * Handle the three cases separately, in the hope of getting
181 	 * better/faster code.
182 	 */
183 	switch (base) {
184 	case 10:
185 		if (val < 10) {	/* many numbers are 1 digit */
186 			*--cp = to_char(val);
187 			return (cp);
188 		}
189 		ndig = 0;
190 		/*
191 		 * On many machines, unsigned arithmetic is harder than
192 		 * signed arithmetic, so we do at most one unsigned mod and
193 		 * divide; this is sufficient to reduce the range of
194 		 * the incoming value to where signed arithmetic works.
195 		 */
196 		if (val > LONG_MAX) {
197 			*--cp = to_char(val % 10);
198 			ndig++;
199 			sval = val / 10;
200 		} else
201 			sval = val;
202 		do {
203 			*--cp = to_char(sval % 10);
204 			ndig++;
205 			/*
206 			 * If (*grp == CHAR_MAX) then no more grouping
207 			 * should be performed.
208 			 */
209 			if (needgrp && ndig == *grp && *grp != CHAR_MAX
210 					&& sval > 9) {
211 				*--cp = thousep;
212 				ndig = 0;
213 				/*
214 				 * If (*(grp+1) == '\0') then we have to
215 				 * use *grp character (last grouping rule)
216 				 * for all next cases
217 				 */
218 				if (*(grp+1) != '\0')
219 					grp++;
220 			}
221 			sval /= 10;
222 		} while (sval != 0);
223 		break;
224 
225 	case 8:
226 		do {
227 			*--cp = to_char(val & 7);
228 			val >>= 3;
229 		} while (val);
230 		if (octzero && *cp != '0')
231 			*--cp = '0';
232 		break;
233 
234 	case 16:
235 		do {
236 			*--cp = xdigs[val & 15];
237 			val >>= 4;
238 		} while (val);
239 		break;
240 
241 	default:			/* oops */
242 		abort();
243 	}
244 	return (cp);
245 }
246 
247 /* Identical to __ultoa, but for intmax_t. */
248 static wchar_t *
249 __ujtoa(uintmax_t val, wchar_t *endp, int base, int octzero, wchar_t *xdigs,
250 	int needgrp, char thousep, const char *grp)
251 {
252 	wchar_t *cp = endp;
253 	intmax_t sval;
254 	int ndig;
255 
256 	/* quick test for small values; __ultoa is typically much faster */
257 	/* (perhaps instead we should run until small, then call __ultoa?) */
258 	if (val <= ULONG_MAX)
259 		return (__ultoa((u_long)val, endp, base, octzero, xdigs,
260 		    needgrp, thousep, grp));
261 	switch (base) {
262 	case 10:
263 		if (val < 10) {
264 			*--cp = to_char(val % 10);
265 			return (cp);
266 		}
267 		ndig = 0;
268 		if (val > INTMAX_MAX) {
269 			*--cp = to_char(val % 10);
270 			ndig++;
271 			sval = val / 10;
272 		} else
273 			sval = val;
274 		do {
275 			*--cp = to_char(sval % 10);
276 			ndig++;
277 			/*
278 			 * If (*grp == CHAR_MAX) then no more grouping
279 			 * should be performed.
280 			 */
281 			if (needgrp && *grp != CHAR_MAX && ndig == *grp
282 					&& sval > 9) {
283 				*--cp = thousep;
284 				ndig = 0;
285 				/*
286 				 * If (*(grp+1) == '\0') then we have to
287 				 * use *grp character (last grouping rule)
288 				 * for all next cases
289 				 */
290 				if (*(grp+1) != '\0')
291 					grp++;
292 			}
293 			sval /= 10;
294 		} while (sval != 0);
295 		break;
296 
297 	case 8:
298 		do {
299 			*--cp = to_char(val & 7);
300 			val >>= 3;
301 		} while (val);
302 		if (octzero && *cp != '0')
303 			*--cp = '0';
304 		break;
305 
306 	case 16:
307 		do {
308 			*--cp = xdigs[val & 15];
309 			val >>= 4;
310 		} while (val);
311 		break;
312 
313 	default:
314 		abort();
315 	}
316 	return (cp);
317 }
318 
319 /*
320  * Convert a multibyte character string argument for the %s format to a wide
321  * string representation. ``prec'' specifies the maximum number of bytes
322  * to output. If ``prec'' is greater than or equal to zero, we can't assume
323  * that the multibyte char. string ends in a null character.
324  */
325 static wchar_t *
326 __mbsconv(char *mbsarg, int prec)
327 {
328 	wchar_t *convbuf, *wcp;
329 	const char *p;
330 	size_t insize, nchars, nconv;
331 	mbstate_t mbs;
332 
333 	/*
334 	 * Supplied argument is a multibyte string; convert it to wide
335 	 * characters first.
336 	 */
337 	if (prec >= 0) {
338 		/*
339 		 * String is not guaranteed to be NUL-terminated. Find the
340 		 * number of characters to print.
341 		 */
342 		memset(&mbs, 0, sizeof(mbs));
343 		p = mbsarg;
344 		insize = nchars = 0;
345 		while (nchars != (size_t)prec) {
346 			nconv = mbrlen(p, MB_CUR_MAX, &mbs);
347 			if (nconv == 0 || nconv == (size_t)-1 ||
348 			    nconv == (size_t)-2)
349 				break;
350 			p += nconv;
351 			nchars++;
352 			insize += nconv;
353 		}
354 		if (nconv == (size_t)-1 || nconv == (size_t)-2)
355 			return (NULL);
356 	} else
357 		insize = strlen(mbsarg);
358 
359 	/*
360 	 * Allocate buffer for the result and perform the conversion,
361 	 * converting at most `size' bytes of the input multibyte string to
362 	 * wide characters for printing.
363 	 */
364 	convbuf = malloc((insize + 1) * sizeof(*convbuf));
365 	if (convbuf == NULL)
366 		return (NULL);
367 	wcp = convbuf;
368 	p = mbsarg;
369 	memset(&mbs, 0, sizeof(mbs));
370 	while (insize != 0) {
371 		nconv = mbrtowc(wcp, p, insize, &mbs);
372 		if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
373 			break;
374 		wcp++;
375 		p += nconv;
376 		insize -= nconv;
377 	}
378 	if (nconv == (size_t)-1 || nconv == (size_t)-2) {
379 		free(convbuf);
380 		return (NULL);
381 	}
382 	*wcp = L'\0';
383 
384 	return (convbuf);
385 }
386 
387 /*
388  * MT-safe version
389  */
390 int
391 vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap)
392 
393 {
394 	int ret;
395 
396 	FLOCKFILE(fp);
397 	ret = __vfwprintf(fp, fmt0, ap);
398 	FUNLOCKFILE(fp);
399 	return (ret);
400 }
401 
402 #ifdef FLOATING_POINT
403 #include <math.h>
404 #include "floatio.h"
405 
406 #define	BUF		((MAXEXP*2)+MAXFRACT+1)		/* + decimal point */
407 #define	DEFPREC		6
408 
409 static wchar_t *cvt(double, int, int, char *, int *, wchar_t, int *,
410 	    wchar_t **);
411 static int exponent(wchar_t *, int, wchar_t);
412 
413 #else /* no FLOATING_POINT */
414 
415 #define	BUF		136
416 
417 #endif /* FLOATING_POINT */
418 
419 #define STATIC_ARG_TBL_SIZE 8           /* Size of static argument table. */
420 
421 /*
422  * Flags used during conversion.
423  */
424 #define	ALT		0x001		/* alternate form */
425 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
426 #define	LADJUST		0x004		/* left adjustment */
427 #define	LONGDBL		0x008		/* long double */
428 #define	LONGINT		0x010		/* long integer */
429 #define	LLONGINT	0x020		/* long long integer */
430 #define	SHORTINT	0x040		/* short integer */
431 #define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
432 #define	FPT		0x100		/* Floating point number */
433 #define	GROUPING	0x200		/* use grouping ("'" flag) */
434 					/* C99 additional size modifiers: */
435 #define	SIZET		0x400		/* size_t */
436 #define	PTRDIFFT	0x800		/* ptrdiff_t */
437 #define	INTMAXT		0x1000		/* intmax_t */
438 #define	CHARINT		0x2000		/* print char using int format */
439 
440 /*
441  * Non-MT-safe version
442  */
443 int
444 __vfwprintf(FILE *fp, const wchar_t *fmt0, va_list ap)
445 {
446 	wchar_t *fmt;		/* format string */
447 	wchar_t ch;		/* character from fmt */
448 	int n, n2;		/* handy integer (short term usage) */
449 	wchar_t *cp;		/* handy char pointer (short term usage) */
450 	int flags;		/* flags as above */
451 	int ret;		/* return value accumulator */
452 	int width;		/* width from format (%8d), or 0 */
453 	int prec;		/* precision from format (%.3d), or -1 */
454 	wchar_t sign;		/* sign prefix (' ', '+', '-', or \0) */
455 	char thousands_sep;	/* locale specific thousands separator */
456 	const char *grouping;	/* locale specific numeric grouping rules */
457 #ifdef FLOATING_POINT
458 	char *decimal_point;	/* locale specific decimal point */
459 	char softsign;		/* temporary negative sign for floats */
460 	double _double;		/* double precision arguments %[eEfgG] */
461 	int expt;		/* integer value of exponent */
462 	int expsize;		/* character count for expstr */
463 	int ndig;		/* actual number of digits returned by cvt */
464 	wchar_t expstr[7];	/* buffer for exponent string */
465 	wchar_t *dtoaresult;	/* buffer allocated by dtoa */
466 #endif
467 	u_long	ulval;		/* integer arguments %[diouxX] */
468 	uintmax_t ujval;	/* %j, %ll, %q, %t, %z integers */
469 	int base;		/* base for [diouxX] conversion */
470 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
471 	int realsz;		/* field size expanded by dprec, sign, etc */
472 	int size;		/* size of converted field or string */
473 	int prsize;             /* max size of printed field */
474 	wchar_t *xdigs;		/* digits for [xX] conversion */
475 	wchar_t buf[BUF];	/* space for %c, %[diouxX], %[eEfFgG] */
476 	wchar_t ox[2];		/* space for 0x hex-prefix */
477 	union arg *argtable;	/* args, built due to positional arg */
478 	union arg statargtable [STATIC_ARG_TBL_SIZE];
479 	int nextarg;		/* 1-based argument index */
480 	va_list orgap;		/* original argument pointer */
481 	wchar_t *convbuf;	/* multibyte to wide conversion result */
482 
483 	/*
484 	 * Choose PADSIZE to trade efficiency vs. size.  If larger printf
485 	 * fields occur frequently, increase PADSIZE and make the initialisers
486 	 * below longer.
487 	 */
488 #define	PADSIZE	16		/* pad chunk size */
489 	static wchar_t blanks[PADSIZE] =
490 	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
491 	static wchar_t zeroes[PADSIZE] =
492 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
493 
494 	/*
495 	 * BEWARE, these `goto error' on error, PRINT uses `n2' and
496 	 * PAD uses `n'.
497 	 */
498 #define	PRINT(ptr, len)	do {			\
499 	for (n2 = 0; n2 < (len); n2++)		\
500 		__fputwc((ptr)[n2], fp);	\
501 } while (0)
502 #define	PAD(howmany, with)	do {		\
503 	if ((n = (howmany)) > 0) {		\
504 		while (n > PADSIZE) {		\
505 			PRINT(with, PADSIZE);	\
506 			n -= PADSIZE;		\
507 		}				\
508 		PRINT(with, n);			\
509 	}					\
510 } while (0)
511 
512 	/*
513 	 * Get the argument indexed by nextarg.   If the argument table is
514 	 * built, use it to get the argument.  If its not, get the next
515 	 * argument (and arguments must be gotten sequentially).
516 	 */
517 #define GETARG(type) \
518 	((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
519 	    (nextarg++, va_arg(ap, type)))
520 
521 	/*
522 	 * To extend shorts properly, we need both signed and unsigned
523 	 * argument extraction methods.
524 	 */
525 #define	SARG() \
526 	(flags&LONGINT ? GETARG(long) : \
527 	    flags&SHORTINT ? (long)(short)GETARG(int) : \
528 	    flags&CHARINT ? (long)(signed char)GETARG(int) : \
529 	    (long)GETARG(int))
530 #define	UARG() \
531 	(flags&LONGINT ? GETARG(u_long) : \
532 	    flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
533 	    flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
534 	    (u_long)GETARG(u_int))
535 #define	INTMAX_SIZE	(INTMAXT|SIZET|PTRDIFFT|LLONGINT)
536 #define SJARG() \
537 	(flags&INTMAXT ? GETARG(intmax_t) : \
538 	    flags&SIZET ? (intmax_t)GETARG(size_t) : \
539 	    flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
540 	    (intmax_t)GETARG(long long))
541 #define	UJARG() \
542 	(flags&INTMAXT ? GETARG(uintmax_t) : \
543 	    flags&SIZET ? (uintmax_t)GETARG(size_t) : \
544 	    flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
545 	    (uintmax_t)GETARG(unsigned long long))
546 
547 	/*
548 	 * Get * arguments, including the form *nn$.  Preserve the nextarg
549 	 * that the argument can be gotten once the type is determined.
550 	 */
551 #define GETASTER(val) \
552 	n2 = 0; \
553 	cp = fmt; \
554 	while (is_digit(*cp)) { \
555 		n2 = 10 * n2 + to_digit(*cp); \
556 		cp++; \
557 	} \
558 	if (*cp == '$') { \
559 		int hold = nextarg; \
560 		if (argtable == NULL) { \
561 			argtable = statargtable; \
562 			__find_arguments (fmt0, orgap, &argtable); \
563 		} \
564 		nextarg = n2; \
565 		val = GETARG (int); \
566 		nextarg = hold; \
567 		fmt = ++cp; \
568 	} else { \
569 		val = GETARG (int); \
570 	}
571 
572 
573 	thousands_sep = '\0';
574 	grouping = NULL;
575 #ifdef FLOATING_POINT
576 	dtoaresult = NULL;
577 	decimal_point = localeconv()->decimal_point;
578 #endif
579 	convbuf = NULL;
580 	/* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
581 	if (cantwrite(fp))
582 		return (EOF);
583 
584 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
585 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
586 	    fp->_file >= 0)
587 		return (__sbprintf(fp, fmt0, ap));
588 
589 	fmt = (wchar_t *)fmt0;
590 	argtable = NULL;
591 	nextarg = 1;
592 	va_copy(orgap, ap);
593 	ret = 0;
594 
595 	/*
596 	 * Scan the format for conversions (`%' character).
597 	 */
598 	for (;;) {
599 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
600 			/* void */;
601 		if ((n = fmt - cp) != 0) {
602 			if ((unsigned)ret + n > INT_MAX) {
603 				ret = EOF;
604 				goto error;
605 			}
606 			PRINT(cp, n);
607 			ret += n;
608 		}
609 		if (ch == '\0')
610 			goto done;
611 		fmt++;		/* skip over '%' */
612 
613 		flags = 0;
614 		dprec = 0;
615 		width = 0;
616 		prec = -1;
617 		sign = '\0';
618 
619 rflag:		ch = *fmt++;
620 reswitch:	switch (ch) {
621 		case ' ':
622 			/*-
623 			 * ``If the space and + flags both appear, the space
624 			 * flag will be ignored.''
625 			 *	-- ANSI X3J11
626 			 */
627 			if (!sign)
628 				sign = ' ';
629 			goto rflag;
630 		case '#':
631 			flags |= ALT;
632 			goto rflag;
633 		case '*':
634 			/*-
635 			 * ``A negative field width argument is taken as a
636 			 * - flag followed by a positive field width.''
637 			 *	-- ANSI X3J11
638 			 * They don't exclude field widths read from args.
639 			 */
640 			GETASTER (width);
641 			if (width >= 0)
642 				goto rflag;
643 			width = -width;
644 			/* FALLTHROUGH */
645 		case '-':
646 			flags |= LADJUST;
647 			goto rflag;
648 		case '+':
649 			sign = '+';
650 			goto rflag;
651 		case '\'':
652 			flags |= GROUPING;
653 			thousands_sep = *(localeconv()->thousands_sep);
654 			grouping = localeconv()->grouping;
655 			goto rflag;
656 		case '.':
657 			if ((ch = *fmt++) == '*') {
658 				GETASTER (n);
659 				prec = n < 0 ? -1 : n;
660 				goto rflag;
661 			}
662 			n = 0;
663 			while (is_digit(ch)) {
664 				n = 10 * n + to_digit(ch);
665 				ch = *fmt++;
666 			}
667 			prec = n < 0 ? -1 : n;
668 			goto reswitch;
669 		case '0':
670 			/*-
671 			 * ``Note that 0 is taken as a flag, not as the
672 			 * beginning of a field width.''
673 			 *	-- ANSI X3J11
674 			 */
675 			flags |= ZEROPAD;
676 			goto rflag;
677 		case '1': case '2': case '3': case '4':
678 		case '5': case '6': case '7': case '8': case '9':
679 			n = 0;
680 			do {
681 				n = 10 * n + to_digit(ch);
682 				ch = *fmt++;
683 			} while (is_digit(ch));
684 			if (ch == '$') {
685 				nextarg = n;
686 				if (argtable == NULL) {
687 					argtable = statargtable;
688 					__find_arguments (fmt0, orgap,
689 					    &argtable);
690 				}
691 				goto rflag;
692 			}
693 			width = n;
694 			goto reswitch;
695 #ifdef FLOATING_POINT
696 		case 'L':
697 			flags |= LONGDBL;
698 			goto rflag;
699 #endif
700 		case 'h':
701 			if (flags & SHORTINT) {
702 				flags &= ~SHORTINT;
703 				flags |= CHARINT;
704 			} else
705 				flags |= SHORTINT;
706 			goto rflag;
707 		case 'j':
708 			flags |= INTMAXT;
709 			goto rflag;
710 		case 'l':
711 			if (flags & LONGINT) {
712 				flags &= ~LONGINT;
713 				flags |= LLONGINT;
714 			} else
715 				flags |= LONGINT;
716 			goto rflag;
717 		case 'q':
718 			flags |= LLONGINT;	/* not necessarily */
719 			goto rflag;
720 		case 't':
721 			flags |= PTRDIFFT;
722 			goto rflag;
723 		case 'z':
724 			flags |= SIZET;
725 			goto rflag;
726 		case 'c':
727 			if (flags & LONGINT)
728 				*(cp = buf) = (wchar_t)GETARG(wint_t);
729 			else
730 				*(cp = buf) = (wchar_t)btowc(GETARG(int));
731 			size = 1;
732 			sign = '\0';
733 			break;
734 		case 'D':
735 			flags |= LONGINT;
736 			/*FALLTHROUGH*/
737 		case 'd':
738 		case 'i':
739 			if (flags & INTMAX_SIZE) {
740 				ujval = SJARG();
741 				if ((intmax_t)ujval < 0) {
742 					ujval = -ujval;
743 					sign = '-';
744 				}
745 			} else {
746 				ulval = SARG();
747 				if ((long)ulval < 0) {
748 					ulval = -ulval;
749 					sign = '-';
750 				}
751 			}
752 			base = 10;
753 			goto number;
754 #ifdef FLOATING_POINT
755 #ifdef HEXFLOAT
756 		case 'a':
757 		case 'A':
758 #endif
759 		case 'e':
760 		case 'E':
761 			/*-
762 			 * Grouping apply to %i, %d, %u, %f, %F, %g, %G
763 			 * conversion specifiers only. For other conversions
764 			 * behavior is undefined.
765 			 *	-- POSIX
766 			 */
767 			flags &= ~GROUPING;
768 			/*FALLTHROUGH*/
769 		case 'f':
770 		case 'F':
771 			goto fp_begin;
772 		case 'g':
773 		case 'G':
774 			if (prec == 0)
775 				prec = 1;
776 fp_begin:		if (prec == -1)
777 				prec = DEFPREC;
778 			if (flags & LONGDBL)
779 				/* XXX this loses precision. */
780 				_double = (double)GETARG(long double);
781 			else
782 				_double = GETARG(double);
783 			/* do this before tricky precision changes */
784 			if (isinf(_double)) {
785 				if (_double < 0)
786 					sign = '-';
787 				if (iswupper(ch))
788 					cp = L"INF";
789 				else
790 					cp = L"inf";
791 				size = 3;
792 				break;
793 			}
794 			if (isnan(_double)) {
795 				if (iswupper(ch))
796 					cp = L"NAN";
797 				else
798 					cp = L"nan";
799 				size = 3;
800 				break;
801 			}
802 			flags |= FPT;
803 			if (dtoaresult != NULL) {
804 				free(dtoaresult);
805 				dtoaresult = NULL;
806 			}
807 			cp = cvt(_double, prec, flags, &softsign,
808 				&expt, ch, &ndig, &dtoaresult);
809 			if (ch == 'g' || ch == 'G') {
810 				if (expt <= -4 || expt > prec)
811 					ch = (ch == 'g') ? 'e' : 'E';
812 				else
813 					ch = 'g';
814 			}
815 			if (ch == 'e' || ch == 'E') {
816 				--expt;
817 				expsize = exponent(expstr, expt, ch);
818 				size = expsize + ndig;
819 				if (ndig > 1 || flags & ALT)
820 					++size;
821 			} else if (ch == 'f' || ch == 'F') {
822 				if (expt > 0) {
823 					size = expt;
824 					if (prec || flags & ALT)
825 						size += prec + 1;
826 				} else	/* "0.X" */
827 					size = prec + 2;
828 			} else if (expt >= ndig) {	/* fixed g fmt */
829 				size = expt;
830 				if (flags & ALT)
831 					++size;
832 			} else
833 				size = ndig + (expt > 0 ?
834 					1 : 2 - expt);
835 
836 			if (softsign)
837 				sign = '-';
838 			break;
839 #endif /* FLOATING_POINT */
840 		case 'n':
841 			/*
842 			 * Assignment-like behavior is specified if the
843 			 * value overflows or is otherwise unrepresentable.
844 			 * C99 says to use `signed char' for %hhn conversions.
845 			 */
846 			if (flags & LLONGINT)
847 				*GETARG(long long *) = ret;
848 			else if (flags & SIZET)
849 				*GETARG(ssize_t *) = (ssize_t)ret;
850 			else if (flags & PTRDIFFT)
851 				*GETARG(ptrdiff_t *) = ret;
852 			else if (flags & INTMAXT)
853 				*GETARG(intmax_t *) = ret;
854 			else if (flags & LONGINT)
855 				*GETARG(long *) = ret;
856 			else if (flags & SHORTINT)
857 				*GETARG(short *) = ret;
858 			else if (flags & CHARINT)
859 				*GETARG(signed char *) = ret;
860 			else
861 				*GETARG(int *) = ret;
862 			continue;	/* no output */
863 		case 'O':
864 			flags |= LONGINT;
865 			/*FALLTHROUGH*/
866 		case 'o':
867 			if (flags & INTMAX_SIZE)
868 				ujval = UJARG();
869 			else
870 				ulval = UARG();
871 			base = 8;
872 			goto nosign;
873 		case 'p':
874 			/*-
875 			 * ``The argument shall be a pointer to void.  The
876 			 * value of the pointer is converted to a sequence
877 			 * of printable characters, in an implementation-
878 			 * defined manner.''
879 			 *	-- ANSI X3J11
880 			 */
881 			ujval = (uintmax_t)(uintptr_t)GETARG(void *);
882 			base = 16;
883 			xdigs = L"0123456789abcdef";
884 			flags = flags | INTMAXT | HEXPREFIX;
885 			ch = 'x';
886 			goto nosign;
887 		case 's':
888 			if (flags & LONGINT) {
889 				if ((cp = GETARG(wchar_t *)) == NULL)
890 					cp = L"(null)";
891 			} else {
892 				char *mbp;
893 
894 				if (convbuf != NULL)
895 					free(convbuf);
896 				if ((mbp = GETARG(char *)) == NULL)
897 					cp = L"(null)";
898 				else {
899 					convbuf = __mbsconv(mbp, prec);
900 					if (convbuf == NULL)
901 						goto error;
902 					cp = convbuf;
903 				}
904 			}
905 
906 			if (prec >= 0) {
907 				/*
908 				 * can't use wcslen; can only look for the
909 				 * NUL in the first `prec' characters, and
910 				 * wcslen() will go further.
911 				 */
912 				wchar_t *p = wmemchr(cp, 0, (size_t)prec);
913 
914 				if (p != NULL) {
915 					size = p - cp;
916 					if (size > prec)
917 						size = prec;
918 				} else
919 					size = prec;
920 			} else
921 				size = wcslen(cp);
922 			sign = '\0';
923 			break;
924 		case 'U':
925 			flags |= LONGINT;
926 			/*FALLTHROUGH*/
927 		case 'u':
928 			if (flags & INTMAX_SIZE)
929 				ujval = UJARG();
930 			else
931 				ulval = UARG();
932 			base = 10;
933 			goto nosign;
934 		case 'X':
935 			xdigs = L"0123456789ABCDEF";
936 			goto hex;
937 		case 'x':
938 			xdigs = L"0123456789abcdef";
939 hex:
940 			if (flags & INTMAX_SIZE)
941 				ujval = UJARG();
942 			else
943 				ulval = UARG();
944 			base = 16;
945 			/* leading 0x/X only if non-zero */
946 			if (flags & ALT &&
947 			    (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
948 				flags |= HEXPREFIX;
949 
950 			flags &= ~GROUPING;
951 			/* unsigned conversions */
952 nosign:			sign = '\0';
953 			/*-
954 			 * ``... diouXx conversions ... if a precision is
955 			 * specified, the 0 flag will be ignored.''
956 			 *	-- ANSI X3J11
957 			 */
958 number:			if ((dprec = prec) >= 0)
959 				flags &= ~ZEROPAD;
960 
961 			/*-
962 			 * ``The result of converting a zero value with an
963 			 * explicit precision of zero is no characters.''
964 			 *	-- ANSI X3J11
965 			 */
966 			cp = buf + BUF;
967 			if (flags & INTMAX_SIZE) {
968 				if (ujval != 0 || prec != 0)
969 					cp = __ujtoa(ujval, cp, base,
970 					    flags & ALT, xdigs,
971 					    flags & GROUPING, thousands_sep,
972 					    grouping);
973 			} else {
974 				if (ulval != 0 || prec != 0)
975 					cp = __ultoa(ulval, cp, base,
976 					    flags & ALT, xdigs,
977 					    flags & GROUPING, thousands_sep,
978 					    grouping);
979 			}
980 			size = buf + BUF - cp;
981 			break;
982 		default:	/* "%?" prints ?, unless ? is NUL */
983 			if (ch == '\0')
984 				goto done;
985 			/* pretend it was %c with argument ch */
986 			cp = buf;
987 			*cp = ch;
988 			size = 1;
989 			sign = '\0';
990 			break;
991 		}
992 
993 		/*
994 		 * All reasonable formats wind up here.  At this point, `cp'
995 		 * points to a string which (if not flags&LADJUST) should be
996 		 * padded out to `width' places.  If flags&ZEROPAD, it should
997 		 * first be prefixed by any sign or other prefix; otherwise,
998 		 * it should be blank padded before the prefix is emitted.
999 		 * After any left-hand padding and prefixing, emit zeroes
1000 		 * required by a decimal [diouxX] precision, then print the
1001 		 * string proper, then emit zeroes required by any leftover
1002 		 * floating precision; finally, if LADJUST, pad with blanks.
1003 		 *
1004 		 * Compute actual size, so we know how much to pad.
1005 		 * size excludes decimal prec; realsz includes it.
1006 		 */
1007 		realsz = dprec > size ? dprec : size;
1008 		if (sign)
1009 			realsz++;
1010 		else if (flags & HEXPREFIX)
1011 			realsz += 2;
1012 
1013 		prsize = width > realsz ? width : realsz;
1014 		if ((unsigned)ret + prsize > INT_MAX) {
1015 			ret = EOF;
1016 			goto error;
1017 		}
1018 
1019 		/* right-adjusting blank padding */
1020 		if ((flags & (LADJUST|ZEROPAD)) == 0)
1021 			PAD(width - realsz, blanks);
1022 
1023 		/* prefix */
1024 		if (sign) {
1025 			PRINT(&sign, 1);
1026 		} else if (flags & HEXPREFIX) {
1027 			ox[0] = '0';
1028 			ox[1] = ch;
1029 			PRINT(ox, 2);
1030 		}
1031 
1032 		/* right-adjusting zero padding */
1033 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1034 			PAD(width - realsz, zeroes);
1035 
1036 		/* leading zeroes from decimal precision */
1037 		PAD(dprec - size, zeroes);
1038 
1039 		/* the string or number proper */
1040 #ifdef FLOATING_POINT
1041 		if ((flags & FPT) == 0) {
1042 			PRINT(cp, size);
1043 		} else {	/* glue together f_p fragments */
1044 			if (ch >= 'f') {	/* 'f' or 'g' */
1045 				if (_double == 0) {
1046 					/* kludge for __dtoa irregularity */
1047 					PRINT(L"0", 1);
1048 					if (expt < ndig || (flags & ALT) != 0) {
1049 						PRINT(decimal_point, 1);
1050 						PAD(ndig - 1, zeroes);
1051 					}
1052 				} else if (expt <= 0) {
1053 					PRINT(L"0", 1);
1054 					PRINT(decimal_point, 1);
1055 					PAD(-expt, zeroes);
1056 					PRINT(cp, ndig);
1057 				} else if (expt >= ndig) {
1058 					PRINT(cp, ndig);
1059 					PAD(expt - ndig, zeroes);
1060 					if (flags & ALT)
1061 						PRINT(decimal_point, 1);
1062 				} else {
1063 					PRINT(cp, expt);
1064 					cp += expt;
1065 					PRINT(decimal_point, 1);
1066 					PRINT(cp, ndig-expt);
1067 				}
1068 			} else {	/* 'e' or 'E' */
1069 				if (ndig > 1 || flags & ALT) {
1070 					ox[0] = *cp++;
1071 					ox[1] = *decimal_point;
1072 					PRINT(ox, 2);
1073 					if (_double) {
1074 						PRINT(cp, ndig-1);
1075 					} else	/* 0.[0..] */
1076 						/* __dtoa irregularity */
1077 						PAD(ndig - 1, zeroes);
1078 				} else	/* XeYYY */
1079 					PRINT(cp, 1);
1080 				PRINT(expstr, expsize);
1081 			}
1082 		}
1083 #else
1084 		PRINT(cp, size);
1085 #endif
1086 		/* left-adjusting padding (always blank) */
1087 		if (flags & LADJUST)
1088 			PAD(width - realsz, blanks);
1089 
1090 		/* finally, adjust ret */
1091 		ret += prsize;
1092 	}
1093 done:
1094 error:
1095 #ifdef FLOATING_POINT
1096 	if (dtoaresult != NULL)
1097 		free(dtoaresult);
1098 #endif
1099 	if (convbuf != NULL)
1100 		free(convbuf);
1101 	if (__sferror(fp))
1102 		ret = EOF;
1103 	if ((argtable != NULL) && (argtable != statargtable))
1104 		free (argtable);
1105 	return (ret);
1106 	/* NOTREACHED */
1107 }
1108 
1109 /*
1110  * Find all arguments when a positional parameter is encountered.  Returns a
1111  * table, indexed by argument number, of pointers to each arguments.  The
1112  * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1113  * It will be replaces with a malloc-ed one if it overflows.
1114  */
1115 static void
1116 __find_arguments (const wchar_t *fmt0, va_list ap, union arg **argtable)
1117 {
1118 	wchar_t *fmt;		/* format string */
1119 	wchar_t ch;		/* character from fmt */
1120 	int n, n2;		/* handy integer (short term usage) */
1121 	wchar_t *cp;		/* handy char pointer (short term usage) */
1122 	int flags;		/* flags as above */
1123 	int width;		/* width from format (%8d), or 0 */
1124 	enum typeid *typetable; /* table of types */
1125 	enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1126 	int tablesize;		/* current size of type table */
1127 	int tablemax;		/* largest used index in table */
1128 	int nextarg;		/* 1-based argument index */
1129 
1130 	/*
1131 	 * Add an argument type to the table, expanding if necessary.
1132 	 */
1133 #define ADDTYPE(type) \
1134 	((nextarg >= tablesize) ? \
1135 		__grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1136 	(nextarg > tablemax) ? tablemax = nextarg : 0, \
1137 	typetable[nextarg++] = type)
1138 
1139 #define	ADDSARG() \
1140 	((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1141 		((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1142 		((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1143 		((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1144 		((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1145 
1146 #define	ADDUARG() \
1147 	((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1148 		((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1149 		((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1150 		((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1151 		((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1152 
1153 	/*
1154 	 * Add * arguments to the type array.
1155 	 */
1156 #define ADDASTER() \
1157 	n2 = 0; \
1158 	cp = fmt; \
1159 	while (is_digit(*cp)) { \
1160 		n2 = 10 * n2 + to_digit(*cp); \
1161 		cp++; \
1162 	} \
1163 	if (*cp == '$') { \
1164 		int hold = nextarg; \
1165 		nextarg = n2; \
1166 		ADDTYPE (T_INT); \
1167 		nextarg = hold; \
1168 		fmt = ++cp; \
1169 	} else { \
1170 		ADDTYPE (T_INT); \
1171 	}
1172 	fmt = (wchar_t *)fmt0;
1173 	typetable = stattypetable;
1174 	tablesize = STATIC_ARG_TBL_SIZE;
1175 	tablemax = 0;
1176 	nextarg = 1;
1177 	memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1178 
1179 	/*
1180 	 * Scan the format for conversions (`%' character).
1181 	 */
1182 	for (;;) {
1183 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1184 			/* void */;
1185 		if (ch == '\0')
1186 			goto done;
1187 		fmt++;		/* skip over '%' */
1188 
1189 		flags = 0;
1190 		width = 0;
1191 
1192 rflag:		ch = *fmt++;
1193 reswitch:	switch (ch) {
1194 		case ' ':
1195 		case '#':
1196 			goto rflag;
1197 		case '*':
1198 			ADDASTER ();
1199 			goto rflag;
1200 		case '-':
1201 		case '+':
1202 		case '\'':
1203 			goto rflag;
1204 		case '.':
1205 			if ((ch = *fmt++) == '*') {
1206 				ADDASTER ();
1207 				goto rflag;
1208 			}
1209 			while (is_digit(ch)) {
1210 				ch = *fmt++;
1211 			}
1212 			goto reswitch;
1213 		case '0':
1214 			goto rflag;
1215 		case '1': case '2': case '3': case '4':
1216 		case '5': case '6': case '7': case '8': case '9':
1217 			n = 0;
1218 			do {
1219 				n = 10 * n + to_digit(ch);
1220 				ch = *fmt++;
1221 			} while (is_digit(ch));
1222 			if (ch == '$') {
1223 				nextarg = n;
1224 				goto rflag;
1225 			}
1226 			width = n;
1227 			goto reswitch;
1228 #ifdef FLOATING_POINT
1229 		case 'L':
1230 			flags |= LONGDBL;
1231 			goto rflag;
1232 #endif
1233 		case 'h':
1234 			if (flags & SHORTINT) {
1235 				flags &= ~SHORTINT;
1236 				flags |= CHARINT;
1237 			} else
1238 				flags |= SHORTINT;
1239 			goto rflag;
1240 		case 'j':
1241 			flags |= INTMAXT;
1242 			goto rflag;
1243 		case 'l':
1244 			if (flags & LONGINT) {
1245 				flags &= ~LONGINT;
1246 				flags |= LLONGINT;
1247 			} else
1248 				flags |= LONGINT;
1249 			goto rflag;
1250 		case 'q':
1251 			flags |= LLONGINT;	/* not necessarily */
1252 			goto rflag;
1253 		case 't':
1254 			flags |= PTRDIFFT;
1255 			goto rflag;
1256 		case 'z':
1257 			flags |= SIZET;
1258 			goto rflag;
1259 		case 'c':
1260 			if (flags & LONGINT)
1261 				ADDTYPE(T_WINT);
1262 			else
1263 				ADDTYPE(T_INT);
1264 			break;
1265 		case 'D':
1266 			flags |= LONGINT;
1267 			/*FALLTHROUGH*/
1268 		case 'd':
1269 		case 'i':
1270 			ADDSARG();
1271 			break;
1272 #ifdef FLOATING_POINT
1273 #ifdef HEXFLOAT
1274 		case 'a':
1275 		case 'A':
1276 #endif
1277 		case 'e':
1278 		case 'E':
1279 		case 'f':
1280 		case 'g':
1281 		case 'G':
1282 			if (flags & LONGDBL)
1283 				ADDTYPE(T_LONG_DOUBLE);
1284 			else
1285 				ADDTYPE(T_DOUBLE);
1286 			break;
1287 #endif /* FLOATING_POINT */
1288 		case 'n':
1289 			if (flags & INTMAXT)
1290 				ADDTYPE(TP_INTMAXT);
1291 			else if (flags & PTRDIFFT)
1292 				ADDTYPE(TP_PTRDIFFT);
1293 			else if (flags & SIZET)
1294 				ADDTYPE(TP_SIZET);
1295 			else if (flags & LLONGINT)
1296 				ADDTYPE(TP_LLONG);
1297 			else if (flags & LONGINT)
1298 				ADDTYPE(TP_LONG);
1299 			else if (flags & SHORTINT)
1300 				ADDTYPE(TP_SHORT);
1301 			else if (flags & CHARINT)
1302 				ADDTYPE(TP_SCHAR);
1303 			else
1304 				ADDTYPE(TP_INT);
1305 			continue;	/* no output */
1306 		case 'O':
1307 			flags |= LONGINT;
1308 			/*FALLTHROUGH*/
1309 		case 'o':
1310 			ADDUARG();
1311 			break;
1312 		case 'p':
1313 			ADDTYPE(TP_VOID);
1314 			break;
1315 		case 's':
1316 			if (flags & LONGINT)
1317 				ADDTYPE(TP_WCHAR);
1318 			else
1319 				ADDTYPE(TP_CHAR);
1320 			break;
1321 		case 'U':
1322 			flags |= LONGINT;
1323 			/*FALLTHROUGH*/
1324 		case 'u':
1325 		case 'X':
1326 		case 'x':
1327 			ADDUARG();
1328 			break;
1329 		default:	/* "%?" prints ?, unless ? is NUL */
1330 			if (ch == '\0')
1331 				goto done;
1332 			break;
1333 		}
1334 	}
1335 done:
1336 	/*
1337 	 * Build the argument table.
1338 	 */
1339 	if (tablemax >= STATIC_ARG_TBL_SIZE) {
1340 		*argtable = (union arg *)
1341 		    malloc (sizeof (union arg) * (tablemax + 1));
1342 	}
1343 
1344 	(*argtable) [0].intarg = 0;
1345 	for (n = 1; n <= tablemax; n++) {
1346 		switch (typetable [n]) {
1347 		    case T_UNUSED: /* whoops! */
1348 			(*argtable) [n].intarg = va_arg (ap, int);
1349 			break;
1350 		    case TP_SCHAR:
1351 			(*argtable) [n].pschararg = va_arg (ap, signed char *);
1352 			break;
1353 		    case TP_SHORT:
1354 			(*argtable) [n].pshortarg = va_arg (ap, short *);
1355 			break;
1356 		    case T_INT:
1357 			(*argtable) [n].intarg = va_arg (ap, int);
1358 			break;
1359 		    case T_U_INT:
1360 			(*argtable) [n].uintarg = va_arg (ap, unsigned int);
1361 			break;
1362 		    case TP_INT:
1363 			(*argtable) [n].pintarg = va_arg (ap, int *);
1364 			break;
1365 		    case T_LONG:
1366 			(*argtable) [n].longarg = va_arg (ap, long);
1367 			break;
1368 		    case T_U_LONG:
1369 			(*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1370 			break;
1371 		    case TP_LONG:
1372 			(*argtable) [n].plongarg = va_arg (ap, long *);
1373 			break;
1374 		    case T_LLONG:
1375 			(*argtable) [n].longlongarg = va_arg (ap, long long);
1376 			break;
1377 		    case T_U_LLONG:
1378 			(*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1379 			break;
1380 		    case TP_LLONG:
1381 			(*argtable) [n].plonglongarg = va_arg (ap, long long *);
1382 			break;
1383 		    case T_PTRDIFFT:
1384 			(*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1385 			break;
1386 		    case TP_PTRDIFFT:
1387 			(*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1388 			break;
1389 		    case T_SIZET:
1390 			(*argtable) [n].sizearg = va_arg (ap, size_t);
1391 			break;
1392 		    case TP_SIZET:
1393 			(*argtable) [n].psizearg = va_arg (ap, ssize_t *);
1394 			break;
1395 		    case T_INTMAXT:
1396 			(*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1397 			break;
1398 		    case T_UINTMAXT:
1399 			(*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1400 			break;
1401 		    case TP_INTMAXT:
1402 			(*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1403 			break;
1404 #ifdef FLOATING_POINT
1405 		    case T_DOUBLE:
1406 			(*argtable) [n].doublearg = va_arg (ap, double);
1407 			break;
1408 		    case T_LONG_DOUBLE:
1409 			(*argtable) [n].longdoublearg = va_arg (ap, long double);
1410 			break;
1411 #endif
1412 		    case TP_CHAR:
1413 			(*argtable) [n].pchararg = va_arg (ap, char *);
1414 			break;
1415 		    case TP_VOID:
1416 			(*argtable) [n].pvoidarg = va_arg (ap, void *);
1417 			break;
1418 		    case T_WINT:
1419 			(*argtable) [n].wintarg = va_arg (ap, wint_t);
1420 			break;
1421 		    case TP_WCHAR:
1422 			(*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
1423 			break;
1424 		}
1425 	}
1426 
1427 	if ((typetable != NULL) && (typetable != stattypetable))
1428 		free (typetable);
1429 }
1430 
1431 /*
1432  * Increase the size of the type table.
1433  */
1434 static void
1435 __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize)
1436 {
1437 	enum typeid *const oldtable = *typetable;
1438 	const int oldsize = *tablesize;
1439 	enum typeid *newtable;
1440 	int newsize = oldsize * 2;
1441 
1442 	if (newsize < nextarg + 1)
1443 		newsize = nextarg + 1;
1444 	if (oldsize == STATIC_ARG_TBL_SIZE) {
1445 		if ((newtable = malloc(newsize)) == NULL)
1446 			abort();			/* XXX handle better */
1447 		bcopy(oldtable, newtable, oldsize);
1448 	} else {
1449 		if ((newtable = reallocf(oldtable, newsize)) == NULL)
1450 			abort();			/* XXX handle better */
1451 	}
1452 	memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
1453 
1454 	*typetable = newtable;
1455 	*tablesize = newsize;
1456 }
1457 
1458 
1459 #ifdef FLOATING_POINT
1460 
1461 extern char *__dtoa(double, int, int, int *, int *, char **, char **);
1462 
1463 static wchar_t *
1464 cvt(double value, int ndigits, int flags, char *sign, int *decpt,
1465     wchar_t ch, int *length, wchar_t **dtoaresultp)
1466 {
1467 	int i, mode, dsgn;
1468 	wchar_t *digits, *bp, *result, *rve;
1469 	char *tresult, *trve;
1470 
1471 	if (ch == 'f')
1472 		mode = 3;		/* ndigits after the decimal point */
1473 	else {
1474 		/*
1475 		 * To obtain ndigits after the decimal point for the 'e'
1476 		 * and 'E' formats, round to ndigits + 1 significant
1477 		 * figures.
1478 		 */
1479 		if (ch == 'e' || ch == 'E')
1480 			ndigits++;
1481 		mode = 2;		/* ndigits significant digits */
1482 	}
1483 	__dtoa(value, mode, ndigits, decpt, &dsgn, &trve, &tresult);
1484 	if ((result = malloc((trve - tresult + 1) * sizeof(*result))) == NULL)
1485 		abort();		/* XXX handle better */
1486 	for (i = 0; i < trve - tresult + 1; i++)
1487 		result[i] = (wchar_t)(unsigned char)tresult[i];
1488 	rve = result + (trve - tresult);
1489 	free(tresult);
1490 	*dtoaresultp = result;
1491 	digits = result;
1492 	*sign = dsgn != 0;
1493 	if ((ch != 'g' && ch != 'G') || flags & ALT) {
1494 		/* print trailing zeros */
1495 		bp = digits + ndigits;
1496 		if (ch == 'f') {
1497 			if (*digits == '0' && value)
1498 				*decpt = -ndigits + 1;
1499 			bp += *decpt;
1500 		}
1501 		if (value == 0)	/* kludge for __dtoa irregularity */
1502 			rve = bp;
1503 		while (rve < bp)
1504 			*rve++ = '0';
1505 	}
1506 	*length = rve - digits;
1507 	return (digits);
1508 }
1509 
1510 static int
1511 exponent(wchar_t *p0, int exp, wchar_t fmtch)
1512 {
1513 	wchar_t *p, *t;
1514 	wchar_t expbuf[MAXEXP];
1515 
1516 	p = p0;
1517 	*p++ = fmtch;
1518 	if (exp < 0) {
1519 		exp = -exp;
1520 		*p++ = '-';
1521 	}
1522 	else
1523 		*p++ = '+';
1524 	t = expbuf + MAXEXP;
1525 	if (exp > 9) {
1526 		do {
1527 			*--t = to_char(exp % 10);
1528 		} while ((exp /= 10) > 9);
1529 		*--t = to_char(exp);
1530 		for (; t < expbuf + MAXEXP; *p++ = *t++);
1531 	}
1532 	else {
1533 		*p++ = '0';
1534 		*p++ = to_char(exp);
1535 	}
1536 	return (p - p0);
1537 }
1538 #endif /* FLOATING_POINT */
1539