]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfprintf.c
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 "libc_private.h"
70 #include "printflocal.h"
72 static int __sprint(FILE *, locale_t
, struct __suio
*);
73 static int __sbprintf(FILE *, locale_t
, const char *, va_list) __printflike(3, 0);
74 static char *__wcsconv(wchar_t *, int, locale_t
);
76 __private_extern__
const char *__fix_nogrouping(const char *);
79 #include "printfcommon.h"
81 struct grouping_state
{
82 char *thousands_sep
; /* locale-specific thousands separator */
83 int thousep_len
; /* length of thousands_sep */
84 const char *grouping
; /* locale-specific numeric grouping rules */
85 int lead
; /* sig figs before decimal or group sep */
86 int nseps
; /* number of group separators with ' */
87 int nrepeats
; /* number of repeats of the last group */
91 * Initialize the thousands' grouping state in preparation to print a
92 * number with ndigits digits. This routine returns the total number
93 * of bytes that will be needed.
96 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
100 locale
= localeconv_l(loc
);
101 gs
->grouping
= __fix_nogrouping(locale
->grouping
);
102 gs
->thousands_sep
= locale
->thousands_sep
;
103 gs
->thousep_len
= strlen(gs
->thousands_sep
);
105 gs
->nseps
= gs
->nrepeats
= 0;
107 while (*gs
->grouping
!= CHAR_MAX
) {
108 if (gs
->lead
<= *gs
->grouping
)
110 gs
->lead
-= *gs
->grouping
;
111 if (*(gs
->grouping
+1)) {
117 return ((gs
->nseps
+ gs
->nrepeats
) * gs
->thousep_len
);
121 * Print a number with thousands' separators.
124 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
125 const CHAR
*cp
, const CHAR
*ep
, locale_t loc
)
127 const CHAR
*cp0
= cp
;
129 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, loc
))
132 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
133 if (gs
->nrepeats
> 0)
139 if (io_print(iop
, gs
->thousands_sep
, gs
->thousep_len
, loc
))
141 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, loc
))
151 * Flush out all the vectors defined by the given uio,
152 * then reset it so that it can be reused.
155 __sprint(FILE *fp
, locale_t loc __unused
, struct __suio
*uio
)
159 if (uio
->uio_resid
== 0) {
163 err
= __sfvwrite(fp
, uio
);
170 * Helper function for `fprintf to unbuffered unix file': creates a
171 * temporary buffer. We only work on write-only files; this avoids
172 * worries about ungetc buffers and so forth.
175 __sbprintf(FILE *fp
, locale_t loc
, const char *fmt
, va_list ap
)
179 unsigned char buf
[BUFSIZ
];
184 /* XXX This is probably not needed. */
185 if (prepwrite(fp
) != 0)
188 /* copy the important variables */
189 fake
._flags
= fp
->_flags
& ~__SNBF
;
190 fake
._file
= fp
->_file
;
191 fake
._cookie
= fp
->_cookie
;
192 fake
._write
= fp
->_write
;
193 fake
._orientation
= fp
->_orientation
;
194 fake
._mbstate
= fp
->_mbstate
;
196 /* set up the buffer */
197 fake
._bf
._base
= fake
._p
= buf
;
198 fake
._bf
._size
= fake
._w
= sizeof(buf
);
199 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
201 /* do the work, then copy any error status */
202 ret
= __vfprintf(&fake
, loc
, fmt
, ap
);
203 if (ret
>= 0 && __fflush(&fake
))
205 if (fake
._flags
& __SERR
)
206 fp
->_flags
|= __SERR
;
211 * Convert a wide character string argument for the %ls format to a multibyte
212 * string representation. If not -1, prec specifies the maximum number of
213 * bytes to output, and also means that we can't assume that the wide char.
214 * string ends is null-terminated.
217 __wcsconv(wchar_t *wcsarg
, int prec
, locale_t loc
)
219 static const mbstate_t initial
;
221 char buf
[MB_LEN_MAX
];
226 /* Allocate space for the maximum number of bytes we could output. */
230 nbytes
= wcsrtombs_l(NULL
, (const wchar_t **)&p
, 0, &mbs
, loc
);
231 if (nbytes
== (size_t)-1)
235 * Optimisation: if the output precision is small enough,
236 * just allocate enough memory for the maximum instead of
237 * scanning the string.
246 clen
= wcrtomb_l(buf
, *p
++, &mbs
, loc
);
247 if (clen
== 0 || clen
== (size_t)-1 ||
248 nbytes
+ clen
> prec
)
254 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
257 /* Fill the output buffer. */
260 if ((nbytes
= wcsrtombs_l(convbuf
, (const wchar_t **)&p
,
261 nbytes
, &mbs
, loc
)) == (size_t)-1) {
265 convbuf
[nbytes
] = '\0';
273 vfprintf_l(FILE * __restrict fp
, locale_t loc
, const char * __restrict fmt0
, va_list ap
)
278 NORMALIZE_LOCALE(loc
);
280 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
281 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
283 ret
= __sbprintf(fp
, loc
, fmt0
, ap
);
285 ret
= __vfprintf(fp
, loc
, fmt0
, ap
);
291 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
294 return vfprintf_l(fp
, __current_locale(), fmt0
, ap
);
298 * The size of the buffer we use as scratch space for integer
299 * conversions, among other things. We need enough space to
300 * write a uintmax_t in octal (plus one byte).
302 #if UINTMAX_MAX <= UINT64_MAX
305 #error "BUF must be large enough to format a uintmax_t"
309 * Non-MT-safe version
311 __private_extern__
int
312 __vfprintf(FILE *fp
, locale_t loc
, const char *fmt0
, va_list ap
)
314 char *fmt
; /* format string */
315 int ch
; /* character from fmt */
316 int n
, n2
; /* handy integer (short term usage) */
317 char *cp
; /* handy char pointer (short term usage) */
318 int flags
; /* flags as above */
319 int ret
; /* return value accumulator */
320 int width
; /* width from format (%8d), or 0 */
321 int prec
; /* precision from format; <0 for N/A */
322 char sign
; /* sign prefix (' ', '+', '-', or \0) */
323 struct grouping_state gs
; /* thousands' grouping info */
325 #ifndef NO_FLOATING_POINT
327 * We can decompose the printed representation of floating
328 * point numbers into several parts, some of which may be empty:
330 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
333 * A: 'sign' holds this value if present; '\0' otherwise
334 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
335 * C: cp points to the string MMMNNN. Leading and trailing
336 * zeros are not in the string and must be added.
337 * D: expchar holds this character; '\0' if no exponent, e.g. %f
338 * F: at least two digits for decimal, at least one digit for hex
340 char *decimal_point
; /* locale specific decimal point */
341 int decpt_len
; /* length of decimal_point */
342 int signflag
; /* true if float is negative */
343 union { /* floating point arguments %[aAeEfFgG] */
347 int expt
; /* integer value of exponent */
348 char expchar
; /* exponent character: [eEpP\0] */
349 char *dtoaend
; /* pointer to end of converted digits */
350 int expsize
; /* character count for expstr */
351 int ndig
; /* actual number of digits returned by dtoa */
352 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
353 char *dtoaresult
; /* buffer allocated by dtoa */
356 union arg vval
; /* Vector argument. */
357 char *pct
; /* Pointer to '%' at beginning of specifier. */
358 char vsep
; /* Vector separator character. */
360 u_long ulval
; /* integer arguments %[diouxX] */
361 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
362 int base
; /* base for [diouxX] conversion */
363 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
364 int realsz
; /* field size expanded by dprec, sign, etc */
365 int size
; /* size of converted field or string */
366 int prsize
; /* max size of printed field */
367 const char *xdigs
; /* digits for %[xX] conversion */
368 struct io_state io
; /* I/O buffering state */
369 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
370 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
371 union arg
*argtable
; /* args, built due to positional arg */
372 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
373 int nextarg
; /* 1-based argument index */
374 va_list orgap
; /* original argument pointer */
375 char *convbuf
; /* wide to multibyte conversion result */
377 static const char xdigs_lower
[16] = "0123456789abcdef";
378 static const char xdigs_upper
[16] = "0123456789ABCDEF";
380 /* BEWARE, these `goto error' on error. */
381 #define PRINT(ptr, len) { \
382 if (io_print(&io, (ptr), (len), loc)) \
385 #define PAD(howmany, with) { \
386 if (io_pad(&io, (howmany), (with), loc)) \
389 #define PRINTANDPAD(p, ep, len, with) { \
390 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
394 if (io_flush(&io, loc)) \
399 * Get the argument indexed by nextarg. If the argument table is
400 * built, use it to get the argument. If its not, get the next
401 * argument (and arguments must be gotten sequentially).
403 #define GETARG(type) \
404 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
405 (nextarg++, va_arg(ap, type)))
408 * To extend shorts properly, we need both signed and unsigned
409 * argument extraction methods.
412 (flags&LONGINT ? GETARG(long) : \
413 flags&SHORTINT ? (long)(short)GETARG(int) : \
414 flags&CHARINT ? (long)(signed char)GETARG(int) : \
417 (flags&LONGINT ? GETARG(u_long) : \
418 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
419 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
420 (u_long)GETARG(u_int))
421 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
423 (flags&INTMAXT ? GETARG(intmax_t) : \
424 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
425 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
426 (intmax_t)GETARG(long long))
428 (flags&INTMAXT ? GETARG(uintmax_t) : \
429 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
430 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
431 (uintmax_t)GETARG(unsigned long long))
434 * Get * arguments, including the form *nn$. Preserve the nextarg
435 * that the argument can be gotten once the type is determined.
437 #define GETASTER(val) \
440 while (is_digit(*cp)) { \
441 n2 = 10 * n2 + to_digit(*cp); \
445 int hold = nextarg; \
446 if (argtable == NULL) { \
447 argtable = statargtable; \
448 if (__find_arguments (fmt0, orgap, &argtable)) { \
454 val = GETARG (int); \
458 val = GETARG (int); \
461 #if 0 // xprintf pending API review
462 if (__use_xprintf
== 0 && getenv("USE_XPRINTF"))
464 if (__use_xprintf
> 0)
465 return (__xvprintf(fp
, loc
, fmt0
, ap
));
468 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
469 if (prepwrite(fp
) != 0) {
482 #ifndef NO_FLOATING_POINT
484 decimal_point
= localeconv_l(loc
)->decimal_point
;
485 /* The overwhelmingly common case is decpt_len == 1. */
486 decpt_len
= (decimal_point
[1] == '\0' ? 1 : strlen(decimal_point
));
490 * Scan the format for conversions (`%' character).
493 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
495 if ((n
= fmt
- cp
) != 0) {
496 if ((unsigned)ret
+ n
> INT_MAX
) {
508 fmt
++; /* skip over '%' */
518 vsep
= 'X'; /* Illegal value, changed to defaults later. */
522 reswitch
: switch (ch
) {
525 * ``If the space and + flags both appear, the space
526 * flag will be ignored.''
536 case ',': case ';': case ':': case '_':
542 * ``A negative field width argument is taken as a
543 * - flag followed by a positive field width.''
545 * They don't exclude field widths read from args.
562 if ((ch
= *fmt
++) == '*') {
567 while (is_digit(ch
)) {
568 prec
= 10 * prec
+ to_digit(ch
);
574 * ``Note that 0 is taken as a flag, not as the
575 * beginning of a field width.''
580 case '1': case '2': case '3': case '4':
581 case '5': case '6': case '7': case '8': case '9':
584 n
= 10 * n
+ to_digit(ch
);
586 } while (is_digit(ch
));
589 if (argtable
== NULL
) {
590 argtable
= statargtable
;
591 if (__find_arguments (fmt0
, orgap
,
601 #ifndef NO_FLOATING_POINT
607 if (flags
& SHORTINT
) {
617 if (flags
& LONGINT
) {
624 flags
|= LLONGINT
; /* not necessarily */
640 if (flags
& LONGINT
) {
641 static const mbstate_t initial
;
646 mbseqlen
= wcrtomb_l(cp
= buf
,
647 (wchar_t)GETARG(wint_t), &mbs
, loc
);
648 if (mbseqlen
== (size_t)-1) {
649 fp
->_flags
|= __SERR
;
652 size
= (int)mbseqlen
;
654 *(cp
= buf
) = GETARG(int);
668 if (flags
& INTMAX_SIZE
) {
670 if ((intmax_t)ujval
< 0) {
676 if ((long)ulval
< 0) {
683 #ifndef NO_FLOATING_POINT
687 if (flags
& VECTOR
) {
703 if (dtoaresult
!= NULL
)
704 freedtoa(dtoaresult
);
705 if (flags
& LONGDBL
) {
706 fparg
.ldbl
= GETARG(long double);
708 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
709 &expt
, &signflag
, &dtoaend
);
711 fparg
.dbl
= GETARG(double);
713 __hdtoa(fparg
.dbl
, xdigs
, prec
,
714 &expt
, &signflag
, &dtoaend
);
724 if (flags
& VECTOR
) {
730 if (prec
< 0) /* account for digit before decpt */
738 if (flags
& VECTOR
) {
748 if (flags
& VECTOR
) {
753 expchar
= ch
- ('g' - 'e');
759 if (dtoaresult
!= NULL
)
760 freedtoa(dtoaresult
);
761 if (flags
& LONGDBL
) {
762 fparg
.ldbl
= GETARG(long double);
764 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
765 &expt
, &signflag
, &dtoaend
);
767 fparg
.dbl
= GETARG(double);
769 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
770 &expt
, &signflag
, &dtoaend
);
777 if (expt
== INT_MAX
) { /* inf or nan */
779 cp
= (ch
>= 'a') ? "nan" : "NAN";
782 cp
= (ch
>= 'a') ? "inf" : "INF";
789 if (ch
== 'g' || ch
== 'G') {
790 if (expt
> -4 && expt
<= prec
) {
791 /* Make %[gG] smell like %[fF] */
801 * Make %[gG] smell like %[eE], but
802 * trim trailing zeroes if no # flag.
809 expsize
= exponent(expstr
, expt
- 1, expchar
);
810 size
= expsize
+ prec
;
811 if (prec
> 1 || flags
& ALT
)
814 /* space for digits before decimal point */
819 /* space for decimal pt and following digits */
820 if (prec
|| flags
& ALT
)
821 size
+= prec
+ decpt_len
;
822 if ((flags
& GROUPING
) && expt
> 0)
823 size
+= grouping_init(&gs
, expt
, loc
);
826 #endif /* !NO_FLOATING_POINT */
830 * Assignment-like behavior is specified if the
831 * value overflows or is otherwise unrepresentable.
832 * C99 says to use `signed char' for %hhn conversions.
834 void *ptr
= GETARG(void *);
837 else if (flags
& LLONGINT
)
838 *(long long *)ptr
= ret
;
839 else if (flags
& SIZET
)
840 *(ssize_t
*)ptr
= (ssize_t
)ret
;
841 else if (flags
& PTRDIFFT
)
842 *(ptrdiff_t *)ptr
= ret
;
843 else if (flags
& INTMAXT
)
844 *(intmax_t *)ptr
= ret
;
845 else if (flags
& LONGINT
)
847 else if (flags
& SHORTINT
)
849 else if (flags
& CHARINT
)
850 *(signed char *)ptr
= ret
;
853 continue; /* no output */
863 if (flags
& INTMAX_SIZE
)
871 * ``The argument shall be a pointer to void. The
872 * value of the pointer is converted to a sequence
873 * of printable characters, in an implementation-
881 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
884 flags
= flags
| INTMAXT
;
891 if (flags
& LONGINT
) {
896 if ((wcp
= GETARG(wchar_t *)) == NULL
)
899 convbuf
= __wcsconv(wcp
, prec
, loc
);
900 if (convbuf
== NULL
) {
901 fp
->_flags
|= __SERR
;
906 } else if ((cp
= GETARG(char *)) == NULL
)
908 size
= (prec
>= 0) ? strnlen(cp
, prec
) : strlen(cp
);
919 if (flags
& INTMAX_SIZE
)
935 if (flags
& INTMAX_SIZE
)
940 /* leading 0x/X only if non-zero */
942 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
946 /* unsigned conversions */
949 * ``... diouXx conversions ... if a precision is
950 * specified, the 0 flag will be ignored.''
952 * except for %#.0o and zero value
954 number
: if ((dprec
= prec
) >= 0)
958 * ``The result of converting a zero value with an
959 * explicit precision of zero is no characters.''
962 * ``The C Standard is clear enough as is. The call
963 * printf("%#.0o", 0) should print 0.''
964 * -- Defect Report #151
967 if (flags
& INTMAX_SIZE
) {
968 if (ujval
!= 0 || prec
!= 0 ||
969 (flags
& ALT
&& base
== 8))
970 cp
= __ujtoa(ujval
, cp
, base
,
973 if (ulval
!= 0 || prec
!= 0 ||
974 (flags
& ALT
&& base
== 8))
975 cp
= __ultoa(ulval
, cp
, base
,
978 size
= buf
+ BUF
- cp
;
979 if (size
> BUF
) /* should never happen */
980 LIBC_ABORT("size (%d) > BUF (%d)", size
, BUF
);
981 if ((flags
& GROUPING
) && size
!= 0)
982 size
+= grouping_init(&gs
, size
, loc
);
989 default: /* "%?" prints ?, unless ? is NUL */
992 /* pretend it was %c with argument ch */
1001 if (flags
& VECTOR
) {
1003 * Do the minimum amount of work necessary to construct
1004 * a format specifier that can be used to recursively
1005 * call vfprintf() for each element in the vector.
1007 int i
, j
; /* Counter. */
1008 int vcnt
; /* Number of elements in vector. */
1009 char *vfmt
; /* Pointer to format specifier. */
1011 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1012 int vwidth
= 0; /* Width specified via '*'. */
1013 int vprec
= 0; /* Precision specified via '*'. */
1014 char *vstr
; /* Used for asprintf(). */
1015 int vlen
; /* Length returned by asprintf(). */
1017 V_CHAR
, V_SHORT
, V_INT
,
1018 V_PCHAR
, V_PSHORT
, V_PINT
,
1021 V_LONGLONG
, V_PLONGLONG
,
1023 #endif /* V64TYPE */
1026 vval
.vectorarg
= GETARG(VECTORTYPE
);
1028 * Set vfmt. If vfmt_buf may not be big enough,
1029 * malloc() space, taking care to free it later.
1030 * (EXTRAHH is for possible extra "hh")
1032 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1035 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1037 /* Set the separator character, if not specified. */
1045 /* Create the format specifier. */
1046 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1048 case ',': case ';': case ':': case '_':
1049 case 'v': case 'h': case 'l':
1053 if (pct
[i
- 1] != '.')
1064 * Determine the number of elements in the vector and
1065 * finish up the format specifier.
1067 if (flags
& SHORTINT
) {
1081 } else if (flags
& LONGINT
) {
1083 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1085 } else if (flags
& LLONGINT
) {
1107 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1111 * The default case should never
1118 #endif /* V64TYPE */
1133 * The default case should never
1148 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1154 /* Get a vector element. */
1156 #define VPRINT(type, ind, args...) do { \
1159 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1162 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1165 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1168 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1171 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1174 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1177 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1180 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1183 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1186 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1190 PRINT(vstr, vlen); \
1194 #else /* !V64TYPE */
1195 #define VPRINT(type, ind, args...) do { \
1198 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1201 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1204 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1207 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1210 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1213 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1216 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1220 PRINT(vstr, vlen); \
1224 #endif /* V64TYPE */
1226 /* Actually print. */
1229 /* First element. */
1231 for (i
= 1; i
< vcnt
; i
++) {
1240 /* First element. */
1241 VPRINT(vtype
, 0, prec
);
1242 for (i
= 1; i
< vcnt
; i
++) {
1248 VPRINT(vtype
, i
, prec
);
1253 /* First element. */
1254 VPRINT(vtype
, 0, width
);
1255 for (i
= 1; i
< vcnt
; i
++) {
1261 VPRINT(vtype
, i
, width
);
1264 /* First element. */
1265 VPRINT(vtype
, 0, width
, prec
);
1266 for (i
= 1; i
< vcnt
; i
++) {
1272 VPRINT(vtype
, i
, width
, prec
);
1278 if (vfmt
!= vfmt_buf
)
1283 #endif /* VECTORS */
1285 * All reasonable formats wind up here. At this point, `cp'
1286 * points to a string which (if not flags&LADJUST) should be
1287 * padded out to `width' places. If flags&ZEROPAD, it should
1288 * first be prefixed by any sign or other prefix; otherwise,
1289 * it should be blank padded before the prefix is emitted.
1290 * After any left-hand padding and prefixing, emit zeroes
1291 * required by a decimal [diouxX] precision, then print the
1292 * string proper, then emit zeroes required by any leftover
1293 * floating precision; finally, if LADJUST, pad with blanks.
1295 * Compute actual size, so we know how much to pad.
1296 * size excludes decimal prec; realsz includes it.
1298 realsz
= dprec
> size
? dprec
: size
;
1304 prsize
= width
> realsz
? width
: realsz
;
1305 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1310 /* right-adjusting blank padding */
1311 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1312 PAD(width
- realsz
, blanks
);
1318 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1323 /* right-adjusting zero padding */
1324 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1325 PAD(width
- realsz
, zeroes
);
1327 /* the string or number proper */
1328 #ifndef NO_FLOATING_POINT
1329 if ((flags
& FPT
) == 0) {
1331 /* leading zeroes from decimal precision */
1332 PAD(dprec
- size
, zeroes
);
1334 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, loc
) < 0)
1339 #ifndef NO_FLOATING_POINT
1340 } else { /* glue together f_p fragments */
1341 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1344 if (prec
|| flags
& ALT
)
1345 PRINT(decimal_point
,decpt_len
);
1347 /* already handled initial 0's */
1351 n
= grouping_print(&gs
, &io
,
1357 PRINTANDPAD(cp
, dtoaend
,
1361 if (prec
|| flags
& ALT
)
1362 PRINT(decimal_point
,decpt_len
);
1364 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1365 } else { /* %[eE] or sufficiently long %[gG] */
1366 if (prec
> 1 || flags
& ALT
) {
1368 PRINT(decimal_point
, decpt_len
);
1370 PAD(prec
- ndig
, zeroes
);
1373 PRINT(expstr
, expsize
);
1377 /* left-adjusting padding (always blank) */
1378 if (flags
& LADJUST
)
1379 PAD(width
- realsz
, blanks
);
1381 /* finally, adjust ret */
1384 FLUSH(); /* copy out the I/O vectors */
1390 #ifndef NO_FLOATING_POINT
1391 if (dtoaresult
!= NULL
)
1392 freedtoa(dtoaresult
);
1394 if (convbuf
!= NULL
)
1398 if ((argtable
!= NULL
) && (argtable
!= statargtable
))