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