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);
389 * Allocate buffer for the result and perform the conversion,
390 * converting at most `size' bytes of the input multibyte string to
391 * wide characters for printing.
393 convbuf = malloc((insize + 1) * sizeof(*convbuf));
399 while (insize != 0) {
400 nconv = mbrtowc(wcp, p, insize, &mbs);
401 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
407 if (nconv == (size_t)-1 || nconv == (size_t)-2) {
420 vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap)
426 ret = __vfwprintf(fp, fmt0, ap);
431 #ifndef NO_FLOATING_POINT
434 #define freedtoa __freedtoa
443 static int exponent(wchar_t *, int, wchar_t);
445 #endif /* !NO_FLOATING_POINT */
448 * The size of the buffer we use as scratch space for integer
449 * conversions, among other things. Technically, we would need the
450 * most space for base 10 conversions with thousands' grouping
451 * characters between each pair of digits. 100 bytes is a
452 * conservative overestimate even for a 128-bit uintmax_t.
456 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
459 * Flags used during conversion.
461 #define ALT 0x001 /* alternate form */
462 #define LADJUST 0x004 /* left adjustment */
463 #define LONGDBL 0x008 /* long double */
464 #define LONGINT 0x010 /* long integer */
465 #define LLONGINT 0x020 /* long long integer */
466 #define SHORTINT 0x040 /* short integer */
467 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
468 #define FPT 0x100 /* Floating point number */
469 #define GROUPING 0x200 /* use grouping ("'" flag) */
470 /* C99 additional size modifiers: */
471 #define SIZET 0x400 /* size_t */
472 #define PTRDIFFT 0x800 /* ptrdiff_t */
473 #define INTMAXT 0x1000 /* intmax_t */
474 #define CHARINT 0x2000 /* print char using int format */
477 * Non-MT-safe version
480 __vfwprintf(FILE *fp, const wchar_t *fmt0, va_list ap)
482 wchar_t *fmt; /* format string */
483 wchar_t ch; /* character from fmt */
484 int n, n2, n3; /* handy integer (short term usage) */
485 wchar_t *cp; /* handy char pointer (short term usage) */
486 int flags; /* flags as above */
487 int ret; /* return value accumulator */
488 int width; /* width from format (%8d), or 0 */
489 int prec; /* precision from format; <0 for N/A */
490 wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */
491 char thousands_sep; /* locale specific thousands separator */
492 const char *grouping; /* locale specific numeric grouping rules */
493 #ifndef NO_FLOATING_POINT
495 * We can decompose the printed representation of floating
496 * point numbers into several parts, some of which may be empty:
498 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
501 * A: 'sign' holds this value if present; '\0' otherwise
502 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
503 * C: cp points to the string MMMNNN. Leading and trailing
504 * zeros are not in the string and must be added.
505 * D: expchar holds this character; '\0' if no exponent, e.g. %f
506 * F: at least two digits for decimal, at least one digit for hex
508 char *decimal_point; /* locale specific decimal point */
509 int signflag; /* true if float is negative */
510 union { /* floating point arguments %[aAeEfFgG] */
514 int expt; /* integer value of exponent */
515 char expchar; /* exponent character: [eEpP\0] */
516 char *dtoaend; /* pointer to end of converted digits */
517 int expsize; /* character count for expstr */
518 int lead; /* sig figs before decimal or group sep */
519 int ndig; /* actual number of digits returned by dtoa */
520 wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
521 char *dtoaresult; /* buffer allocated by dtoa */
522 int nseps; /* number of group separators with ' */
523 int nrepeats; /* number of repeats of the last group */
525 u_long ulval; /* integer arguments %[diouxX] */
526 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
527 int base; /* base for [diouxX] conversion */
528 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
529 int realsz; /* field size expanded by dprec, sign, etc */
530 int size; /* size of converted field or string */
531 int prsize; /* max size of printed field */
532 const char *xdigs; /* digits for [xX] conversion */
533 wchar_t buf[BUF]; /* buffer with space for digits of uintmax_t */
534 wchar_t ox[2]; /* space for 0x hex-prefix */
535 union arg *argtable; /* args, built due to positional arg */
536 union arg statargtable [STATIC_ARG_TBL_SIZE];
537 int nextarg; /* 1-based argument index */
538 va_list orgap; /* original argument pointer */
539 wchar_t *convbuf; /* multibyte to wide conversion result */
542 * Choose PADSIZE to trade efficiency vs. size. If larger printf
543 * fields occur frequently, increase PADSIZE and make the initialisers
546 #define PADSIZE 16 /* pad chunk size */
547 static wchar_t blanks[PADSIZE] =
548 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
549 static wchar_t zeroes[PADSIZE] =
550 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
552 static const char xdigs_lower[16] = "0123456789abcdef";
553 static const char xdigs_upper[16] = "0123456789ABCDEF";
556 * BEWARE, these `goto error' on error, PRINT uses `n2' and
559 #define PRINT(ptr, len) do { \
560 for (n3 = 0; n3 < (len); n3++) \
561 __xfputwc((ptr)[n3], fp); \
563 #define PAD(howmany, with) do { \
564 if ((n = (howmany)) > 0) { \
565 while (n > PADSIZE) { \
566 PRINT(with, PADSIZE); \
572 #define PRINTANDPAD(p, ep, len, with) do { \
578 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
582 * Get the argument indexed by nextarg. If the argument table is
583 * built, use it to get the argument. If its not, get the next
584 * argument (and arguments must be gotten sequentially).
586 #define GETARG(type) \
587 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
588 (nextarg++, va_arg(ap, type)))
591 * To extend shorts properly, we need both signed and unsigned
592 * argument extraction methods.
595 (flags&LONGINT ? GETARG(long) : \
596 flags&SHORTINT ? (long)(short)GETARG(int) : \
597 flags&CHARINT ? (long)(signed char)GETARG(int) : \
600 (flags&LONGINT ? GETARG(u_long) : \
601 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
602 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
603 (u_long)GETARG(u_int))
604 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
606 (flags&INTMAXT ? GETARG(intmax_t) : \
607 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
608 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
609 (intmax_t)GETARG(long long))
611 (flags&INTMAXT ? GETARG(uintmax_t) : \
612 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
613 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
614 (uintmax_t)GETARG(unsigned long long))
617 * Get * arguments, including the form *nn$. Preserve the nextarg
618 * that the argument can be gotten once the type is determined.
620 #define GETASTER(val) \
623 while (is_digit(*cp)) { \
624 n2 = 10 * n2 + to_digit(*cp); \
628 int hold = nextarg; \
629 if (argtable == NULL) { \
630 argtable = statargtable; \
631 __find_arguments (fmt0, orgap, &argtable); \
634 val = GETARG (int); \
638 val = GETARG (int); \
642 thousands_sep = '\0';
644 #ifndef NO_FLOATING_POINT
645 decimal_point = localeconv()->decimal_point;
648 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
649 if (prepwrite(fp) != 0)
652 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
653 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
655 return (__sbprintf(fp, fmt0, ap));
657 fmt = (wchar_t *)fmt0;
664 * Scan the format for conversions (`%' character).
667 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
669 if ((n = fmt - cp) != 0) {
670 if ((unsigned)ret + n > INT_MAX) {
679 fmt++; /* skip over '%' */
689 reswitch: switch (ch) {
692 * ``If the space and + flags both appear, the space
693 * flag will be ignored.''
704 * ``A negative field width argument is taken as a
705 * - flag followed by a positive field width.''
707 * They don't exclude field widths read from args.
722 thousands_sep = *(localeconv()->thousands_sep);
723 grouping = localeconv()->grouping;
726 if ((ch = *fmt++) == '*') {
731 while (is_digit(ch)) {
732 prec = 10 * prec + to_digit(ch);
738 * ``Note that 0 is taken as a flag, not as the
739 * beginning of a field width.''
744 case '1': case '2': case '3': case '4':
745 case '5': case '6': case '7': case '8': case '9':
748 n = 10 * n + to_digit(ch);
750 } while (is_digit(ch));
753 if (argtable == NULL) {
754 argtable = statargtable;
755 __find_arguments (fmt0, orgap,
762 #ifndef NO_FLOATING_POINT
768 if (flags & SHORTINT) {
778 if (flags & LONGINT) {
785 flags |= LLONGINT; /* not necessarily */
798 *(cp = buf) = (wchar_t)GETARG(wint_t);
800 *(cp = buf) = (wchar_t)btowc(GETARG(int));
809 if (flags & INTMAX_SIZE) {
811 if ((intmax_t)ujval < 0) {
817 if ((long)ulval < 0) {
824 #ifndef NO_FLOATING_POINT
838 if (flags & LONGDBL) {
839 fparg.ldbl = GETARG(long double);
841 __hldtoa(fparg.ldbl, xdigs, prec,
842 &expt, &signflag, &dtoaend);
844 fparg.dbl = GETARG(double);
846 __hdtoa(fparg.dbl, xdigs, prec,
847 &expt, &signflag, &dtoaend);
850 prec = dtoaend - dtoaresult;
855 ndig = dtoaend - dtoaresult;
856 cp = convbuf = __mbsconv(dtoaresult, -1);
857 freedtoa(dtoaresult);
862 if (prec < 0) /* account for digit before decpt */
873 expchar = ch - ('g' - 'e');
881 if (flags & LONGDBL) {
882 fparg.ldbl = GETARG(long double);
884 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
885 &expt, &signflag, &dtoaend);
887 fparg.dbl = GETARG(double);
889 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
890 &expt, &signflag, &dtoaend);
894 ndig = dtoaend - dtoaresult;
895 cp = convbuf = __mbsconv(dtoaresult, -1);
896 freedtoa(dtoaresult);
900 if (expt == INT_MAX) { /* inf or nan */
902 cp = (ch >= 'a') ? L"nan" : L"NAN";
905 cp = (ch >= 'a') ? L"inf" : L"INF";
910 if (ch == 'g' || ch == 'G') {
911 if (expt > -4 && expt <= prec) {
912 /* Make %[gG] smell like %[fF] */
922 * Make %[gG] smell like %[eE], but
923 * trim trailing zeroes if no # flag.
930 expsize = exponent(expstr, expt - 1, expchar);
931 size = expsize + prec;
932 if (prec > 1 || flags & ALT)
935 /* space for digits before decimal point */
940 /* space for decimal pt and following digits */
941 if (prec || flags & ALT)
943 if (grouping && expt > 0) {
944 /* space for thousands' grouping */
945 nseps = nrepeats = 0;
947 while (*grouping != CHAR_MAX) {
948 if (lead <= *grouping)
957 size += nseps + nrepeats;
962 #endif /* !NO_FLOATING_POINT */
965 * Assignment-like behavior is specified if the
966 * value overflows or is otherwise unrepresentable.
967 * C99 says to use `signed char' for %hhn conversions.
969 if (flags & LLONGINT)
970 *GETARG(long long *) = ret;
971 else if (flags & SIZET)
972 *GETARG(ssize_t *) = (ssize_t)ret;
973 else if (flags & PTRDIFFT)
974 *GETARG(ptrdiff_t *) = ret;
975 else if (flags & INTMAXT)
976 *GETARG(intmax_t *) = ret;
977 else if (flags & LONGINT)
978 *GETARG(long *) = ret;
979 else if (flags & SHORTINT)
980 *GETARG(short *) = ret;
981 else if (flags & CHARINT)
982 *GETARG(signed char *) = ret;
984 *GETARG(int *) = ret;
985 continue; /* no output */
990 if (flags & INTMAX_SIZE)
998 * ``The argument shall be a pointer to void. The
999 * value of the pointer is converted to a sequence
1000 * of printable characters, in an implementation-
1004 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
1006 xdigs = xdigs_lower;
1007 flags = flags | INTMAXT;
1014 if (flags & LONGINT) {
1015 if ((cp = GETARG(wchar_t *)) == NULL)
1020 if (convbuf != NULL)
1022 if ((mbp = GETARG(char *)) == NULL)
1025 convbuf = __mbsconv(mbp, prec);
1026 if (convbuf == NULL) {
1027 fp->_flags |= __SERR;
1036 * can't use wcslen; can only look for the
1037 * NUL in the first `prec' characters, and
1038 * wcslen() will go further.
1040 wchar_t *p = wmemchr(cp, 0, (size_t)prec);
1056 if (flags & INTMAX_SIZE)
1063 xdigs = xdigs_upper;
1066 xdigs = xdigs_lower;
1068 if (flags & INTMAX_SIZE)
1073 /* leading 0x/X only if non-zero */
1075 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1079 /* unsigned conversions */
1080 nosign: sign = '\0';
1082 * ``... diouXx conversions ... if a precision is
1083 * specified, the 0 flag will be ignored.''
1086 number: if ((dprec = prec) >= 0)
1090 * ``The result of converting a zero value with an
1091 * explicit precision of zero is no characters.''
1094 * ``The C Standard is clear enough as is. The call
1095 * printf("%#.0o", 0) should print 0.''
1096 * -- Defect Report #151
1099 if (flags & INTMAX_SIZE) {
1100 if (ujval != 0 || prec != 0 ||
1101 (flags & ALT && base == 8))
1102 cp = __ujtoa(ujval, cp, base,
1104 flags & GROUPING, thousands_sep,
1107 if (ulval != 0 || prec != 0 ||
1108 (flags & ALT && base == 8))
1109 cp = __ultoa(ulval, cp, base,
1111 flags & GROUPING, thousands_sep,
1114 size = buf + BUF - cp;
1115 if (size > BUF) /* should never happen */
1118 default: /* "%?" prints ?, unless ? is NUL */
1121 /* pretend it was %c with argument ch */
1130 * All reasonable formats wind up here. At this point, `cp'
1131 * points to a string which (if not flags&LADJUST) should be
1132 * padded out to `width' places. If flags&ZEROPAD, it should
1133 * first be prefixed by any sign or other prefix; otherwise,
1134 * it should be blank padded before the prefix is emitted.
1135 * After any left-hand padding and prefixing, emit zeroes
1136 * required by a decimal [diouxX] precision, then print the
1137 * string proper, then emit zeroes required by any leftover
1138 * floating precision; finally, if LADJUST, pad with blanks.
1140 * Compute actual size, so we know how much to pad.
1141 * size excludes decimal prec; realsz includes it.
1143 realsz = dprec > size ? dprec : size;
1149 prsize = width > realsz ? width : realsz;
1150 if ((unsigned)ret + prsize > INT_MAX) {
1155 /* right-adjusting blank padding */
1156 if ((flags & (LADJUST|ZEROPAD)) == 0)
1157 PAD(width - realsz, blanks);
1163 if (ox[1]) { /* ox[1] is either x, X, or \0 */
1168 /* right-adjusting zero padding */
1169 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1170 PAD(width - realsz, zeroes);
1172 /* leading zeroes from decimal precision */
1173 PAD(dprec - size, zeroes);
1175 /* the string or number proper */
1176 #ifndef NO_FLOATING_POINT
1177 if ((flags & FPT) == 0) {
1179 } else { /* glue together f_p fragments */
1180 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1183 if (prec || flags & ALT)
1184 PRINT(decimal_point, 1);
1186 /* already handled initial 0's */
1189 PRINTANDPAD(cp, convbuf + ndig, lead, zeroes);
1192 while (nseps>0 || nrepeats>0) {
1199 PRINT(&thousands_sep,
1206 if (cp > convbuf + ndig)
1207 cp = convbuf + ndig;
1209 if (prec || flags & ALT) {
1210 buf[0] = *decimal_point;
1214 PRINTANDPAD(cp, convbuf + ndig, prec, zeroes);
1215 } else { /* %[eE] or sufficiently long %[gG] */
1216 if (prec > 1 || flags & ALT) {
1218 buf[1] = *decimal_point;
1221 PAD(prec - ndig, zeroes);
1224 PRINT(expstr, expsize);
1230 /* left-adjusting padding (always blank) */
1231 if (flags & LADJUST)
1232 PAD(width - realsz, blanks);
1234 /* finally, adjust ret */
1240 if (convbuf != NULL)
1244 if ((argtable != NULL) && (argtable != statargtable))
1251 * Find all arguments when a positional parameter is encountered. Returns a
1252 * table, indexed by argument number, of pointers to each arguments. The
1253 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1254 * It will be replaces with a malloc-ed one if it overflows.
1257 __find_arguments (const wchar_t *fmt0, va_list ap, union arg **argtable)
1259 wchar_t *fmt; /* format string */
1260 wchar_t ch; /* character from fmt */
1261 int n, n2; /* handy integer (short term usage) */
1262 wchar_t *cp; /* handy char pointer (short term usage) */
1263 int flags; /* flags as above */
1264 int width; /* width from format (%8d), or 0 */
1265 enum typeid *typetable; /* table of types */
1266 enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1267 int tablesize; /* current size of type table */
1268 int tablemax; /* largest used index in table */
1269 int nextarg; /* 1-based argument index */
1272 * Add an argument type to the table, expanding if necessary.
1274 #define ADDTYPE(type) \
1275 ((nextarg >= tablesize) ? \
1276 __grow_type_table(nextarg, &typetable, &tablesize) : (void)0, \
1277 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1278 typetable[nextarg++] = type)
1281 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1282 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1283 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1284 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1285 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1288 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1289 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1290 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1291 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1292 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1295 * Add * arguments to the type array.
1297 #define ADDASTER() \
1300 while (is_digit(*cp)) { \
1301 n2 = 10 * n2 + to_digit(*cp); \
1305 int hold = nextarg; \
1313 fmt = (wchar_t *)fmt0;
1314 typetable = stattypetable;
1315 tablesize = STATIC_ARG_TBL_SIZE;
1318 for (n = 0; n < STATIC_ARG_TBL_SIZE; n++)
1319 typetable[n] = T_UNUSED;
1322 * Scan the format for conversions (`%' character).
1325 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1329 fmt++; /* skip over '%' */
1335 reswitch: switch (ch) {
1347 if ((ch = *fmt++) == '*') {
1351 while (is_digit(ch)) {
1357 case '1': case '2': case '3': case '4':
1358 case '5': case '6': case '7': case '8': case '9':
1361 n = 10 * n + to_digit(ch);
1363 } while (is_digit(ch));
1370 #ifndef NO_FLOATING_POINT
1376 if (flags & SHORTINT) {
1386 if (flags & LONGINT) {
1393 flags |= LLONGINT; /* not necessarily */
1405 if (flags & LONGINT)
1417 #ifndef NO_FLOATING_POINT
1425 if (flags & LONGDBL)
1426 ADDTYPE(T_LONG_DOUBLE);
1430 #endif /* !NO_FLOATING_POINT */
1432 if (flags & INTMAXT)
1433 ADDTYPE(TP_INTMAXT);
1434 else if (flags & PTRDIFFT)
1435 ADDTYPE(TP_PTRDIFFT);
1436 else if (flags & SIZET)
1438 else if (flags & LLONGINT)
1440 else if (flags & LONGINT)
1442 else if (flags & SHORTINT)
1444 else if (flags & CHARINT)
1448 continue; /* no output */
1462 if (flags & LONGINT)
1475 default: /* "%?" prints ?, unless ? is NUL */
1483 * Build the argument table.
1485 if (tablemax >= STATIC_ARG_TBL_SIZE) {
1486 *argtable = (union arg *)
1487 malloc (sizeof (union arg) * (tablemax + 1));
1490 (*argtable) [0].intarg = 0;
1491 for (n = 1; n <= tablemax; n++) {
1492 switch (typetable [n]) {
1493 case T_UNUSED: /* whoops! */
1494 (*argtable) [n].intarg = va_arg (ap, int);
1497 (*argtable) [n].pschararg = va_arg (ap, signed char *);
1500 (*argtable) [n].pshortarg = va_arg (ap, short *);
1503 (*argtable) [n].intarg = va_arg (ap, int);
1506 (*argtable) [n].uintarg = va_arg (ap, unsigned int);
1509 (*argtable) [n].pintarg = va_arg (ap, int *);
1512 (*argtable) [n].longarg = va_arg (ap, long);
1515 (*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1518 (*argtable) [n].plongarg = va_arg (ap, long *);
1521 (*argtable) [n].longlongarg = va_arg (ap, long long);
1524 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1527 (*argtable) [n].plonglongarg = va_arg (ap, long long *);
1530 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1533 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1536 (*argtable) [n].sizearg = va_arg (ap, size_t);
1539 (*argtable) [n].psizearg = va_arg (ap, size_t *);
1542 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1545 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1548 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1551 #ifndef NO_FLOATING_POINT
1552 (*argtable) [n].doublearg = va_arg (ap, double);
1556 #ifndef NO_FLOATING_POINT
1557 (*argtable) [n].longdoublearg = va_arg (ap, long double);
1561 (*argtable) [n].pchararg = va_arg (ap, char *);
1564 (*argtable) [n].pvoidarg = va_arg (ap, void *);
1567 (*argtable) [n].wintarg = va_arg (ap, wint_t);
1570 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
1575 if ((typetable != NULL) && (typetable != stattypetable))
1580 * Increase the size of the type table.
1583 __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize)
1585 enum typeid *const oldtable = *typetable;
1586 const int oldsize = *tablesize;
1587 enum typeid *newtable;
1588 int n, newsize = oldsize * 2;
1590 if (newsize < nextarg + 1)
1591 newsize = nextarg + 1;
1592 if (oldsize == STATIC_ARG_TBL_SIZE) {
1593 if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL)
1594 abort(); /* XXX handle better */
1595 bcopy(oldtable, newtable, oldsize * sizeof(enum typeid));
1597 newtable = reallocf(oldtable, newsize * sizeof(enum typeid));
1598 if (newtable == NULL)
1599 abort(); /* XXX handle better */
1601 for (n = oldsize; n < newsize; n++)
1602 newtable[n] = T_UNUSED;
1604 *typetable = newtable;
1605 *tablesize = newsize;
1609 #ifndef NO_FLOATING_POINT
1612 exponent(wchar_t *p0, int exp, wchar_t fmtch)
1615 wchar_t expbuf[MAXEXPDIG];
1625 t = expbuf + MAXEXPDIG;
1628 *--t = to_char(exp % 10);
1629 } while ((exp /= 10) > 9);
1630 *--t = to_char(exp);
1631 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1635 * Exponents for decimal floating point conversions
1636 * (%[eEgG]) must be at least two characters long,
1637 * whereas exponents for hexadecimal conversions can
1638 * be only one character long.
1640 if (fmtch == 'e' || fmtch == 'E')
1642 *p++ = to_char(exp);
1646 #endif /* !NO_FLOATING_POINT */