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 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 * Actual wprintf innards.
44 * Avoid making gratuitous changes to this source file; it should be kept
45 * as close as possible to vfprintf.c for ease of maintenance.
48 #include "namespace.h"
49 #include <sys/types.h>
62 #include "un-namespace.h"
64 #include "libc_private.h"
73 long long longlongarg;
74 unsigned long long ulonglongarg;
81 signed char *pschararg;
85 long long *plonglongarg;
86 ptrdiff_t *pptrdiffarg;
89 #ifndef NO_FLOATING_POINT
91 long double longdoublearg;
98 * Type ids for argument type table.
101 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT,
102 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
103 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET,
104 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
105 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
108 static int __sbprintf(FILE *, const wchar_t *, va_list);
109 static wint_t __xfputwc(wchar_t, FILE *);
110 static wchar_t *__ujtoa(uintmax_t, wchar_t *, int, int, const char *, int,
112 static wchar_t *__ultoa(u_long, wchar_t *, int, int, const char *, int,
114 static wchar_t *__mbsconv(char *, int);
115 static void __find_arguments(const wchar_t *, va_list, union arg **);
116 static void __grow_type_table(int, enum typeid **, int *);
119 * Helper function for `fprintf to unbuffered unix file': creates a
120 * temporary buffer. We only work on write-only files; this avoids
121 * worries about ungetc buffers and so forth.
124 __sbprintf(FILE *fp, const wchar_t *fmt, va_list ap)
128 unsigned char buf[BUFSIZ];
130 /* copy the important variables */
131 fake._flags = fp->_flags & ~__SNBF;
132 fake._file = fp->_file;
133 fake._cookie = fp->_cookie;
134 fake._write = fp->_write;
135 fake._extra = fp->_extra;
137 /* set up the buffer */
138 fake._bf._base = fake._p = buf;
139 fake._bf._size = fake._w = sizeof(buf);
140 fake._lbfsize = 0; /* not actually used, but Just In Case */
142 /* do the work, then copy any error status */
143 ret = __vfwprintf(&fake, fmt, ap);
144 if (ret >= 0 && __fflush(&fake))
146 if (fake._flags & __SERR)
147 fp->_flags |= __SERR;
152 * Like __fputwc, but handles fake string (__SSTR) files properly.
153 * File must already be locked.
156 __xfputwc(wchar_t wc, FILE *fp)
158 static const mbstate_t initial;
160 char buf[MB_LEN_MAX];
165 if ((fp->_flags & __SSTR) == 0)
166 return (__fputwc(wc, fp));
169 if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) {
170 fp->_flags |= __SERR;
178 return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF);
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, wchar_t *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[val & 15];
270 /* Identical to __ultoa, but for intmax_t. */
272 __ujtoa(uintmax_t val, wchar_t *endp, int base, int octzero,
273 const char *xdigs, 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[val & 15];
343 * Convert a multibyte character string argument for the %s format to a wide
344 * string representation. ``prec'' specifies the maximum number of bytes
345 * to output. If ``prec'' is greater than or equal to zero, we can't assume
346 * that the multibyte char. string ends in a null character.
349 __mbsconv(char *mbsarg, int prec)
351 static const mbstate_t initial;
353 wchar_t *convbuf, *wcp;
355 size_t insize, nchars, nconv;
361 * Supplied argument is a multibyte string; convert it to wide
366 * String is not guaranteed to be NUL-terminated. Find the
367 * number of characters to print.
372 while (nchars != (size_t)prec) {
373 nconv = mbrlen(p, MB_CUR_MAX, &mbs);
374 if (nconv == 0 || nconv == (size_t)-1 ||
381 if (nconv == (size_t)-1 || nconv == (size_t)-2)
384 insize = strlen(mbsarg);
387 * Allocate buffer for the result and perform the conversion,
388 * converting at most `size' bytes of the input multibyte string to
389 * wide characters for printing.
391 convbuf = malloc((insize + 1) * sizeof(*convbuf));
397 while (insize != 0) {
398 nconv = mbrtowc(wcp, p, insize, &mbs);
399 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
405 if (nconv == (size_t)-1 || nconv == (size_t)-2) {
418 vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap)
424 ret = __vfwprintf(fp, fmt0, ap);
429 #ifndef NO_FLOATING_POINT
432 #define freedtoa __freedtoa
441 static int exponent(wchar_t *, int, wchar_t);
443 #endif /* !NO_FLOATING_POINT */
446 * The size of the buffer we use as scratch space for integer
447 * conversions, among other things. Technically, we would need the
448 * most space for base 10 conversions with thousands' grouping
449 * characters between each pair of digits. 100 bytes is a
450 * conservative overestimate even for a 128-bit uintmax_t.
454 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
457 * Flags used during conversion.
459 #define ALT 0x001 /* alternate form */
460 #define LADJUST 0x004 /* left adjustment */
461 #define LONGDBL 0x008 /* long double */
462 #define LONGINT 0x010 /* long integer */
463 #define LLONGINT 0x020 /* long long integer */
464 #define SHORTINT 0x040 /* short integer */
465 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
466 #define FPT 0x100 /* Floating point number */
467 #define GROUPING 0x200 /* use grouping ("'" flag) */
468 /* C99 additional size modifiers: */
469 #define SIZET 0x400 /* size_t */
470 #define PTRDIFFT 0x800 /* ptrdiff_t */
471 #define INTMAXT 0x1000 /* intmax_t */
472 #define CHARINT 0x2000 /* print char using int format */
475 * Non-MT-safe version
478 __vfwprintf(FILE *fp, const wchar_t *fmt0, va_list ap)
480 wchar_t *fmt; /* format string */
481 wchar_t ch; /* character from fmt */
482 int n, n2, n3; /* handy integer (short term usage) */
483 wchar_t *cp; /* handy char pointer (short term usage) */
484 int flags; /* flags as above */
485 int ret; /* return value accumulator */
486 int width; /* width from format (%8d), or 0 */
487 int prec; /* precision from format; <0 for N/A */
488 wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */
489 char thousands_sep; /* locale specific thousands separator */
490 const char *grouping; /* locale specific numeric grouping rules */
491 #ifndef NO_FLOATING_POINT
493 * We can decompose the printed representation of floating
494 * point numbers into several parts, some of which may be empty:
496 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
499 * A: 'sign' holds this value if present; '\0' otherwise
500 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
501 * C: cp points to the string MMMNNN. Leading and trailing
502 * zeros are not in the string and must be added.
503 * D: expchar holds this character; '\0' if no exponent, e.g. %f
504 * F: at least two digits for decimal, at least one digit for hex
506 char *decimal_point; /* locale specific decimal point */
507 int signflag; /* true if float is negative */
508 union { /* floating point arguments %[aAeEfFgG] */
512 int expt; /* integer value of exponent */
513 char expchar; /* exponent character: [eEpP\0] */
514 char *dtoaend; /* pointer to end of converted digits */
515 int expsize; /* character count for expstr */
516 int lead; /* sig figs before decimal or group sep */
517 int ndig; /* actual number of digits returned by dtoa */
518 wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
519 char *dtoaresult; /* buffer allocated by dtoa */
520 int nseps; /* number of group separators with ' */
521 int nrepeats; /* number of repeats of the last group */
523 u_long ulval; /* integer arguments %[diouxX] */
524 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
525 int base; /* base for [diouxX] conversion */
526 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
527 int realsz; /* field size expanded by dprec, sign, etc */
528 int size; /* size of converted field or string */
529 int prsize; /* max size of printed field */
530 const char *xdigs; /* digits for [xX] conversion */
531 wchar_t buf[BUF]; /* buffer with space for digits of uintmax_t */
532 wchar_t ox[2]; /* space for 0x hex-prefix */
533 union arg *argtable; /* args, built due to positional arg */
534 union arg statargtable [STATIC_ARG_TBL_SIZE];
535 int nextarg; /* 1-based argument index */
536 va_list orgap; /* original argument pointer */
537 wchar_t *convbuf; /* multibyte to wide conversion result */
540 * Choose PADSIZE to trade efficiency vs. size. If larger printf
541 * fields occur frequently, increase PADSIZE and make the initialisers
544 #define PADSIZE 16 /* pad chunk size */
545 static wchar_t blanks[PADSIZE] =
546 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
547 static wchar_t zeroes[PADSIZE] =
548 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
550 static const char xdigs_lower[16] = "0123456789abcdef";
551 static const char xdigs_upper[16] = "0123456789ABCDEF";
554 * BEWARE, these `goto error' on error, PRINT uses `n2' and
557 #define PRINT(ptr, len) do { \
558 for (n3 = 0; n3 < (len); n3++) \
559 __xfputwc((ptr)[n3], fp); \
561 #define PAD(howmany, with) do { \
562 if ((n = (howmany)) > 0) { \
563 while (n > PADSIZE) { \
564 PRINT(with, PADSIZE); \
570 #define PRINTANDPAD(p, ep, len, with) do { \
576 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
580 * Get the argument indexed by nextarg. If the argument table is
581 * built, use it to get the argument. If its not, get the next
582 * argument (and arguments must be gotten sequentially).
584 #define GETARG(type) \
585 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
586 (nextarg++, va_arg(ap, type)))
589 * To extend shorts properly, we need both signed and unsigned
590 * argument extraction methods.
593 (flags&LONGINT ? GETARG(long) : \
594 flags&SHORTINT ? (long)(short)GETARG(int) : \
595 flags&CHARINT ? (long)(signed char)GETARG(int) : \
598 (flags&LONGINT ? GETARG(u_long) : \
599 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
600 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
601 (u_long)GETARG(u_int))
602 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
604 (flags&INTMAXT ? GETARG(intmax_t) : \
605 flags&SIZET ? (intmax_t)GETARG(size_t) : \
606 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
607 (intmax_t)GETARG(long long))
609 (flags&INTMAXT ? GETARG(uintmax_t) : \
610 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
611 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
612 (uintmax_t)GETARG(unsigned long long))
615 * Get * arguments, including the form *nn$. Preserve the nextarg
616 * that the argument can be gotten once the type is determined.
618 #define GETASTER(val) \
621 while (is_digit(*cp)) { \
622 n2 = 10 * n2 + to_digit(*cp); \
626 int hold = nextarg; \
627 if (argtable == NULL) { \
628 argtable = statargtable; \
629 __find_arguments (fmt0, orgap, &argtable); \
632 val = GETARG (int); \
636 val = GETARG (int); \
640 thousands_sep = '\0';
642 #ifndef NO_FLOATING_POINT
643 decimal_point = localeconv()->decimal_point;
646 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
647 if (prepwrite(fp) != 0)
650 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
651 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
653 return (__sbprintf(fp, fmt0, ap));
655 fmt = (wchar_t *)fmt0;
662 * Scan the format for conversions (`%' character).
665 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
667 if ((n = fmt - cp) != 0) {
668 if ((unsigned)ret + n > INT_MAX) {
677 fmt++; /* skip over '%' */
687 reswitch: switch (ch) {
690 * ``If the space and + flags both appear, the space
691 * flag will be ignored.''
702 * ``A negative field width argument is taken as a
703 * - flag followed by a positive field width.''
705 * They don't exclude field widths read from args.
720 thousands_sep = *(localeconv()->thousands_sep);
721 grouping = localeconv()->grouping;
724 if ((ch = *fmt++) == '*') {
729 while (is_digit(ch)) {
730 prec = 10 * prec + to_digit(ch);
736 * ``Note that 0 is taken as a flag, not as the
737 * beginning of a field width.''
742 case '1': case '2': case '3': case '4':
743 case '5': case '6': case '7': case '8': case '9':
746 n = 10 * n + to_digit(ch);
748 } while (is_digit(ch));
751 if (argtable == NULL) {
752 argtable = statargtable;
753 __find_arguments (fmt0, orgap,
760 #ifndef NO_FLOATING_POINT
766 if (flags & SHORTINT) {
776 if (flags & LONGINT) {
783 flags |= LLONGINT; /* not necessarily */
796 *(cp = buf) = (wchar_t)GETARG(wint_t);
798 *(cp = buf) = (wchar_t)btowc(GETARG(int));
807 if (flags & INTMAX_SIZE) {
809 if ((intmax_t)ujval < 0) {
815 if ((long)ulval < 0) {
822 #ifndef NO_FLOATING_POINT
836 if (flags & LONGDBL) {
837 fparg.ldbl = GETARG(long double);
839 __hldtoa(fparg.ldbl, xdigs, prec,
840 &expt, &signflag, &dtoaend);
842 fparg.dbl = GETARG(double);
844 __hdtoa(fparg.dbl, xdigs, prec,
845 &expt, &signflag, &dtoaend);
848 prec = dtoaend - dtoaresult;
853 ndig = dtoaend - dtoaresult;
854 cp = convbuf = __mbsconv(dtoaresult, -1);
855 freedtoa(dtoaresult);
860 if (prec < 0) /* account for digit before decpt */
871 expchar = ch - ('g' - 'e');
879 if (flags & LONGDBL) {
880 fparg.ldbl = GETARG(long double);
882 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
883 &expt, &signflag, &dtoaend);
885 fparg.dbl = GETARG(double);
887 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
888 &expt, &signflag, &dtoaend);
892 ndig = dtoaend - dtoaresult;
893 cp = convbuf = __mbsconv(dtoaresult, -1);
894 freedtoa(dtoaresult);
898 if (expt == INT_MAX) { /* inf or nan */
900 cp = (ch >= 'a') ? L"nan" : L"NAN";
903 cp = (ch >= 'a') ? L"inf" : L"INF";
908 if (ch == 'g' || ch == 'G') {
909 if (expt > -4 && expt <= prec) {
910 /* Make %[gG] smell like %[fF] */
920 * Make %[gG] smell like %[eE], but
921 * trim trailing zeroes if no # flag.
928 expsize = exponent(expstr, expt - 1, expchar);
929 size = expsize + prec;
930 if (prec > 1 || flags & ALT)
933 /* space for digits before decimal point */
938 /* space for decimal pt and following digits */
939 if (prec || flags & ALT)
941 if (grouping && expt > 0) {
942 /* space for thousands' grouping */
943 nseps = nrepeats = 0;
945 while (*grouping != CHAR_MAX) {
946 if (lead <= *grouping)
955 size += nseps + nrepeats;
960 #endif /* !NO_FLOATING_POINT */
963 * Assignment-like behavior is specified if the
964 * value overflows or is otherwise unrepresentable.
965 * C99 says to use `signed char' for %hhn conversions.
967 if (flags & LLONGINT)
968 *GETARG(long long *) = ret;
969 else if (flags & SIZET)
970 *GETARG(ssize_t *) = (ssize_t)ret;
971 else if (flags & PTRDIFFT)
972 *GETARG(ptrdiff_t *) = ret;
973 else if (flags & INTMAXT)
974 *GETARG(intmax_t *) = ret;
975 else if (flags & LONGINT)
976 *GETARG(long *) = ret;
977 else if (flags & SHORTINT)
978 *GETARG(short *) = ret;
979 else if (flags & CHARINT)
980 *GETARG(signed char *) = ret;
982 *GETARG(int *) = ret;
983 continue; /* no output */
988 if (flags & INTMAX_SIZE)
996 * ``The argument shall be a pointer to void. The
997 * value of the pointer is converted to a sequence
998 * of printable characters, in an implementation-
1002 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
1004 xdigs = xdigs_lower;
1005 flags = flags | INTMAXT;
1012 if (flags & LONGINT) {
1013 if ((cp = GETARG(wchar_t *)) == NULL)
1018 if (convbuf != NULL)
1020 if ((mbp = GETARG(char *)) == NULL)
1023 convbuf = __mbsconv(mbp, prec);
1024 if (convbuf == NULL) {
1025 fp->_flags |= __SERR;
1034 * can't use wcslen; can only look for the
1035 * NUL in the first `prec' characters, and
1036 * wcslen() will go further.
1038 wchar_t *p = wmemchr(cp, 0, (size_t)prec);
1054 if (flags & INTMAX_SIZE)
1061 xdigs = xdigs_upper;
1064 xdigs = xdigs_lower;
1066 if (flags & INTMAX_SIZE)
1071 /* leading 0x/X only if non-zero */
1073 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1077 /* unsigned conversions */
1078 nosign: sign = '\0';
1080 * ``... diouXx conversions ... if a precision is
1081 * specified, the 0 flag will be ignored.''
1084 number: if ((dprec = prec) >= 0)
1088 * ``The result of converting a zero value with an
1089 * explicit precision of zero is no characters.''
1092 * ``The C Standard is clear enough as is. The call
1093 * printf("%#.0o", 0) should print 0.''
1094 * -- Defect Report #151
1097 if (flags & INTMAX_SIZE) {
1098 if (ujval != 0 || prec != 0 ||
1099 (flags & ALT && base == 8))
1100 cp = __ujtoa(ujval, cp, base,
1102 flags & GROUPING, thousands_sep,
1105 if (ulval != 0 || prec != 0 ||
1106 (flags & ALT && base == 8))
1107 cp = __ultoa(ulval, cp, base,
1109 flags & GROUPING, thousands_sep,
1112 size = buf + BUF - cp;
1113 if (size > BUF) /* should never happen */
1116 default: /* "%?" prints ?, unless ? is NUL */
1119 /* pretend it was %c with argument ch */
1128 * All reasonable formats wind up here. At this point, `cp'
1129 * points to a string which (if not flags&LADJUST) should be
1130 * padded out to `width' places. If flags&ZEROPAD, it should
1131 * first be prefixed by any sign or other prefix; otherwise,
1132 * it should be blank padded before the prefix is emitted.
1133 * After any left-hand padding and prefixing, emit zeroes
1134 * required by a decimal [diouxX] precision, then print the
1135 * string proper, then emit zeroes required by any leftover
1136 * floating precision; finally, if LADJUST, pad with blanks.
1138 * Compute actual size, so we know how much to pad.
1139 * size excludes decimal prec; realsz includes it.
1141 realsz = dprec > size ? dprec : size;
1147 prsize = width > realsz ? width : realsz;
1148 if ((unsigned)ret + prsize > INT_MAX) {
1153 /* right-adjusting blank padding */
1154 if ((flags & (LADJUST|ZEROPAD)) == 0)
1155 PAD(width - realsz, blanks);
1161 if (ox[1]) { /* ox[1] is either x, X, or \0 */
1166 /* right-adjusting zero padding */
1167 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1168 PAD(width - realsz, zeroes);
1170 /* leading zeroes from decimal precision */
1171 PAD(dprec - size, zeroes);
1173 /* the string or number proper */
1174 #ifndef NO_FLOATING_POINT
1175 if ((flags & FPT) == 0) {
1177 } else { /* glue together f_p fragments */
1178 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1181 if (prec || flags & ALT)
1182 PRINT(decimal_point, 1);
1184 /* already handled initial 0's */
1187 PRINTANDPAD(cp, convbuf + ndig, lead, zeroes);
1190 while (nseps>0 || nrepeats>0) {
1197 PRINT(&thousands_sep,
1204 if (cp > convbuf + ndig)
1205 cp = convbuf + ndig;
1207 if (prec || flags & ALT) {
1208 buf[0] = *decimal_point;
1212 PRINTANDPAD(cp, convbuf + ndig, prec, zeroes);
1213 } else { /* %[eE] or sufficiently long %[gG] */
1214 if (prec > 1 || flags & ALT) {
1216 buf[1] = *decimal_point;
1219 PAD(prec - ndig, zeroes);
1222 PRINT(expstr, expsize);
1228 /* left-adjusting padding (always blank) */
1229 if (flags & LADJUST)
1230 PAD(width - realsz, blanks);
1232 /* finally, adjust ret */
1238 if (convbuf != NULL)
1242 if ((argtable != NULL) && (argtable != statargtable))
1249 * Find all arguments when a positional parameter is encountered. Returns a
1250 * table, indexed by argument number, of pointers to each arguments. The
1251 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1252 * It will be replaces with a malloc-ed one if it overflows.
1255 __find_arguments (const wchar_t *fmt0, va_list ap, union arg **argtable)
1257 wchar_t *fmt; /* format string */
1258 wchar_t ch; /* character from fmt */
1259 int n, n2; /* handy integer (short term usage) */
1260 wchar_t *cp; /* handy char pointer (short term usage) */
1261 int flags; /* flags as above */
1262 int width; /* width from format (%8d), or 0 */
1263 enum typeid *typetable; /* table of types */
1264 enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1265 int tablesize; /* current size of type table */
1266 int tablemax; /* largest used index in table */
1267 int nextarg; /* 1-based argument index */
1270 * Add an argument type to the table, expanding if necessary.
1272 #define ADDTYPE(type) \
1273 ((nextarg >= tablesize) ? \
1274 __grow_type_table(nextarg, &typetable, &tablesize) : (void)0, \
1275 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1276 typetable[nextarg++] = type)
1279 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1280 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1281 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1282 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1283 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1286 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1287 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1288 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1289 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1290 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1293 * Add * arguments to the type array.
1295 #define ADDASTER() \
1298 while (is_digit(*cp)) { \
1299 n2 = 10 * n2 + to_digit(*cp); \
1303 int hold = nextarg; \
1311 fmt = (wchar_t *)fmt0;
1312 typetable = stattypetable;
1313 tablesize = STATIC_ARG_TBL_SIZE;
1316 for (n = 0; n < STATIC_ARG_TBL_SIZE; n++)
1317 typetable[n] = T_UNUSED;
1320 * Scan the format for conversions (`%' character).
1323 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1327 fmt++; /* skip over '%' */
1333 reswitch: switch (ch) {
1345 if ((ch = *fmt++) == '*') {
1349 while (is_digit(ch)) {
1355 case '1': case '2': case '3': case '4':
1356 case '5': case '6': case '7': case '8': case '9':
1359 n = 10 * n + to_digit(ch);
1361 } while (is_digit(ch));
1368 #ifndef NO_FLOATING_POINT
1374 if (flags & SHORTINT) {
1384 if (flags & LONGINT) {
1391 flags |= LLONGINT; /* not necessarily */
1403 if (flags & LONGINT)
1415 #ifndef NO_FLOATING_POINT
1423 if (flags & LONGDBL)
1424 ADDTYPE(T_LONG_DOUBLE);
1428 #endif /* !NO_FLOATING_POINT */
1430 if (flags & INTMAXT)
1431 ADDTYPE(TP_INTMAXT);
1432 else if (flags & PTRDIFFT)
1433 ADDTYPE(TP_PTRDIFFT);
1434 else if (flags & SIZET)
1436 else if (flags & LLONGINT)
1438 else if (flags & LONGINT)
1440 else if (flags & SHORTINT)
1442 else if (flags & CHARINT)
1446 continue; /* no output */
1460 if (flags & LONGINT)
1473 default: /* "%?" prints ?, unless ? is NUL */
1481 * Build the argument table.
1483 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1484 *argtable = (union arg *)
1485 malloc (sizeof (union arg) * (tablemax + 1));
1488 (*argtable) [0].intarg = 0;
1489 for (n = 1; n <= tablemax; n++) {
1490 switch (typetable [n]) {
1491 case T_UNUSED: /* whoops! */
1492 (*argtable) [n].intarg = va_arg (ap, int);
1495 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1498 (*argtable) [n].pshortarg = va_arg (ap, short *);
1501 (*argtable) [n].intarg = va_arg (ap, int);
1504 (*argtable) [n].uintarg = va_arg (ap, unsigned int);
1507 (*argtable) [n].pintarg = va_arg (ap, int *);
1510 (*argtable) [n].longarg = va_arg (ap, long);
1513 (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1516 (*argtable) [n].plongarg = va_arg (ap, long *);
1519 (*argtable) [n].longlongarg = va_arg (ap, long long);
1522 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1525 (*argtable) [n].plonglongarg = va_arg (ap, long long *);
1528 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1531 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1534 (*argtable) [n].sizearg = va_arg (ap, size_t);
1537 (*argtable) [n].psizearg = va_arg (ap, size_t *);
1540 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1543 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1546 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1549 #ifndef NO_FLOATING_POINT
1550 (*argtable) [n].doublearg = va_arg (ap, double);
1554 #ifndef NO_FLOATING_POINT
1555 (*argtable) [n].longdoublearg = va_arg (ap, long double);
1559 (*argtable) [n].pchararg = va_arg (ap, char *);
1562 (*argtable) [n].pvoidarg = va_arg (ap, void *);
1565 (*argtable) [n].wintarg = va_arg (ap, wint_t);
1568 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
1573 if ((typetable != NULL) && (typetable != stattypetable))
1578 * Increase the size of the type table.
1581 __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize)
1583 enum typeid *const oldtable = *typetable;
1584 const int oldsize = *tablesize;
1585 enum typeid *newtable;
1586 int n, newsize = oldsize * 2;
1588 if (newsize < nextarg + 1)
1589 newsize = nextarg + 1;
1590 if (oldsize == STATIC_ARG_TBL_SIZE) {
1591 if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL)
1592 abort(); /* XXX handle better */
1593 bcopy(oldtable, newtable, oldsize * sizeof(enum typeid));
1595 newtable = reallocf(oldtable, newsize * sizeof(enum typeid));
1596 if (newtable == NULL)
1597 abort(); /* XXX handle better */
1599 for (n = oldsize; n < newsize; n++)
1600 newtable[n] = T_UNUSED;
1602 *typetable = newtable;
1603 *tablesize = newsize;
1607 #ifndef NO_FLOATING_POINT
1610 exponent(wchar_t *p0, int exp, wchar_t fmtch)
1613 wchar_t expbuf[MAXEXPDIG];
1623 t = expbuf + MAXEXPDIG;
1626 *--t = to_char(exp % 10);
1627 } while ((exp /= 10) > 9);
1628 *--t = to_char(exp);
1629 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1633 * Exponents for decimal floating point conversions
1634 * (%[eEgG]) must be at least two characters long,
1635 * whereas exponents for hexadecimal conversions can
1636 * be only one character long.
1638 if (fmtch == 'e' || fmtch == 'E')
1640 *p++ = to_char(exp);
1644 #endif /* !NO_FLOATING_POINT */