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.16 2006/07/05 15:04:54 joerg Exp $
42 * Actual printf innards.
44 * This code is large and complicated...
47 #include "namespace.h"
48 #include <sys/types.h>
61 #include "un-namespace.h"
63 #include "libc_private.h"
65 #include "priv_stdio.h"
67 /* Borrowed from sys/systm.h, which is _KERNEL-only: */
68 #define CTASSERT(x) _CTASSERT(x, __LINE__)
69 #define _CTASSERT(x, y) __CTASSERT(x, y)
70 #define __CTASSERT(x, y) typedef char __assert ## y[(x) ? 1 : -1]
72 /* This code assumes that a quad_t can fit in a long long: */
73 CTASSERT(sizeof(quad_t) == sizeof(long long));
79 unsigned long ulongarg;
80 long long longlongarg;
81 unsigned long long ulonglongarg;
88 signed char *pschararg;
92 long long *plonglongarg;
93 ptrdiff_t *pptrdiffarg;
96 #ifndef NO_FLOATING_POINT
98 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, T_WINT, TP_WCHAR
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, const char *, int,
119 static char * __ultoa(u_long, char *, int, int, const 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;
165 memset(WCIO_GET(&fake), 0, sizeof(struct wchar_io_data));
167 /* set up the buffer */
168 fake._bf._base = fake.pub._p = buf;
169 fake._bf._size = fake.pub._w = sizeof(buf);
170 fake.pub._lbfsize = 0; /* not actually used, but Just In Case */
172 /* do the work, then copy any error status */
173 ret = __vfprintf(&fake, fmt, ap);
174 if (ret >= 0 && __fflush(&fake))
176 if (fake.pub._flags & __SERR)
177 fp->pub._flags |= __SERR;
182 * Macros for converting digits to letters and vice versa
184 #define to_digit(c) ((c) - '0')
185 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
186 #define to_char(n) ((n) + '0')
189 * Convert an unsigned long to ASCII for printf purposes, returning
190 * a pointer to the first character of the string representation.
191 * Octal numbers can be forced to have a leading zero; hex numbers
192 * use the given digits.
195 __ultoa(u_long val, char *endp, int base, int octzero, const char *xdigs,
196 int needgrp, char thousep, const char *grp)
203 * Handle the three cases separately, in the hope of getting
204 * better/faster code.
208 if (val < 10) { /* many numbers are 1 digit */
209 *--cp = to_char(val);
214 * On many machines, unsigned arithmetic is harder than
215 * signed arithmetic, so we do at most one unsigned mod and
216 * divide; this is sufficient to reduce the range of
217 * the incoming value to where signed arithmetic works.
219 if (val > LONG_MAX) {
220 *--cp = to_char(val % 10);
226 *--cp = to_char(sval % 10);
229 * If (*grp == CHAR_MAX) then no more grouping
230 * should be performed.
232 if (needgrp && ndig == *grp && *grp != CHAR_MAX &&
237 * If (*(grp+1) == '\0') then we have to
238 * use *grp character (last grouping rule)
241 if (*(grp + 1) != '\0')
250 *--cp = to_char(val & 7);
253 if (octzero && *cp != '0')
259 *--cp = xdigs[(size_t)val & 15];
270 /* Identical to __ultoa, but for intmax_t. */
272 __ujtoa(uintmax_t val, char *endp, int base, int octzero, const char *xdigs,
273 int needgrp, char thousep, const char *grp)
279 /* quick test for small values; __ultoa is typically much faster */
280 /* (perhaps instead we should run until small, then call __ultoa?) */
281 if (val <= ULONG_MAX)
282 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
283 needgrp, thousep, grp));
287 *--cp = to_char(val % 10);
291 if (val > INTMAX_MAX) {
292 *--cp = to_char(val % 10);
298 *--cp = to_char(sval % 10);
301 * If (*grp == CHAR_MAX) then no more grouping
302 * should be performed.
304 if (needgrp && *grp != CHAR_MAX && ndig == *grp &&
309 * If (*(grp+1) == '\0') then we have to
310 * use *grp character (last grouping rule)
313 if (*(grp + 1) != '\0')
322 *--cp = to_char(val & 7);
325 if (octzero && *cp != '0')
331 *--cp = xdigs[(size_t)val & 15];
343 * Convert a wide character string argument for the %ls format to a multibyte
344 * string representation. If not -1, prec specifies the maximum number of
345 * bytes to output, and also means that we can't assume that the wide char.
346 * string ends is null-terminated.
349 __wcsconv(wchar_t *wcsarg, int prec)
351 static const mbstate_t initial;
353 char buf[MB_LEN_MAX];
358 /* Allocate space for the maximum number of bytes we could output. */
362 nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
363 if (nbytes == (size_t)-1)
367 * Optimisation: if the output precision is small enough,
368 * just allocate enough memory for the maximum instead of
369 * scanning the string.
378 clen = wcrtomb(buf, *p++, &mbs);
379 if (clen == 0 || clen == (size_t)-1 ||
380 nbytes + clen > prec)
386 if ((convbuf = malloc(nbytes + 1)) == NULL)
389 /* Fill the output buffer. */
392 if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p,
393 nbytes, &mbs)) == (size_t)-1) {
397 convbuf[nbytes] = '\0';
405 vfprintf(FILE *fp, const char *fmt0, va_list ap)
410 ret = __vfprintf(fp, fmt0, ap);
415 #ifndef NO_FLOATING_POINT
419 #define BUF ((MAXEXPDIG * 2) + MAXFRACT + 1) /* + decimal point */
422 static char *cvt (double, int, int, char *, int *, int, int *, char **);
423 static int exponent (char *, int, int);
425 #else /* no NO_FLOATING_POINT */
429 #endif /* NO_FLOATING_POINT */
431 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
434 * Flags used during conversion.
436 #define ALT 0x001 /* alternate form */
437 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
438 #define LADJUST 0x004 /* left adjustment */
439 #define LONGDBL 0x008 /* long double */
440 #define LONGINT 0x010 /* long integer */
441 #define LLONGINT 0x020 /* long long integer */
442 #define SHORTINT 0x040 /* short integer */
443 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
444 #define FPT 0x100 /* Floating point number */
445 #define GROUPING 0x200 /* use grouping ("'" flag) */
446 /* C99 additional size modifiers: */
447 #define SIZET 0x400 /* size_t */
448 #define PTRDIFFT 0x800 /* ptrdiff_t */
449 #define INTMAXT 0x1000 /* intmax_t */
450 #define CHARINT 0x2000 /* print char using int format */
452 * Non-MT-safe version
455 __vfprintf(FILE *fp, const char *fmt0, va_list ap)
457 char *fmt; /* format string */
458 int ch; /* character from fmt */
459 int n, n2; /* handy integer (short term usage) */
460 char *cp; /* handy char pointer (short term usage) */
461 struct __siov *iovp; /* for PRINT macro */
462 int flags; /* flags as above */
463 int ret; /* return value accumulator */
464 int width; /* width from format (%8d), or 0 */
465 int prec; /* precision from format (%.3d), or -1 */
466 char sign; /* sign prefix (' ', '+', '-', or \0) */
467 char thousands_sep; /* locale specific thousands separator */
468 const char *grouping; /* locale specific numeric grouping rules */
469 #ifndef NO_FLOATING_POINT
470 const char *decimal_point; /* locale specific decimal point */
471 char softsign; /* temporary negative sign for floats */
472 double _double; /* double precision arguments %[eEfgG] */
473 int expt; /* integer value of exponent */
474 int expsize; /* character count for expstr */
475 int ndig; /* actual number of digits returned by cvt */
476 char expstr[7]; /* buffer for exponent string */
477 char *dtoaresult; /* buffer allocated by dtoa */
479 u_long ulval; /* integer arguments %[diouxX] */
480 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
481 int base; /* base for [diouxX] conversion */
482 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
483 int realsz; /* field size expanded by dprec, sign, etc */
484 int size; /* size of converted field or string */
485 int prsize; /* max size of printed field */
486 const char *xdigs; /* digits for [xX] conversion */
488 struct __suio uio; /* output information: summary */
489 struct __siov iov[NIOV];/* ... and individual io vectors */
490 char buf[BUF]; /* space for %c, %[diouxX], %[eEfFgG] */
491 char ox[2]; /* space for 0x hex-prefix */
492 union arg *argtable; /* args, built due to positional arg */
493 union arg statargtable[STATIC_ARG_TBL_SIZE];
494 int nextarg; /* 1-based argument index */
495 va_list orgap; /* original argument pointer */
496 char *convbuf; /* wide to multibyte conversion result */
505 * Choose PADSIZE to trade efficiency vs. size. If larger printf
506 * fields occur frequently, increase PADSIZE and make the initialisers
509 #define PADSIZE 16 /* pad chunk size */
510 static char blanks[PADSIZE] =
511 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
512 static char zeroes[PADSIZE] =
513 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
516 * BEWARE, these `goto error' on error, and PAD uses `n'.
518 #define PRINT(ptr, len) { \
519 iovp->iov_base = __DECONST(void *, ptr); \
520 iovp->iov_len = (len); \
521 uio.uio_resid += (len); \
523 if (++uio.uio_iovcnt >= NIOV) { \
524 if (__sprint(fp, &uio)) \
529 #define PAD(howmany, with) { \
530 if ((n = (howmany)) > 0) { \
531 while (n > PADSIZE) { \
532 PRINT(with, PADSIZE); \
539 if (uio.uio_resid && __sprint(fp, &uio)) \
541 uio.uio_iovcnt = 0; \
546 * Get the argument indexed by nextarg. If the argument table is
547 * built, use it to get the argument. If its not, get the next
548 * argument (and arguments must be gotten sequentially).
550 #define GETARG(type) \
551 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
552 (nextarg++, va_arg(ap, type)))
555 * To extend shorts properly, we need both signed and unsigned
556 * argument extraction methods.
559 (flags&LONGINT ? GETARG(long) : \
560 flags&SHORTINT ? (long)(short)GETARG(int) : \
561 flags&CHARINT ? (long)(signed char)GETARG(int) : \
564 (flags&LONGINT ? GETARG(u_long) : \
565 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
566 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
567 (u_long)GETARG(u_int))
568 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
570 (flags&INTMAXT ? GETARG(intmax_t) : \
571 flags&SIZET ? (intmax_t)GETARG(size_t) : \
572 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
573 (intmax_t)GETARG(long long))
575 (flags&INTMAXT ? GETARG(uintmax_t) : \
576 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
577 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
578 (uintmax_t)GETARG(unsigned long long))
581 * Get * arguments, including the form *nn$. Preserve the nextarg
582 * that the argument can be gotten once the type is determined.
584 #define GETASTER(val) \
587 while (is_digit(*cp)) { \
588 n2 = 10 * n2 + to_digit(*cp); \
592 int hold = nextarg; \
593 if (argtable == NULL) { \
594 argtable = statargtable; \
595 __find_arguments (fmt0, orgap, &argtable); \
598 val = GETARG (int); \
602 val = GETARG (int); \
606 thousands_sep = '\0';
609 #ifndef NO_FLOATING_POINT
611 decimal_point = localeconv()->decimal_point;
613 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
618 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
619 if ((fp->pub._flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
620 fp->pub._fileno >= 0) {
621 return (__sbprintf(fp, fmt0, ap));
628 uio.uio_iov = iovp = iov;
634 * Scan the format for conversions (`%' character).
637 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
639 if ((n = fmt - cp) != 0) {
640 if ((unsigned)ret + n > INT_MAX) {
649 fmt++; /* skip over '%' */
658 reswitch: switch (ch) {
661 * ``If the space and + flags both appear, the space
662 * flag will be ignored.''
673 * ``A negative field width argument is taken as a
674 * - flag followed by a positive field width.''
676 * They don't exclude field widths read from args.
691 thousands_sep = *(localeconv()->thousands_sep);
692 grouping = localeconv()->grouping;
695 if ((ch = *fmt++) == '*') {
697 prec = n < 0 ? -1 : n;
701 while (is_digit(ch)) {
702 n = 10 * n + to_digit(ch);
705 prec = n < 0 ? -1 : n;
709 * ``Note that 0 is taken as a flag, not as the
710 * beginning of a field width.''
715 case '1': case '2': case '3': case '4':
716 case '5': case '6': case '7': case '8': case '9':
719 n = 10 * n + to_digit(ch);
721 } while (is_digit(ch));
724 if (argtable == NULL) {
725 argtable = statargtable;
726 __find_arguments (fmt0, orgap,
733 #ifndef NO_FLOATING_POINT
739 if (flags & SHORTINT) {
749 if (flags & LONGINT) {
768 if (flags & LONGINT) {
769 static const mbstate_t initial;
774 mbseqlen = wcrtomb(cp = buf,
775 (wchar_t)GETARG(wint_t), &mbs);
776 if (mbseqlen == (size_t)-1) {
777 fp->pub._flags |= __SERR;
780 size = (int)mbseqlen;
782 *(cp = buf) = GETARG(int);
792 if (flags & INTMAX_SIZE) {
794 if ((intmax_t)ujval < 0) {
800 if ((long)ulval < 0) {
807 #ifndef NO_FLOATING_POINT
815 * Grouping apply to %i, %d, %u, %f, %F, %g, %G
816 * conversion specifiers only. For other conversions
817 * behavior is undefined.
829 fp_begin: if (prec == -1)
832 /* XXX this loses precision. */
833 _double = (double)GETARG(long double);
835 _double = GETARG(double);
836 /* do this before tricky precision changes */
837 if (isinf(_double)) {
847 if (isnan(_double)) {
856 if (dtoaresult != NULL) {
860 cp = cvt(_double, prec, flags, &softsign,
861 &expt, ch, &ndig, &dtoaresult);
862 if (ch == 'g' || ch == 'G') {
863 if (expt <= -4 || expt > prec)
864 ch = (ch == 'g') ? 'e' : 'E';
868 if (ch == 'e' || ch == 'E') {
870 expsize = exponent(expstr, expt, ch);
871 size = expsize + ndig;
872 if (ndig > 1 || flags & ALT)
874 } else if (ch == 'f' || ch == 'F') {
877 if (prec || flags & ALT)
881 } else if (expt >= ndig) { /* fixed g fmt */
886 size = ndig + (expt > 0 ?
892 #endif /* NO_FLOATING_POINT */
895 * Assignment-like behavior is specified if the
896 * value overflows or is otherwise unrepresentable.
897 * C99 says to use `signed char' for %hhn conversions.
899 if (flags & LLONGINT)
900 *GETARG(long long *) = ret;
901 else if (flags & SIZET)
902 *GETARG(ssize_t *) = (ssize_t)ret;
903 else if (flags & PTRDIFFT)
904 *GETARG(ptrdiff_t *) = ret;
905 else if (flags & INTMAXT)
906 *GETARG(intmax_t *) = ret;
907 else if (flags & LONGINT)
908 *GETARG(long *) = ret;
909 else if (flags & SHORTINT)
910 *GETARG(short *) = ret;
911 else if (flags & CHARINT)
912 *GETARG(signed char *) = ret;
914 *GETARG(int *) = ret;
915 continue; /* no output */
920 if (flags & INTMAX_SIZE)
928 * ``The argument shall be a pointer to void. The
929 * value of the pointer is converted to a sequence
930 * of printable characters, in an implementation-
934 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
936 xdigs = "0123456789abcdef";
937 flags = flags | INTMAXT | HEXPREFIX;
944 if (flags & LONGINT) {
949 if ((wcp = GETARG(wchar_t *)) == NULL)
952 convbuf = __wcsconv(wcp, prec);
953 if (convbuf == NULL) {
954 fp->pub._flags |= __SERR;
959 } else if ((cp = GETARG(char *)) == NULL)
963 * can't use strlen; can only look for the
964 * NUL in the first `prec' characters, and
965 * strlen() will go further.
967 char *p = memchr(cp, 0, (size_t)prec);
983 if (flags & INTMAX_SIZE)
990 xdigs = "0123456789ABCDEF";
993 xdigs = "0123456789abcdef";
995 if (flags & INTMAX_SIZE)
1000 /* leading 0x/X only if non-zero */
1002 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1006 /* unsigned conversions */
1007 nosign: sign = '\0';
1009 * ``... diouXx conversions ... if a precision is
1010 * specified, the 0 flag will be ignored.''
1013 number: if ((dprec = prec) >= 0)
1017 * ``The result of converting a zero value with an
1018 * explicit precision of zero is no characters.''
1022 if (flags & INTMAX_SIZE) {
1023 if (ujval != 0 || prec != 0)
1024 cp = __ujtoa(ujval, cp, base,
1026 flags & GROUPING, thousands_sep,
1029 if (ulval != 0 || prec != 0)
1030 cp = __ultoa(ulval, cp, base,
1032 flags & GROUPING, thousands_sep,
1035 size = buf + BUF - cp;
1037 default: /* "%?" prints ?, unless ? is NUL */
1040 /* pretend it was %c with argument ch */
1049 * All reasonable formats wind up here. At this point, `cp'
1050 * points to a string which (if not flags&LADJUST) should be
1051 * padded out to `width' places. If flags&ZEROPAD, it should
1052 * first be prefixed by any sign or other prefix; otherwise,
1053 * it should be blank padded before the prefix is emitted.
1054 * After any left-hand padding and prefixing, emit zeroes
1055 * required by a decimal [diouxX] precision, then print the
1056 * string proper, then emit zeroes required by any leftover
1057 * floating precision; finally, if LADJUST, pad with blanks.
1059 * Compute actual size, so we know how much to pad.
1060 * size excludes decimal prec; realsz includes it.
1062 realsz = dprec > size ? dprec : size;
1065 else if (flags & HEXPREFIX)
1068 prsize = width > realsz ? width : realsz;
1069 if ((unsigned)ret + prsize > INT_MAX) {
1074 /* right-adjusting blank padding */
1075 if ((flags & (LADJUST|ZEROPAD)) == 0)
1076 PAD(width - realsz, blanks);
1081 } else if (flags & HEXPREFIX) {
1087 /* right-adjusting zero padding */
1088 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1089 PAD(width - realsz, zeroes);
1091 /* leading zeroes from decimal precision */
1092 PAD(dprec - size, zeroes);
1094 /* the string or number proper */
1095 #ifndef NO_FLOATING_POINT
1096 if ((flags & FPT) == 0) {
1098 } else { /* glue together f_p fragments */
1099 if (ch >= 'f') { /* 'f' or 'g' */
1101 /* kludge for __dtoa irregularity */
1103 if (expt < ndig || (flags & ALT) != 0) {
1104 PRINT(decimal_point, 1);
1105 PAD(ndig - 1, zeroes);
1107 } else if (expt <= 0) {
1109 PRINT(decimal_point, 1);
1112 } else if (expt >= ndig) {
1114 PAD(expt - ndig, zeroes);
1116 PRINT(decimal_point, 1);
1120 PRINT(decimal_point, 1);
1121 PRINT(cp, ndig-expt);
1123 } else { /* 'e' or 'E' */
1124 if (ndig > 1 || flags & ALT) {
1126 ox[1] = *decimal_point;
1130 } else /* 0.[0..] */
1131 /* __dtoa irregularity */
1132 PAD(ndig - 1, zeroes);
1135 PRINT(expstr, expsize);
1141 /* left-adjusting padding (always blank) */
1142 if (flags & LADJUST)
1143 PAD(width - realsz, blanks);
1145 /* finally, adjust ret */
1148 FLUSH(); /* copy out the I/O vectors */
1153 #ifndef NO_FLOATING_POINT
1154 if (dtoaresult != NULL)
1157 if (convbuf != NULL)
1161 if ((argtable != NULL) && (argtable != statargtable))
1168 * Find all arguments when a positional parameter is encountered. Returns a
1169 * table, indexed by argument number, of pointers to each arguments. The
1170 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1171 * It will be replaces with a malloc-ed one if it overflows.
1174 __find_arguments (const char *fmt0, va_list ap, union arg **argtable)
1176 char *fmt; /* format string */
1177 int ch; /* character from fmt */
1178 int n, n2; /* handy integer (short term usage) */
1179 char *cp; /* handy char pointer (short term usage) */
1180 int flags; /* flags as above */
1181 int width; /* width from format (%8d), or 0 */
1182 enum typeid *typetable; /* table of types */
1183 enum typeid stattypetable[STATIC_ARG_TBL_SIZE];
1184 int tablesize; /* current size of type table */
1185 int tablemax; /* largest used index in table */
1186 int nextarg; /* 1-based argument index */
1189 * Add an argument type to the table, expanding if necessary.
1191 #define ADDTYPE(type) \
1192 ((nextarg >= tablesize) ? \
1193 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1194 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1195 typetable[nextarg++] = type)
1198 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1199 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1200 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1201 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1202 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1205 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1206 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1207 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1208 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1209 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1212 * Add * arguments to the type array.
1214 #define ADDASTER() \
1217 while (is_digit(*cp)) { \
1218 n2 = 10 * n2 + to_digit(*cp); \
1222 int hold = nextarg; \
1231 typetable = stattypetable;
1232 tablesize = STATIC_ARG_TBL_SIZE;
1235 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1238 * Scan the format for conversions (`%' character).
1241 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1245 fmt++; /* skip over '%' */
1251 reswitch: switch (ch) {
1263 if ((ch = *fmt++) == '*') {
1267 while (is_digit(ch)) {
1273 case '1': case '2': case '3': case '4':
1274 case '5': case '6': case '7': case '8': case '9':
1277 n = 10 * n + to_digit(ch);
1279 } while (is_digit(ch));
1286 #ifndef NO_FLOATING_POINT
1292 if (flags & SHORTINT) {
1302 if (flags & LONGINT) {
1321 if (flags & LONGINT)
1333 #ifndef NO_FLOATING_POINT
1343 if (flags & LONGDBL)
1344 ADDTYPE(T_LONG_DOUBLE);
1348 #endif /* NO_FLOATING_POINT */
1350 if (flags & INTMAXT)
1351 ADDTYPE(TP_INTMAXT);
1352 else if (flags & PTRDIFFT)
1353 ADDTYPE(TP_PTRDIFFT);
1354 else if (flags & SIZET)
1356 else if (flags & LLONGINT)
1358 else if (flags & LONGINT)
1360 else if (flags & SHORTINT)
1362 else if (flags & CHARINT)
1366 continue; /* no output */
1380 if (flags & LONGINT)
1393 default: /* "%?" prints ?, unless ? is NUL */
1401 * Build the argument table.
1403 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1404 *argtable = (union arg *)
1405 malloc(sizeof(union arg) * (tablemax + 1));
1407 (*argtable)[0].intarg = 0;
1408 for (n = 1; n <= tablemax; n++) {
1409 switch (typetable [n]) {
1410 case T_UNUSED: /* "whoops" */
1412 (*argtable)[n].intarg = va_arg(ap, int);
1415 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1418 (*argtable)[n].pshortarg = va_arg(ap, short *);
1421 (*argtable)[n].uintarg = va_arg(ap, unsigned int);
1424 (*argtable)[n].pintarg = va_arg(ap, int *);
1427 (*argtable)[n].longarg = va_arg(ap, long);
1430 (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
1433 (*argtable)[n].plongarg = va_arg(ap, long *);
1436 (*argtable)[n].longlongarg = va_arg(ap, long long);
1439 (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
1442 (*argtable)[n].plonglongarg = va_arg(ap, long long *);
1445 (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
1448 (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
1451 (*argtable)[n].sizearg = va_arg(ap, size_t);
1454 (*argtable)[n].psizearg = va_arg(ap, ssize_t *);
1457 (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
1460 (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
1463 (*argtable)[n].pintmaxarg = va_arg (ap, intmax_t *);
1466 (*argtable)[n].doublearg = va_arg(ap, double);
1469 (*argtable)[n].longdoublearg = va_arg(ap, long double);
1472 (*argtable)[n].pchararg = va_arg(ap, char *);
1475 (*argtable)[n].pvoidarg = va_arg(ap, void *);
1478 (*argtable)[n].wintarg = va_arg(ap, wint_t);
1481 (*argtable)[n].pwchararg = va_arg(ap, wchar_t *);
1486 if ((typetable != NULL) && (typetable != stattypetable))
1491 * Increase the size of the type table.
1494 __grow_type_table(int nextarg, enum typeid **typetable, int *tablesize)
1496 enum typeid * const oldtable = *typetable;
1497 const int oldsize = *tablesize;
1498 enum typeid *newtable;
1499 int n, newsize = oldsize * 2;
1501 if (newsize < nextarg + 1)
1502 newsize = nextarg + 1;
1503 if (oldsize == STATIC_ARG_TBL_SIZE) {
1504 if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL)
1505 abort(); /* XXX handle better */
1506 bcopy(oldtable, newtable, oldsize * sizeof(enum typeid));
1508 if ((newtable = reallocf(oldtable, newsize * sizeof(enum typeid))) == NULL)
1509 abort(); /* XXX handle better */
1511 for (n = oldsize; n < newsize; n++)
1512 newtable[n] = T_UNUSED;
1514 *typetable = newtable;
1515 *tablesize = newsize;
1519 #ifndef NO_FLOATING_POINT
1521 extern char *__dtoa (double, int, int, int *, int *, char **, char **);
1524 cvt(double value, int ndigits, int flags, char *sign, int *decpt,
1525 int ch, int *length, char **dtoaresultp)
1528 char *digits, *bp, *rve;
1531 mode = 3; /* ndigits after the decimal point */
1534 * To obtain ndigits after the decimal point for the 'e'
1535 * and 'E' formats, round to ndigits + 1 significant
1538 if (ch == 'e' || ch == 'E')
1540 mode = 2; /* ndigits significant digits */
1542 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve, dtoaresultp);
1544 if ((ch != 'g' && ch != 'G') || flags & ALT) {
1545 /* print trailing zeros */
1546 bp = digits + ndigits;
1548 if (*digits == '0' && value)
1549 *decpt = -ndigits + 1;
1552 if (value == 0) /* kludge for __dtoa irregularity */
1557 *length = rve - digits;
1562 exponent(char *p0, int expo, int fmtch)
1565 char expbuf[MAXEXPDIG];
1575 t = expbuf + MAXEXPDIG;
1578 *--t = to_char(expo % 10);
1579 } while ((expo /= 10) > 9);
1580 *--t = to_char(expo);
1581 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1585 *p++ = to_char(expo);
1589 #endif /* NO_FLOATING_POINT */