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
37 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
41 static const char rcsid[] =
43 #endif /* LIBC_SCCS and not lint */
46 * Actual printf innards.
48 * This code is large and complicated...
51 #include "namespace.h"
52 #include <sys/types.h>
68 #include "un-namespace.h"
70 #include "libc_private.h"
74 /* Define FLOATING_POINT to get floating point. */
75 #define FLOATING_POINT
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 __P((FILE *, struct __suio *));
116 static int __sbprintf __P((FILE *, const char *, va_list)) __printflike(2, 0);
117 static char * __ujtoa __P((uintmax_t, char *, int, int, char *, int, char, const char *));
118 static char * __ultoa __P((u_long, char *, int, int, char *, int, char, const char *));
119 static void __find_arguments __P((const char *, va_list, union arg **));
120 static void __grow_type_table __P((int, enum typeid **, int *));
123 * Flush out all the vectors defined by the given uio,
124 * then reset it so that it can be reused.
127 __sprint(FILE *fp, struct __suio *uio)
131 if (uio->uio_resid == 0) {
135 err = __sfvwrite(fp, uio);
142 * Helper function for `fprintf to unbuffered unix file': creates a
143 * temporary buffer. We only work on write-only files; this avoids
144 * worries about ungetc buffers and so forth.
147 __sbprintf(FILE *fp, const char *fmt, va_list ap)
151 unsigned char buf[BUFSIZ];
153 /* copy the important variables */
154 fake._flags = fp->_flags & ~__SNBF;
155 fake._file = fp->_file;
156 fake._cookie = fp->_cookie;
157 fake._write = fp->_write;
159 /* set up the buffer */
160 fake._bf._base = fake._p = buf;
161 fake._bf._size = fake._w = sizeof(buf);
162 fake._lbfsize = 0; /* not actually used, but Just In Case */
164 /* do the work, then copy any error status */
165 ret = __vfprintf(&fake, fmt, ap);
166 if (ret >= 0 && __fflush(&fake))
168 if (fake._flags & __SERR)
169 fp->_flags |= __SERR;
174 * Macros for converting digits to letters and vice versa
176 #define to_digit(c) ((c) - '0')
177 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
178 #define to_char(n) ((n) + '0')
181 * Convert an unsigned long to ASCII for printf purposes, returning
182 * a pointer to the first character of the string representation.
183 * Octal numbers can be forced to have a leading zero; hex numbers
184 * use the given digits.
187 __ultoa(u_long val, char *endp, int base, int octzero, char *xdigs,
188 int needgrp, char thousep, const char *grp)
190 register char *cp = endp;
195 * Handle the three cases separately, in the hope of getting
196 * better/faster code.
200 if (val < 10) { /* many numbers are 1 digit */
201 *--cp = to_char(val);
206 * On many machines, unsigned arithmetic is harder than
207 * signed arithmetic, so we do at most one unsigned mod and
208 * divide; this is sufficient to reduce the range of
209 * the incoming value to where signed arithmetic works.
211 if (val > LONG_MAX) {
212 *--cp = to_char(val % 10);
218 *--cp = to_char(sval % 10);
221 * If (*grp == CHAR_MAX) then no more grouping
222 * should be performed.
224 if (needgrp && ndig == *grp && *grp != CHAR_MAX
229 * If (*(grp+1) == '\0') then we have to
230 * use *grp character (last grouping rule)
233 if (*(grp+1) != '\0') grp++;
241 *--cp = to_char(val & 7);
244 if (octzero && *cp != '0')
250 *--cp = xdigs[val & 15];
261 /* Identical to __ultoa, but for intmax_t. */
263 __ujtoa(uintmax_t val, char *endp, int base, int octzero, char *xdigs,
264 int needgrp, char thousep, const char *grp)
270 /* quick test for small values; __ultoa is typically much faster */
271 /* (perhaps instead we should run until small, then call __ultoa?) */
272 if (val <= ULONG_MAX)
273 return (__ultoa((u_long)val, endp, base, octzero, xdigs,
274 needgrp, thousep, grp));
278 *--cp = to_char(val % 10);
282 if (val > INTMAX_MAX) {
283 *--cp = to_char(val % 10);
289 *--cp = to_char(sval % 10);
292 * If (*grp == CHAR_MAX) then no more grouping
293 * should be performed.
295 if (needgrp && *grp != CHAR_MAX && ndig == *grp
300 * If (*(grp+1) == '\0') then we have to
301 * use *grp character (last grouping rule)
304 if (*(grp+1) != '\0') grp++;
312 *--cp = to_char(val & 7);
315 if (octzero && *cp != '0')
321 *--cp = xdigs[val & 15];
336 vfprintf(FILE *fp, const char *fmt0, va_list ap)
341 ret = __vfprintf(fp, fmt0, ap);
346 #ifdef FLOATING_POINT
350 #define BUF ((MAXEXP*2)+MAXFRACT+1) /* + decimal point */
353 static char *cvt __P((double, int, int, char *, int *, int, int *, char **));
354 static int exponent __P((char *, int, int));
356 #else /* no FLOATING_POINT */
360 #endif /* FLOATING_POINT */
362 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
365 * Flags used during conversion.
367 #define ALT 0x001 /* alternate form */
368 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */
369 #define LADJUST 0x004 /* left adjustment */
370 #define LONGDBL 0x008 /* long double */
371 #define LONGINT 0x010 /* long integer */
372 #define LLONGINT 0x020 /* long long integer */
373 #define SHORTINT 0x040 /* short integer */
374 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
375 #define FPT 0x100 /* Floating point number */
376 #define GROUPING 0x200 /* use grouping ("'" flag) */
377 /* C99 additional size modifiers: */
378 #define SIZET 0x400 /* size_t */
379 #define PTRDIFFT 0x800 /* ptrdiff_t */
380 #define INTMAXT 0x1000 /* intmax_t */
381 #define CHARINT 0x2000 /* print char using int format */
384 * Non-MT-safe version
387 __vfprintf(FILE *fp, const char *fmt0, va_list ap)
389 char *fmt; /* format string */
390 int ch; /* character from fmt */
391 int n, n2; /* handy integer (short term usage) */
392 char *cp; /* handy char pointer (short term usage) */
393 struct __siov *iovp; /* for PRINT macro */
394 int flags; /* flags as above */
395 int ret; /* return value accumulator */
396 int width; /* width from format (%8d), or 0 */
397 int prec; /* precision from format (%.3d), or -1 */
398 char sign; /* sign prefix (' ', '+', '-', or \0) */
399 char thousands_sep; /* locale specific thousands separator */
400 const char *grouping; /* locale specific numeric grouping rules */
401 #ifdef FLOATING_POINT
402 char *decimal_point; /* locale specific decimal point */
403 char softsign; /* temporary negative sign for floats */
404 double _double; /* double precision arguments %[eEfgG] */
405 int expt; /* integer value of exponent */
406 int expsize; /* character count for expstr */
407 int ndig; /* actual number of digits returned by cvt */
408 char expstr[7]; /* buffer for exponent string */
409 char *dtoaresult; /* buffer allocated by dtoa */
411 u_long ulval; /* integer arguments %[diouxX] */
412 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
413 int base; /* base for [diouxX] conversion */
414 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
415 int realsz; /* field size expanded by dprec, sign, etc */
416 int size; /* size of converted field or string */
417 int prsize; /* max size of printed field */
418 char *xdigs; /* digits for [xX] conversion */
420 struct __suio uio; /* output information: summary */
421 struct __siov iov[NIOV];/* ... and individual io vectors */
422 char buf[BUF]; /* space for %c, %[diouxX], %[eEfFgG] */
423 char ox[2]; /* space for 0x hex-prefix */
424 union arg *argtable; /* args, built due to positional arg */
425 union arg statargtable [STATIC_ARG_TBL_SIZE];
426 int nextarg; /* 1-based argument index */
427 va_list orgap; /* original argument pointer */
430 * Choose PADSIZE to trade efficiency vs. size. If larger printf
431 * fields occur frequently, increase PADSIZE and make the initialisers
434 #define PADSIZE 16 /* pad chunk size */
435 static char blanks[PADSIZE] =
436 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
437 static char zeroes[PADSIZE] =
438 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
441 * BEWARE, these `goto error' on error, and PAD uses `n'.
443 #define PRINT(ptr, len) { \
444 iovp->iov_base = (ptr); \
445 iovp->iov_len = (len); \
446 uio.uio_resid += (len); \
448 if (++uio.uio_iovcnt >= NIOV) { \
449 if (__sprint(fp, &uio)) \
454 #define PAD(howmany, with) { \
455 if ((n = (howmany)) > 0) { \
456 while (n > PADSIZE) { \
457 PRINT(with, PADSIZE); \
464 if (uio.uio_resid && __sprint(fp, &uio)) \
466 uio.uio_iovcnt = 0; \
471 * Get the argument indexed by nextarg. If the argument table is
472 * built, use it to get the argument. If its not, get the next
473 * argument (and arguments must be gotten sequentially).
475 #define GETARG(type) \
476 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
477 (nextarg++, va_arg(ap, type)))
480 * To extend shorts properly, we need both signed and unsigned
481 * argument extraction methods.
484 (flags&LONGINT ? GETARG(long) : \
485 flags&SHORTINT ? (long)(short)GETARG(int) : \
486 flags&CHARINT ? (long)(signed char)GETARG(int) : \
489 (flags&LONGINT ? GETARG(u_long) : \
490 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
491 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
492 (u_long)GETARG(u_int))
493 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
495 (flags&INTMAXT ? GETARG(intmax_t) : \
496 flags&SIZET ? (intmax_t)GETARG(size_t) : \
497 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
498 (intmax_t)GETARG(long long))
500 (flags&INTMAXT ? GETARG(uintmax_t) : \
501 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
502 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
503 (uintmax_t)GETARG(unsigned long long))
506 * Get * arguments, including the form *nn$. Preserve the nextarg
507 * that the argument can be gotten once the type is determined.
509 #define GETASTER(val) \
512 while (is_digit(*cp)) { \
513 n2 = 10 * n2 + to_digit(*cp); \
517 int hold = nextarg; \
518 if (argtable == NULL) { \
519 argtable = statargtable; \
520 __find_arguments (fmt0, orgap, &argtable); \
523 val = GETARG (int); \
527 val = GETARG (int); \
531 thousands_sep = '\0';
533 #ifdef FLOATING_POINT
535 decimal_point = localeconv()->decimal_point;
537 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
541 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
542 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
544 return (__sbprintf(fp, fmt0, ap));
550 uio.uio_iov = iovp = iov;
556 * Scan the format for conversions (`%' character).
559 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
561 if ((n = fmt - cp) != 0) {
562 if ((unsigned)ret + n > INT_MAX) {
571 fmt++; /* skip over '%' */
580 reswitch: switch (ch) {
583 * ``If the space and + flags both appear, the space
584 * flag will be ignored.''
595 * ``A negative field width argument is taken as a
596 * - flag followed by a positive field width.''
598 * They don't exclude field widths read from args.
613 thousands_sep = *(localeconv()->thousands_sep);
614 grouping = localeconv()->grouping;
617 if ((ch = *fmt++) == '*') {
619 prec = n < 0 ? -1 : n;
623 while (is_digit(ch)) {
624 n = 10 * n + to_digit(ch);
627 prec = n < 0 ? -1 : n;
631 * ``Note that 0 is taken as a flag, not as the
632 * beginning of a field width.''
637 case '1': case '2': case '3': case '4':
638 case '5': case '6': case '7': case '8': case '9':
641 n = 10 * n + to_digit(ch);
643 } while (is_digit(ch));
646 if (argtable == NULL) {
647 argtable = statargtable;
648 __find_arguments (fmt0, orgap,
655 #ifdef FLOATING_POINT
661 if (flags & SHORTINT) {
671 if (flags & LONGINT) {
678 flags |= LLONGINT; /* not necessarily */
687 *(cp = buf) = GETARG(int);
696 if (flags & INTMAX_SIZE) {
698 if ((intmax_t)ujval < 0) {
704 if ((long)ulval < 0) {
711 #ifdef FLOATING_POINT
719 * Grouping apply to %i, %d, %u, %f, %F, %g, %G
720 * conversion specifiers only. For other conversions
721 * behavior is undefined.
733 fp_begin: if (prec == -1)
736 /* XXX this loses precision. */
737 _double = (double)GETARG(long double);
739 _double = GETARG(double);
740 /* do this before tricky precision changes */
741 if (isinf(_double)) {
751 if (isnan(_double)) {
760 if (dtoaresult != NULL) {
764 cp = cvt(_double, prec, flags, &softsign,
765 &expt, ch, &ndig, &dtoaresult);
766 if (ch == 'g' || ch == 'G') {
767 if (expt <= -4 || expt > prec)
768 ch = (ch == 'g') ? 'e' : 'E';
772 if (ch == 'e' || ch == 'E') {
774 expsize = exponent(expstr, expt, ch);
775 size = expsize + ndig;
776 if (ndig > 1 || flags & ALT)
778 } else if (ch == 'f' || ch == 'F') {
781 if (prec || flags & ALT)
785 } else if (expt >= ndig) { /* fixed g fmt */
790 size = ndig + (expt > 0 ?
796 #endif /* FLOATING_POINT */
799 * Assignment-like behavior is specified if the
800 * value overflows or is otherwise unrepresentable.
801 * C99 says to use `signed char' for %hhn conversions.
803 if (flags & LLONGINT)
804 *GETARG(long long *) = ret;
805 else if (flags & SIZET)
806 *GETARG(ssize_t *) = (ssize_t)ret;
807 else if (flags & PTRDIFFT)
808 *GETARG(ptrdiff_t *) = ret;
809 else if (flags & INTMAXT)
810 *GETARG(intmax_t *) = ret;
811 else if (flags & LONGINT)
812 *GETARG(long *) = ret;
813 else if (flags & SHORTINT)
814 *GETARG(short *) = ret;
815 else if (flags & CHARINT)
816 *GETARG(signed char *) = ret;
818 *GETARG(int *) = ret;
819 continue; /* no output */
824 if (flags & INTMAX_SIZE)
832 * ``The argument shall be a pointer to void. The
833 * value of the pointer is converted to a sequence
834 * of printable characters, in an implementation-
838 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
840 xdigs = "0123456789abcdef";
841 flags = flags | INTMAXT | HEXPREFIX;
845 if ((cp = GETARG(char *)) == NULL)
849 * can't use strlen; can only look for the
850 * NUL in the first `prec' characters, and
851 * strlen() will go further.
853 char *p = memchr(cp, 0, (size_t)prec);
869 if (flags & INTMAX_SIZE)
876 xdigs = "0123456789ABCDEF";
879 xdigs = "0123456789abcdef";
881 if (flags & INTMAX_SIZE)
886 /* leading 0x/X only if non-zero */
888 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
893 /* unsigned conversions */
896 * ``... diouXx conversions ... if a precision is
897 * specified, the 0 flag will be ignored.''
900 number: if ((dprec = prec) >= 0)
904 * ``The result of converting a zero value with an
905 * explicit precision of zero is no characters.''
909 if (flags & INTMAX_SIZE) {
910 if (ujval != 0 || prec != 0)
911 cp = __ujtoa(ujval, cp, base,
913 flags & GROUPING, thousands_sep,
916 if (ulval != 0 || prec != 0)
917 cp = __ultoa(ulval, cp, base,
919 flags & GROUPING, thousands_sep,
922 size = buf + BUF - cp;
924 default: /* "%?" prints ?, unless ? is NUL */
927 /* pretend it was %c with argument ch */
936 * All reasonable formats wind up here. At this point, `cp'
937 * points to a string which (if not flags&LADJUST) should be
938 * padded out to `width' places. If flags&ZEROPAD, it should
939 * first be prefixed by any sign or other prefix; otherwise,
940 * it should be blank padded before the prefix is emitted.
941 * After any left-hand padding and prefixing, emit zeroes
942 * required by a decimal [diouxX] precision, then print the
943 * string proper, then emit zeroes required by any leftover
944 * floating precision; finally, if LADJUST, pad with blanks.
946 * Compute actual size, so we know how much to pad.
947 * size excludes decimal prec; realsz includes it.
949 realsz = dprec > size ? dprec : size;
952 else if (flags & HEXPREFIX)
955 prsize = width > realsz ? width : realsz;
956 if ((unsigned)ret + prsize > INT_MAX) {
961 /* right-adjusting blank padding */
962 if ((flags & (LADJUST|ZEROPAD)) == 0)
963 PAD(width - realsz, blanks);
968 } else if (flags & HEXPREFIX) {
974 /* right-adjusting zero padding */
975 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
976 PAD(width - realsz, zeroes);
978 /* leading zeroes from decimal precision */
979 PAD(dprec - size, zeroes);
981 /* the string or number proper */
982 #ifdef FLOATING_POINT
983 if ((flags & FPT) == 0) {
985 } else { /* glue together f_p fragments */
986 if (ch >= 'f') { /* 'f' or 'g' */
988 /* kludge for __dtoa irregularity */
990 if (expt < ndig || (flags & ALT) != 0) {
991 PRINT(decimal_point, 1);
992 PAD(ndig - 1, zeroes);
994 } else if (expt <= 0) {
996 PRINT(decimal_point, 1);
999 } else if (expt >= ndig) {
1001 PAD(expt - ndig, zeroes);
1003 PRINT(decimal_point, 1);
1007 PRINT(decimal_point, 1);
1008 PRINT(cp, ndig-expt);
1010 } else { /* 'e' or 'E' */
1011 if (ndig > 1 || flags & ALT) {
1013 ox[1] = *decimal_point;
1017 } else /* 0.[0..] */
1018 /* __dtoa irregularity */
1019 PAD(ndig - 1, zeroes);
1022 PRINT(expstr, expsize);
1028 /* left-adjusting padding (always blank) */
1029 if (flags & LADJUST)
1030 PAD(width - realsz, blanks);
1032 /* finally, adjust ret */
1035 FLUSH(); /* copy out the I/O vectors */
1040 #ifdef FLOATING_POINT
1041 if (dtoaresult != NULL)
1046 if ((argtable != NULL) && (argtable != statargtable))
1053 * Find all arguments when a positional parameter is encountered. Returns a
1054 * table, indexed by argument number, of pointers to each arguments. The
1055 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1056 * It will be replaces with a malloc-ed one if it overflows.
1059 __find_arguments (const char *fmt0, va_list ap, union arg **argtable)
1061 char *fmt; /* format string */
1062 int ch; /* character from fmt */
1063 int n, n2; /* handy integer (short term usage) */
1064 char *cp; /* handy char pointer (short term usage) */
1065 int flags; /* flags as above */
1066 int width; /* width from format (%8d), or 0 */
1067 enum typeid *typetable; /* table of types */
1068 enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1069 int tablesize; /* current size of type table */
1070 int tablemax; /* largest used index in table */
1071 int nextarg; /* 1-based argument index */
1074 * Add an argument type to the table, expanding if necessary.
1076 #define ADDTYPE(type) \
1077 ((nextarg >= tablesize) ? \
1078 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1079 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1080 typetable[nextarg++] = type)
1083 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1084 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1085 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1086 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1087 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1090 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1091 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1092 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1093 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1094 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1097 * Add * arguments to the type array.
1099 #define ADDASTER() \
1102 while (is_digit(*cp)) { \
1103 n2 = 10 * n2 + to_digit(*cp); \
1107 int hold = nextarg; \
1116 typetable = stattypetable;
1117 tablesize = STATIC_ARG_TBL_SIZE;
1120 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
1123 * Scan the format for conversions (`%' character).
1126 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1130 fmt++; /* skip over '%' */
1136 reswitch: switch (ch) {
1148 if ((ch = *fmt++) == '*') {
1152 while (is_digit(ch)) {
1158 case '1': case '2': case '3': case '4':
1159 case '5': case '6': case '7': case '8': case '9':
1162 n = 10 * n + to_digit(ch);
1164 } while (is_digit(ch));
1171 #ifdef FLOATING_POINT
1177 if (flags & SHORTINT) {
1187 if (flags & LONGINT) {
1194 flags |= LLONGINT; /* not necessarily */
1212 #ifdef FLOATING_POINT
1222 if (flags & LONGDBL)
1223 ADDTYPE(T_LONG_DOUBLE);
1227 #endif /* FLOATING_POINT */
1229 if (flags & INTMAXT)
1230 ADDTYPE(TP_INTMAXT);
1231 else if (flags & PTRDIFFT)
1232 ADDTYPE(TP_PTRDIFFT);
1233 else if (flags & SIZET)
1235 else if (flags & LLONGINT)
1237 else if (flags & LONGINT)
1239 else if (flags & SHORTINT)
1241 else if (flags & CHARINT)
1245 continue; /* no output */
1266 default: /* "%?" prints ?, unless ? is NUL */
1274 * Build the argument table.
1276 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1277 *argtable = (union arg *)
1278 malloc (sizeof (union arg) * (tablemax + 1));
1281 (*argtable) [0].intarg = 0;
1282 for (n = 1; n <= tablemax; n++) {
1283 switch (typetable [n]) {
1284 case T_UNUSED: /* whoops! */
1285 (*argtable) [n].intarg = va_arg (ap, int);
1288 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1291 (*argtable) [n].pshortarg = va_arg (ap, short *);
1294 (*argtable) [n].intarg = va_arg (ap, int);
1297 (*argtable) [n].uintarg = va_arg (ap, unsigned int);
1300 (*argtable) [n].pintarg = va_arg (ap, int *);
1303 (*argtable) [n].longarg = va_arg (ap, long);
1306 (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1309 (*argtable) [n].plongarg = va_arg (ap, long *);
1312 (*argtable) [n].longlongarg = va_arg (ap, long long);
1315 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1318 (*argtable) [n].plonglongarg = va_arg (ap, long long *);
1321 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1324 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1327 (*argtable) [n].sizearg = va_arg (ap, size_t);
1330 (*argtable) [n].psizearg = va_arg (ap, ssize_t *);
1333 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1336 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1339 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1341 #ifdef FLOATING_POINT
1343 (*argtable) [n].doublearg = va_arg (ap, double);
1346 (*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 __P((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 */
1418 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve,
1420 if ((ch != 'g' && ch != 'G') || flags & ALT) {
1421 /* print trailing zeros */
1422 bp = digits + ndigits;
1424 if (*digits == '0' && value)
1425 *decpt = -ndigits + 1;
1428 if (value == 0) /* kludge for __dtoa irregularity */
1433 *length = rve - digits;
1438 exponent(char *p0, int exp, int fmtch)
1441 char expbuf[MAXEXP];
1451 t = expbuf + MAXEXP;
1454 *--t = to_char(exp % 10);
1455 } while ((exp /= 10) > 9);
1456 *--t = to_char(exp);
1457 for (; t < expbuf + MAXEXP; *p++ = *t++);
1461 *p++ = to_char(exp);
1465 #endif /* FLOATING_POINT */