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.11 2005/07/23 23:14:44 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"
64 #include "priv_stdio.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 void __find_arguments(const char *, va_list, union arg **);
122 static void __grow_type_table(int, enum typeid **, int *);
125 * Flush out all the vectors defined by the given uio,
126 * then reset it so that it can be reused.
129 __sprint(FILE *fp, struct __suio *uio)
133 if (uio->uio_resid == 0) {
137 err = __sfvwrite(fp, uio);
144 * Helper function for `fprintf to unbuffered unix file': creates a
145 * temporary buffer. We only work on write-only files; this avoids
146 * worries about ungetc buffers and so forth.
149 __sbprintf(FILE *fp, const char *fmt, va_list ap)
153 unsigned char buf[BUFSIZ];
155 /* copy the important variables */
156 fake.pub._flags = fp->pub._flags & ~__SNBF;
157 fake.pub._fileno = fp->pub._fileno;
158 fake._cookie = fp->_cookie;
159 fake._write = fp->_write;
162 fake.fl_mutex = fp->fl_mutex;
163 fake.fl_owner = fp->fl_owner;
164 fake.fl_count = fp->fl_count;
166 /* set up the buffer */
167 fake._bf._base = fake.pub._p = buf;
168 fake._bf._size = fake.pub._w = sizeof(buf);
169 fake.pub._lbfsize = 0; /* not actually used, but Just In Case */
171 /* do the work, then copy any error status */
172 ret = __vfprintf(&fake, fmt, ap);
173 if (ret >= 0 && __fflush(&fake))
175 if (fake.pub._flags & __SERR)
176 fp->pub._flags |= __SERR;
181 * Macros for converting digits to letters and vice versa
183 #define to_digit(c) ((c) - '0')
184 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
185 #define to_char(n) ((n) + '0')
188 * Convert an unsigned long to ASCII for printf purposes, returning
189 * a pointer to the first character of the string representation.
190 * Octal numbers can be forced to have a leading zero; hex numbers
191 * use the given digits.
194 __ultoa(u_long val, char *endp, int base, int octzero, char *xdigs,
195 int needgrp, char thousep, const char *grp)
202 * Handle the three cases separately, in the hope of getting
203 * better/faster code.
207 if (val < 10) { /* many numbers are 1 digit */
208 *--cp = to_char(val);
213 * On many machines, unsigned arithmetic is harder than
214 * signed arithmetic, so we do at most one unsigned mod and
215 * divide; this is sufficient to reduce the range of
216 * the incoming value to where signed arithmetic works.
218 if (val > LONG_MAX) {
219 *--cp = to_char(val % 10);
225 *--cp = to_char(sval % 10);
228 * If (*grp == CHAR_MAX) then no more grouping
229 * should be performed.
231 if (needgrp && ndig == *grp && *grp != CHAR_MAX &&
236 * If (*(grp+1) == '\0') then we have to
237 * use *grp character (last grouping rule)
240 if (*(grp + 1) != '\0')
249 *--cp = to_char(val & 7);
252 if (octzero && *cp != '0')
258 *--cp = xdigs[val & 15];
269 /* Identical to __ultoa, but for intmax_t. */
271 __ujtoa(u_quad_t val, char *endp, int base, int octzero, char *xdigs,
272 int needgrp, char thousep, const char *grp)
278 /* quick test for small values; __ultoa is typically much faster */
279 /* (perhaps instead we should run until small, then call __ultoa?) */
280 if (val <= ULONG_MAX)
281 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
282 needgrp, thousep, grp));
286 *--cp = to_char(val % 10);
290 if (val > INTMAX_MAX) {
291 *--cp = to_char(val % 10);
297 *--cp = to_char(sval % 10);
300 * If (*grp == CHAR_MAX) then no more grouping
301 * should be performed.
303 if (needgrp && *grp != CHAR_MAX && ndig == *grp &&
308 * If (*(grp+1) == '\0') then we have to
309 * use *grp character (last grouping rule)
312 if (*(grp + 1) != '\0')
321 *--cp = to_char(val & 7);
324 if (octzero && *cp != '0')
330 *--cp = xdigs[val & 15];
345 vfprintf(FILE *fp, const char *fmt0, va_list ap)
350 ret = __vfprintf(fp, fmt0, ap);
355 #ifdef FLOATING_POINT
359 #define BUF ((MAXEXP * 2) + MAXFRACT + 1) /* + decimal point */
362 static char *cvt (double, int, int, char *, int *, int, int *, char **);
363 static int exponent (char *, int, int);
365 #else /* no FLOATING_POINT */
369 #endif /* FLOATING_POINT */
371 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
374 * Flags used during conversion.
376 #define ALT 0x001 /* alternate form */
377 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
378 #define LADJUST 0x004 /* left adjustment */
379 #define LONGDBL 0x008 /* long double */
380 #define LONGINT 0x010 /* long integer */
381 #define LLONGINT 0x020 /* long long integer */
382 #define SHORTINT 0x040 /* short integer */
383 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
384 #define FPT 0x100 /* Floating point number */
385 #define GROUPING 0x200 /* use grouping ("'" flag) */
386 /* C99 additional size modifiers: */
387 #define SIZET 0x400 /* size_t */
388 #define PTRDIFFT 0x800 /* ptrdiff_t */
389 #define INTMAXT 0x1000 /* intmax_t */
390 #define CHARINT 0x2000 /* print char using int format */
392 * Non-MT-safe version
395 __vfprintf(FILE *fp, const char *fmt0, va_list ap)
397 char *fmt; /* format string */
398 int ch; /* character from fmt */
399 int n, n2; /* handy integer (short term usage) */
400 char *cp; /* handy char pointer (short term usage) */
401 struct __siov *iovp; /* for PRINT macro */
402 int flags; /* flags as above */
403 int ret; /* return value accumulator */
404 int width; /* width from format (%8d), or 0 */
405 int prec; /* precision from format (%.3d), or -1 */
406 char sign; /* sign prefix (' ', '+', '-', or \0) */
407 char thousands_sep; /* locale specific thousands separator */
408 const char *grouping; /* locale specific numeric grouping rules */
409 #ifdef FLOATING_POINT
410 const char *decimal_point; /* locale specific decimal point */
411 char softsign; /* temporary negative sign for floats */
412 double _double; /* double precision arguments %[eEfgG] */
413 int expt; /* integer value of exponent */
414 int expsize; /* character count for expstr */
415 int ndig; /* actual number of digits returned by cvt */
416 char expstr[7]; /* buffer for exponent string */
417 char *dtoaresult; /* buffer allocated by dtoa */
419 u_long ulval; /* integer arguments %[diouxX] */
420 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
421 int base; /* base for [diouxX] conversion */
422 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
423 int realsz; /* field size expanded by dprec, sign, etc */
424 int size; /* size of converted field or string */
425 int prsize; /* max size of printed field */
426 char *xdigs; /* digits for [xX] conversion */
428 struct __suio uio; /* output information: summary */
429 struct __siov iov[NIOV];/* ... and individual io vectors */
430 char buf[BUF]; /* space for %c, %[diouxX], %[eEfFgG] */
431 char ox[2]; /* space for 0x hex-prefix */
432 union arg *argtable; /* args, built due to positional arg */
433 union arg statargtable[STATIC_ARG_TBL_SIZE];
434 int nextarg; /* 1-based argument index */
435 va_list orgap; /* original argument pointer */
438 * Choose PADSIZE to trade efficiency vs. size. If larger printf
439 * fields occur frequently, increase PADSIZE and make the initialisers
442 #define PADSIZE 16 /* pad chunk size */
443 static char blanks[PADSIZE] =
444 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
445 static char zeroes[PADSIZE] =
446 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
449 * BEWARE, these `goto error' on error, and PAD uses `n'.
451 #define PRINT(ptr, len) { \
452 iovp->iov_base = __DECONST(void *, ptr); \
453 iovp->iov_len = (len); \
454 uio.uio_resid += (len); \
456 if (++uio.uio_iovcnt >= NIOV) { \
457 if (__sprint(fp, &uio)) \
462 #define PAD(howmany, with) { \
463 if ((n = (howmany)) > 0) { \
464 while (n > PADSIZE) { \
465 PRINT(with, PADSIZE); \
472 if (uio.uio_resid && __sprint(fp, &uio)) \
474 uio.uio_iovcnt = 0; \
479 * Get the argument indexed by nextarg. If the argument table is
480 * built, use it to get the argument. If its not, get the next
481 * argument (and arguments must be gotten sequentially).
483 #define GETARG(type) \
484 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
485 (nextarg++, va_arg(ap, type)))
488 * To extend shorts properly, we need both signed and unsigned
489 * argument extraction methods.
492 (flags&LONGINT ? GETARG(long) : \
493 flags&SHORTINT ? (long)(short)GETARG(int) : \
494 flags&CHARINT ? (long)(signed char)GETARG(int) : \
497 (flags&LONGINT ? GETARG(u_long) : \
498 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
499 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
500 (u_long)GETARG(u_int))
501 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
503 (flags&INTMAXT ? GETARG(intmax_t) : \
504 flags&SIZET ? (intmax_t)GETARG(size_t) : \
505 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
506 (intmax_t)GETARG(long long))
508 (flags&INTMAXT ? GETARG(uintmax_t) : \
509 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
510 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
511 (uintmax_t)GETARG(unsigned long long))
514 * Get * arguments, including the form *nn$. Preserve the nextarg
515 * that the argument can be gotten once the type is determined.
517 #define GETASTER(val) \
520 while (is_digit(*cp)) { \
521 n2 = 10 * n2 + to_digit(*cp); \
525 int hold = nextarg; \
526 if (argtable == NULL) { \
527 argtable = statargtable; \
528 __find_arguments (fmt0, orgap, &argtable); \
531 val = GETARG (int); \
535 val = GETARG (int); \
539 thousands_sep = '\0';
541 #ifdef FLOATING_POINT
543 decimal_point = localeconv()->decimal_point;
545 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
550 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
551 if ((fp->pub._flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
552 fp->pub._fileno >= 0) {
553 return (__sbprintf(fp, fmt0, ap));
560 uio.uio_iov = iovp = iov;
566 * Scan the format for conversions (`%' character).
569 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
571 if ((n = fmt - cp) != 0) {
572 if ((unsigned)ret + n > INT_MAX) {
581 fmt++; /* skip over '%' */
590 reswitch: switch (ch) {
593 * ``If the space and + flags both appear, the space
594 * flag will be ignored.''
605 * ``A negative field width argument is taken as a
606 * - flag followed by a positive field width.''
608 * They don't exclude field widths read from args.
623 thousands_sep = *(localeconv()->thousands_sep);
624 grouping = localeconv()->grouping;
627 if ((ch = *fmt++) == '*') {
629 prec = n < 0 ? -1 : n;
633 while (is_digit(ch)) {
634 n = 10 * n + to_digit(ch);
637 prec = n < 0 ? -1 : n;
641 * ``Note that 0 is taken as a flag, not as the
642 * beginning of a field width.''
647 case '1': case '2': case '3': case '4':
648 case '5': case '6': case '7': case '8': case '9':
651 n = 10 * n + to_digit(ch);
653 } while (is_digit(ch));
656 if (argtable == NULL) {
657 argtable = statargtable;
658 __find_arguments (fmt0, orgap,
665 #ifdef FLOATING_POINT
671 if (flags & SHORTINT) {
681 if (flags & LONGINT) {
697 *(cp = buf) = GETARG(int);
706 if (flags & INTMAX_SIZE) {
708 if ((intmax_t)ujval < 0) {
714 if ((long)ulval < 0) {
721 #ifdef FLOATING_POINT
729 * Grouping apply to %i, %d, %u, %f, %F, %g, %G
730 * conversion specifiers only. For other conversions
731 * behavior is undefined.
743 fp_begin: if (prec == -1)
746 /* XXX this loses precision. */
747 _double = (double)GETARG(long double);
749 _double = GETARG(double);
750 /* do this before tricky precision changes */
751 if (isinf(_double)) {
761 if (isnan(_double)) {
770 if (dtoaresult != NULL) {
774 cp = cvt(_double, prec, flags, &softsign,
775 &expt, ch, &ndig, &dtoaresult);
776 if (ch == 'g' || ch == 'G') {
777 if (expt <= -4 || expt > prec)
778 ch = (ch == 'g') ? 'e' : 'E';
782 if (ch == 'e' || ch == 'E') {
784 expsize = exponent(expstr, expt, ch);
785 size = expsize + ndig;
786 if (ndig > 1 || flags & ALT)
788 } else if (ch == 'f' || ch == 'F') {
791 if (prec || flags & ALT)
795 } else if (expt >= ndig) { /* fixed g fmt */
800 size = ndig + (expt > 0 ?
806 #endif /* FLOATING_POINT */
809 * Assignment-like behavior is specified if the
810 * value overflows or is otherwise unrepresentable.
811 * C99 says to use `signed char' for %hhn conversions.
813 if (flags & LLONGINT)
814 *GETARG(long long *) = ret;
815 else if (flags & SIZET)
816 *GETARG(ssize_t *) = (ssize_t)ret;
817 else if (flags & PTRDIFFT)
818 *GETARG(ptrdiff_t *) = ret;
819 else if (flags & INTMAXT)
820 *GETARG(intmax_t *) = ret;
821 else if (flags & LONGINT)
822 *GETARG(long *) = ret;
823 else if (flags & SHORTINT)
824 *GETARG(short *) = ret;
825 else if (flags & CHARINT)
826 *GETARG(signed char *) = ret;
828 *GETARG(int *) = ret;
829 continue; /* no output */
834 if (flags & INTMAX_SIZE)
842 * ``The argument shall be a pointer to void. The
843 * value of the pointer is converted to a sequence
844 * of printable characters, in an implementation-
848 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
850 xdigs = "0123456789abcdef";
851 flags = flags | INTMAXT | HEXPREFIX;
855 if ((cp = GETARG(char *)) == NULL)
859 * can't use strlen; can only look for the
860 * NUL in the first `prec' characters, and
861 * strlen() will go further.
863 char *p = memchr(cp, 0, (size_t)prec);
879 if (flags & INTMAX_SIZE)
886 xdigs = "0123456789ABCDEF";
889 xdigs = "0123456789abcdef";
891 if (flags & INTMAX_SIZE)
896 /* leading 0x/X only if non-zero */
898 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
902 /* unsigned conversions */
905 * ``... diouXx conversions ... if a precision is
906 * specified, the 0 flag will be ignored.''
909 number: if ((dprec = prec) >= 0)
913 * ``The result of converting a zero value with an
914 * explicit precision of zero is no characters.''
918 if (flags & INTMAX_SIZE) {
919 if (ujval != 0 || prec != 0)
920 cp = __ujtoa(ujval, cp, base,
922 flags & GROUPING, thousands_sep,
925 if (ulval != 0 || prec != 0)
926 cp = __ultoa(ulval, cp, base,
928 flags & GROUPING, thousands_sep,
931 size = buf + BUF - cp;
933 default: /* "%?" prints ?, unless ? is NUL */
936 /* pretend it was %c with argument ch */
945 * All reasonable formats wind up here. At this point, `cp'
946 * points to a string which (if not flags&LADJUST) should be
947 * padded out to `width' places. If flags&ZEROPAD, it should
948 * first be prefixed by any sign or other prefix; otherwise,
949 * it should be blank padded before the prefix is emitted.
950 * After any left-hand padding and prefixing, emit zeroes
951 * required by a decimal [diouxX] precision, then print the
952 * string proper, then emit zeroes required by any leftover
953 * floating precision; finally, if LADJUST, pad with blanks.
955 * Compute actual size, so we know how much to pad.
956 * size excludes decimal prec; realsz includes it.
958 realsz = dprec > size ? dprec : size;
961 else if (flags & HEXPREFIX)
964 prsize = width > realsz ? width : realsz;
965 if ((unsigned)ret + prsize > INT_MAX) {
970 /* right-adjusting blank padding */
971 if ((flags & (LADJUST|ZEROPAD)) == 0)
972 PAD(width - realsz, blanks);
977 } else if (flags & HEXPREFIX) {
983 /* right-adjusting zero padding */
984 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
985 PAD(width - realsz, zeroes);
987 /* leading zeroes from decimal precision */
988 PAD(dprec - size, zeroes);
990 /* the string or number proper */
991 #ifdef FLOATING_POINT
992 if ((flags & FPT) == 0) {
994 } else { /* glue together f_p fragments */
995 if (ch >= 'f') { /* 'f' or 'g' */
997 /* kludge for __dtoa irregularity */
999 if (expt < ndig || (flags & ALT) != 0) {
1000 PRINT(decimal_point, 1);
1001 PAD(ndig - 1, zeroes);
1003 } else if (expt <= 0) {
1005 PRINT(decimal_point, 1);
1008 } else if (expt >= ndig) {
1010 PAD(expt - ndig, zeroes);
1012 PRINT(decimal_point, 1);
1016 PRINT(decimal_point, 1);
1017 PRINT(cp, ndig-expt);
1019 } else { /* 'e' or 'E' */
1020 if (ndig > 1 || flags & ALT) {
1022 ox[1] = *decimal_point;
1026 } else /* 0.[0..] */
1027 /* __dtoa irregularity */
1028 PAD(ndig - 1, zeroes);
1031 PRINT(expstr, expsize);
1037 /* left-adjusting padding (always blank) */
1038 if (flags & LADJUST)
1039 PAD(width - realsz, blanks);
1041 /* finally, adjust ret */
1044 FLUSH(); /* copy out the I/O vectors */
1049 #ifdef FLOATING_POINT
1050 if (dtoaresult != NULL)
1055 if ((argtable != NULL) && (argtable != statargtable))
1062 * Find all arguments when a positional parameter is encountered. Returns a
1063 * table, indexed by argument number, of pointers to each arguments. The
1064 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1065 * It will be replaces with a malloc-ed one if it overflows.
1068 __find_arguments (const char *fmt0, va_list ap, union arg **argtable)
1070 char *fmt; /* format string */
1071 int ch; /* character from fmt */
1072 int n, n2; /* handy integer (short term usage) */
1073 char *cp; /* handy char pointer (short term usage) */
1074 int flags; /* flags as above */
1075 int width; /* width from format (%8d), or 0 */
1076 enum typeid *typetable; /* table of types */
1077 enum typeid stattypetable[STATIC_ARG_TBL_SIZE];
1078 int tablesize; /* current size of type table */
1079 int tablemax; /* largest used index in table */
1080 int nextarg; /* 1-based argument index */
1083 * Add an argument type to the table, expanding if necessary.
1085 #define ADDTYPE(type) \
1086 ((nextarg >= tablesize) ? \
1087 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1088 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1089 typetable[nextarg++] = type)
1092 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1093 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1094 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1095 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1096 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1099 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1100 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1101 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1102 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1103 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1106 * Add * arguments to the type array.
1108 #define ADDASTER() \
1111 while (is_digit(*cp)) { \
1112 n2 = 10 * n2 + to_digit(*cp); \
1116 int hold = nextarg; \
1125 typetable = stattypetable;
1126 tablesize = STATIC_ARG_TBL_SIZE;
1129 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1132 * Scan the format for conversions (`%' character).
1135 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1139 fmt++; /* skip over '%' */
1145 reswitch: switch (ch) {
1157 if ((ch = *fmt++) == '*') {
1161 while (is_digit(ch)) {
1167 case '1': case '2': case '3': case '4':
1168 case '5': case '6': case '7': case '8': case '9':
1171 n = 10 * n + to_digit(ch);
1173 } while (is_digit(ch));
1180 #ifdef FLOATING_POINT
1186 if (flags & SHORTINT) {
1196 if (flags & LONGINT) {
1221 #ifdef FLOATING_POINT
1231 if (flags & LONGDBL)
1232 ADDTYPE(T_LONG_DOUBLE);
1236 #endif /* FLOATING_POINT */
1238 if (flags & INTMAXT)
1239 ADDTYPE(TP_INTMAXT);
1240 else if (flags & PTRDIFFT)
1241 ADDTYPE(TP_PTRDIFFT);
1242 else if (flags & SIZET)
1244 else if (flags & LLONGINT)
1246 else if (flags & LONGINT)
1248 else if (flags & SHORTINT)
1250 else if (flags & CHARINT)
1254 continue; /* no output */
1275 default: /* "%?" prints ?, unless ? is NUL */
1283 * Build the argument table.
1285 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1286 *argtable = (union arg *)
1287 malloc(sizeof(union arg) * (tablemax + 1));
1289 (*argtable)[0].intarg = 0;
1290 for (n = 1; n <= tablemax; n++) {
1291 switch (typetable [n]) {
1292 case T_UNUSED: /* "whoops" */
1294 (*argtable)[n].intarg = va_arg(ap, int);
1297 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1299 (*argtable)[n].pshortarg = va_arg(ap, short *);
1302 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
1305 (*argtable)[n].pintarg = va_arg(ap, int *);
1308 (*argtable)[n].longarg = va_arg(ap, long);
1311 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
1314 (*argtable)[n].plongarg = va_arg(ap, long *);
1317 (*argtable)[n].longlongarg = va_arg(ap, long long);
1320 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
1323 (*argtable)[n].plonglongarg = va_arg(ap, long long *);
1326 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
1329 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
1332 (*argtable)[n].sizearg = va_arg(ap, size_t);
1335 (*argtable)[n].psizearg = va_arg(ap, ssize_t *);
1338 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
1341 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1344 (*argtable)[n].pintmaxarg = va_arg (ap, intmax_t *);
1347 (*argtable)[n].doublearg = va_arg(ap, double);
1350 (*argtable)[n].longdoublearg = va_arg(ap, long double);
1353 (*argtable)[n].pchararg = va_arg(ap, char *);
1356 (*argtable)[n].pvoidarg = va_arg(ap, void *);
1361 if ((typetable != NULL) && (typetable != stattypetable))
1366 * Increase the size of the type table.
1369 __grow_type_table(int nextarg, enum typeid **typetable, int *tablesize)
1371 enum typeid * const oldtable = *typetable;
1372 const int oldsize = *tablesize;
1373 enum typeid *newtable;
1374 int newsize = oldsize * 2;
1376 if (newsize < nextarg + 1)
1377 newsize = nextarg + 1;
1378 if (oldsize == STATIC_ARG_TBL_SIZE) {
1379 if ((newtable = malloc(newsize)) == NULL)
1380 abort(); /* XXX handle better */
1381 bcopy(oldtable, newtable, oldsize);
1383 if ((newtable = reallocf(oldtable, newsize)) == NULL)
1384 abort(); /* XXX handle better */
1386 memset(&newtable[oldsize], T_UNUSED, newsize - oldsize);
1388 *typetable = newtable;
1389 *tablesize = newsize;
1393 #ifdef FLOATING_POINT
1395 extern char *__dtoa (double, int, int, int *, int *, char **, char **);
1398 cvt(double value, int ndigits, int flags, char *sign, int *decpt,
1399 int ch, int *length, char **dtoaresultp)
1402 char *digits, *bp, *rve;
1405 mode = 3; /* ndigits after the decimal point */
1408 * To obtain ndigits after the decimal point for the 'e'
1409 * and 'E' formats, round to ndigits + 1 significant
1412 if (ch == 'e' || ch == 'E')
1414 mode = 2; /* ndigits significant digits */
1416 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, dtoaresultp);
1418 if ((ch != 'g' && ch != 'G') || flags & ALT) {
1419 /* print trailing zeros */
1420 bp = digits + ndigits;
1422 if (*digits == '0' && value)
1423 *decpt = -ndigits + 1;
1426 if (value == 0) /* kludge for __dtoa irregularity */
1431 *length = rve - digits;
1436 exponent(char *p0, int exp, int fmtch)
1439 char expbuf[MAXEXP];
1449 t = expbuf + MAXEXP;
1452 *--t = to_char(exp % 10);
1453 } while ((exp /= 10) > 9);
1454 *--t = to_char(exp);
1455 for (; t < expbuf + MAXEXP; *p++ = *t++);
1459 *p++ = to_char(exp);
1463 #endif /* FLOATING_POINT */