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