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