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
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wint-conversion"
36 #include <TargetConditionals.h>
37 #if !TARGET_OS_DRIVERKIT
38 #define OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE 1
41 #if defined(LIBC_SCCS) && !defined(lint)
42 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
43 #endif /* LIBC_SCCS and not lint */
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.90 2009/02/28 06:06:57 das Exp $");
47 #include "xlocale_private.h"
50 * Actual printf innards.
52 * This code is large and complicated...
55 #include "namespace.h"
56 #include <sys/types.h>
67 #if 0 // xprintf pending API review
73 #include "un-namespace.h"
75 #include <os/assumes.h>
76 #include <mach-o/dyld_priv.h>
77 #include <mach/vm_region.h>
79 #include "libc_private.h"
82 #include "printflocal.h"
84 static int __sprint(FILE *, locale_t
, struct __suio
*);
86 static int __sbprintf(FILE *, locale_t
, const char *, va_list) __printflike(3, 0);
88 static char *__wcsconv(wchar_t *, int, locale_t
);
90 __private_extern__
const char *__fix_nogrouping(const char *);
93 #include "printfcommon.h"
95 struct grouping_state
{
96 char *thousands_sep
; /* locale-specific thousands separator */
97 int thousep_len
; /* length of thousands_sep */
98 const char *grouping
; /* locale-specific numeric grouping rules */
99 int lead
; /* sig figs before decimal or group sep */
100 int nseps
; /* number of group separators with ' */
101 int nrepeats
; /* number of repeats of the last group */
105 * Initialize the thousands' grouping state in preparation to print a
106 * number with ndigits digits. This routine returns the total number
107 * of bytes that will be needed.
110 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
112 struct lconv
*locale
;
114 locale
= localeconv_l(loc
);
115 gs
->grouping
= __fix_nogrouping(locale
->grouping
);
116 gs
->thousands_sep
= locale
->thousands_sep
;
117 gs
->thousep_len
= strlen(gs
->thousands_sep
);
119 gs
->nseps
= gs
->nrepeats
= 0;
121 while (*gs
->grouping
!= CHAR_MAX
) {
122 if (gs
->lead
<= *gs
->grouping
)
124 gs
->lead
-= *gs
->grouping
;
125 if (*(gs
->grouping
+1)) {
131 return ((gs
->nseps
+ gs
->nrepeats
) * gs
->thousep_len
);
135 * Print a number with thousands' separators.
138 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
139 const CHAR
*cp
, const CHAR
*ep
, locale_t loc
)
141 const CHAR
*cp0
= cp
;
143 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, loc
))
146 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
147 if (gs
->nrepeats
> 0)
153 if (io_print(iop
, gs
->thousands_sep
, gs
->thousep_len
, loc
))
155 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, loc
))
165 * Flush out all the vectors defined by the given uio,
166 * then reset it so that it can be reused.
169 __sprint(FILE *fp
, locale_t loc __unused
, struct __suio
*uio
)
173 if (uio
->uio_resid
== 0) {
177 err
= __sfvwrite(fp
, uio
);
185 * Helper function for `fprintf to unbuffered unix file': creates a
186 * temporary buffer. We only work on write-only files; this avoids
187 * worries about ungetc buffers and so forth.
190 __sbprintf(FILE *fp
, locale_t loc
, const char *fmt
, va_list ap
)
194 unsigned char buf
[BUFSIZ
];
199 /* XXX This is probably not needed. */
200 if (prepwrite(fp
) != 0)
203 /* copy the important variables */
204 fake
._flags
= fp
->_flags
& ~__SNBF
;
205 fake
._file
= fp
->_file
;
206 fake
._cookie
= fp
->_cookie
;
207 fake
._write
= fp
->_write
;
208 fake
._orientation
= fp
->_orientation
;
209 fake
._mbstate
= fp
->_mbstate
;
211 /* set up the buffer */
212 fake
._bf
._base
= fake
._p
= buf
;
213 fake
._bf
._size
= fake
._w
= sizeof(buf
);
214 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
216 /* do the work, then copy any error status */
217 ret
= __vfprintf(&fake
, loc
, fmt
, ap
);
218 if (ret
>= 0 && __fflush(&fake
))
220 if (fake
._flags
& __SERR
)
221 fp
->_flags
|= __SERR
;
227 * Convert a wide character string argument for the %ls format to a multibyte
228 * string representation. If not -1, prec specifies the maximum number of
229 * bytes to output, and also means that we can't assume that the wide char.
230 * string ends is null-terminated.
233 __wcsconv(wchar_t *wcsarg
, int prec
, locale_t loc
)
235 static const mbstate_t initial
;
237 char buf
[MB_LEN_MAX
];
242 /* Allocate space for the maximum number of bytes we could output. */
246 nbytes
= wcsrtombs_l(NULL
, (const wchar_t **)&p
, 0, &mbs
, loc
);
247 if (nbytes
== (size_t)-1)
251 * Optimisation: if the output precision is small enough,
252 * just allocate enough memory for the maximum instead of
253 * scanning the string.
262 clen
= wcrtomb_l(buf
, *p
++, &mbs
, loc
);
263 if (clen
== 0 || clen
== (size_t)-1 ||
264 nbytes
+ clen
> prec
)
270 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
273 /* Fill the output buffer. */
276 if ((nbytes
= wcsrtombs_l(convbuf
, (const wchar_t **)&p
,
277 nbytes
, &mbs
, loc
)) == (size_t)-1) {
281 convbuf
[nbytes
] = '\0';
289 vfprintf_l(FILE * __restrict fp
, locale_t loc
, const char * __restrict fmt0
, va_list ap
)
295 ret
= __xvprintf(XPRINTF_PLAIN
, NULL
, fp
, loc
, fmt0
, ap
);
301 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
307 ret
= __xvprintf(XPRINTF_PLAIN
, NULL
, fp
, __current_locale(), fmt0
, ap
);
313 * The size of the buffer we use as scratch space for integer
314 * conversions, among other things. We need enough space to
315 * write a uintmax_t in octal (plus one byte).
317 #if UINTMAX_MAX <= UINT64_MAX
320 #error "BUF must be large enough to format a uintmax_t"
323 __private_extern__
bool
324 __printf_is_memory_read_only(void *addr
, size_t __unused size
)
326 vm_address_t address
= addr
;
327 vm_size_t vmsize
= 0;
328 vm_region_basic_info_data_64_t info
;
329 mach_msg_type_number_t info_cnt
= VM_REGION_BASIC_INFO_COUNT_64
;
330 memory_object_name_t object
= MACH_PORT_NULL
;
331 kern_return_t kr
= KERN_SUCCESS
;
333 kr
= vm_region_64(mach_task_self(),
336 VM_REGION_BASIC_INFO_64
,
337 (vm_region_info_t
) &info
,
340 return (kr
== KERN_SUCCESS
) && !(info
.protection
& VM_PROT_WRITE
);
344 * Non-MT-safe version
346 __private_extern__
int
347 __vfprintf(FILE *fp
, locale_t loc
, const char *fmt0
, va_list ap
)
349 char *fmt
; /* format string */
350 int ch
; /* character from fmt */
351 ssize_t n
, n2
; /* handy integer (short term usage) */
352 char *cp
; /* handy char pointer (short term usage) */
353 int flags
; /* flags as above */
354 ssize_t ret
; /* return value accumulator */
355 ssize_t width
; /* width from format (%8d), or 0 */
356 ssize_t prec
; /* precision from format; <0 for N/A */
357 char sign
; /* sign prefix (' ', '+', '-', or \0) */
358 struct grouping_state gs
; /* thousands' grouping info */
360 #ifndef ALLOW_DYNAMIC_PERCENT_N
361 bool static_format_checked
= false;
362 #endif // ALLOW_DYNAMIC_PERCENT_N
364 #ifndef NO_FLOATING_POINT
366 * We can decompose the printed representation of floating
367 * point numbers into several parts, some of which may be empty:
369 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
372 * A: 'sign' holds this value if present; '\0' otherwise
373 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
374 * C: cp points to the string MMMNNN. Leading and trailing
375 * zeros are not in the string and must be added.
376 * D: expchar holds this character; '\0' if no exponent, e.g. %f
377 * F: at least two digits for decimal, at least one digit for hex
379 char *decimal_point
; /* locale specific decimal point */
380 int decpt_len
; /* length of decimal_point */
381 int signflag
; /* true if float is negative */
382 union { /* floating point arguments %[aAeEfFgG] */
386 int expt
; /* integer value of exponent */
387 char expchar
; /* exponent character: [eEpP\0] */
388 char *dtoaend
; /* pointer to end of converted digits */
389 int expsize
; /* character count for expstr */
390 int ndig
; /* actual number of digits returned by dtoa */
391 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
392 char *dtoaresult
; /* buffer allocated by dtoa */
395 union arg vval
; /* Vector argument. */
396 char *pct
; /* Pointer to '%' at beginning of specifier. */
397 char vsep
; /* Vector separator character. */
399 u_long ulval
; /* integer arguments %[diouxX] */
400 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
401 int base
; /* base for [diouxX] conversion */
402 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
403 ssize_t realsz
; /* field size expanded by dprec, sign, etc */
404 ssize_t size
; /* size of converted field or string */
405 ssize_t prsize
; /* max size of printed field */
406 const char *xdigs
; /* digits for %[xX] conversion */
407 struct io_state io
; /* I/O buffering state */
408 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
409 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
410 union arg
*argtable
; /* args, built due to positional arg */
411 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
412 int nextarg
; /* 1-based argument index */
413 va_list orgap
; /* original argument pointer */
414 char *convbuf
; /* wide to multibyte conversion result */
416 static const char xdigs_lower
[16] = "0123456789abcdef";
417 static const char xdigs_upper
[16] = "0123456789ABCDEF";
419 /* BEWARE, these `goto error' on error. */
420 #define PRINT(ptr, len) { \
421 if (io_print(&io, (ptr), (len), loc)) \
424 #define PAD(howmany, with) { \
425 if (io_pad(&io, (howmany), (with), loc)) \
428 #define PRINTANDPAD(p, ep, len, with) { \
429 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
433 if (io_flush(&io, loc)) \
438 * Get the argument indexed by nextarg. If the argument table is
439 * built, use it to get the argument. If its not, get the next
440 * argument (and arguments must be gotten sequentially).
442 #define GETARG(type) \
443 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
444 (nextarg++, va_arg(ap, type)))
447 * To extend shorts properly, we need both signed and unsigned
448 * argument extraction methods.
451 (flags&LONGINT ? GETARG(long) : \
452 flags&SHORTINT ? (long)(short)GETARG(int) : \
453 flags&CHARINT ? (long)(signed char)GETARG(int) : \
456 (flags&LONGINT ? GETARG(u_long) : \
457 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
458 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
459 (u_long)GETARG(u_int))
460 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
462 (flags&INTMAXT ? GETARG(intmax_t) : \
463 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
464 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
465 (intmax_t)GETARG(long long))
467 (flags&INTMAXT ? GETARG(uintmax_t) : \
468 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
469 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
470 (uintmax_t)GETARG(unsigned long long))
473 * Get * arguments, including the form *nn$. Preserve the nextarg
474 * that the argument can be gotten once the type is determined.
476 #define GETASTER(val) \
479 while (is_digit(*cp)) { \
480 n2 = 10 * n2 + to_digit(*cp); \
484 int hold = nextarg; \
485 if (argtable == NULL) { \
486 argtable = statargtable; \
487 if (__find_arguments (fmt0, orgap, &argtable)) { \
493 val = GETARG (int); \
497 val = GETARG (int); \
500 /* The following has been moved to __v2printf() */
502 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
503 if (prepwrite(fp
) != 0) {
517 #ifndef NO_FLOATING_POINT
519 decimal_point
= localeconv_l(loc
)->decimal_point
;
520 /* The overwhelmingly common case is decpt_len == 1. */
521 decpt_len
= (decimal_point
[1] == '\0' ? 1 : strlen(decimal_point
));
525 * Scan the format for conversions (`%' character).
528 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
530 if ((n
= fmt
- cp
) != 0) {
531 if (ret
+ n
>= INT_MAX
) {
544 fmt
++; /* skip over '%' */
554 vsep
= 'X'; /* Illegal value, changed to defaults later. */
558 reswitch
: switch (ch
) {
561 * ``If the space and + flags both appear, the space
562 * flag will be ignored.''
572 case ',': case ';': case ':': case '_':
578 * ``A negative field width argument is taken as a
579 * - flag followed by a positive field width.''
581 * They don't exclude field widths read from args.
598 if ((ch
= *fmt
++) == '*') {
603 while (is_digit(ch
)) {
604 prec
= 10 * prec
+ to_digit(ch
);
610 * ``Note that 0 is taken as a flag, not as the
611 * beginning of a field width.''
616 case '1': case '2': case '3': case '4':
617 case '5': case '6': case '7': case '8': case '9':
620 n
= 10 * n
+ to_digit(ch
);
622 } while (is_digit(ch
));
625 if (argtable
== NULL
) {
626 argtable
= statargtable
;
627 if (__find_arguments (fmt0
, orgap
,
637 #ifndef NO_FLOATING_POINT
643 if (flags
& SHORTINT
) {
653 if (flags
& LONGINT
) {
660 flags
|= LLONGINT
; /* not necessarily */
676 if (flags
& LONGINT
) {
677 static const mbstate_t initial
;
682 mbseqlen
= wcrtomb_l(cp
= buf
,
683 (wchar_t)GETARG(wint_t), &mbs
, loc
);
684 if (mbseqlen
== (size_t)-1) {
685 fp
->_flags
|= __SERR
;
688 size
= (int)mbseqlen
;
690 *(cp
= buf
) = GETARG(int);
704 if (flags
& INTMAX_SIZE
) {
706 if ((intmax_t)ujval
< 0) {
712 if ((long)ulval
< 0) {
719 #ifndef NO_FLOATING_POINT
723 if (flags
& VECTOR
) {
739 if (dtoaresult
!= NULL
)
740 freedtoa(dtoaresult
);
741 if (flags
& LONGDBL
) {
742 fparg
.ldbl
= GETARG(long double);
744 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
745 &expt
, &signflag
, &dtoaend
);
747 fparg
.dbl
= GETARG(double);
749 __hdtoa(fparg
.dbl
, xdigs
, prec
,
750 &expt
, &signflag
, &dtoaend
);
760 if (flags
& VECTOR
) {
766 if (prec
< 0) /* account for digit before decpt */
774 if (flags
& VECTOR
) {
784 if (flags
& VECTOR
) {
789 expchar
= ch
- ('g' - 'e');
795 if (dtoaresult
!= NULL
)
796 freedtoa(dtoaresult
);
797 if (flags
& LONGDBL
) {
798 fparg
.ldbl
= GETARG(long double);
800 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
801 &expt
, &signflag
, &dtoaend
);
803 fparg
.dbl
= GETARG(double);
805 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
806 &expt
, &signflag
, &dtoaend
);
813 if (expt
== INT_MAX
) { /* inf or nan */
815 cp
= (ch
>= 'a') ? "nan" : "NAN";
818 cp
= (ch
>= 'a') ? "inf" : "INF";
825 if (ch
== 'g' || ch
== 'G') {
826 if (expt
> -4 && expt
<= prec
) {
827 /* Make %[gG] smell like %[fF] */
837 * Make %[gG] smell like %[eE], but
838 * trim trailing zeroes if no # flag.
845 expsize
= exponent(expstr
, expt
- 1, expchar
);
846 size
= expsize
+ prec
;
847 if (prec
> 1 || flags
& ALT
)
850 /* space for digits before decimal point */
855 /* space for decimal pt and following digits */
856 if (prec
|| flags
& ALT
)
857 size
+= prec
+ decpt_len
;
858 if ((flags
& GROUPING
) && expt
> 0)
859 size
+= grouping_init(&gs
, expt
, loc
);
862 #endif /* !NO_FLOATING_POINT */
866 * Assignment-like behavior is specified if the
867 * value overflows or is otherwise unrepresentable.
868 * C99 says to use `signed char' for %hhn conversions.
870 void *ptr
= GETARG(void *);
874 #ifndef ALLOW_DYNAMIC_PERCENT_N
875 if (!static_format_checked
) {
876 static_format_checked
= __printf_is_memory_read_only((void*)fmt0
, strlen(fmt0
));
878 if (!static_format_checked
) {
879 #if OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE
880 os_crash("%%n used in a non-immutable format string: %s", fmt0
);
882 os_crash("%%n used in a non-immutable format string");
885 #endif // ALLOW_DYNAMIC_PERCENT_N
887 if (flags
& LLONGINT
)
888 *(long long *)ptr
= ret
;
889 else if (flags
& SIZET
)
890 *(ssize_t
*)ptr
= (ssize_t
)ret
;
891 else if (flags
& PTRDIFFT
)
892 *(ptrdiff_t *)ptr
= ret
;
893 else if (flags
& INTMAXT
)
894 *(intmax_t *)ptr
= ret
;
895 else if (flags
& LONGINT
)
897 else if (flags
& SHORTINT
)
899 else if (flags
& CHARINT
)
900 *(signed char *)ptr
= ret
;
903 continue; /* no output */
913 if (flags
& INTMAX_SIZE
)
921 * ``The argument shall be a pointer to void. The
922 * value of the pointer is converted to a sequence
923 * of printable characters, in an implementation-
931 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
934 flags
= flags
| INTMAXT
;
941 if (flags
& LONGINT
) {
945 if ((wcp
= GETARG(wchar_t *)) == NULL
) {
949 convbuf
= __wcsconv(wcp
, prec
, loc
);
950 if (convbuf
== NULL
) {
951 fp
->_flags
|= __SERR
;
956 } else if ((cp
= GETARG(char *)) == NULL
)
959 size_t cp_len
= (prec
>= 0) ? strnlen(cp
, prec
) : strlen(cp
);
960 if (cp_len
< INT_MAX
) {
977 if (flags
& INTMAX_SIZE
)
993 if (flags
& INTMAX_SIZE
)
998 /* leading 0x/X only if non-zero */
1000 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1004 /* unsigned conversions */
1005 nosign
: sign
= '\0';
1007 * ``... diouXx conversions ... if a precision is
1008 * specified, the 0 flag will be ignored.''
1010 * except for %#.0o and zero value
1012 number
: if ((dprec
= prec
) >= 0)
1016 * ``The result of converting a zero value with an
1017 * explicit precision of zero is no characters.''
1020 * ``The C Standard is clear enough as is. The call
1021 * printf("%#.0o", 0) should print 0.''
1022 * -- Defect Report #151
1025 if (flags
& INTMAX_SIZE
) {
1026 if (ujval
!= 0 || prec
!= 0 ||
1027 (flags
& ALT
&& base
== 8))
1028 cp
= __ujtoa(ujval
, cp
, base
,
1029 flags
& ALT
, xdigs
);
1031 if (ulval
!= 0 || prec
!= 0 ||
1032 (flags
& ALT
&& base
== 8))
1033 cp
= __ultoa(ulval
, cp
, base
,
1034 flags
& ALT
, xdigs
);
1036 size
= buf
+ BUF
- cp
;
1037 if (size
> BUF
) /* should never happen */
1038 LIBC_ABORT("size (%zd) > BUF (%d)", size
, BUF
);
1039 if ((flags
& GROUPING
) && size
!= 0)
1040 size
+= grouping_init(&gs
, size
, loc
);
1046 #endif /* VECTORS */
1047 default: /* "%?" prints ?, unless ? is NUL */
1050 /* pretend it was %c with argument ch */
1059 if (flags
& VECTOR
) {
1061 * Do the minimum amount of work necessary to construct
1062 * a format specifier that can be used to recursively
1063 * call vfprintf() for each element in the vector.
1065 int i
, j
; /* Counter. */
1066 int vcnt
; /* Number of elements in vector. */
1067 char *vfmt
; /* Pointer to format specifier. */
1069 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1070 int vwidth
= 0; /* Width specified via '*'. */
1071 int vprec
= 0; /* Precision specified via '*'. */
1072 char *vstr
; /* Used for asprintf(). */
1073 int vlen
; /* Length returned by asprintf(). */
1075 V_CHAR
, V_SHORT
, V_INT
,
1076 V_PCHAR
, V_PSHORT
, V_PINT
,
1079 V_LONGLONG
, V_PLONGLONG
,
1081 #endif /* V64TYPE */
1084 vval
.vectorarg
= GETARG(VECTORTYPE
);
1086 * Set vfmt. If vfmt_buf may not be big enough,
1087 * malloc() space, taking care to free it later.
1088 * (EXTRAHH is for possible extra "hh")
1090 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1093 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1095 /* Set the separator character, if not specified. */
1103 /* Create the format specifier. */
1104 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1106 case ',': case ';': case ':': case '_':
1107 case 'v': case 'h': case 'l':
1111 if (pct
[i
- 1] != '.')
1122 * Determine the number of elements in the vector and
1123 * finish up the format specifier.
1125 if (flags
& SHORTINT
) {
1139 } else if (flags
& LONGINT
) {
1141 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1143 } else if (flags
& LLONGINT
) {
1165 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1169 * The default case should never
1176 #endif /* V64TYPE */
1191 * The default case should never
1206 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1212 /* Get a vector element. */
1214 #define VPRINT(type, ind, args...) do { \
1217 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1220 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1223 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1226 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1229 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1232 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1235 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1238 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1241 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1244 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1248 PRINT(vstr, vlen); \
1252 #else /* !V64TYPE */
1253 #define VPRINT(type, ind, args...) do { \
1256 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1259 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1262 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1265 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1268 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1271 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1274 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1278 PRINT(vstr, vlen); \
1282 #endif /* V64TYPE */
1284 /* Actually print. */
1287 /* First element. */
1289 for (i
= 1; i
< vcnt
; i
++) {
1298 /* First element. */
1299 VPRINT(vtype
, 0, prec
);
1300 for (i
= 1; i
< vcnt
; i
++) {
1306 VPRINT(vtype
, i
, prec
);
1311 /* First element. */
1312 VPRINT(vtype
, 0, width
);
1313 for (i
= 1; i
< vcnt
; i
++) {
1319 VPRINT(vtype
, i
, width
);
1322 /* First element. */
1323 VPRINT(vtype
, 0, width
, prec
);
1324 for (i
= 1; i
< vcnt
; i
++) {
1330 VPRINT(vtype
, i
, width
, prec
);
1336 if (vfmt
!= vfmt_buf
)
1341 #endif /* VECTORS */
1343 * All reasonable formats wind up here. At this point, `cp'
1344 * points to a string which (if not flags&LADJUST) should be
1345 * padded out to `width' places. If flags&ZEROPAD, it should
1346 * first be prefixed by any sign or other prefix; otherwise,
1347 * it should be blank padded before the prefix is emitted.
1348 * After any left-hand padding and prefixing, emit zeroes
1349 * required by a decimal [diouxX] precision, then print the
1350 * string proper, then emit zeroes required by any leftover
1351 * floating precision; finally, if LADJUST, pad with blanks.
1353 * Compute actual size, so we know how much to pad.
1354 * size excludes decimal prec; realsz includes it.
1356 realsz
= dprec
> size
? dprec
: size
;
1362 prsize
= width
> realsz
? width
: realsz
;
1363 if (ret
+ prsize
>= INT_MAX
) {
1369 /* right-adjusting blank padding */
1370 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1371 PAD(width
- realsz
, blanks
);
1377 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1382 /* right-adjusting zero padding */
1383 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1384 PAD(width
- realsz
, zeroes
);
1386 /* the string or number proper */
1387 #ifndef NO_FLOATING_POINT
1388 if ((flags
& FPT
) == 0) {
1390 /* leading zeroes from decimal precision */
1391 PAD(dprec
- size
, zeroes
);
1393 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, loc
) < 0)
1398 #ifndef NO_FLOATING_POINT
1399 } else { /* glue together f_p fragments */
1400 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1403 if (prec
|| flags
& ALT
)
1404 PRINT(decimal_point
,decpt_len
);
1406 /* already handled initial 0's */
1410 n
= grouping_print(&gs
, &io
,
1416 PRINTANDPAD(cp
, dtoaend
,
1420 if (prec
|| flags
& ALT
)
1421 PRINT(decimal_point
,decpt_len
);
1423 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1424 } else { /* %[eE] or sufficiently long %[gG] */
1425 if (prec
> 1 || flags
& ALT
) {
1427 PRINT(decimal_point
, decpt_len
);
1429 PAD(prec
- ndig
, zeroes
);
1432 PRINT(expstr
, expsize
);
1436 /* left-adjusting padding (always blank) */
1437 if (flags
& LADJUST
)
1438 PAD(width
- realsz
, blanks
);
1440 /* finally, adjust ret */
1443 FLUSH(); /* copy out the I/O vectors */
1449 #ifndef NO_FLOATING_POINT
1450 if (dtoaresult
!= NULL
)
1451 freedtoa(dtoaresult
);
1456 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1458 return (ret
< 0 || ret
>= INT_MAX
) ? -1 : (int)ret
;
1461 #pragma clang diagnostic pop