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