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.8 2005/04/21 16:36:35 joerg 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;
162 /* set up the buffer */
163 fake._bf._base = fake._p = buf;
164 fake._bf._size = fake._w = sizeof(buf);
165 fake._lbfsize = 0; /* not actually used, but Just In Case */
167 /* do the work, then copy any error status */
168 ret = __vfprintf(&fake, fmt, ap);
169 if (ret >= 0 && __fflush(&fake))
171 if (fake._flags & __SERR)
172 fp->_flags |= __SERR;
177 * Macros for converting digits to letters and vice versa
179 #define to_digit(c) ((c) - '0')
180 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
181 #define to_char(n) ((n) + '0')
184 * Convert an unsigned long to ASCII for printf purposes, returning
185 * a pointer to the first character of the string representation.
186 * Octal numbers can be forced to have a leading zero; hex numbers
187 * use the given digits.
190 __ultoa(u_long val, char *endp, int base, int octzero, char *xdigs,
191 int needgrp, char thousep, const char *grp)
198 * Handle the three cases separately, in the hope of getting
199 * better/faster code.
203 if (val < 10) { /* many numbers are 1 digit */
204 *--cp = to_char(val);
209 * On many machines, unsigned arithmetic is harder than
210 * signed arithmetic, so we do at most one unsigned mod and
211 * divide; this is sufficient to reduce the range of
212 * the incoming value to where signed arithmetic works.
214 if (val > LONG_MAX) {
215 *--cp = to_char(val % 10);
221 *--cp = to_char(sval % 10);
224 * If (*grp == CHAR_MAX) then no more grouping
225 * should be performed.
227 if (needgrp && ndig == *grp && *grp != CHAR_MAX &&
232 * If (*(grp+1) == '\0') then we have to
233 * use *grp character (last grouping rule)
236 if (*(grp + 1) != '\0')
245 *--cp = to_char(val & 7);
248 if (octzero && *cp != '0')
254 *--cp = xdigs[val & 15];
265 /* Identical to __ultoa, but for intmax_t. */
267 __ujtoa(u_quad_t val, char *endp, int base, int octzero, char *xdigs,
268 int needgrp, char thousep, const char *grp)
274 /* quick test for small values; __ultoa is typically much faster */
275 /* (perhaps instead we should run until small, then call __ultoa?) */
276 if (val <= ULONG_MAX)
277 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
278 needgrp, thousep, grp));
282 *--cp = to_char(val % 10);
286 if (val > INTMAX_MAX) {
287 *--cp = to_char(val % 10);
293 *--cp = to_char(sval % 10);
296 * If (*grp == CHAR_MAX) then no more grouping
297 * should be performed.
299 if (needgrp && *grp != CHAR_MAX && ndig == *grp &&
304 * If (*(grp+1) == '\0') then we have to
305 * use *grp character (last grouping rule)
308 if (*(grp + 1) != '\0')
317 *--cp = to_char(val & 7);
320 if (octzero && *cp != '0')
326 *--cp = xdigs[val & 15];
341 vfprintf(FILE *fp, const char *fmt0, va_list ap)
346 ret = __vfprintf(fp, fmt0, ap);
351 #ifdef FLOATING_POINT
355 #define BUF ((MAXEXP * 2) + MAXFRACT + 1) /* + decimal point */
358 static char *cvt (double, int, int, char *, int *, int, int *, char **);
359 static int exponent (char *, int, int);
361 #else /* no FLOATING_POINT */
365 #endif /* FLOATING_POINT */
367 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
370 * Flags used during conversion.
372 #define ALT 0x001 /* alternate form */
373 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
374 #define LADJUST 0x004 /* left adjustment */
375 #define LONGDBL 0x008 /* long double */
376 #define LONGINT 0x010 /* long integer */
377 #define LLONGINT 0x020 /* long long integer */
378 #define SHORTINT 0x040 /* short integer */
379 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
380 #define FPT 0x100 /* Floating point number */
381 #define GROUPING 0x200 /* use grouping ("'" flag) */
382 /* C99 additional size modifiers: */
383 #define SIZET 0x400 /* size_t */
384 #define PTRDIFFT 0x800 /* ptrdiff_t */
385 #define INTMAXT 0x1000 /* intmax_t */
386 #define CHARINT 0x2000 /* print char using int format */
388 * Non-MT-safe version
391 __vfprintf(FILE *fp, const char *fmt0, va_list ap)
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 const 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 */
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 */
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 */
434 * Choose PADSIZE to trade efficiency vs. size. If larger printf
435 * fields occur frequently, increase PADSIZE and make the initialisers
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'};
445 * BEWARE, these `goto error' on error, and PAD uses `n'.
447 #define PRINT(ptr, len) { \
448 iovp->iov_base = __DECONST(void *, ptr); \
449 iovp->iov_len = (len); \
450 uio.uio_resid += (len); \
452 if (++uio.uio_iovcnt >= NIOV) { \
453 if (__sprint(fp, &uio)) \
458 #define PAD(howmany, with) { \
459 if ((n = (howmany)) > 0) { \
460 while (n > PADSIZE) { \
461 PRINT(with, PADSIZE); \
468 if (uio.uio_resid && __sprint(fp, &uio)) \
470 uio.uio_iovcnt = 0; \
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).
479 #define GETARG(type) \
480 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
481 (nextarg++, va_arg(ap, type)))
484 * To extend shorts properly, we need both signed and unsigned
485 * argument extraction methods.
488 (flags&LONGINT ? GETARG(long) : \
489 flags&SHORTINT ? (long)(short)GETARG(int) : \
490 flags&CHARINT ? (long)(signed char)GETARG(int) : \
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)
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))
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))
510 * Get * arguments, including the form *nn$. Preserve the nextarg
511 * that the argument can be gotten once the type is determined.
513 #define GETASTER(val) \
516 while (is_digit(*cp)) { \
517 n2 = 10 * n2 + to_digit(*cp); \
521 int hold = nextarg; \
522 if (argtable == NULL) { \
523 argtable = statargtable; \
524 __find_arguments (fmt0, orgap, &argtable); \
527 val = GETARG (int); \
531 val = GETARG (int); \
535 thousands_sep = '\0';
537 #ifdef FLOATING_POINT
539 decimal_point = localeconv()->decimal_point;
541 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
546 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
547 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
549 return (__sbprintf(fp, fmt0, ap));
556 uio.uio_iov = iovp = iov;
562 * Scan the format for conversions (`%' character).
565 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
567 if ((n = fmt - cp) != 0) {
568 if ((unsigned)ret + n > INT_MAX) {
577 fmt++; /* skip over '%' */
586 reswitch: switch (ch) {
589 * ``If the space and + flags both appear, the space
590 * flag will be ignored.''
601 * ``A negative field width argument is taken as a
602 * - flag followed by a positive field width.''
604 * They don't exclude field widths read from args.
619 thousands_sep = *(localeconv()->thousands_sep);
620 grouping = localeconv()->grouping;
623 if ((ch = *fmt++) == '*') {
625 prec = n < 0 ? -1 : n;
629 while (is_digit(ch)) {
630 n = 10 * n + to_digit(ch);
633 prec = n < 0 ? -1 : n;
637 * ``Note that 0 is taken as a flag, not as the
638 * beginning of a field width.''
643 case '1': case '2': case '3': case '4':
644 case '5': case '6': case '7': case '8': case '9':
647 n = 10 * n + to_digit(ch);
649 } while (is_digit(ch));
652 if (argtable == NULL) {
653 argtable = statargtable;
654 __find_arguments (fmt0, orgap,
661 #ifdef FLOATING_POINT
667 if (flags & SHORTINT) {
677 if (flags & LONGINT) {
693 *(cp = buf) = GETARG(int);
702 if (flags & INTMAX_SIZE) {
704 if ((intmax_t)ujval < 0) {
710 if ((long)ulval < 0) {
717 #ifdef FLOATING_POINT
725 * Grouping apply to %i, %d, %u, %f, %F, %g, %G
726 * conversion specifiers only. For other conversions
727 * behavior is undefined.
739 fp_begin: if (prec == -1)
742 /* XXX this loses precision. */
743 _double = (double)GETARG(long double);
745 _double = GETARG(double);
746 /* do this before tricky precision changes */
747 if (isinf(_double)) {
757 if (isnan(_double)) {
766 if (dtoaresult != NULL) {
770 cp = cvt(_double, prec, flags, &softsign,
771 &expt, ch, &ndig, &dtoaresult);
772 if (ch == 'g' || ch == 'G') {
773 if (expt <= -4 || expt > prec)
774 ch = (ch == 'g') ? 'e' : 'E';
778 if (ch == 'e' || ch == 'E') {
780 expsize = exponent(expstr, expt, ch);
781 size = expsize + ndig;
782 if (ndig > 1 || flags & ALT)
784 } else if (ch == 'f' || ch == 'F') {
787 if (prec || flags & ALT)
791 } else if (expt >= ndig) { /* fixed g fmt */
796 size = ndig + (expt > 0 ?
802 #endif /* FLOATING_POINT */
805 * Assignment-like behavior is specified if the
806 * value overflows or is otherwise unrepresentable.
807 * C99 says to use `signed char' for %hhn conversions.
809 if (flags & LLONGINT)
810 *GETARG(long long *) = ret;
811 else if (flags & SIZET)
812 *GETARG(ssize_t *) = (ssize_t)ret;
813 else if (flags & PTRDIFFT)
814 *GETARG(ptrdiff_t *) = ret;
815 else if (flags & INTMAXT)
816 *GETARG(intmax_t *) = ret;
817 else if (flags & LONGINT)
818 *GETARG(long *) = ret;
819 else if (flags & SHORTINT)
820 *GETARG(short *) = ret;
821 else if (flags & CHARINT)
822 *GETARG(signed char *) = ret;
824 *GETARG(int *) = ret;
825 continue; /* no output */
830 if (flags & INTMAX_SIZE)
838 * ``The argument shall be a pointer to void. The
839 * value of the pointer is converted to a sequence
840 * of printable characters, in an implementation-
844 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
846 xdigs = "0123456789abcdef";
847 flags = flags | INTMAXT | HEXPREFIX;
851 if ((cp = GETARG(char *)) == NULL)
855 * can't use strlen; can only look for the
856 * NUL in the first `prec' characters, and
857 * strlen() will go further.
859 char *p = memchr(cp, 0, (size_t)prec);
875 if (flags & INTMAX_SIZE)
882 xdigs = "0123456789ABCDEF";
885 xdigs = "0123456789abcdef";
887 if (flags & INTMAX_SIZE)
892 /* leading 0x/X only if non-zero */
894 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
898 /* unsigned conversions */
901 * ``... diouXx conversions ... if a precision is
902 * specified, the 0 flag will be ignored.''
905 number: if ((dprec = prec) >= 0)
909 * ``The result of converting a zero value with an
910 * explicit precision of zero is no characters.''
914 if (flags & INTMAX_SIZE) {
915 if (ujval != 0 || prec != 0)
916 cp = __ujtoa(ujval, cp, base,
918 flags & GROUPING, thousands_sep,
921 if (ulval != 0 || prec != 0)
922 cp = __ultoa(ulval, cp, base,
924 flags & GROUPING, thousands_sep,
927 size = buf + BUF - cp;
929 default: /* "%?" prints ?, unless ? is NUL */
932 /* pretend it was %c with argument ch */
941 * All reasonable formats wind up here. At this point, `cp'
942 * points to a string which (if not flags&LADJUST) should be
943 * padded out to `width' places. If flags&ZEROPAD, it should
944 * first be prefixed by any sign or other prefix; otherwise,
945 * it should be blank padded before the prefix is emitted.
946 * After any left-hand padding and prefixing, emit zeroes
947 * required by a decimal [diouxX] precision, then print the
948 * string proper, then emit zeroes required by any leftover
949 * floating precision; finally, if LADJUST, pad with blanks.
951 * Compute actual size, so we know how much to pad.
952 * size excludes decimal prec; realsz includes it.
954 realsz = dprec > size ? dprec : size;
957 else if (flags & HEXPREFIX)
960 prsize = width > realsz ? width : realsz;
961 if ((unsigned)ret + prsize > INT_MAX) {
966 /* right-adjusting blank padding */
967 if ((flags & (LADJUST|ZEROPAD)) == 0)
968 PAD(width - realsz, blanks);
973 } else if (flags & HEXPREFIX) {
979 /* right-adjusting zero padding */
980 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
981 PAD(width - realsz, zeroes);
983 /* leading zeroes from decimal precision */
984 PAD(dprec - size, zeroes);
986 /* the string or number proper */
987 #ifdef FLOATING_POINT
988 if ((flags & FPT) == 0) {
990 } else { /* glue together f_p fragments */
991 if (ch >= 'f') { /* 'f' or 'g' */
993 /* kludge for __dtoa irregularity */
995 if (expt < ndig || (flags & ALT) != 0) {
996 PRINT(decimal_point, 1);
997 PAD(ndig - 1, zeroes);
999 } else if (expt <= 0) {
1001 PRINT(decimal_point, 1);
1004 } else if (expt >= ndig) {
1006 PAD(expt - ndig, zeroes);
1008 PRINT(decimal_point, 1);
1012 PRINT(decimal_point, 1);
1013 PRINT(cp, ndig-expt);
1015 } else { /* 'e' or 'E' */
1016 if (ndig > 1 || flags & ALT) {
1018 ox[1] = *decimal_point;
1022 } else /* 0.[0..] */
1023 /* __dtoa irregularity */
1024 PAD(ndig - 1, zeroes);
1027 PRINT(expstr, expsize);
1033 /* left-adjusting padding (always blank) */
1034 if (flags & LADJUST)
1035 PAD(width - realsz, blanks);
1037 /* finally, adjust ret */
1040 FLUSH(); /* copy out the I/O vectors */
1045 #ifdef FLOATING_POINT
1046 if (dtoaresult != NULL)
1051 if ((argtable != NULL) && (argtable != statargtable))
1058 * Find all arguments when a positional parameter is encountered. Returns a
1059 * table, indexed by argument number, of pointers to each arguments. The
1060 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1061 * It will be replaces with a malloc-ed one if it overflows.
1064 __find_arguments (const char *fmt0, va_list ap, union arg **argtable)
1066 char *fmt; /* format string */
1067 int ch; /* character from fmt */
1068 int n, n2; /* handy integer (short term usage) */
1069 char *cp; /* handy char pointer (short term usage) */
1070 int flags; /* flags as above */
1071 int width; /* width from format (%8d), or 0 */
1072 enum typeid *typetable; /* table of types */
1073 enum typeid stattypetable[STATIC_ARG_TBL_SIZE];
1074 int tablesize; /* current size of type table */
1075 int tablemax; /* largest used index in table */
1076 int nextarg; /* 1-based argument index */
1079 * Add an argument type to the table, expanding if necessary.
1081 #define ADDTYPE(type) \
1082 ((nextarg >= tablesize) ? \
1083 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1084 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1085 typetable[nextarg++] = type)
1088 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1089 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1090 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1091 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1092 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1095 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1096 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1097 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1098 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1099 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1102 * Add * arguments to the type array.
1104 #define ADDASTER() \
1107 while (is_digit(*cp)) { \
1108 n2 = 10 * n2 + to_digit(*cp); \
1112 int hold = nextarg; \
1121 typetable = stattypetable;
1122 tablesize = STATIC_ARG_TBL_SIZE;
1125 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1128 * Scan the format for conversions (`%' character).
1131 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1135 fmt++; /* skip over '%' */
1141 reswitch: switch (ch) {
1153 if ((ch = *fmt++) == '*') {
1157 while (is_digit(ch)) {
1163 case '1': case '2': case '3': case '4':
1164 case '5': case '6': case '7': case '8': case '9':
1167 n = 10 * n + to_digit(ch);
1169 } while (is_digit(ch));
1176 #ifdef FLOATING_POINT
1182 if (flags & SHORTINT) {
1192 if (flags & LONGINT) {
1217 #ifdef FLOATING_POINT
1227 if (flags & LONGDBL)
1228 ADDTYPE(T_LONG_DOUBLE);
1232 #endif /* FLOATING_POINT */
1234 if (flags & INTMAXT)
1235 ADDTYPE(TP_INTMAXT);
1236 else if (flags & PTRDIFFT)
1237 ADDTYPE(TP_PTRDIFFT);
1238 else if (flags & SIZET)
1240 else if (flags & LLONGINT)
1242 else if (flags & LONGINT)
1244 else if (flags & SHORTINT)
1246 else if (flags & CHARINT)
1250 continue; /* no output */
1271 default: /* "%?" prints ?, unless ? is NUL */
1279 * Build the argument table.
1281 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1282 *argtable = (union arg *)
1283 malloc(sizeof(union arg) * (tablemax + 1));
1285 (*argtable)[0].intarg = 0;
1286 for (n = 1; n <= tablemax; n++) {
1287 switch (typetable [n]) {
1288 case T_UNUSED: /* "whoops" */
1290 (*argtable)[n].intarg = va_arg(ap, int);
1293 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1295 (*argtable)[n].pshortarg = va_arg(ap, short *);
1298 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
1301 (*argtable)[n].pintarg = va_arg(ap, int *);
1304 (*argtable)[n].longarg = va_arg(ap, long);
1307 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
1310 (*argtable)[n].plongarg = va_arg(ap, long *);
1313 (*argtable)[n].longlongarg = va_arg(ap, long long);
1316 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
1319 (*argtable)[n].plonglongarg = va_arg(ap, long long *);
1322 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
1325 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
1328 (*argtable)[n].sizearg = va_arg(ap, size_t);
1331 (*argtable)[n].psizearg = va_arg(ap, ssize_t *);
1334 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
1337 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1340 (*argtable)[n].pintmaxarg = va_arg (ap, intmax_t *);
1343 (*argtable)[n].doublearg = va_arg(ap, double);
1346 (*argtable)[n].longdoublearg = va_arg(ap, long double);
1349 (*argtable)[n].pchararg = va_arg(ap, char *);
1352 (*argtable)[n].pvoidarg = va_arg(ap, void *);
1357 if ((typetable != NULL) && (typetable != stattypetable))
1362 * Increase the size of the type table.
1365 __grow_type_table(int nextarg, enum typeid **typetable, int *tablesize)
1367 enum typeid * const oldtable = *typetable;
1368 const int oldsize = *tablesize;
1369 enum typeid *newtable;
1370 int newsize = oldsize * 2;
1372 if (newsize < nextarg + 1)
1373 newsize = nextarg + 1;
1374 if (oldsize == STATIC_ARG_TBL_SIZE) {
1375 if ((newtable = malloc(newsize)) == NULL)
1376 abort(); /* XXX handle better */
1377 bcopy(oldtable, newtable, oldsize);
1379 if ((newtable = reallocf(oldtable, newsize)) == NULL)
1380 abort(); /* XXX handle better */
1382 memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
1384 *typetable = newtable;
1385 *tablesize = newsize;
1389 #ifdef FLOATING_POINT
1391 extern char *__dtoa (double, int, int, int *, int *, char **, char **);
1394 cvt(double value, int ndigits, int flags, char *sign, int *decpt,
1395 int ch, int *length, char **dtoaresultp)
1398 char *digits, *bp, *rve;
1401 mode = 3; /* ndigits after the decimal point */
1404 * To obtain ndigits after the decimal point for the 'e'
1405 * and 'E' formats, round to ndigits + 1 significant
1408 if (ch == 'e' || ch == 'E')
1410 mode = 2; /* ndigits significant digits */
1412 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, dtoaresultp);
1414 if ((ch != 'g' && ch != 'G') || flags & ALT) {
1415 /* print trailing zeros */
1416 bp = digits + ndigits;
1418 if (*digits == '0' && value)
1419 *decpt = -ndigits + 1;
1422 if (value == 0) /* kludge for __dtoa irregularity */
1427 *length = rve - digits;
1432 exponent(char *p0, int exp, int fmtch)
1435 char expbuf[MAXEXP];
1445 t = expbuf + MAXEXP;
1448 *--t = to_char(exp % 10);
1449 } while ((exp /= 10) > 9);
1450 *--t = to_char(exp);
1451 for (; t < expbuf + MAXEXP; *p++ = *t++);
1455 *p++ = to_char(exp);
1459 #endif /* FLOATING_POINT */