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 * Copyright (c) 2011 The FreeBSD Foundation
10 * Portions of this software were developed by David Chisnall
11 * under sponsorship from the FreeBSD Foundation.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)vfprintf.c 8.1 (Berkeley) 6/4/93
38 * $FreeBSD: head/lib/libc/stdio/vfprintf.c 268930 2014-07-20 21:24:29Z pfg $
42 * Actual printf innards.
44 * This code is large and complicated...
47 #include "namespace.h"
48 #include <sys/types.h>
53 #include <pthread.h> /* for FAKE_FILE PTHREAD_MUTEX_INITIALIZER */
64 #include "xlocale_private.h"
65 #include "un-namespace.h"
67 #include "libc_private.h"
69 #include "printflocal.h"
71 static int __sprint(FILE *, struct __suio
*, locale_t
);
72 static int __sbprintf(FILE *, locale_t
, const char *, va_list) __printflike(3, 0)
74 static char *__wcsconv(wchar_t *, int);
77 #include "printfcommon.h"
79 struct grouping_state
{
80 char *thousands_sep
; /* locale-specific thousands separator */
81 int thousep_len
; /* length of thousands_sep */
82 const char *grouping
; /* locale-specific numeric grouping rules */
83 int lead
; /* sig figs before decimal or group sep */
84 int nseps
; /* number of group separators with ' */
85 int nrepeats
; /* number of repeats of the last group */
89 * Initialize the thousands' grouping state in preparation to print a
90 * number with ndigits digits. This routine returns the total number
91 * of bytes that will be needed.
94 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
98 locale
= localeconv_l(loc
);
99 gs
->grouping
= locale
->grouping
;
100 gs
->thousands_sep
= locale
->thousands_sep
;
101 gs
->thousep_len
= strlen(gs
->thousands_sep
);
103 gs
->nseps
= gs
->nrepeats
= 0;
105 while (*gs
->grouping
!= CHAR_MAX
) {
106 if (gs
->lead
<= *gs
->grouping
)
108 gs
->lead
-= *gs
->grouping
;
109 if (*(gs
->grouping
+1)) {
115 return ((gs
->nseps
+ gs
->nrepeats
) * gs
->thousep_len
);
119 * Print a number with thousands' separators.
122 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
123 const CHAR
*cp
, const CHAR
*ep
, locale_t locale
)
125 const CHAR
*cp0
= cp
;
127 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, locale
))
130 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
131 if (gs
->nrepeats
> 0)
137 if (io_print(iop
, gs
->thousands_sep
, gs
->thousep_len
, locale
))
139 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, locale
))
149 * Flush out all the vectors defined by the given uio,
150 * then reset it so that it can be reused.
153 __sprint(FILE *fp
, struct __suio
*uio
, locale_t locale __unused
)
157 if (uio
->uio_resid
== 0) {
161 err
= __sfvwrite(fp
, uio
);
168 * Helper function for `fprintf to unbuffered unix file': creates a
169 * temporary buffer. We only work on write-only files; this avoids
170 * worries about ungetc buffers and so forth.
173 __sbprintf(FILE *fp
, locale_t locale
, const char *fmt
, va_list ap
)
176 FILE fake
= FAKE_FILE
;
177 unsigned char buf
[BUFSIZ
];
179 /* XXX This is probably not needed. */
180 if (prepwrite(fp
) != 0)
183 /* copy the important variables */
184 fake
.pub
._flags
= fp
->pub
._flags
& ~__SNBF
;
185 fake
.pub
._fileno
= fp
->pub
._fileno
;
186 fake
._cookie
= fp
->_cookie
;
187 fake
._write
= fp
->_write
;
189 /* set up the buffer */
190 fake
._bf
._base
= fake
.pub
._p
= buf
;
191 fake
._bf
._size
= fake
.pub
._w
= sizeof(buf
);
192 fake
.pub
._lbfsize
= 0; /* not actually used, but Just In Case */
194 /* do the work, then copy any error status */
195 ret
= __vfprintf(&fake
, locale
, fmt
, ap
);
196 if (ret
>= 0 && __fflush(&fake
))
198 if (fake
.pub
._flags
& __SERR
)
199 fp
->pub
._flags
|= __SERR
;
204 * Convert a wide character string argument for the %ls format to a multibyte
205 * string representation. If not -1, prec specifies the maximum number of
206 * bytes to output, and also means that we can't assume that the wide char.
207 * string ends is null-terminated.
210 __wcsconv(wchar_t *wcsarg
, int prec
)
212 static const mbstate_t initial
;
214 char buf
[MB_LEN_MAX
];
219 /* Allocate space for the maximum number of bytes we could output. */
223 nbytes
= wcsrtombs(NULL
, (const wchar_t **)&p
, 0, &mbs
);
224 if (nbytes
== (size_t)-1)
228 * Optimisation: if the output precision is small enough,
229 * just allocate enough memory for the maximum instead of
230 * scanning the string.
239 clen
= wcrtomb(buf
, *p
++, &mbs
);
240 if (clen
== 0 || clen
== (size_t)-1 ||
241 nbytes
+ clen
> prec
)
247 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
250 /* Fill the output buffer. */
253 if ((nbytes
= wcsrtombs(convbuf
, (const wchar_t **)&p
,
254 nbytes
, &mbs
)) == (size_t)-1) {
258 convbuf
[nbytes
] = '\0';
266 vfprintf_l(FILE * __restrict fp
, locale_t locale
, const char * __restrict fmt0
,
273 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
274 if ((fp
->pub
._flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
275 fp
->pub
._fileno
>= 0)
276 ret
= __sbprintf(fp
, locale
, fmt0
, ap
);
278 ret
= __vfprintf(fp
, locale
, fmt0
, ap
);
283 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
285 return vfprintf_l(fp
, __get_locale(), fmt0
, ap
);
289 * The size of the buffer we use as scratch space for integer
290 * conversions, among other things. We need enough space to
291 * write a uintmax_t in octal (plus one byte).
293 #if UINTMAX_MAX <= UINT64_MAX
296 #error "BUF must be large enough to format a uintmax_t"
300 * Non-MT-safe version
303 __vfprintf(FILE *fp
, locale_t locale
, const char *fmt0
, va_list ap
)
305 char *fmt
; /* format string */
306 int ch
; /* character from fmt */
307 int n
, n2
; /* handy integer (short term usage) */
308 char *cp
; /* handy char pointer (short term usage) */
309 int flags
; /* flags as above */
310 int ret
; /* return value accumulator */
311 int width
; /* width from format (%8d), or 0 */
312 int prec
; /* precision from format; <0 for N/A */
313 char sign
; /* sign prefix (' ', '+', '-', or \0) */
314 struct grouping_state gs
; /* thousands' grouping info */
316 #ifndef NO_FLOATING_POINT
318 * We can decompose the printed representation of floating
319 * point numbers into several parts, some of which may be empty:
321 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
324 * A: 'sign' holds this value if present; '\0' otherwise
325 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
326 * C: cp points to the string MMMNNN. Leading and trailing
327 * zeros are not in the string and must be added.
328 * D: expchar holds this character; '\0' if no exponent, e.g. %f
329 * F: at least two digits for decimal, at least one digit for hex
331 char *decimal_point
; /* locale specific decimal point */
332 int decpt_len
; /* length of decimal_point */
333 int signflag
; /* true if float is negative */
334 union { /* floating point arguments %[aAeEfFgG] */
338 int expt
; /* integer value of exponent */
339 char expchar
; /* exponent character: [eEpP\0] */
340 char *dtoaend
; /* pointer to end of converted digits */
341 int expsize
; /* character count for expstr */
342 int ndig
; /* actual number of digits returned by dtoa */
343 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
344 char *dtoaresult
; /* buffer allocated by dtoa */
346 u_long ulval
; /* integer arguments %[diouxX] */
347 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
348 int base
; /* base for [diouxX] conversion */
349 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
350 int realsz
; /* field size expanded by dprec, sign, etc */
351 int size
; /* size of converted field or string */
352 int prsize
; /* max size of printed field */
353 const char *xdigs
; /* digits for %[xX] conversion */
354 struct io_state io
; /* I/O buffering state */
355 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
356 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
357 union arg
*argtable
; /* args, built due to positional arg */
358 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
359 int nextarg
; /* 1-based argument index */
360 va_list orgap
; /* original argument pointer */
361 char *convbuf
; /* wide to multibyte conversion result */
363 static const char xdigs_lower
[16] = "0123456789abcdef";
364 static const char xdigs_upper
[16] = "0123456789ABCDEF";
366 /* BEWARE, these `goto error' on error. */
367 #define PRINT(ptr, len) { \
368 if (io_print(&io, (ptr), (len), locale)) \
371 #define PAD(howmany, with) { \
372 if (io_pad(&io, (howmany), (with), locale)) \
375 #define PRINTANDPAD(p, ep, len, with) { \
376 if (io_printandpad(&io, (p), (ep), (len), (with), locale)) \
380 if (io_flush(&io, locale)) \
385 * Get the argument indexed by nextarg. If the argument table is
386 * built, use it to get the argument. If its not, get the next
387 * argument (and arguments must be gotten sequentially).
389 #define GETARG(type) \
390 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
391 (nextarg++, va_arg(ap, type)))
394 * To extend shorts properly, we need both signed and unsigned
395 * argument extraction methods.
398 (flags&LONGINT ? GETARG(long) : \
399 flags&SHORTINT ? (long)(short)GETARG(int) : \
400 flags&CHARINT ? (long)(signed char)GETARG(int) : \
403 (flags&LONGINT ? GETARG(u_long) : \
404 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
405 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
406 (u_long)GETARG(u_int))
407 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
409 (flags&INTMAXT ? GETARG(intmax_t) : \
410 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
411 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
412 (intmax_t)GETARG(long long))
414 (flags&INTMAXT ? GETARG(uintmax_t) : \
415 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
416 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
417 (uintmax_t)GETARG(unsigned long long))
420 * Get * arguments, including the form *nn$. Preserve the nextarg
421 * that the argument can be gotten once the type is determined.
423 #define GETASTER(val) \
426 while (is_digit(*cp)) { \
427 n2 = 10 * n2 + to_digit(*cp); \
431 int hold = nextarg; \
432 if (argtable == NULL) { \
433 argtable = statargtable; \
434 if (__find_arguments (fmt0, orgap, &argtable)) { \
440 val = GETARG (int); \
444 val = GETARG (int); \
447 if (__use_xprintf
== 0 && getenv("USE_XPRINTF"))
449 if (__use_xprintf
> 0)
450 return (__xvprintf(fp
, fmt0
, ap
));
452 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
453 if (prepwrite(fp
) != 0) {
465 #ifndef NO_FLOATING_POINT
467 decimal_point
= localeconv_l(locale
)->decimal_point
;
468 /* The overwhelmingly common case is decpt_len == 1. */
469 decpt_len
= (decimal_point
[1] == '\0' ? 1 : strlen(decimal_point
));
473 * Scan the format for conversions (`%' character).
476 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
478 if ((n
= fmt
- cp
) != 0) {
479 if ((unsigned)ret
+ n
> INT_MAX
) {
489 fmt
++; /* skip over '%' */
500 reswitch
: switch (ch
) {
503 * ``If the space and + flags both appear, the space
504 * flag will be ignored.''
515 * ``A negative field width argument is taken as a
516 * - flag followed by a positive field width.''
518 * They don't exclude field widths read from args.
535 if ((ch
= *fmt
++) == '*') {
540 while (is_digit(ch
)) {
541 prec
= 10 * prec
+ to_digit(ch
);
547 * ``Note that 0 is taken as a flag, not as the
548 * beginning of a field width.''
553 case '1': case '2': case '3': case '4':
554 case '5': case '6': case '7': case '8': case '9':
557 n
= 10 * n
+ to_digit(ch
);
559 } while (is_digit(ch
));
562 if (argtable
== NULL
) {
563 argtable
= statargtable
;
564 if (__find_arguments (fmt0
, orgap
,
574 #ifndef NO_FLOATING_POINT
580 if (flags
& SHORTINT
) {
590 if (flags
& LONGINT
) {
597 flags
|= LLONGINT
; /* not necessarily */
609 if (flags
& LONGINT
) {
610 static const mbstate_t initial
;
615 mbseqlen
= wcrtomb(cp
= buf
,
616 (wchar_t)GETARG(wint_t), &mbs
);
617 if (mbseqlen
== (size_t)-1) {
618 fp
->pub
._flags
|= __SERR
;
621 size
= (int)mbseqlen
;
623 *(cp
= buf
) = GETARG(int);
633 if (flags
& INTMAX_SIZE
) {
635 if ((intmax_t)ujval
< 0) {
641 if ((long)ulval
< 0) {
648 #ifndef NO_FLOATING_POINT
662 if (dtoaresult
!= NULL
)
663 freedtoa(dtoaresult
);
664 if (flags
& LONGDBL
) {
665 fparg
.ldbl
= GETARG(long double);
667 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
668 &expt
, &signflag
, &dtoaend
);
670 fparg
.dbl
= GETARG(double);
672 __hdtoa(fparg
.dbl
, xdigs
, prec
,
673 &expt
, &signflag
, &dtoaend
);
683 if (prec
< 0) /* account for digit before decpt */
694 expchar
= ch
- ('g' - 'e');
700 if (dtoaresult
!= NULL
)
701 freedtoa(dtoaresult
);
702 if (flags
& LONGDBL
) {
703 fparg
.ldbl
= GETARG(long double);
705 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
706 &expt
, &signflag
, &dtoaend
);
708 fparg
.dbl
= GETARG(double);
710 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
711 &expt
, &signflag
, &dtoaend
);
718 if (expt
== INT_MAX
) { /* inf or nan */
720 cp
= (ch
>= 'a') ? "nan" : "NAN";
723 cp
= (ch
>= 'a') ? "inf" : "INF";
730 if (ch
== 'g' || ch
== 'G') {
731 if (expt
> -4 && expt
<= prec
) {
732 /* Make %[gG] smell like %[fF] */
742 * Make %[gG] smell like %[eE], but
743 * trim trailing zeroes if no # flag.
750 expsize
= exponent(expstr
, expt
- 1, expchar
);
751 size
= expsize
+ prec
;
752 if (prec
> 1 || flags
& ALT
)
755 /* space for digits before decimal point */
760 /* space for decimal pt and following digits */
761 if (prec
|| flags
& ALT
)
762 size
+= prec
+ decpt_len
;
763 if ((flags
& GROUPING
) && expt
> 0)
764 size
+= grouping_init(&gs
, expt
, locale
);
767 #endif /* !NO_FLOATING_POINT */
770 * Assignment-like behavior is specified if the
771 * value overflows or is otherwise unrepresentable.
772 * C99 says to use `signed char' for %hhn conversions.
774 if (flags
& LLONGINT
)
775 *GETARG(long long *) = ret
;
776 else if (flags
& SIZET
)
777 *GETARG(ssize_t
*) = (ssize_t
)ret
;
778 else if (flags
& PTRDIFFT
)
779 *GETARG(ptrdiff_t *) = ret
;
780 else if (flags
& INTMAXT
)
781 *GETARG(intmax_t *) = ret
;
782 else if (flags
& LONGINT
)
783 *GETARG(long *) = ret
;
784 else if (flags
& SHORTINT
)
785 *GETARG(short *) = ret
;
786 else if (flags
& CHARINT
)
787 *GETARG(signed char *) = ret
;
789 *GETARG(int *) = ret
;
790 continue; /* no output */
795 if (flags
& INTMAX_SIZE
)
803 * ``The argument shall be a pointer to void. The
804 * value of the pointer is converted to a sequence
805 * of printable characters, in an implementation-
809 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
812 flags
= flags
| INTMAXT
;
819 if (flags
& LONGINT
) {
824 if ((wcp
= GETARG(wchar_t *)) == NULL
)
827 convbuf
= __wcsconv(wcp
, prec
);
828 if (convbuf
== NULL
) {
829 fp
->pub
._flags
|= __SERR
;
834 } else if ((cp
= GETARG(char *)) == NULL
)
836 size
= (prec
>= 0) ? strnlen(cp
, prec
) : strlen(cp
);
843 if (flags
& INTMAX_SIZE
)
855 if (flags
& INTMAX_SIZE
)
860 /* leading 0x/X only if non-zero */
862 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
866 /* unsigned conversions */
869 * ``... diouXx conversions ... if a precision is
870 * specified, the 0 flag will be ignored.''
873 number
: if ((dprec
= prec
) >= 0)
877 * ``The result of converting a zero value with an
878 * explicit precision of zero is no characters.''
881 * ``The C Standard is clear enough as is. The call
882 * printf("%#.0o", 0) should print 0.''
883 * -- Defect Report #151
886 if (flags
& INTMAX_SIZE
) {
887 if (ujval
!= 0 || prec
!= 0 ||
888 (flags
& ALT
&& base
== 8))
889 cp
= __ujtoa(ujval
, cp
, base
,
892 if (ulval
!= 0 || prec
!= 0 ||
893 (flags
& ALT
&& base
== 8))
894 cp
= __ultoa(ulval
, cp
, base
,
897 size
= buf
+ BUF
- cp
;
898 if (size
> BUF
) /* should never happen */
900 if ((flags
& GROUPING
) && size
!= 0)
901 size
+= grouping_init(&gs
, size
, locale
);
903 default: /* "%?" prints ?, unless ? is NUL */
906 /* pretend it was %c with argument ch */
915 * All reasonable formats wind up here. At this point, `cp'
916 * points to a string which (if not flags&LADJUST) should be
917 * padded out to `width' places. If flags&ZEROPAD, it should
918 * first be prefixed by any sign or other prefix; otherwise,
919 * it should be blank padded before the prefix is emitted.
920 * After any left-hand padding and prefixing, emit zeroes
921 * required by a decimal [diouxX] precision, then print the
922 * string proper, then emit zeroes required by any leftover
923 * floating precision; finally, if LADJUST, pad with blanks.
925 * Compute actual size, so we know how much to pad.
926 * size excludes decimal prec; realsz includes it.
928 realsz
= dprec
> size
? dprec
: size
;
934 prsize
= width
> realsz
? width
: realsz
;
935 if ((unsigned)ret
+ prsize
> INT_MAX
) {
941 /* right-adjusting blank padding */
942 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
943 PAD(width
- realsz
, blanks
);
949 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
954 /* right-adjusting zero padding */
955 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
956 PAD(width
- realsz
, zeroes
);
958 /* the string or number proper */
959 #ifndef NO_FLOATING_POINT
960 if ((flags
& FPT
) == 0) {
962 /* leading zeroes from decimal precision */
963 PAD(dprec
- size
, zeroes
);
965 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, locale
) < 0)
970 #ifndef NO_FLOATING_POINT
971 } else { /* glue together f_p fragments */
972 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
975 if (prec
|| flags
& ALT
)
976 PRINT(decimal_point
,decpt_len
);
978 /* already handled initial 0's */
982 n
= grouping_print(&gs
, &io
,
983 cp
, dtoaend
, locale
);
988 PRINTANDPAD(cp
, dtoaend
,
992 if (prec
|| flags
& ALT
)
993 PRINT(decimal_point
,decpt_len
);
995 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
996 } else { /* %[eE] or sufficiently long %[gG] */
997 if (prec
> 1 || flags
& ALT
) {
999 PRINT(decimal_point
, decpt_len
);
1001 PAD(prec
- ndig
, zeroes
);
1004 PRINT(expstr
, expsize
);
1008 /* left-adjusting padding (always blank) */
1009 if (flags
& LADJUST
)
1010 PAD(width
- realsz
, blanks
);
1012 /* finally, adjust ret */
1015 FLUSH(); /* copy out the I/O vectors */
1021 #ifndef NO_FLOATING_POINT
1022 if (dtoaresult
!= NULL
)
1023 freedtoa(dtoaresult
);
1025 if (convbuf
!= NULL
)
1029 if ((argtable
!= NULL
) && (argtable
!= statargtable
))