]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfwprintf.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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
36 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.42 2009/11/25 04:27:55 wollman Exp $");
41 #include "xlocale_private.h"
44 * Actual wprintf innards.
46 * Avoid making gratuitous changes to this source file; it should be kept
47 * as close as possible to vfprintf.c for ease of maintenance.
50 #include "namespace.h"
51 #include <sys/types.h>
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 wchar_t *, va_list);
74 static wint_t __xfputwc(wchar_t, FILE *, locale_t
);
75 static wchar_t *__mbsconv(char *, int, locale_t
);
76 __private_extern__
const char *__fix_nogrouping(const char *);
79 #include "printfcommon.h"
81 struct grouping_state
{
82 wchar_t thousands_sep
; /* locale-specific thousands separator */
83 const char *grouping
; /* locale-specific numeric grouping rules */
84 int lead
; /* sig figs before decimal or group sep */
85 int nseps
; /* number of group separators with ' */
86 int nrepeats
; /* number of repeats of the last group */
89 static const mbstate_t initial_mbs
;
92 get_decpt(locale_t loc
)
99 nconv
= mbrtowc_l(&decpt
, localeconv_l(loc
)->decimal_point
, MB_CUR_MAX_L(loc
), &mbs
, loc
);
100 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
101 decpt
= '.'; /* failsafe */
105 static inline wchar_t
106 get_thousep(locale_t loc
)
113 nconv
= mbrtowc_l(&thousep
, localeconv_l(loc
)->thousands_sep
,
114 MB_CUR_MAX_L(loc
), &mbs
, loc
);
115 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
116 thousep
= '\0'; /* failsafe */
121 * Initialize the thousands' grouping state in preparation to print a
122 * number with ndigits digits. This routine returns the total number
123 * of wide characters that will be printed.
126 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
129 gs
->grouping
= __fix_nogrouping(localeconv_l(loc
)->grouping
);
130 gs
->thousands_sep
= get_thousep(loc
);
132 gs
->nseps
= gs
->nrepeats
= 0;
134 while (*gs
->grouping
!= CHAR_MAX
) {
135 if (gs
->lead
<= *gs
->grouping
)
137 gs
->lead
-= *gs
->grouping
;
138 if (*(gs
->grouping
+1)) {
144 return (gs
->nseps
+ gs
->nrepeats
);
148 * Print a number with thousands' separators.
151 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
152 const CHAR
*cp
, const CHAR
*ep
, locale_t loc
)
154 const CHAR
*cp0
= cp
;
156 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, loc
))
159 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
160 if (gs
->nrepeats
> 0)
166 if (io_print(iop
, &gs
->thousands_sep
, 1, loc
))
168 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, loc
))
179 * Flush out all the vectors defined by the given uio,
180 * then reset it so that it can be reused.
182 * XXX The fact that we do this a character at a time and convert to a
183 * multibyte character sequence even if the destination is a wide
184 * string eclipses the benefits of buffering.
187 __sprint(FILE *fp
, locale_t loc
, struct __suio
*uio
)
194 for (; uio
->uio_resid
!= 0; uio
->uio_resid
-= len
, iov
++) {
195 p
= (wchar_t *)iov
->iov_base
;
197 for (i
= 0; i
< len
; i
++) {
198 if (__xfputwc(p
[i
], fp
, loc
) == WEOF
)
207 * Helper function for `fprintf to unbuffered unix file': creates a
208 * temporary buffer. We only work on write-only files; this avoids
209 * worries about ungetc buffers and so forth.
212 __sbprintf(FILE *fp
, locale_t loc
, const wchar_t *fmt
, va_list ap
)
216 unsigned char buf
[BUFSIZ
];
221 /* XXX This is probably not needed. */
222 if (prepwrite(fp
) != 0)
225 /* copy the important variables */
226 fake
._flags
= fp
->_flags
& ~__SNBF
;
227 fake
._file
= fp
->_file
;
228 fake
._cookie
= fp
->_cookie
;
229 fake
._write
= fp
->_write
;
230 fake
._orientation
= fp
->_orientation
;
231 fake
._mbstate
= fp
->_mbstate
;
233 /* set up the buffer */
234 fake
._bf
._base
= fake
._p
= buf
;
235 fake
._bf
._size
= fake
._w
= sizeof(buf
);
236 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
238 /* do the work, then copy any error status */
239 ret
= __vfwprintf(&fake
, loc
, fmt
, ap
);
240 if (ret
>= 0 && __fflush(&fake
))
242 if (fake
._flags
& __SERR
)
243 fp
->_flags
|= __SERR
;
248 * Like __fputwc, but handles fake string (__SSTR) files properly.
249 * File must already be locked.
252 __xfputwc(wchar_t wc
, FILE *fp
, locale_t loc
)
255 char buf
[MB_LEN_MAX
];
260 if ((fp
->_flags
& __SSTR
) == 0)
261 return (__fputwc(wc
, fp
, loc
));
264 if ((len
= wcrtomb_l(buf
, wc
, &mbs
, loc
)) == (size_t)-1) {
265 fp
->_flags
|= __SERR
;
273 return (__sfvwrite(fp
, &uio
) != EOF
? (wint_t)wc
: WEOF
);
277 * Convert a multibyte character string argument for the %s format to a wide
278 * string representation. ``prec'' specifies the maximum number of bytes
279 * to output. If ``prec'' is greater than or equal to zero, we can't assume
280 * that the multibyte char. string ends in a null character.
283 __mbsconv(char *mbsarg
, int prec
, locale_t loc
)
286 wchar_t *convbuf
, *wcp
;
288 size_t insize
, nchars
, nconv
= 0;
289 int mb_cur_max
= MB_CUR_MAX_L(loc
);
295 * Supplied argument is a multibyte string; convert it to wide
300 * String is not guaranteed to be NUL-terminated. Find the
301 * number of characters to print.
304 insize
= nchars
= nconv
= 0;
306 while (nchars
!= (size_t)prec
) {
307 nconv
= mbrlen_l(p
, mb_cur_max
, &mbs
, loc
);
308 if (nconv
== 0 || nconv
== (size_t)-1 ||
315 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
318 insize
= strlen(mbsarg
);
323 * Allocate buffer for the result and perform the conversion,
324 * converting at most `size' bytes of the input multibyte string to
325 * wide characters for printing.
327 convbuf
= malloc((insize
+ 1) * sizeof(*convbuf
));
333 while (insize
!= 0) {
334 nconv
= mbrtowc_l(wcp
, p
, insize
, &mbs
, loc
);
335 if (nconv
== 0 || nconv
== (size_t)-1 || nconv
== (size_t)-2)
341 if (nconv
== (size_t)-1 || nconv
== (size_t)-2) {
354 vfwprintf_l(FILE * __restrict fp
, locale_t loc
, const wchar_t * __restrict fmt0
, va_list ap
)
358 NORMALIZE_LOCALE(loc
);
360 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
361 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
363 ret
= __sbprintf(fp
, loc
, fmt0
, ap
);
365 ret
= __vfwprintf(fp
, loc
, fmt0
, ap
);
371 vfwprintf(FILE * __restrict fp
, const wchar_t * __restrict fmt0
, va_list ap
)
373 return vfwprintf_l(fp
, __current_locale(), fmt0
, ap
);
377 * The size of the buffer we use as scratch space for integer
378 * conversions, among other things. We need enough space to
379 * write a uintmax_t in octal (plus one byte).
381 #if UINTMAX_MAX <= UINT64_MAX
384 #error "BUF must be large enough to format a uintmax_t"
388 * Non-MT-safe version
390 __private_extern__
int
391 __vfwprintf(FILE *fp
, locale_t loc
, const wchar_t *fmt0
, va_list ap
)
393 wchar_t *fmt
; /* format string */
394 wchar_t ch
; /* character from fmt */
395 int n
, n2
; /* handy integer (short term usage) */
396 wchar_t *cp
; /* handy char pointer (short term usage) */
397 int flags
; /* flags as above */
398 int ret
; /* return value accumulator */
399 int width
; /* width from format (%8d), or 0 */
400 int prec
; /* precision from format; <0 for N/A */
401 wchar_t sign
; /* sign prefix (' ', '+', '-', or \0) */
402 struct grouping_state gs
; /* thousands' grouping info */
403 #ifndef NO_FLOATING_POINT
405 * We can decompose the printed representation of floating
406 * point numbers into several parts, some of which may be empty:
408 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
411 * A: 'sign' holds this value if present; '\0' otherwise
412 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
413 * C: cp points to the string MMMNNN. Leading and trailing
414 * zeros are not in the string and must be added.
415 * D: expchar holds this character; '\0' if no exponent, e.g. %f
416 * F: at least two digits for decimal, at least one digit for hex
418 wchar_t decimal_point
; /* locale specific decimal point */
419 int signflag
; /* true if float is negative */
420 union { /* floating point arguments %[aAeEfFgG] */
424 int expt
; /* integer value of exponent */
425 char expchar
; /* exponent character: [eEpP\0] */
426 char *dtoaend
; /* pointer to end of converted digits */
427 int expsize
; /* character count for expstr */
428 int ndig
; /* actual number of digits returned by dtoa */
429 wchar_t expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
430 char *dtoaresult
; /* buffer allocated by dtoa */
433 union arg vval
; /* Vector argument. */
434 wchar_t *pct
; /* Pointer to '%' at beginning of specifier. */
435 wchar_t vsep
; /* Vector separator character. */
437 u_long ulval
; /* integer arguments %[diouxX] */
438 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
439 int base
; /* base for [diouxX] conversion */
440 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
441 int realsz
; /* field size expanded by dprec, sign, etc */
442 int size
; /* size of converted field or string */
443 int prsize
; /* max size of printed field */
444 const char *xdigs
; /* digits for [xX] conversion */
445 struct io_state io
; /* I/O buffering state */
446 wchar_t buf
[BUF
]; /* buffer with space for digits of uintmax_t */
447 wchar_t ox
[2]; /* space for 0x hex-prefix */
448 union arg
*argtable
; /* args, built due to positional arg */
449 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
450 int nextarg
; /* 1-based argument index */
451 va_list orgap
; /* original argument pointer */
452 wchar_t *convbuf
; /* multibyte to wide conversion result */
454 static const char xdigs_lower
[16] = "0123456789abcdef";
455 static const char xdigs_upper
[16] = "0123456789ABCDEF";
457 /* BEWARE, these `goto error' on error. */
458 #define PRINT(ptr, len) do { \
459 if (io_print(&io, (ptr), (len), loc)) \
462 #define PAD(howmany, with) { \
463 if (io_pad(&io, (howmany), (with), loc)) \
466 #define PRINTANDPAD(p, ep, len, with) { \
467 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
471 if (io_flush(&io, loc)) \
476 * Get the argument indexed by nextarg. If the argument table is
477 * built, use it to get the argument. If its not, get the next
478 * argument (and arguments must be gotten sequentially).
480 #define GETARG(type) \
481 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
482 (nextarg++, va_arg(ap, type)))
485 * To extend shorts properly, we need both signed and unsigned
486 * argument extraction methods.
489 (flags&LONGINT ? GETARG(long) : \
490 flags&SHORTINT ? (long)(short)GETARG(int) : \
491 flags&CHARINT ? (long)(signed char)GETARG(int) : \
494 (flags&LONGINT ? GETARG(u_long) : \
495 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
496 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
497 (u_long)GETARG(u_int))
498 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
500 (flags&INTMAXT ? GETARG(intmax_t) : \
501 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
502 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
503 (intmax_t)GETARG(long long))
505 (flags&INTMAXT ? GETARG(uintmax_t) : \
506 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
507 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
508 (uintmax_t)GETARG(unsigned long long))
511 * Get * arguments, including the form *nn$. Preserve the nextarg
512 * that the argument can be gotten once the type is determined.
514 #define GETASTER(val) \
517 while (is_digit(*cp)) { \
518 n2 = 10 * n2 + to_digit(*cp); \
522 int hold = nextarg; \
523 if (argtable == NULL) { \
524 argtable = statargtable; \
525 if (__find_warguments (fmt0, orgap, &argtable)) { \
531 val = GETARG (int); \
535 val = GETARG (int); \
539 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
540 if (prepwrite(fp
) != 0) {
547 fmt
= (wchar_t *)fmt0
;
553 #ifndef NO_FLOATING_POINT
554 decimal_point
= get_decpt(loc
);
558 * Scan the format for conversions (`%' character).
561 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
563 if ((n
= fmt
- cp
) != 0) {
564 if ((unsigned)ret
+ n
> INT_MAX
) {
576 fmt
++; /* skip over '%' */
586 vsep
= 'X'; /* Illegal value, changed to defaults later. */
590 reswitch
: switch (ch
) {
593 * ``If the space and + flags both appear, the space
594 * flag will be ignored.''
604 case ',': case ';': case ':': case '_':
610 * ``A negative field width argument is taken as a
611 * - flag followed by a positive field width.''
613 * They don't exclude field widths read from args.
630 if ((ch
= *fmt
++) == '*') {
635 while (is_digit(ch
)) {
636 prec
= 10 * prec
+ to_digit(ch
);
642 * ``Note that 0 is taken as a flag, not as the
643 * beginning of a field width.''
648 case '1': case '2': case '3': case '4':
649 case '5': case '6': case '7': case '8': case '9':
652 n
= 10 * n
+ to_digit(ch
);
654 } while (is_digit(ch
));
657 if (argtable
== NULL
) {
658 argtable
= statargtable
;
659 if (__find_warguments (fmt0
, orgap
,
669 #ifndef NO_FLOATING_POINT
675 if (flags
& SHORTINT
) {
685 if (flags
& LONGINT
) {
692 flags
|= LLONGINT
; /* not necessarily */
709 *(cp
= buf
) = (wchar_t)GETARG(wint_t);
711 *(cp
= buf
) = (wchar_t)btowc_l(GETARG(int), loc
);
724 if (flags
& INTMAX_SIZE
) {
726 if ((intmax_t)ujval
< 0) {
732 if ((long)ulval
< 0) {
739 #ifndef NO_FLOATING_POINT
743 if (flags
& VECTOR
) {
759 if (flags
& LONGDBL
) {
760 fparg
.ldbl
= GETARG(long double);
762 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
763 &expt
, &signflag
, &dtoaend
);
765 fparg
.dbl
= GETARG(double);
767 __hdtoa(fparg
.dbl
, xdigs
, prec
,
768 &expt
, &signflag
, &dtoaend
);
771 prec
= dtoaend
- dtoaresult
;
776 ndig
= dtoaend
- dtoaresult
;
777 cp
= convbuf
= __mbsconv(dtoaresult
, -1, loc
);
778 freedtoa(dtoaresult
);
783 if (flags
& VECTOR
) {
789 if (prec
< 0) /* account for digit before decpt */
797 if (flags
& VECTOR
) {
807 if (flags
& VECTOR
) {
812 expchar
= ch
- ('g' - 'e');
820 if (flags
& LONGDBL
) {
821 fparg
.ldbl
= GETARG(long double);
823 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
824 &expt
, &signflag
, &dtoaend
);
826 fparg
.dbl
= GETARG(double);
828 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
829 &expt
, &signflag
, &dtoaend
);
833 ndig
= dtoaend
- dtoaresult
;
834 cp
= convbuf
= __mbsconv(dtoaresult
, -1, loc
);
835 freedtoa(dtoaresult
);
839 if (expt
== INT_MAX
) { /* inf or nan */
841 cp
= (ch
>= 'a') ? L
"nan" : L
"NAN";
844 cp
= (ch
>= 'a') ? L
"inf" : L
"INF";
850 if (ch
== 'g' || ch
== 'G') {
851 if (expt
> -4 && expt
<= prec
) {
852 /* Make %[gG] smell like %[fF] */
862 * Make %[gG] smell like %[eE], but
863 * trim trailing zeroes if no # flag.
870 expsize
= exponent(expstr
, expt
- 1, expchar
);
871 size
= expsize
+ prec
;
872 if (prec
> 1 || flags
& ALT
)
875 /* space for digits before decimal point */
880 /* space for decimal pt and following digits */
881 if (prec
|| flags
& ALT
)
883 if ((flags
& GROUPING
) && expt
> 0)
884 size
+= grouping_init(&gs
, expt
, loc
);
887 #endif /* !NO_FLOATING_POINT */
891 * Assignment-like behavior is specified if the
892 * value overflows or is otherwise unrepresentable.
893 * C99 says to use `signed char' for %hhn conversions.
895 void *ptr
= GETARG(void *);
898 else if (flags
& LLONGINT
)
899 *(long long *)ptr
= ret
;
900 else if (flags
& SIZET
)
901 *(ssize_t
*)ptr
= (ssize_t
)ret
;
902 else if (flags
& PTRDIFFT
)
903 *(ptrdiff_t *)ptr
= ret
;
904 else if (flags
& INTMAXT
)
905 *(intmax_t *)ptr
= ret
;
906 else if (flags
& LONGINT
)
908 else if (flags
& SHORTINT
)
910 else if (flags
& CHARINT
)
911 *(signed char *)ptr
= ret
;
914 continue; /* no output */
924 if (flags
& INTMAX_SIZE
)
932 * ``The argument shall be a pointer to void. The
933 * value of the pointer is converted to a sequence
934 * of printable characters, in an implementation-
942 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
945 flags
= flags
| INTMAXT
;
952 if (flags
& LONGINT
) {
953 if ((cp
= GETARG(wchar_t *)) == NULL
)
960 if ((mbp
= GETARG(char *)) == NULL
)
963 convbuf
= __mbsconv(mbp
, prec
, loc
);
964 if (convbuf
== NULL
) {
965 fp
->_flags
|= __SERR
;
971 #if 0 // wcsnlen needs API review first
972 size
= (prec
>= 0) ? wcsnlen(cp
, prec
) : wcslen(cp
);
975 if(prec
>= 0 && prec
< size
)
988 if (flags
& INTMAX_SIZE
)
1003 #endif /* VECTORS */
1004 if (flags
& INTMAX_SIZE
)
1009 /* leading 0x/X only if non-zero */
1011 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1015 /* unsigned conversions */
1016 nosign
: sign
= '\0';
1018 * ``... diouXx conversions ... if a precision is
1019 * specified, the 0 flag will be ignored.''
1021 * except for %#.0o and zero value
1023 number
: if ((dprec
= prec
) >= 0)
1027 * ``The result of converting a zero value with an
1028 * explicit precision of zero is no characters.''
1031 * ``The C Standard is clear enough as is. The call
1032 * printf("%#.0o", 0) should print 0.''
1033 * -- Defect Report #151
1036 if (flags
& INTMAX_SIZE
) {
1037 if (ujval
!= 0 || prec
!= 0 ||
1038 (flags
& ALT
&& base
== 8))
1039 cp
= __ujtoa(ujval
, cp
, base
,
1040 flags
& ALT
, xdigs
);
1042 if (ulval
!= 0 || prec
!= 0 ||
1043 (flags
& ALT
&& base
== 8))
1044 cp
= __ultoa(ulval
, cp
, base
,
1045 flags
& ALT
, xdigs
);
1047 size
= buf
+ BUF
- cp
;
1048 if (size
> BUF
) /* should never happen */
1049 LIBC_ABORT("size (%d) > BUF (%d)", size
, BUF
);
1050 if ((flags
& GROUPING
) && size
!= 0)
1051 size
+= grouping_init(&gs
, size
, loc
);
1057 #endif /* VECTORS */
1058 default: /* "%?" prints ?, unless ? is NUL */
1061 /* pretend it was %c with argument ch */
1070 if (flags
& VECTOR
) {
1072 * Do the minimum amount of work necessary to construct
1073 * a format specifier that can be used to recursively
1074 * call vfprintf() for each element in the vector.
1076 int i
, j
; /* Counter. */
1077 int vcnt
; /* Number of elements in vector. */
1078 char *vfmt
; /* Pointer to format specifier. */
1080 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1081 int vwidth
= 0; /* Width specified via '*'. */
1082 int vprec
= 0; /* Precision specified via '*'. */
1083 char *vstr
; /* Used for asprintf(). */
1084 int vlen
; /* Length returned by asprintf(). */
1086 V_CHAR
, V_SHORT
, V_INT
,
1087 V_PCHAR
, V_PSHORT
, V_PINT
,
1090 V_LONGLONG
, V_PLONGLONG
,
1092 #endif /* V64TYPE */
1095 vval
.vectorarg
= GETARG(VECTORTYPE
);
1097 * Set vfmt. If vfmt_buf may not be big enough,
1098 * malloc() space, taking care to free it later.
1099 * (EXTRAHH is for possible extra "hh")
1101 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1104 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1106 /* Set the separator character, if not specified. */
1114 /* Create the format specifier. */
1115 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1117 case ',': case ';': case ':': case '_':
1118 case 'v': case 'h': case 'l':
1122 if (pct
[i
- 1] != '.')
1133 * Determine the number of elements in the vector and
1134 * finish up the format specifier.
1136 if (flags
& SHORTINT
) {
1150 } else if (flags
& LONGINT
) {
1152 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1154 } else if (flags
& LLONGINT
) {
1176 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1180 * The default case should never
1187 #endif /* V64TYPE */
1202 * The default case should never
1217 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1223 /* Get a vector element. */
1225 #define VPRINT(type, ind, args...) do { \
1228 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1231 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1234 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1237 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1240 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1243 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1246 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1249 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1252 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1255 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1259 PRINT(vstr, vlen); \
1262 #else /* !V64TYPE */
1263 #define VPRINT(type, ind, args...) do { \
1266 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1269 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1272 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1275 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1278 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1281 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1284 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1288 PRINT(vstr, vlen); \
1291 #endif /* V64TYPE */
1293 /* Actually print. */
1296 /* First element. */
1298 for (i
= 1; i
< vcnt
; i
++) {
1307 /* First element. */
1308 VPRINT(vtype
, 0, prec
);
1309 for (i
= 1; i
< vcnt
; i
++) {
1315 VPRINT(vtype
, i
, prec
);
1320 /* First element. */
1321 VPRINT(vtype
, 0, width
);
1322 for (i
= 1; i
< vcnt
; i
++) {
1328 VPRINT(vtype
, i
, width
);
1331 /* First element. */
1332 VPRINT(vtype
, 0, width
, prec
);
1333 for (i
= 1; i
< vcnt
; i
++) {
1339 VPRINT(vtype
, i
, width
, prec
);
1345 if (vfmt
!= vfmt_buf
)
1350 #endif /* VECTORS */
1352 * All reasonable formats wind up here. At this point, `cp'
1353 * points to a string which (if not flags&LADJUST) should be
1354 * padded out to `width' places. If flags&ZEROPAD, it should
1355 * first be prefixed by any sign or other prefix; otherwise,
1356 * it should be blank padded before the prefix is emitted.
1357 * After any left-hand padding and prefixing, emit zeroes
1358 * required by a decimal [diouxX] precision, then print the
1359 * string proper, then emit zeroes required by any leftover
1360 * floating precision; finally, if LADJUST, pad with blanks.
1362 * Compute actual size, so we know how much to pad.
1363 * size excludes decimal prec; realsz includes it.
1365 realsz
= dprec
> size
? dprec
: size
;
1371 prsize
= width
> realsz
? width
: realsz
;
1372 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1377 /* right-adjusting blank padding */
1378 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1379 PAD(width
- realsz
, blanks
);
1385 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1390 /* right-adjusting zero padding */
1391 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1392 PAD(width
- realsz
, zeroes
);
1394 /* the string or number proper */
1395 #ifndef NO_FLOATING_POINT
1396 if ((flags
& FPT
) == 0) {
1398 /* leading zeroes from decimal precision */
1399 PAD(dprec
- size
, zeroes
);
1401 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, loc
) < 0)
1406 #ifndef NO_FLOATING_POINT
1407 } else { /* glue together f_p fragments */
1408 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1411 if (prec
|| flags
& ALT
)
1412 PRINT(&decimal_point
, 1);
1414 /* already handled initial 0's */
1418 n
= grouping_print(&gs
, &io
,
1419 cp
, convbuf
+ ndig
, loc
);
1424 PRINTANDPAD(cp
, convbuf
+ ndig
,
1428 if (prec
|| flags
& ALT
)
1429 PRINT(&decimal_point
, 1);
1431 PRINTANDPAD(cp
, convbuf
+ ndig
, prec
, zeroes
);
1432 } else { /* %[eE] or sufficiently long %[gG] */
1433 if (prec
> 1 || flags
& ALT
) {
1435 buf
[1] = decimal_point
;
1438 PAD(prec
- ndig
, zeroes
);
1441 PRINT(expstr
, expsize
);
1445 /* left-adjusting padding (always blank) */
1446 if (flags
& LADJUST
)
1447 PAD(width
- realsz
, blanks
);
1449 /* finally, adjust ret */
1452 FLUSH(); /* copy out the I/O vectors */
1458 if (convbuf
!= NULL
)
1462 if ((argtable
!= NULL
) && (argtable
!= statargtable
))