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 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.90 2009/02/28 06:06:57 das Exp $");
39 #include "xlocale_private.h"
42 * Actual printf innards.
44 * This code is large and complicated...
47 #include "namespace.h"
48 #include <sys/types.h>
59 #if 0 // xprintf pending API review
65 #include "un-namespace.h"
67 #include <os/assumes.h>
68 #include <mach-o/dyld_priv.h>
69 #include <mach/vm_region.h>
71 #include "libc_private.h"
74 #include "printflocal.h"
76 static int __sprint(FILE *, locale_t
, struct __suio
*);
78 static int __sbprintf(FILE *, locale_t
, const char *, va_list) __printflike(3, 0);
80 static char *__wcsconv(wchar_t *, int, locale_t
);
82 __private_extern__
const char *__fix_nogrouping(const char *);
85 #include "printfcommon.h"
87 struct grouping_state
{
88 char *thousands_sep
; /* locale-specific thousands separator */
89 int thousep_len
; /* length of thousands_sep */
90 const char *grouping
; /* locale-specific numeric grouping rules */
91 int lead
; /* sig figs before decimal or group sep */
92 int nseps
; /* number of group separators with ' */
93 int nrepeats
; /* number of repeats of the last group */
97 * Initialize the thousands' grouping state in preparation to print a
98 * number with ndigits digits. This routine returns the total number
99 * of bytes that will be needed.
102 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
104 struct lconv
*locale
;
106 locale
= localeconv_l(loc
);
107 gs
->grouping
= __fix_nogrouping(locale
->grouping
);
108 gs
->thousands_sep
= locale
->thousands_sep
;
109 gs
->thousep_len
= strlen(gs
->thousands_sep
);
111 gs
->nseps
= gs
->nrepeats
= 0;
113 while (*gs
->grouping
!= CHAR_MAX
) {
114 if (gs
->lead
<= *gs
->grouping
)
116 gs
->lead
-= *gs
->grouping
;
117 if (*(gs
->grouping
+1)) {
123 return ((gs
->nseps
+ gs
->nrepeats
) * gs
->thousep_len
);
127 * Print a number with thousands' separators.
130 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
131 const CHAR
*cp
, const CHAR
*ep
, locale_t loc
)
133 const CHAR
*cp0
= cp
;
135 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, loc
))
138 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
139 if (gs
->nrepeats
> 0)
145 if (io_print(iop
, gs
->thousands_sep
, gs
->thousep_len
, loc
))
147 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, loc
))
157 * Flush out all the vectors defined by the given uio,
158 * then reset it so that it can be reused.
161 __sprint(FILE *fp
, locale_t loc __unused
, struct __suio
*uio
)
165 if (uio
->uio_resid
== 0) {
169 err
= __sfvwrite(fp
, uio
);
177 * Helper function for `fprintf to unbuffered unix file': creates a
178 * temporary buffer. We only work on write-only files; this avoids
179 * worries about ungetc buffers and so forth.
182 __sbprintf(FILE *fp
, locale_t loc
, const char *fmt
, va_list ap
)
186 unsigned char buf
[BUFSIZ
];
191 /* XXX This is probably not needed. */
192 if (prepwrite(fp
) != 0)
195 /* copy the important variables */
196 fake
._flags
= fp
->_flags
& ~__SNBF
;
197 fake
._file
= fp
->_file
;
198 fake
._cookie
= fp
->_cookie
;
199 fake
._write
= fp
->_write
;
200 fake
._orientation
= fp
->_orientation
;
201 fake
._mbstate
= fp
->_mbstate
;
203 /* set up the buffer */
204 fake
._bf
._base
= fake
._p
= buf
;
205 fake
._bf
._size
= fake
._w
= sizeof(buf
);
206 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
208 /* do the work, then copy any error status */
209 ret
= __vfprintf(&fake
, loc
, fmt
, ap
);
210 if (ret
>= 0 && __fflush(&fake
))
212 if (fake
._flags
& __SERR
)
213 fp
->_flags
|= __SERR
;
219 * Convert a wide character string argument for the %ls format to a multibyte
220 * string representation. If not -1, prec specifies the maximum number of
221 * bytes to output, and also means that we can't assume that the wide char.
222 * string ends is null-terminated.
225 __wcsconv(wchar_t *wcsarg
, int prec
, locale_t loc
)
227 static const mbstate_t initial
;
229 char buf
[MB_LEN_MAX
];
234 /* Allocate space for the maximum number of bytes we could output. */
238 nbytes
= wcsrtombs_l(NULL
, (const wchar_t **)&p
, 0, &mbs
, loc
);
239 if (nbytes
== (size_t)-1)
243 * Optimisation: if the output precision is small enough,
244 * just allocate enough memory for the maximum instead of
245 * scanning the string.
254 clen
= wcrtomb_l(buf
, *p
++, &mbs
, loc
);
255 if (clen
== 0 || clen
== (size_t)-1 ||
256 nbytes
+ clen
> prec
)
262 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
265 /* Fill the output buffer. */
268 if ((nbytes
= wcsrtombs_l(convbuf
, (const wchar_t **)&p
,
269 nbytes
, &mbs
, loc
)) == (size_t)-1) {
273 convbuf
[nbytes
] = '\0';
281 vfprintf_l(FILE * __restrict fp
, locale_t loc
, const char * __restrict fmt0
, va_list ap
)
287 ret
= __xvprintf(XPRINTF_PLAIN
, NULL
, fp
, loc
, fmt0
, ap
);
293 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
299 ret
= __xvprintf(XPRINTF_PLAIN
, NULL
, fp
, __current_locale(), fmt0
, ap
);
305 * The size of the buffer we use as scratch space for integer
306 * conversions, among other things. We need enough space to
307 * write a uintmax_t in octal (plus one byte).
309 #if UINTMAX_MAX <= UINT64_MAX
312 #error "BUF must be large enough to format a uintmax_t"
315 __private_extern__
bool
316 __printf_is_memory_read_only(void *addr
, size_t __unused size
)
318 vm_address_t address
= addr
;
319 vm_size_t vmsize
= 0;
320 vm_region_basic_info_data_64_t info
;
321 mach_msg_type_number_t info_cnt
= VM_REGION_BASIC_INFO_COUNT_64
;
322 memory_object_name_t object
= MACH_PORT_NULL
;
323 kern_return_t kr
= KERN_SUCCESS
;
325 kr
= vm_region_64(mach_task_self(),
328 VM_REGION_BASIC_INFO_64
,
329 (vm_region_info_t
) &info
,
332 return (kr
== KERN_SUCCESS
) && !(info
.protection
& VM_PROT_WRITE
);
336 * Non-MT-safe version
338 __private_extern__
int
339 __vfprintf(FILE *fp
, locale_t loc
, const char *fmt0
, va_list ap
)
341 char *fmt
; /* format string */
342 int ch
; /* character from fmt */
343 ssize_t n
, n2
; /* handy integer (short term usage) */
344 char *cp
; /* handy char pointer (short term usage) */
345 int flags
; /* flags as above */
346 ssize_t ret
; /* return value accumulator */
347 ssize_t width
; /* width from format (%8d), or 0 */
348 ssize_t prec
; /* precision from format; <0 for N/A */
349 char sign
; /* sign prefix (' ', '+', '-', or \0) */
350 struct grouping_state gs
; /* thousands' grouping info */
352 #ifndef ALLOW_DYNAMIC_PERCENT_N
353 bool static_format_checked
= false;
354 #endif // ALLOW_DYNAMIC_PERCENT_N
356 #ifndef NO_FLOATING_POINT
358 * We can decompose the printed representation of floating
359 * point numbers into several parts, some of which may be empty:
361 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
364 * A: 'sign' holds this value if present; '\0' otherwise
365 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
366 * C: cp points to the string MMMNNN. Leading and trailing
367 * zeros are not in the string and must be added.
368 * D: expchar holds this character; '\0' if no exponent, e.g. %f
369 * F: at least two digits for decimal, at least one digit for hex
371 char *decimal_point
; /* locale specific decimal point */
372 int decpt_len
; /* length of decimal_point */
373 int signflag
; /* true if float is negative */
374 union { /* floating point arguments %[aAeEfFgG] */
378 int expt
; /* integer value of exponent */
379 char expchar
; /* exponent character: [eEpP\0] */
380 char *dtoaend
; /* pointer to end of converted digits */
381 int expsize
; /* character count for expstr */
382 int ndig
; /* actual number of digits returned by dtoa */
383 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
384 char *dtoaresult
; /* buffer allocated by dtoa */
387 union arg vval
; /* Vector argument. */
388 char *pct
; /* Pointer to '%' at beginning of specifier. */
389 char vsep
; /* Vector separator character. */
391 u_long ulval
; /* integer arguments %[diouxX] */
392 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
393 int base
; /* base for [diouxX] conversion */
394 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
395 ssize_t realsz
; /* field size expanded by dprec, sign, etc */
396 ssize_t size
; /* size of converted field or string */
397 ssize_t prsize
; /* max size of printed field */
398 const char *xdigs
; /* digits for %[xX] conversion */
399 struct io_state io
; /* I/O buffering state */
400 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
401 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
402 union arg
*argtable
; /* args, built due to positional arg */
403 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
404 int nextarg
; /* 1-based argument index */
405 va_list orgap
; /* original argument pointer */
406 char *convbuf
; /* wide to multibyte conversion result */
408 static const char xdigs_lower
[16] = "0123456789abcdef";
409 static const char xdigs_upper
[16] = "0123456789ABCDEF";
411 /* BEWARE, these `goto error' on error. */
412 #define PRINT(ptr, len) { \
413 if (io_print(&io, (ptr), (len), loc)) \
416 #define PAD(howmany, with) { \
417 if (io_pad(&io, (howmany), (with), loc)) \
420 #define PRINTANDPAD(p, ep, len, with) { \
421 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
425 if (io_flush(&io, loc)) \
430 * Get the argument indexed by nextarg. If the argument table is
431 * built, use it to get the argument. If its not, get the next
432 * argument (and arguments must be gotten sequentially).
434 #define GETARG(type) \
435 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
436 (nextarg++, va_arg(ap, type)))
439 * To extend shorts properly, we need both signed and unsigned
440 * argument extraction methods.
443 (flags&LONGINT ? GETARG(long) : \
444 flags&SHORTINT ? (long)(short)GETARG(int) : \
445 flags&CHARINT ? (long)(signed char)GETARG(int) : \
448 (flags&LONGINT ? GETARG(u_long) : \
449 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
450 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
451 (u_long)GETARG(u_int))
452 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
454 (flags&INTMAXT ? GETARG(intmax_t) : \
455 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
456 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
457 (intmax_t)GETARG(long long))
459 (flags&INTMAXT ? GETARG(uintmax_t) : \
460 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
461 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
462 (uintmax_t)GETARG(unsigned long long))
465 * Get * arguments, including the form *nn$. Preserve the nextarg
466 * that the argument can be gotten once the type is determined.
468 #define GETASTER(val) \
471 while (is_digit(*cp)) { \
472 n2 = 10 * n2 + to_digit(*cp); \
476 int hold = nextarg; \
477 if (argtable == NULL) { \
478 argtable = statargtable; \
479 if (__find_arguments (fmt0, orgap, &argtable)) { \
485 val = GETARG (int); \
489 val = GETARG (int); \
492 /* The following has been moved to __v2printf() */
494 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
495 if (prepwrite(fp
) != 0) {
509 #ifndef NO_FLOATING_POINT
511 decimal_point
= localeconv_l(loc
)->decimal_point
;
512 /* The overwhelmingly common case is decpt_len == 1. */
513 decpt_len
= (decimal_point
[1] == '\0' ? 1 : strlen(decimal_point
));
517 * Scan the format for conversions (`%' character).
520 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
522 if ((n
= fmt
- cp
) != 0) {
523 if (ret
+ n
>= INT_MAX
) {
536 fmt
++; /* skip over '%' */
546 vsep
= 'X'; /* Illegal value, changed to defaults later. */
550 reswitch
: switch (ch
) {
553 * ``If the space and + flags both appear, the space
554 * flag will be ignored.''
564 case ',': case ';': case ':': case '_':
570 * ``A negative field width argument is taken as a
571 * - flag followed by a positive field width.''
573 * They don't exclude field widths read from args.
590 if ((ch
= *fmt
++) == '*') {
595 while (is_digit(ch
)) {
596 prec
= 10 * prec
+ to_digit(ch
);
602 * ``Note that 0 is taken as a flag, not as the
603 * beginning of a field width.''
608 case '1': case '2': case '3': case '4':
609 case '5': case '6': case '7': case '8': case '9':
612 n
= 10 * n
+ to_digit(ch
);
614 } while (is_digit(ch
));
617 if (argtable
== NULL
) {
618 argtable
= statargtable
;
619 if (__find_arguments (fmt0
, orgap
,
629 #ifndef NO_FLOATING_POINT
635 if (flags
& SHORTINT
) {
645 if (flags
& LONGINT
) {
652 flags
|= LLONGINT
; /* not necessarily */
668 if (flags
& LONGINT
) {
669 static const mbstate_t initial
;
674 mbseqlen
= wcrtomb_l(cp
= buf
,
675 (wchar_t)GETARG(wint_t), &mbs
, loc
);
676 if (mbseqlen
== (size_t)-1) {
677 fp
->_flags
|= __SERR
;
680 size
= (int)mbseqlen
;
682 *(cp
= buf
) = GETARG(int);
696 if (flags
& INTMAX_SIZE
) {
698 if ((intmax_t)ujval
< 0) {
704 if ((long)ulval
< 0) {
711 #ifndef NO_FLOATING_POINT
715 if (flags
& VECTOR
) {
731 if (dtoaresult
!= NULL
)
732 freedtoa(dtoaresult
);
733 if (flags
& LONGDBL
) {
734 fparg
.ldbl
= GETARG(long double);
736 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
737 &expt
, &signflag
, &dtoaend
);
739 fparg
.dbl
= GETARG(double);
741 __hdtoa(fparg
.dbl
, xdigs
, prec
,
742 &expt
, &signflag
, &dtoaend
);
752 if (flags
& VECTOR
) {
758 if (prec
< 0) /* account for digit before decpt */
766 if (flags
& VECTOR
) {
776 if (flags
& VECTOR
) {
781 expchar
= ch
- ('g' - 'e');
787 if (dtoaresult
!= NULL
)
788 freedtoa(dtoaresult
);
789 if (flags
& LONGDBL
) {
790 fparg
.ldbl
= GETARG(long double);
792 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
793 &expt
, &signflag
, &dtoaend
);
795 fparg
.dbl
= GETARG(double);
797 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
798 &expt
, &signflag
, &dtoaend
);
805 if (expt
== INT_MAX
) { /* inf or nan */
807 cp
= (ch
>= 'a') ? "nan" : "NAN";
810 cp
= (ch
>= 'a') ? "inf" : "INF";
817 if (ch
== 'g' || ch
== 'G') {
818 if (expt
> -4 && expt
<= prec
) {
819 /* Make %[gG] smell like %[fF] */
829 * Make %[gG] smell like %[eE], but
830 * trim trailing zeroes if no # flag.
837 expsize
= exponent(expstr
, expt
- 1, expchar
);
838 size
= expsize
+ prec
;
839 if (prec
> 1 || flags
& ALT
)
842 /* space for digits before decimal point */
847 /* space for decimal pt and following digits */
848 if (prec
|| flags
& ALT
)
849 size
+= prec
+ decpt_len
;
850 if ((flags
& GROUPING
) && expt
> 0)
851 size
+= grouping_init(&gs
, expt
, loc
);
854 #endif /* !NO_FLOATING_POINT */
858 * Assignment-like behavior is specified if the
859 * value overflows or is otherwise unrepresentable.
860 * C99 says to use `signed char' for %hhn conversions.
862 void *ptr
= GETARG(void *);
866 #ifndef ALLOW_DYNAMIC_PERCENT_N
867 if (!static_format_checked
) {
868 static_format_checked
= __printf_is_memory_read_only((void*)fmt0
, strlen(fmt0
));
870 if (!static_format_checked
) {
871 os_crash("%n used in a non-immutable format string");
873 #endif // ALLOW_DYNAMIC_PERCENT_N
875 if (flags
& LLONGINT
)
876 *(long long *)ptr
= ret
;
877 else if (flags
& SIZET
)
878 *(ssize_t
*)ptr
= (ssize_t
)ret
;
879 else if (flags
& PTRDIFFT
)
880 *(ptrdiff_t *)ptr
= ret
;
881 else if (flags
& INTMAXT
)
882 *(intmax_t *)ptr
= ret
;
883 else if (flags
& LONGINT
)
885 else if (flags
& SHORTINT
)
887 else if (flags
& CHARINT
)
888 *(signed char *)ptr
= ret
;
891 continue; /* no output */
901 if (flags
& INTMAX_SIZE
)
909 * ``The argument shall be a pointer to void. The
910 * value of the pointer is converted to a sequence
911 * of printable characters, in an implementation-
919 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
922 flags
= flags
| INTMAXT
;
929 if (flags
& LONGINT
) {
933 if ((wcp
= GETARG(wchar_t *)) == NULL
) {
937 convbuf
= __wcsconv(wcp
, prec
, loc
);
938 if (convbuf
== NULL
) {
939 fp
->_flags
|= __SERR
;
944 } else if ((cp
= GETARG(char *)) == NULL
)
947 size_t cp_len
= (prec
>= 0) ? strnlen(cp
, prec
) : strlen(cp
);
948 if (cp_len
< INT_MAX
) {
965 if (flags
& INTMAX_SIZE
)
981 if (flags
& INTMAX_SIZE
)
986 /* leading 0x/X only if non-zero */
988 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
992 /* unsigned conversions */
995 * ``... diouXx conversions ... if a precision is
996 * specified, the 0 flag will be ignored.''
998 * except for %#.0o and zero value
1000 number
: if ((dprec
= prec
) >= 0)
1004 * ``The result of converting a zero value with an
1005 * explicit precision of zero is no characters.''
1008 * ``The C Standard is clear enough as is. The call
1009 * printf("%#.0o", 0) should print 0.''
1010 * -- Defect Report #151
1013 if (flags
& INTMAX_SIZE
) {
1014 if (ujval
!= 0 || prec
!= 0 ||
1015 (flags
& ALT
&& base
== 8))
1016 cp
= __ujtoa(ujval
, cp
, base
,
1017 flags
& ALT
, xdigs
);
1019 if (ulval
!= 0 || prec
!= 0 ||
1020 (flags
& ALT
&& base
== 8))
1021 cp
= __ultoa(ulval
, cp
, base
,
1022 flags
& ALT
, xdigs
);
1024 size
= buf
+ BUF
- cp
;
1025 if (size
> BUF
) /* should never happen */
1026 LIBC_ABORT("size (%zd) > BUF (%d)", size
, BUF
);
1027 if ((flags
& GROUPING
) && size
!= 0)
1028 size
+= grouping_init(&gs
, size
, loc
);
1034 #endif /* VECTORS */
1035 default: /* "%?" prints ?, unless ? is NUL */
1038 /* pretend it was %c with argument ch */
1047 if (flags
& VECTOR
) {
1049 * Do the minimum amount of work necessary to construct
1050 * a format specifier that can be used to recursively
1051 * call vfprintf() for each element in the vector.
1053 int i
, j
; /* Counter. */
1054 int vcnt
; /* Number of elements in vector. */
1055 char *vfmt
; /* Pointer to format specifier. */
1057 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1058 int vwidth
= 0; /* Width specified via '*'. */
1059 int vprec
= 0; /* Precision specified via '*'. */
1060 char *vstr
; /* Used for asprintf(). */
1061 int vlen
; /* Length returned by asprintf(). */
1063 V_CHAR
, V_SHORT
, V_INT
,
1064 V_PCHAR
, V_PSHORT
, V_PINT
,
1067 V_LONGLONG
, V_PLONGLONG
,
1069 #endif /* V64TYPE */
1072 vval
.vectorarg
= GETARG(VECTORTYPE
);
1074 * Set vfmt. If vfmt_buf may not be big enough,
1075 * malloc() space, taking care to free it later.
1076 * (EXTRAHH is for possible extra "hh")
1078 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1081 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1083 /* Set the separator character, if not specified. */
1091 /* Create the format specifier. */
1092 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1094 case ',': case ';': case ':': case '_':
1095 case 'v': case 'h': case 'l':
1099 if (pct
[i
- 1] != '.')
1110 * Determine the number of elements in the vector and
1111 * finish up the format specifier.
1113 if (flags
& SHORTINT
) {
1127 } else if (flags
& LONGINT
) {
1129 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1131 } else if (flags
& LLONGINT
) {
1153 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1157 * The default case should never
1164 #endif /* V64TYPE */
1179 * The default case should never
1194 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1200 /* Get a vector element. */
1202 #define VPRINT(type, ind, args...) do { \
1205 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1208 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1211 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1214 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1217 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1220 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1223 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1226 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1229 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1232 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1236 PRINT(vstr, vlen); \
1240 #else /* !V64TYPE */
1241 #define VPRINT(type, ind, args...) do { \
1244 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1247 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1250 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1253 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1256 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1259 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1262 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1266 PRINT(vstr, vlen); \
1270 #endif /* V64TYPE */
1272 /* Actually print. */
1275 /* First element. */
1277 for (i
= 1; i
< vcnt
; i
++) {
1286 /* First element. */
1287 VPRINT(vtype
, 0, prec
);
1288 for (i
= 1; i
< vcnt
; i
++) {
1294 VPRINT(vtype
, i
, prec
);
1299 /* First element. */
1300 VPRINT(vtype
, 0, width
);
1301 for (i
= 1; i
< vcnt
; i
++) {
1307 VPRINT(vtype
, i
, width
);
1310 /* First element. */
1311 VPRINT(vtype
, 0, width
, prec
);
1312 for (i
= 1; i
< vcnt
; i
++) {
1318 VPRINT(vtype
, i
, width
, prec
);
1324 if (vfmt
!= vfmt_buf
)
1329 #endif /* VECTORS */
1331 * All reasonable formats wind up here. At this point, `cp'
1332 * points to a string which (if not flags&LADJUST) should be
1333 * padded out to `width' places. If flags&ZEROPAD, it should
1334 * first be prefixed by any sign or other prefix; otherwise,
1335 * it should be blank padded before the prefix is emitted.
1336 * After any left-hand padding and prefixing, emit zeroes
1337 * required by a decimal [diouxX] precision, then print the
1338 * string proper, then emit zeroes required by any leftover
1339 * floating precision; finally, if LADJUST, pad with blanks.
1341 * Compute actual size, so we know how much to pad.
1342 * size excludes decimal prec; realsz includes it.
1344 realsz
= dprec
> size
? dprec
: size
;
1350 prsize
= width
> realsz
? width
: realsz
;
1351 if (ret
+ prsize
>= INT_MAX
) {
1357 /* right-adjusting blank padding */
1358 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1359 PAD(width
- realsz
, blanks
);
1365 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1370 /* right-adjusting zero padding */
1371 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1372 PAD(width
- realsz
, zeroes
);
1374 /* the string or number proper */
1375 #ifndef NO_FLOATING_POINT
1376 if ((flags
& FPT
) == 0) {
1378 /* leading zeroes from decimal precision */
1379 PAD(dprec
- size
, zeroes
);
1381 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, loc
) < 0)
1386 #ifndef NO_FLOATING_POINT
1387 } else { /* glue together f_p fragments */
1388 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1391 if (prec
|| flags
& ALT
)
1392 PRINT(decimal_point
,decpt_len
);
1394 /* already handled initial 0's */
1398 n
= grouping_print(&gs
, &io
,
1404 PRINTANDPAD(cp
, dtoaend
,
1408 if (prec
|| flags
& ALT
)
1409 PRINT(decimal_point
,decpt_len
);
1411 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1412 } else { /* %[eE] or sufficiently long %[gG] */
1413 if (prec
> 1 || flags
& ALT
) {
1415 PRINT(decimal_point
, decpt_len
);
1417 PAD(prec
- ndig
, zeroes
);
1420 PRINT(expstr
, expsize
);
1424 /* left-adjusting padding (always blank) */
1425 if (flags
& LADJUST
)
1426 PAD(width
- realsz
, blanks
);
1428 /* finally, adjust ret */
1431 FLUSH(); /* copy out the I/O vectors */
1437 #ifndef NO_FLOATING_POINT
1438 if (dtoaresult
!= NULL
)
1439 freedtoa(dtoaresult
);
1444 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1446 return (ret
< 0 || ret
>= INT_MAX
) ? -1 : (int)ret
;