2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)vfprintf.c 8.1 (Berkeley) 6/4/93
37 * $FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.34 2001/12/13 19:45:41 phantom Exp $
38 * $DragonFly: src/lib/libc/stdio/vfprintf.c,v 1.9 2005/05/09 12:43:40 davidxu Exp $
42 * Actual printf innards.
44 * This code is large and complicated...
47 #include "namespace.h"
48 #include <sys/types.h>
60 #include "un-namespace.h"
62 #include "libc_private.h"
66 /* Define FLOATING_POINT to get floating point. */
67 #define FLOATING_POINT
69 /* Borrowed from sys/systm.h, which is _KERNEL-only: */
70 #define CTASSERT(x) _CTASSERT(x, __LINE__)
71 #define _CTASSERT(x, y) __CTASSERT(x, y)
72 #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
74 /* This code assumes that a quad_t can fit in a long long: */
75 CTASSERT(sizeof(quad_t) == sizeof(long long));
81 unsigned long ulongarg;
82 long long longlongarg;
83 unsigned long long ulonglongarg;
90 signed char *pschararg;
94 long long *plonglongarg;
95 ptrdiff_t *pptrdiffarg;
100 long double longdoublearg;
105 * Type ids for argument type table.
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
115 static int __sprint(FILE *, struct __suio *);
116 static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0);
117 static char * __ujtoa(uintmax_t, char *, int, int, char *, int,
119 static char * __ultoa(u_long, char *, int, int, char *, int,
121 static char * __uqtoa(u_quad_t, char *, int, int, char *);
122 static void __find_arguments(const char *, va_list, union arg **);
123 static void __grow_type_table(int, enum typeid **, int *);
126 * Flush out all the vectors defined by the given uio,
127 * then reset it so that it can be reused.
130 __sprint(FILE *fp, struct __suio *uio)
134 if (uio->uio_resid == 0) {
138 err = __sfvwrite(fp, uio);
145 * Helper function for `fprintf to unbuffered unix file': creates a
146 * temporary buffer. We only work on write-only files; this avoids
147 * worries about ungetc buffers and so forth.
150 __sbprintf(FILE *fp, const char *fmt, va_list ap)
154 unsigned char buf[BUFSIZ];
156 /* copy the important variables */
157 fake._flags = fp->_flags & ~__SNBF;
158 fake._file = fp->_file;
159 fake._cookie = fp->_cookie;
160 fake._write = fp->_write;
161 fake._extra = fp->_extra;
163 /* set up the buffer */
164 fake._bf._base = fake._p = buf;
165 fake._bf._size = fake._w = sizeof(buf);
166 fake._lbfsize = 0; /* not actually used, but Just In Case */
168 /* do the work, then copy any error status */
169 ret = __vfprintf(&fake, fmt, ap);
170 if (ret >= 0 && __fflush(&fake))
172 if (fake._flags & __SERR)
173 fp->_flags |= __SERR;
178 * Macros for converting digits to letters and vice versa
180 #define to_digit(c) ((c) - '0')
181 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
182 #define to_char(n) ((n) + '0')
185 * Convert an unsigned long to ASCII for printf purposes, returning
186 * a pointer to the first character of the string representation.
187 * Octal numbers can be forced to have a leading zero; hex numbers
188 * use the given digits.
191 __ultoa(u_long val, char *endp, int base, int octzero, char *xdigs,
192 int needgrp, char thousep, const char *grp)
199 * Handle the three cases separately, in the hope of getting
200 * better/faster code.
204 if (val < 10) { /* many numbers are 1 digit */
205 *--cp = to_char(val);
210 * On many machines, unsigned arithmetic is harder than
211 * signed arithmetic, so we do at most one unsigned mod and
212 * divide; this is sufficient to reduce the range of
213 * the incoming value to where signed arithmetic works.
215 if (val > LONG_MAX) {
216 *--cp = to_char(val % 10);
222 *--cp = to_char(sval % 10);
225 * If (*grp == CHAR_MAX) then no more grouping
226 * should be performed.
228 if (needgrp && ndig == *grp && *grp != CHAR_MAX &&
233 * If (*(grp+1) == '\0') then we have to
234 * use *grp character (last grouping rule)
237 if (*(grp + 1) != '\0')
246 *--cp = to_char(val & 7);
249 if (octzero && *cp != '0')
255 *--cp = xdigs[val & 15];
266 /* Identical to __ultoa, but for intmax_t. */
268 __ujtoa(u_quad_t val, char *endp, int base, int octzero, char *xdigs,
269 int needgrp, char thousep, const char *grp)
275 /* quick test for small values; __ultoa is typically much faster */
276 /* (perhaps instead we should run until small, then call __ultoa?) */
277 if (val <= ULONG_MAX)
278 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
279 needgrp, thousep, grp));
283 *--cp = to_char(val % 10);
287 if (val > INTMAX_MAX) {
288 *--cp = to_char(val % 10);
294 *--cp = to_char(sval % 10);
297 * If (*grp == CHAR_MAX) then no more grouping
298 * should be performed.
300 if (needgrp && *grp != CHAR_MAX && ndig == *grp &&
305 * If (*(grp+1) == '\0') then we have to
306 * use *grp character (last grouping rule)
309 if (*(grp + 1) != '\0')
318 *--cp = to_char(val & 7);
321 if (octzero && *cp != '0')
327 *--cp = xdigs[val & 15];
342 vfprintf(FILE *fp, const char *fmt0, va_list ap)
347 ret = __vfprintf(fp, fmt0, ap);
352 #ifdef FLOATING_POINT
356 #define BUF ((MAXEXP * 2) + MAXFRACT + 1) /* + decimal point */
359 static char *cvt (double, int, int, char *, int *, int, int *, char **);
360 static int exponent (char *, int, int);
362 #else /* no FLOATING_POINT */
366 #endif /* FLOATING_POINT */
368 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
371 * Flags used during conversion.
373 #define ALT 0x001 /* alternate form */
374 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
375 #define LADJUST 0x004 /* left adjustment */
376 #define LONGDBL 0x008 /* long double */
377 #define LONGINT 0x010 /* long integer */
378 #define LLONGINT 0x020 /* long long integer */
379 #define SHORTINT 0x040 /* short integer */
380 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
381 #define FPT 0x100 /* Floating point number */
382 #define GROUPING 0x200 /* use grouping ("'" flag) */
383 /* C99 additional size modifiers: */
384 #define SIZET 0x400 /* size_t */
385 #define PTRDIFFT 0x800 /* ptrdiff_t */
386 #define INTMAXT 0x1000 /* intmax_t */
387 #define CHARINT 0x2000 /* print char using int format */
389 * Non-MT-safe version
392 __vfprintf(FILE *fp, const char *fmt0, va_list ap)
394 char *fmt; /* format string */
395 int ch; /* character from fmt */
396 int n, n2; /* handy integer (short term usage) */
397 char *cp; /* handy char pointer (short term usage) */
398 struct __siov *iovp; /* for PRINT macro */
399 int flags; /* flags as above */
400 int ret; /* return value accumulator */
401 int width; /* width from format (%8d), or 0 */
402 int prec; /* precision from format (%.3d), or -1 */
403 char sign; /* sign prefix (' ', '+', '-', or \0) */
404 char thousands_sep; /* locale specific thousands separator */
405 const char *grouping; /* locale specific numeric grouping rules */
406 #ifdef FLOATING_POINT
407 const char *decimal_point; /* locale specific decimal point */
408 char softsign; /* temporary negative sign for floats */
409 double _double; /* double precision arguments %[eEfgG] */
410 int expt; /* integer value of exponent */
411 int expsize; /* character count for expstr */
412 int ndig; /* actual number of digits returned by cvt */
413 char expstr[7]; /* buffer for exponent string */
414 char *dtoaresult; /* buffer allocated by dtoa */
416 u_long ulval; /* integer arguments %[diouxX] */
417 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
418 int base; /* base for [diouxX] conversion */
419 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
420 int realsz; /* field size expanded by dprec, sign, etc */
421 int size; /* size of converted field or string */
422 int prsize; /* max size of printed field */
423 char *xdigs; /* digits for [xX] conversion */
425 struct __suio uio; /* output information: summary */
426 struct __siov iov[NIOV];/* ... and individual io vectors */
427 char buf[BUF]; /* space for %c, %[diouxX], %[eEfFgG] */
428 char ox[2]; /* space for 0x hex-prefix */
429 union arg *argtable; /* args, built due to positional arg */
430 union arg statargtable[STATIC_ARG_TBL_SIZE];
431 int nextarg; /* 1-based argument index */
432 va_list orgap; /* original argument pointer */
435 * Choose PADSIZE to trade efficiency vs. size. If larger printf
436 * fields occur frequently, increase PADSIZE and make the initialisers
439 #define PADSIZE 16 /* pad chunk size */
440 static char blanks[PADSIZE] =
441 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
442 static char zeroes[PADSIZE] =
443 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
446 * BEWARE, these `goto error' on error, and PAD uses `n'.
448 #define PRINT(ptr, len) { \
449 iovp->iov_base = __DECONST(void *, ptr); \
450 iovp->iov_len = (len); \
451 uio.uio_resid += (len); \
453 if (++uio.uio_iovcnt >= NIOV) { \
454 if (__sprint(fp, &uio)) \
459 #define PAD(howmany, with) { \
460 if ((n = (howmany)) > 0) { \
461 while (n > PADSIZE) { \
462 PRINT(with, PADSIZE); \
469 if (uio.uio_resid && __sprint(fp, &uio)) \
471 uio.uio_iovcnt = 0; \
476 * Get the argument indexed by nextarg. If the argument table is
477 * built, use it to get the argument. If its not, get the next
478 * argument (and arguments must be gotten sequentially).
480 #define GETARG(type) \
481 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
482 (nextarg++, va_arg(ap, type)))
485 * To extend shorts properly, we need both signed and unsigned
486 * argument extraction methods.
489 (flags&LONGINT ? GETARG(long) : \
490 flags&SHORTINT ? (long)(short)GETARG(int) : \
491 flags&CHARINT ? (long)(signed char)GETARG(int) : \
494 (flags&LONGINT ? GETARG(u_long) : \
495 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
496 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
497 (u_long)GETARG(u_int))
498 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
500 (flags&INTMAXT ? GETARG(intmax_t) : \
501 flags&SIZET ? (intmax_t)GETARG(size_t) : \
502 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
503 (intmax_t)GETARG(long long))
505 (flags&INTMAXT ? GETARG(uintmax_t) : \
506 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
507 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
508 (uintmax_t)GETARG(unsigned long long))
511 * Get * arguments, including the form *nn$. Preserve the nextarg
512 * that the argument can be gotten once the type is determined.
514 #define GETASTER(val) \
517 while (is_digit(*cp)) { \
518 n2 = 10 * n2 + to_digit(*cp); \
522 int hold = nextarg; \
523 if (argtable == NULL) { \
524 argtable = statargtable; \
525 __find_arguments (fmt0, orgap, &argtable); \
528 val = GETARG (int); \
532 val = GETARG (int); \
536 thousands_sep = '\0';
538 #ifdef FLOATING_POINT
540 decimal_point = localeconv()->decimal_point;
542 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
547 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
548 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
550 return (__sbprintf(fp, fmt0, ap));
557 uio.uio_iov = iovp = iov;
563 * Scan the format for conversions (`%' character).
566 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
568 if ((n = fmt - cp) != 0) {
569 if ((unsigned)ret + n > INT_MAX) {
578 fmt++; /* skip over '%' */
587 reswitch: switch (ch) {
590 * ``If the space and + flags both appear, the space
591 * flag will be ignored.''
602 * ``A negative field width argument is taken as a
603 * - flag followed by a positive field width.''
605 * They don't exclude field widths read from args.
620 thousands_sep = *(localeconv()->thousands_sep);
621 grouping = localeconv()->grouping;
624 if ((ch = *fmt++) == '*') {
626 prec = n < 0 ? -1 : n;
630 while (is_digit(ch)) {
631 n = 10 * n + to_digit(ch);
634 prec = n < 0 ? -1 : n;
638 * ``Note that 0 is taken as a flag, not as the
639 * beginning of a field width.''
644 case '1': case '2': case '3': case '4':
645 case '5': case '6': case '7': case '8': case '9':
648 n = 10 * n + to_digit(ch);
650 } while (is_digit(ch));
653 if (argtable == NULL) {
654 argtable = statargtable;
655 __find_arguments (fmt0, orgap,
662 #ifdef FLOATING_POINT
668 if (flags & SHORTINT) {
678 if (flags & LONGINT) {
694 *(cp = buf) = GETARG(int);
703 if (flags & INTMAX_SIZE) {
705 if ((intmax_t)ujval < 0) {
711 if ((long)ulval < 0) {
718 #ifdef FLOATING_POINT
726 * Grouping apply to %i, %d, %u, %f, %F, %g, %G
727 * conversion specifiers only. For other conversions
728 * behavior is undefined.
740 fp_begin: if (prec == -1)
743 /* XXX this loses precision. */
744 _double = (double)GETARG(long double);
746 _double = GETARG(double);
747 /* do this before tricky precision changes */
748 if (isinf(_double)) {
758 if (isnan(_double)) {
767 if (dtoaresult != NULL) {
771 cp = cvt(_double, prec, flags, &softsign,
772 &expt, ch, &ndig, &dtoaresult);
773 if (ch == 'g' || ch == 'G') {
774 if (expt <= -4 || expt > prec)
775 ch = (ch == 'g') ? 'e' : 'E';
779 if (ch == 'e' || ch == 'E') {
781 expsize = exponent(expstr, expt, ch);
782 size = expsize + ndig;
783 if (ndig > 1 || flags & ALT)
785 } else if (ch == 'f' || ch == 'F') {
788 if (prec || flags & ALT)
792 } else if (expt >= ndig) { /* fixed g fmt */
797 size = ndig + (expt > 0 ?
803 #endif /* FLOATING_POINT */
806 * Assignment-like behavior is specified if the
807 * value overflows or is otherwise unrepresentable.
808 * C99 says to use `signed char' for %hhn conversions.
810 if (flags & LLONGINT)
811 *GETARG(long long *) = ret;
812 else if (flags & SIZET)
813 *GETARG(ssize_t *) = (ssize_t)ret;
814 else if (flags & PTRDIFFT)
815 *GETARG(ptrdiff_t *) = ret;
816 else if (flags & INTMAXT)
817 *GETARG(intmax_t *) = ret;
818 else if (flags & LONGINT)
819 *GETARG(long *) = ret;
820 else if (flags & SHORTINT)
821 *GETARG(short *) = ret;
822 else if (flags & CHARINT)
823 *GETARG(signed char *) = ret;
825 *GETARG(int *) = ret;
826 continue; /* no output */
831 if (flags & INTMAX_SIZE)
839 * ``The argument shall be a pointer to void. The
840 * value of the pointer is converted to a sequence
841 * of printable characters, in an implementation-
845 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
847 xdigs = "0123456789abcdef";
848 flags = flags | INTMAXT | HEXPREFIX;
852 if ((cp = GETARG(char *)) == NULL)
856 * can't use strlen; can only look for the
857 * NUL in the first `prec' characters, and
858 * strlen() will go further.
860 char *p = memchr(cp, 0, (size_t)prec);
876 if (flags & INTMAX_SIZE)
883 xdigs = "0123456789ABCDEF";
886 xdigs = "0123456789abcdef";
888 if (flags & INTMAX_SIZE)
893 /* leading 0x/X only if non-zero */
895 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
899 /* unsigned conversions */
902 * ``... diouXx conversions ... if a precision is
903 * specified, the 0 flag will be ignored.''
906 number: if ((dprec = prec) >= 0)
910 * ``The result of converting a zero value with an
911 * explicit precision of zero is no characters.''
915 if (flags & INTMAX_SIZE) {
916 if (ujval != 0 || prec != 0)
917 cp = __ujtoa(ujval, cp, base,
919 flags & GROUPING, thousands_sep,
922 if (ulval != 0 || prec != 0)
923 cp = __ultoa(ulval, cp, base,
925 flags & GROUPING, thousands_sep,
928 size = buf + BUF - cp;
930 default: /* "%?" prints ?, unless ? is NUL */
933 /* pretend it was %c with argument ch */
942 * All reasonable formats wind up here. At this point, `cp'
943 * points to a string which (if not flags&LADJUST) should be
944 * padded out to `width' places. If flags&ZEROPAD, it should
945 * first be prefixed by any sign or other prefix; otherwise,
946 * it should be blank padded before the prefix is emitted.
947 * After any left-hand padding and prefixing, emit zeroes
948 * required by a decimal [diouxX] precision, then print the
949 * string proper, then emit zeroes required by any leftover
950 * floating precision; finally, if LADJUST, pad with blanks.
952 * Compute actual size, so we know how much to pad.
953 * size excludes decimal prec; realsz includes it.
955 realsz = dprec > size ? dprec : size;
958 else if (flags & HEXPREFIX)
961 prsize = width > realsz ? width : realsz;
962 if ((unsigned)ret + prsize > INT_MAX) {
967 /* right-adjusting blank padding */
968 if ((flags & (LADJUST|ZEROPAD)) == 0)
969 PAD(width - realsz, blanks);
974 } else if (flags & HEXPREFIX) {
980 /* right-adjusting zero padding */
981 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
982 PAD(width - realsz, zeroes);
984 /* leading zeroes from decimal precision */
985 PAD(dprec - size, zeroes);
987 /* the string or number proper */
988 #ifdef FLOATING_POINT
989 if ((flags & FPT) == 0) {
991 } else { /* glue together f_p fragments */
992 if (ch >= 'f') { /* 'f' or 'g' */
994 /* kludge for __dtoa irregularity */
996 if (expt < ndig || (flags & ALT) != 0) {
997 PRINT(decimal_point, 1);
998 PAD(ndig - 1, zeroes);
1000 } else if (expt <= 0) {
1002 PRINT(decimal_point, 1);
1005 } else if (expt >= ndig) {
1007 PAD(expt - ndig, zeroes);
1009 PRINT(decimal_point, 1);
1013 PRINT(decimal_point, 1);
1014 PRINT(cp, ndig-expt);
1016 } else { /* 'e' or 'E' */
1017 if (ndig > 1 || flags & ALT) {
1019 ox[1] = *decimal_point;
1023 } else /* 0.[0..] */
1024 /* __dtoa irregularity */
1025 PAD(ndig - 1, zeroes);
1028 PRINT(expstr, expsize);
1034 /* left-adjusting padding (always blank) */
1035 if (flags & LADJUST)
1036 PAD(width - realsz, blanks);
1038 /* finally, adjust ret */
1041 FLUSH(); /* copy out the I/O vectors */
1046 #ifdef FLOATING_POINT
1047 if (dtoaresult != NULL)
1052 if ((argtable != NULL) && (argtable != statargtable))
1059 * Find all arguments when a positional parameter is encountered. Returns a
1060 * table, indexed by argument number, of pointers to each arguments. The
1061 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1062 * It will be replaces with a malloc-ed one if it overflows.
1065 __find_arguments (const char *fmt0, va_list ap, union arg **argtable)
1067 char *fmt; /* format string */
1068 int ch; /* character from fmt */
1069 int n, n2; /* handy integer (short term usage) */
1070 char *cp; /* handy char pointer (short term usage) */
1071 int flags; /* flags as above */
1072 int width; /* width from format (%8d), or 0 */
1073 enum typeid *typetable; /* table of types */
1074 enum typeid stattypetable[STATIC_ARG_TBL_SIZE];
1075 int tablesize; /* current size of type table */
1076 int tablemax; /* largest used index in table */
1077 int nextarg; /* 1-based argument index */
1080 * Add an argument type to the table, expanding if necessary.
1082 #define ADDTYPE(type) \
1083 ((nextarg >= tablesize) ? \
1084 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1085 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1086 typetable[nextarg++] = type)
1089 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1090 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1091 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1092 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1093 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1096 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1097 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1098 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1099 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1100 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1103 * Add * arguments to the type array.
1105 #define ADDASTER() \
1108 while (is_digit(*cp)) { \
1109 n2 = 10 * n2 + to_digit(*cp); \
1113 int hold = nextarg; \
1122 typetable = stattypetable;
1123 tablesize = STATIC_ARG_TBL_SIZE;
1126 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1129 * Scan the format for conversions (`%' character).
1132 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1136 fmt++; /* skip over '%' */
1142 reswitch: switch (ch) {
1154 if ((ch = *fmt++) == '*') {
1158 while (is_digit(ch)) {
1164 case '1': case '2': case '3': case '4':
1165 case '5': case '6': case '7': case '8': case '9':
1168 n = 10 * n + to_digit(ch);
1170 } while (is_digit(ch));
1177 #ifdef FLOATING_POINT
1183 if (flags & SHORTINT) {
1193 if (flags & LONGINT) {
1218 #ifdef FLOATING_POINT
1228 if (flags & LONGDBL)
1229 ADDTYPE(T_LONG_DOUBLE);
1233 #endif /* FLOATING_POINT */
1235 if (flags & INTMAXT)
1236 ADDTYPE(TP_INTMAXT);
1237 else if (flags & PTRDIFFT)
1238 ADDTYPE(TP_PTRDIFFT);
1239 else if (flags & SIZET)
1241 else if (flags & LLONGINT)
1243 else if (flags & LONGINT)
1245 else if (flags & SHORTINT)
1247 else if (flags & CHARINT)
1251 continue; /* no output */
1272 default: /* "%?" prints ?, unless ? is NUL */
1280 * Build the argument table.
1282 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1283 *argtable = (union arg *)
1284 malloc(sizeof(union arg) * (tablemax + 1));
1286 (*argtable)[0].intarg = 0;
1287 for (n = 1; n <= tablemax; n++) {
1288 switch (typetable [n]) {
1289 case T_UNUSED: /* "whoops" */
1291 (*argtable)[n].intarg = va_arg(ap, int);
1294 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1296 (*argtable)[n].pshortarg = va_arg(ap, short *);
1299 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
1302 (*argtable)[n].pintarg = va_arg(ap, int *);
1305 (*argtable)[n].longarg = va_arg(ap, long);
1308 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
1311 (*argtable)[n].plongarg = va_arg(ap, long *);
1314 (*argtable)[n].longlongarg = va_arg(ap, long long);
1317 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
1320 (*argtable)[n].plonglongarg = va_arg(ap, long long *);
1323 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
1326 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
1329 (*argtable)[n].sizearg = va_arg(ap, size_t);
1332 (*argtable)[n].psizearg = va_arg(ap, ssize_t *);
1335 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
1338 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1341 (*argtable)[n].pintmaxarg = va_arg (ap, intmax_t *);
1344 (*argtable)[n].doublearg = va_arg(ap, double);
1347 (*argtable)[n].longdoublearg = va_arg(ap, long double);
1350 (*argtable)[n].pchararg = va_arg(ap, char *);
1353 (*argtable)[n].pvoidarg = va_arg(ap, void *);
1358 if ((typetable != NULL) && (typetable != stattypetable))
1363 * Increase the size of the type table.
1366 __grow_type_table(int nextarg, enum typeid **typetable, int *tablesize)
1368 enum typeid * const oldtable = *typetable;
1369 const int oldsize = *tablesize;
1370 enum typeid *newtable;
1371 int newsize = oldsize * 2;
1373 if (newsize < nextarg + 1)
1374 newsize = nextarg + 1;
1375 if (oldsize == STATIC_ARG_TBL_SIZE) {
1376 if ((newtable = malloc(newsize)) == NULL)
1377 abort(); /* XXX handle better */
1378 bcopy(oldtable, newtable, oldsize);
1380 if ((newtable = reallocf(oldtable, newsize)) == NULL)
1381 abort(); /* XXX handle better */
1383 memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
1385 *typetable = newtable;
1386 *tablesize = newsize;
1390 #ifdef FLOATING_POINT
1392 extern char *__dtoa (double, int, int, int *, int *, char **, char **);
1395 cvt(double value, int ndigits, int flags, char *sign, int *decpt,
1396 int ch, int *length, char **dtoaresultp)
1399 char *digits, *bp, *rve;
1402 mode = 3; /* ndigits after the decimal point */
1405 * To obtain ndigits after the decimal point for the 'e'
1406 * and 'E' formats, round to ndigits + 1 significant
1409 if (ch == 'e' || ch == 'E')
1411 mode = 2; /* ndigits significant digits */
1413 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, dtoaresultp);
1415 if ((ch != 'g' && ch != 'G') || flags & ALT) {
1416 /* print trailing zeros */
1417 bp = digits + ndigits;
1419 if (*digits == '0' && value)
1420 *decpt = -ndigits + 1;
1423 if (value == 0) /* kludge for __dtoa irregularity */
1428 *length = rve - digits;
1433 exponent(char *p0, int exp, int fmtch)
1436 char expbuf[MAXEXP];
1446 t = expbuf + MAXEXP;
1449 *--t = to_char(exp % 10);
1450 } while ((exp /= 10) > 9);
1451 *--t = to_char(exp);
1452 for (; t < expbuf + MAXEXP; *p++ = *t++);
1456 *p++ = to_char(exp);
1460 #endif /* FLOATING_POINT */