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