]>
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
*);
74 static int __sbprintf(FILE *, locale_t
, const char *, va_list) __printflike(3, 0);
76 static char *__wcsconv(wchar_t *, int, locale_t
);
78 __private_extern__
const char *__fix_nogrouping(const char *);
81 #include "printfcommon.h"
83 struct grouping_state
{
84 char *thousands_sep
; /* locale-specific thousands separator */
85 int thousep_len
; /* length of thousands_sep */
86 const char *grouping
; /* locale-specific numeric grouping rules */
87 int lead
; /* sig figs before decimal or group sep */
88 int nseps
; /* number of group separators with ' */
89 int nrepeats
; /* number of repeats of the last group */
93 * Initialize the thousands' grouping state in preparation to print a
94 * number with ndigits digits. This routine returns the total number
95 * of bytes that will be needed.
98 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
100 struct lconv
*locale
;
102 locale
= localeconv_l(loc
);
103 gs
->grouping
= __fix_nogrouping(locale
->grouping
);
104 gs
->thousands_sep
= locale
->thousands_sep
;
105 gs
->thousep_len
= strlen(gs
->thousands_sep
);
107 gs
->nseps
= gs
->nrepeats
= 0;
109 while (*gs
->grouping
!= CHAR_MAX
) {
110 if (gs
->lead
<= *gs
->grouping
)
112 gs
->lead
-= *gs
->grouping
;
113 if (*(gs
->grouping
+1)) {
119 return ((gs
->nseps
+ gs
->nrepeats
) * gs
->thousep_len
);
123 * Print a number with thousands' separators.
126 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
127 const CHAR
*cp
, const CHAR
*ep
, locale_t loc
)
129 const CHAR
*cp0
= cp
;
131 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, loc
))
134 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
135 if (gs
->nrepeats
> 0)
141 if (io_print(iop
, gs
->thousands_sep
, gs
->thousep_len
, loc
))
143 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, loc
))
153 * Flush out all the vectors defined by the given uio,
154 * then reset it so that it can be reused.
157 __sprint(FILE *fp
, locale_t loc __unused
, struct __suio
*uio
)
161 if (uio
->uio_resid
== 0) {
165 err
= __sfvwrite(fp
, uio
);
173 * Helper function for `fprintf to unbuffered unix file': creates a
174 * temporary buffer. We only work on write-only files; this avoids
175 * worries about ungetc buffers and so forth.
178 __sbprintf(FILE *fp
, locale_t loc
, const char *fmt
, va_list ap
)
182 unsigned char buf
[BUFSIZ
];
187 /* XXX This is probably not needed. */
188 if (prepwrite(fp
) != 0)
191 /* copy the important variables */
192 fake
._flags
= fp
->_flags
& ~__SNBF
;
193 fake
._file
= fp
->_file
;
194 fake
._cookie
= fp
->_cookie
;
195 fake
._write
= fp
->_write
;
196 fake
._orientation
= fp
->_orientation
;
197 fake
._mbstate
= fp
->_mbstate
;
199 /* set up the buffer */
200 fake
._bf
._base
= fake
._p
= buf
;
201 fake
._bf
._size
= fake
._w
= sizeof(buf
);
202 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
204 /* do the work, then copy any error status */
205 ret
= __vfprintf(&fake
, loc
, fmt
, ap
);
206 if (ret
>= 0 && __fflush(&fake
))
208 if (fake
._flags
& __SERR
)
209 fp
->_flags
|= __SERR
;
215 * Convert a wide character string argument for the %ls format to a multibyte
216 * string representation. If not -1, prec specifies the maximum number of
217 * bytes to output, and also means that we can't assume that the wide char.
218 * string ends is null-terminated.
221 __wcsconv(wchar_t *wcsarg
, int prec
, locale_t loc
)
223 static const mbstate_t initial
;
225 char buf
[MB_LEN_MAX
];
230 /* Allocate space for the maximum number of bytes we could output. */
234 nbytes
= wcsrtombs_l(NULL
, (const wchar_t **)&p
, 0, &mbs
, loc
);
235 if (nbytes
== (size_t)-1)
239 * Optimisation: if the output precision is small enough,
240 * just allocate enough memory for the maximum instead of
241 * scanning the string.
250 clen
= wcrtomb_l(buf
, *p
++, &mbs
, loc
);
251 if (clen
== 0 || clen
== (size_t)-1 ||
252 nbytes
+ clen
> prec
)
258 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
261 /* Fill the output buffer. */
264 if ((nbytes
= wcsrtombs_l(convbuf
, (const wchar_t **)&p
,
265 nbytes
, &mbs
, loc
)) == (size_t)-1) {
269 convbuf
[nbytes
] = '\0';
277 vfprintf_l(FILE * __restrict fp
, locale_t loc
, const char * __restrict fmt0
, va_list ap
)
283 ret
= __xvprintf(XPRINTF_PLAIN
, NULL
, fp
, loc
, fmt0
, ap
);
289 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
295 ret
= __xvprintf(XPRINTF_PLAIN
, NULL
, fp
, __current_locale(), fmt0
, ap
);
301 * The size of the buffer we use as scratch space for integer
302 * conversions, among other things. We need enough space to
303 * write a uintmax_t in octal (plus one byte).
305 #if UINTMAX_MAX <= UINT64_MAX
308 #error "BUF must be large enough to format a uintmax_t"
312 * Non-MT-safe version
314 __private_extern__
int
315 __vfprintf(FILE *fp
, locale_t loc
, const char *fmt0
, va_list ap
)
317 char *fmt
; /* format string */
318 int ch
; /* character from fmt */
319 int n
, n2
; /* handy integer (short term usage) */
320 char *cp
; /* handy char pointer (short term usage) */
321 int flags
; /* flags as above */
322 int ret
; /* return value accumulator */
323 int width
; /* width from format (%8d), or 0 */
324 int prec
; /* precision from format; <0 for N/A */
325 char sign
; /* sign prefix (' ', '+', '-', or \0) */
326 struct grouping_state gs
; /* thousands' grouping info */
328 #ifndef NO_FLOATING_POINT
330 * We can decompose the printed representation of floating
331 * point numbers into several parts, some of which may be empty:
333 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
336 * A: 'sign' holds this value if present; '\0' otherwise
337 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
338 * C: cp points to the string MMMNNN. Leading and trailing
339 * zeros are not in the string and must be added.
340 * D: expchar holds this character; '\0' if no exponent, e.g. %f
341 * F: at least two digits for decimal, at least one digit for hex
343 char *decimal_point
; /* locale specific decimal point */
344 int decpt_len
; /* length of decimal_point */
345 int signflag
; /* true if float is negative */
346 union { /* floating point arguments %[aAeEfFgG] */
350 int expt
; /* integer value of exponent */
351 char expchar
; /* exponent character: [eEpP\0] */
352 char *dtoaend
; /* pointer to end of converted digits */
353 int expsize
; /* character count for expstr */
354 int ndig
; /* actual number of digits returned by dtoa */
355 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
356 char *dtoaresult
; /* buffer allocated by dtoa */
359 union arg vval
; /* Vector argument. */
360 char *pct
; /* Pointer to '%' at beginning of specifier. */
361 char vsep
; /* Vector separator character. */
363 u_long ulval
; /* integer arguments %[diouxX] */
364 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
365 int base
; /* base for [diouxX] conversion */
366 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
367 int realsz
; /* field size expanded by dprec, sign, etc */
368 int size
; /* size of converted field or string */
369 int prsize
; /* max size of printed field */
370 const char *xdigs
; /* digits for %[xX] conversion */
371 struct io_state io
; /* I/O buffering state */
372 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
373 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
374 union arg
*argtable
; /* args, built due to positional arg */
375 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
376 int nextarg
; /* 1-based argument index */
377 va_list orgap
; /* original argument pointer */
378 char *convbuf
; /* wide to multibyte conversion result */
380 static const char xdigs_lower
[16] = "0123456789abcdef";
381 static const char xdigs_upper
[16] = "0123456789ABCDEF";
383 /* BEWARE, these `goto error' on error. */
384 #define PRINT(ptr, len) { \
385 if (io_print(&io, (ptr), (len), loc)) \
388 #define PAD(howmany, with) { \
389 if (io_pad(&io, (howmany), (with), loc)) \
392 #define PRINTANDPAD(p, ep, len, with) { \
393 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
397 if (io_flush(&io, loc)) \
402 * Get the argument indexed by nextarg. If the argument table is
403 * built, use it to get the argument. If its not, get the next
404 * argument (and arguments must be gotten sequentially).
406 #define GETARG(type) \
407 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
408 (nextarg++, va_arg(ap, type)))
411 * To extend shorts properly, we need both signed and unsigned
412 * argument extraction methods.
415 (flags&LONGINT ? GETARG(long) : \
416 flags&SHORTINT ? (long)(short)GETARG(int) : \
417 flags&CHARINT ? (long)(signed char)GETARG(int) : \
420 (flags&LONGINT ? GETARG(u_long) : \
421 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
422 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
423 (u_long)GETARG(u_int))
424 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
426 (flags&INTMAXT ? GETARG(intmax_t) : \
427 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
428 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
429 (intmax_t)GETARG(long long))
431 (flags&INTMAXT ? GETARG(uintmax_t) : \
432 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
433 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
434 (uintmax_t)GETARG(unsigned long long))
437 * Get * arguments, including the form *nn$. Preserve the nextarg
438 * that the argument can be gotten once the type is determined.
440 #define GETASTER(val) \
443 while (is_digit(*cp)) { \
444 n2 = 10 * n2 + to_digit(*cp); \
448 int hold = nextarg; \
449 if (argtable == NULL) { \
450 argtable = statargtable; \
451 if (__find_arguments (fmt0, orgap, &argtable)) { \
457 val = GETARG (int); \
461 val = GETARG (int); \
464 /* The following has been moved to __v2printf() */
466 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
467 if (prepwrite(fp
) != 0) {
481 #ifndef NO_FLOATING_POINT
483 decimal_point
= localeconv_l(loc
)->decimal_point
;
484 /* The overwhelmingly common case is decpt_len == 1. */
485 decpt_len
= (decimal_point
[1] == '\0' ? 1 : strlen(decimal_point
));
489 * Scan the format for conversions (`%' character).
492 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
494 if ((n
= fmt
- cp
) != 0) {
495 if ((unsigned)ret
+ n
> INT_MAX
) {
507 fmt
++; /* skip over '%' */
517 vsep
= 'X'; /* Illegal value, changed to defaults later. */
521 reswitch
: switch (ch
) {
524 * ``If the space and + flags both appear, the space
525 * flag will be ignored.''
535 case ',': case ';': case ':': case '_':
541 * ``A negative field width argument is taken as a
542 * - flag followed by a positive field width.''
544 * They don't exclude field widths read from args.
561 if ((ch
= *fmt
++) == '*') {
566 while (is_digit(ch
)) {
567 prec
= 10 * prec
+ to_digit(ch
);
573 * ``Note that 0 is taken as a flag, not as the
574 * beginning of a field width.''
579 case '1': case '2': case '3': case '4':
580 case '5': case '6': case '7': case '8': case '9':
583 n
= 10 * n
+ to_digit(ch
);
585 } while (is_digit(ch
));
588 if (argtable
== NULL
) {
589 argtable
= statargtable
;
590 if (__find_arguments (fmt0
, orgap
,
600 #ifndef NO_FLOATING_POINT
606 if (flags
& SHORTINT
) {
616 if (flags
& LONGINT
) {
623 flags
|= LLONGINT
; /* not necessarily */
639 if (flags
& LONGINT
) {
640 static const mbstate_t initial
;
645 mbseqlen
= wcrtomb_l(cp
= buf
,
646 (wchar_t)GETARG(wint_t), &mbs
, loc
);
647 if (mbseqlen
== (size_t)-1) {
648 fp
->_flags
|= __SERR
;
651 size
= (int)mbseqlen
;
653 *(cp
= buf
) = GETARG(int);
667 if (flags
& INTMAX_SIZE
) {
669 if ((intmax_t)ujval
< 0) {
675 if ((long)ulval
< 0) {
682 #ifndef NO_FLOATING_POINT
686 if (flags
& VECTOR
) {
702 if (dtoaresult
!= NULL
)
703 freedtoa(dtoaresult
);
704 if (flags
& LONGDBL
) {
705 fparg
.ldbl
= GETARG(long double);
707 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
708 &expt
, &signflag
, &dtoaend
);
710 fparg
.dbl
= GETARG(double);
712 __hdtoa(fparg
.dbl
, xdigs
, prec
,
713 &expt
, &signflag
, &dtoaend
);
723 if (flags
& VECTOR
) {
729 if (prec
< 0) /* account for digit before decpt */
737 if (flags
& VECTOR
) {
747 if (flags
& VECTOR
) {
752 expchar
= ch
- ('g' - 'e');
758 if (dtoaresult
!= NULL
)
759 freedtoa(dtoaresult
);
760 if (flags
& LONGDBL
) {
761 fparg
.ldbl
= GETARG(long double);
763 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
764 &expt
, &signflag
, &dtoaend
);
766 fparg
.dbl
= GETARG(double);
768 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
769 &expt
, &signflag
, &dtoaend
);
776 if (expt
== INT_MAX
) { /* inf or nan */
778 cp
= (ch
>= 'a') ? "nan" : "NAN";
781 cp
= (ch
>= 'a') ? "inf" : "INF";
788 if (ch
== 'g' || ch
== 'G') {
789 if (expt
> -4 && expt
<= prec
) {
790 /* Make %[gG] smell like %[fF] */
800 * Make %[gG] smell like %[eE], but
801 * trim trailing zeroes if no # flag.
808 expsize
= exponent(expstr
, expt
- 1, expchar
);
809 size
= expsize
+ prec
;
810 if (prec
> 1 || flags
& ALT
)
813 /* space for digits before decimal point */
818 /* space for decimal pt and following digits */
819 if (prec
|| flags
& ALT
)
820 size
+= prec
+ decpt_len
;
821 if ((flags
& GROUPING
) && expt
> 0)
822 size
+= grouping_init(&gs
, expt
, loc
);
825 #endif /* !NO_FLOATING_POINT */
829 * Assignment-like behavior is specified if the
830 * value overflows or is otherwise unrepresentable.
831 * C99 says to use `signed char' for %hhn conversions.
833 void *ptr
= GETARG(void *);
836 else if (flags
& LLONGINT
)
837 *(long long *)ptr
= ret
;
838 else if (flags
& SIZET
)
839 *(ssize_t
*)ptr
= (ssize_t
)ret
;
840 else if (flags
& PTRDIFFT
)
841 *(ptrdiff_t *)ptr
= ret
;
842 else if (flags
& INTMAXT
)
843 *(intmax_t *)ptr
= ret
;
844 else if (flags
& LONGINT
)
846 else if (flags
& SHORTINT
)
848 else if (flags
& CHARINT
)
849 *(signed char *)ptr
= ret
;
852 continue; /* no output */
862 if (flags
& INTMAX_SIZE
)
870 * ``The argument shall be a pointer to void. The
871 * value of the pointer is converted to a sequence
872 * of printable characters, in an implementation-
880 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
883 flags
= flags
| INTMAXT
;
890 if (flags
& LONGINT
) {
895 if ((wcp
= GETARG(wchar_t *)) == NULL
)
898 convbuf
= __wcsconv(wcp
, prec
, loc
);
899 if (convbuf
== NULL
) {
900 fp
->_flags
|= __SERR
;
905 } else if ((cp
= GETARG(char *)) == NULL
)
907 size
= (prec
>= 0) ? strnlen(cp
, prec
) : strlen(cp
);
918 if (flags
& INTMAX_SIZE
)
934 if (flags
& INTMAX_SIZE
)
939 /* leading 0x/X only if non-zero */
941 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
945 /* unsigned conversions */
948 * ``... diouXx conversions ... if a precision is
949 * specified, the 0 flag will be ignored.''
951 * except for %#.0o and zero value
953 number
: if ((dprec
= prec
) >= 0)
957 * ``The result of converting a zero value with an
958 * explicit precision of zero is no characters.''
961 * ``The C Standard is clear enough as is. The call
962 * printf("%#.0o", 0) should print 0.''
963 * -- Defect Report #151
966 if (flags
& INTMAX_SIZE
) {
967 if (ujval
!= 0 || prec
!= 0 ||
968 (flags
& ALT
&& base
== 8))
969 cp
= __ujtoa(ujval
, cp
, base
,
972 if (ulval
!= 0 || prec
!= 0 ||
973 (flags
& ALT
&& base
== 8))
974 cp
= __ultoa(ulval
, cp
, base
,
977 size
= buf
+ BUF
- cp
;
978 if (size
> BUF
) /* should never happen */
979 LIBC_ABORT("size (%d) > BUF (%d)", size
, BUF
);
980 if ((flags
& GROUPING
) && size
!= 0)
981 size
+= grouping_init(&gs
, size
, loc
);
988 default: /* "%?" prints ?, unless ? is NUL */
991 /* pretend it was %c with argument ch */
1000 if (flags
& VECTOR
) {
1002 * Do the minimum amount of work necessary to construct
1003 * a format specifier that can be used to recursively
1004 * call vfprintf() for each element in the vector.
1006 int i
, j
; /* Counter. */
1007 int vcnt
; /* Number of elements in vector. */
1008 char *vfmt
; /* Pointer to format specifier. */
1010 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1011 int vwidth
= 0; /* Width specified via '*'. */
1012 int vprec
= 0; /* Precision specified via '*'. */
1013 char *vstr
; /* Used for asprintf(). */
1014 int vlen
; /* Length returned by asprintf(). */
1016 V_CHAR
, V_SHORT
, V_INT
,
1017 V_PCHAR
, V_PSHORT
, V_PINT
,
1020 V_LONGLONG
, V_PLONGLONG
,
1022 #endif /* V64TYPE */
1025 vval
.vectorarg
= GETARG(VECTORTYPE
);
1027 * Set vfmt. If vfmt_buf may not be big enough,
1028 * malloc() space, taking care to free it later.
1029 * (EXTRAHH is for possible extra "hh")
1031 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1034 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1036 /* Set the separator character, if not specified. */
1044 /* Create the format specifier. */
1045 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1047 case ',': case ';': case ':': case '_':
1048 case 'v': case 'h': case 'l':
1052 if (pct
[i
- 1] != '.')
1063 * Determine the number of elements in the vector and
1064 * finish up the format specifier.
1066 if (flags
& SHORTINT
) {
1080 } else if (flags
& LONGINT
) {
1082 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1084 } else if (flags
& LLONGINT
) {
1106 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1110 * The default case should never
1117 #endif /* V64TYPE */
1132 * The default case should never
1147 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1153 /* Get a vector element. */
1155 #define VPRINT(type, ind, args...) do { \
1158 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1161 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1164 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1167 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1170 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1173 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1176 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1179 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1182 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1185 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1189 PRINT(vstr, vlen); \
1193 #else /* !V64TYPE */
1194 #define VPRINT(type, ind, args...) do { \
1197 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1200 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1203 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1206 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1209 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1212 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1215 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1219 PRINT(vstr, vlen); \
1223 #endif /* V64TYPE */
1225 /* Actually print. */
1228 /* First element. */
1230 for (i
= 1; i
< vcnt
; i
++) {
1239 /* First element. */
1240 VPRINT(vtype
, 0, prec
);
1241 for (i
= 1; i
< vcnt
; i
++) {
1247 VPRINT(vtype
, i
, prec
);
1252 /* First element. */
1253 VPRINT(vtype
, 0, width
);
1254 for (i
= 1; i
< vcnt
; i
++) {
1260 VPRINT(vtype
, i
, width
);
1263 /* First element. */
1264 VPRINT(vtype
, 0, width
, prec
);
1265 for (i
= 1; i
< vcnt
; i
++) {
1271 VPRINT(vtype
, i
, width
, prec
);
1277 if (vfmt
!= vfmt_buf
)
1282 #endif /* VECTORS */
1284 * All reasonable formats wind up here. At this point, `cp'
1285 * points to a string which (if not flags&LADJUST) should be
1286 * padded out to `width' places. If flags&ZEROPAD, it should
1287 * first be prefixed by any sign or other prefix; otherwise,
1288 * it should be blank padded before the prefix is emitted.
1289 * After any left-hand padding and prefixing, emit zeroes
1290 * required by a decimal [diouxX] precision, then print the
1291 * string proper, then emit zeroes required by any leftover
1292 * floating precision; finally, if LADJUST, pad with blanks.
1294 * Compute actual size, so we know how much to pad.
1295 * size excludes decimal prec; realsz includes it.
1297 realsz
= dprec
> size
? dprec
: size
;
1303 prsize
= width
> realsz
? width
: realsz
;
1304 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1309 /* right-adjusting blank padding */
1310 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1311 PAD(width
- realsz
, blanks
);
1317 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1322 /* right-adjusting zero padding */
1323 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1324 PAD(width
- realsz
, zeroes
);
1326 /* the string or number proper */
1327 #ifndef NO_FLOATING_POINT
1328 if ((flags
& FPT
) == 0) {
1330 /* leading zeroes from decimal precision */
1331 PAD(dprec
- size
, zeroes
);
1333 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, loc
) < 0)
1338 #ifndef NO_FLOATING_POINT
1339 } else { /* glue together f_p fragments */
1340 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1343 if (prec
|| flags
& ALT
)
1344 PRINT(decimal_point
,decpt_len
);
1346 /* already handled initial 0's */
1350 n
= grouping_print(&gs
, &io
,
1356 PRINTANDPAD(cp
, dtoaend
,
1360 if (prec
|| flags
& ALT
)
1361 PRINT(decimal_point
,decpt_len
);
1363 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1364 } else { /* %[eE] or sufficiently long %[gG] */
1365 if (prec
> 1 || flags
& ALT
) {
1367 PRINT(decimal_point
, decpt_len
);
1369 PAD(prec
- ndig
, zeroes
);
1372 PRINT(expstr
, expsize
);
1376 /* left-adjusting padding (always blank) */
1377 if (flags
& LADJUST
)
1378 PAD(width
- realsz
, blanks
);
1380 /* finally, adjust ret */
1383 FLUSH(); /* copy out the I/O vectors */
1389 #ifndef NO_FLOATING_POINT
1390 if (dtoaresult
!= NULL
)
1391 freedtoa(dtoaresult
);
1393 if (convbuf
!= NULL
)
1397 if ((argtable
!= NULL
) && (argtable
!= statargtable
))