]>
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 <os/assumes.h>
68 #include <mach-o/dyld_priv.h>
70 #include "libc_private.h"
73 #include "printflocal.h"
75 static int __sprint(FILE *, locale_t
, struct __suio
*);
76 static int __sbprintf(FILE *, locale_t
, const wchar_t *, va_list);
77 static wint_t __xfputwc(wchar_t, FILE *, locale_t
);
78 static wchar_t *__mbsconv(char *, int, locale_t
);
79 __private_extern__
const char *__fix_nogrouping(const char *);
82 #include "printfcommon.h"
84 struct grouping_state
{
85 wchar_t thousands_sep
; /* locale-specific thousands separator */
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 */
92 static const mbstate_t initial_mbs
;
95 get_decpt(locale_t loc
)
102 nconv
= mbrtowc_l(&decpt
, localeconv_l(loc
)->decimal_point
, MB_CUR_MAX_L(loc
), &mbs
, loc
);
103 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
104 decpt
= '.'; /* failsafe */
108 static inline wchar_t
109 get_thousep(locale_t loc
)
116 nconv
= mbrtowc_l(&thousep
, localeconv_l(loc
)->thousands_sep
,
117 MB_CUR_MAX_L(loc
), &mbs
, loc
);
118 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
119 thousep
= '\0'; /* failsafe */
124 * Initialize the thousands' grouping state in preparation to print a
125 * number with ndigits digits. This routine returns the total number
126 * of wide characters that will be printed.
129 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
132 gs
->grouping
= __fix_nogrouping(localeconv_l(loc
)->grouping
);
133 gs
->thousands_sep
= get_thousep(loc
);
135 gs
->nseps
= gs
->nrepeats
= 0;
137 while (*gs
->grouping
!= CHAR_MAX
) {
138 if (gs
->lead
<= *gs
->grouping
)
140 gs
->lead
-= *gs
->grouping
;
141 if (*(gs
->grouping
+1)) {
147 return (gs
->nseps
+ gs
->nrepeats
);
151 * Print a number with thousands' separators.
154 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
155 const CHAR
*cp
, const CHAR
*ep
, locale_t loc
)
157 const CHAR
*cp0
= cp
;
159 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, loc
))
162 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
163 if (gs
->nrepeats
> 0)
169 if (io_print(iop
, &gs
->thousands_sep
, 1, loc
))
171 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, loc
))
182 * Flush out all the vectors defined by the given uio,
183 * then reset it so that it can be reused.
185 * XXX The fact that we do this a character at a time and convert to a
186 * multibyte character sequence even if the destination is a wide
187 * string eclipses the benefits of buffering.
190 __sprint(FILE *fp
, locale_t loc
, struct __suio
*uio
)
197 for (; uio
->uio_resid
!= 0; uio
->uio_resid
-= len
, iov
++) {
198 p
= (wchar_t *)iov
->iov_base
;
200 for (i
= 0; i
< len
; i
++) {
201 if (__xfputwc(p
[i
], fp
, loc
) == WEOF
)
210 * Helper function for `fprintf to unbuffered unix file': creates a
211 * temporary buffer. We only work on write-only files; this avoids
212 * worries about ungetc buffers and so forth.
215 __sbprintf(FILE *fp
, locale_t loc
, const wchar_t *fmt
, va_list ap
)
219 unsigned char buf
[BUFSIZ
];
224 /* XXX This is probably not needed. */
225 if (prepwrite(fp
) != 0)
228 /* copy the important variables */
229 fake
._flags
= fp
->_flags
& ~__SNBF
;
230 fake
._file
= fp
->_file
;
231 fake
._cookie
= fp
->_cookie
;
232 fake
._write
= fp
->_write
;
233 fake
._orientation
= fp
->_orientation
;
234 fake
._mbstate
= fp
->_mbstate
;
236 /* set up the buffer */
237 fake
._bf
._base
= fake
._p
= buf
;
238 fake
._bf
._size
= fake
._w
= sizeof(buf
);
239 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
241 /* do the work, then copy any error status */
242 ret
= __vfwprintf(&fake
, loc
, fmt
, ap
);
243 if (ret
>= 0 && __fflush(&fake
))
245 if (fake
._flags
& __SERR
)
246 fp
->_flags
|= __SERR
;
251 * Like __fputwc, but handles fake string (__SSTR) files properly.
252 * File must already be locked.
255 __xfputwc(wchar_t wc
, FILE *fp
, locale_t loc
)
258 char buf
[MB_LEN_MAX
];
263 if ((fp
->_flags
& __SSTR
) == 0)
264 return (__fputwc(wc
, fp
, loc
));
267 if ((len
= wcrtomb_l(buf
, wc
, &mbs
, loc
)) == (size_t)-1) {
268 fp
->_flags
|= __SERR
;
276 return (__sfvwrite(fp
, &uio
) != EOF
? (wint_t)wc
: WEOF
);
280 * Convert a multibyte character string argument for the %s format to a wide
281 * string representation. ``prec'' specifies the maximum number of bytes
282 * to output. If ``prec'' is greater than or equal to zero, we can't assume
283 * that the multibyte char. string ends in a null character.
286 __mbsconv(char *mbsarg
, int prec
, locale_t loc
)
289 wchar_t *convbuf
, *wcp
;
291 size_t insize
, nchars
, nconv
= 0;
292 int mb_cur_max
= MB_CUR_MAX_L(loc
);
298 * Supplied argument is a multibyte string; convert it to wide
303 * String is not guaranteed to be NUL-terminated. Find the
304 * number of characters to print.
307 insize
= nchars
= nconv
= 0;
309 while (nchars
!= (size_t)prec
) {
310 nconv
= mbrlen_l(p
, mb_cur_max
, &mbs
, loc
);
311 if (nconv
== 0 || nconv
== (size_t)-1 ||
318 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
321 insize
= strlen(mbsarg
);
326 * Allocate buffer for the result and perform the conversion,
327 * converting at most `size' bytes of the input multibyte string to
328 * wide characters for printing.
330 convbuf
= malloc((insize
+ 1) * sizeof(*convbuf
));
336 while (insize
!= 0) {
337 nconv
= mbrtowc_l(wcp
, p
, insize
, &mbs
, loc
);
338 if (nconv
== 0 || nconv
== (size_t)-1 || nconv
== (size_t)-2)
344 if (nconv
== (size_t)-1 || nconv
== (size_t)-2) {
357 vfwprintf_l(FILE * __restrict fp
, locale_t loc
, const wchar_t * __restrict fmt0
, va_list ap
)
361 NORMALIZE_LOCALE(loc
);
363 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
364 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
366 ret
= __sbprintf(fp
, loc
, fmt0
, ap
);
368 ret
= __vfwprintf(fp
, loc
, fmt0
, ap
);
374 vfwprintf(FILE * __restrict fp
, const wchar_t * __restrict fmt0
, va_list ap
)
376 return vfwprintf_l(fp
, __current_locale(), fmt0
, ap
);
379 // Defined in vfprintf.c
380 bool __printf_is_memory_read_only(void *addr
, size_t size
);
383 * The size of the buffer we use as scratch space for integer
384 * conversions, among other things. We need enough space to
385 * write a uintmax_t in octal (plus one byte).
387 #if UINTMAX_MAX <= UINT64_MAX
390 #error "BUF must be large enough to format a uintmax_t"
394 * Non-MT-safe version
396 __private_extern__
int
397 __vfwprintf(FILE *fp
, locale_t loc
, const wchar_t *fmt0
, va_list ap
)
399 wchar_t *fmt
; /* format string */
400 wchar_t ch
; /* character from fmt */
401 ssize_t n
, n2
; /* handy integer (short term usage) */
402 wchar_t *cp
; /* handy char pointer (short term usage) */
403 int flags
; /* flags as above */
404 ssize_t ret
; /* return value accumulator */
405 ssize_t width
; /* width from format (%8d), or 0 */
406 ssize_t prec
; /* precision from format; <0 for N/A */
407 wchar_t sign
; /* sign prefix (' ', '+', '-', or \0) */
408 struct grouping_state gs
; /* thousands' grouping info */
410 #ifndef NO_FLOATING_POINT
412 * We can decompose the printed representation of floating
413 * point numbers into several parts, some of which may be empty:
415 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
418 * A: 'sign' holds this value if present; '\0' otherwise
419 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
420 * C: cp points to the string MMMNNN. Leading and trailing
421 * zeros are not in the string and must be added.
422 * D: expchar holds this character; '\0' if no exponent, e.g. %f
423 * F: at least two digits for decimal, at least one digit for hex
425 wchar_t decimal_point
; /* locale specific decimal point */
426 int signflag
; /* true if float is negative */
427 union { /* floating point arguments %[aAeEfFgG] */
431 int expt
; /* integer value of exponent */
432 char expchar
; /* exponent character: [eEpP\0] */
433 char *dtoaend
; /* pointer to end of converted digits */
434 int expsize
; /* character count for expstr */
435 int ndig
; /* actual number of digits returned by dtoa */
436 wchar_t expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
437 char *dtoaresult
; /* buffer allocated by dtoa */
440 union arg vval
; /* Vector argument. */
441 wchar_t *pct
; /* Pointer to '%' at beginning of specifier. */
442 wchar_t vsep
; /* Vector separator character. */
444 u_long ulval
; /* integer arguments %[diouxX] */
445 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
446 int base
; /* base for [diouxX] conversion */
447 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
448 ssize_t realsz
; /* field size expanded by dprec, sign, etc */
449 ssize_t size
; /* size of converted field or string */
450 ssize_t prsize
; /* max size of printed field */
451 const char *xdigs
; /* digits for [xX] conversion */
452 struct io_state io
; /* I/O buffering state */
453 wchar_t buf
[BUF
]; /* buffer with space for digits of uintmax_t */
454 wchar_t ox
[2]; /* space for 0x hex-prefix */
455 union arg
*argtable
; /* args, built due to positional arg */
456 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
457 int nextarg
; /* 1-based argument index */
458 va_list orgap
; /* original argument pointer */
459 wchar_t *convbuf
; /* multibyte to wide conversion result */
461 static const char xdigs_lower
[16] = "0123456789abcdef";
462 static const char xdigs_upper
[16] = "0123456789ABCDEF";
464 /* BEWARE, these `goto error' on error. */
465 #define PRINT(ptr, len) do { \
466 if (io_print(&io, (ptr), (len), loc)) \
469 #define PAD(howmany, with) { \
470 if (io_pad(&io, (howmany), (with), loc)) \
473 #define PRINTANDPAD(p, ep, len, with) { \
474 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
478 if (io_flush(&io, loc)) \
483 * Get the argument indexed by nextarg. If the argument table is
484 * built, use it to get the argument. If its not, get the next
485 * argument (and arguments must be gotten sequentially).
487 #define GETARG(type) \
488 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
489 (nextarg++, va_arg(ap, type)))
492 * To extend shorts properly, we need both signed and unsigned
493 * argument extraction methods.
496 (flags&LONGINT ? GETARG(long) : \
497 flags&SHORTINT ? (long)(short)GETARG(int) : \
498 flags&CHARINT ? (long)(signed char)GETARG(int) : \
501 (flags&LONGINT ? GETARG(u_long) : \
502 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
503 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
504 (u_long)GETARG(u_int))
505 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
507 (flags&INTMAXT ? GETARG(intmax_t) : \
508 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
509 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
510 (intmax_t)GETARG(long long))
512 (flags&INTMAXT ? GETARG(uintmax_t) : \
513 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
514 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
515 (uintmax_t)GETARG(unsigned long long))
518 * Get * arguments, including the form *nn$. Preserve the nextarg
519 * that the argument can be gotten once the type is determined.
521 #define GETASTER(val) \
524 while (is_digit(*cp)) { \
525 n2 = 10 * n2 + to_digit(*cp); \
529 int hold = nextarg; \
530 if (argtable == NULL) { \
531 argtable = statargtable; \
532 if (__find_warguments (fmt0, orgap, &argtable)) { \
538 val = GETARG (int); \
542 val = GETARG (int); \
546 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
547 if (prepwrite(fp
) != 0) {
554 fmt
= (wchar_t *)fmt0
;
560 #ifndef NO_FLOATING_POINT
561 decimal_point
= get_decpt(loc
);
565 * Scan the format for conversions (`%' character).
568 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
570 if ((n
= fmt
- cp
) != 0) {
571 if (ret
+ n
> INT_MAX
) {
584 fmt
++; /* skip over '%' */
594 vsep
= 'X'; /* Illegal value, changed to defaults later. */
598 reswitch
: switch (ch
) {
601 * ``If the space and + flags both appear, the space
602 * flag will be ignored.''
612 case ',': case ';': case ':': case '_':
618 * ``A negative field width argument is taken as a
619 * - flag followed by a positive field width.''
621 * They don't exclude field widths read from args.
638 if ((ch
= *fmt
++) == '*') {
643 while (is_digit(ch
)) {
644 prec
= 10 * prec
+ to_digit(ch
);
650 * ``Note that 0 is taken as a flag, not as the
651 * beginning of a field width.''
656 case '1': case '2': case '3': case '4':
657 case '5': case '6': case '7': case '8': case '9':
660 n
= 10 * n
+ to_digit(ch
);
662 } while (is_digit(ch
));
665 if (argtable
== NULL
) {
666 argtable
= statargtable
;
667 if (__find_warguments (fmt0
, orgap
,
677 #ifndef NO_FLOATING_POINT
683 if (flags
& SHORTINT
) {
693 if (flags
& LONGINT
) {
700 flags
|= LLONGINT
; /* not necessarily */
717 *(cp
= buf
) = (wchar_t)GETARG(wint_t);
719 *(cp
= buf
) = (wchar_t)btowc_l(GETARG(int), loc
);
732 if (flags
& INTMAX_SIZE
) {
734 if ((intmax_t)ujval
< 0) {
740 if ((long)ulval
< 0) {
747 #ifndef NO_FLOATING_POINT
751 if (flags
& VECTOR
) {
767 if (flags
& LONGDBL
) {
768 fparg
.ldbl
= GETARG(long double);
770 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
771 &expt
, &signflag
, &dtoaend
);
773 fparg
.dbl
= GETARG(double);
775 __hdtoa(fparg
.dbl
, xdigs
, prec
,
776 &expt
, &signflag
, &dtoaend
);
779 prec
= dtoaend
- dtoaresult
;
783 ndig
= dtoaend
- dtoaresult
;
784 cp
= convbuf
= __mbsconv(dtoaresult
, -1, loc
);
785 freedtoa(dtoaresult
);
790 if (flags
& VECTOR
) {
796 if (prec
< 0) /* account for digit before decpt */
804 if (flags
& VECTOR
) {
814 if (flags
& VECTOR
) {
819 expchar
= ch
- ('g' - 'e');
826 if (flags
& LONGDBL
) {
827 fparg
.ldbl
= GETARG(long double);
829 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
830 &expt
, &signflag
, &dtoaend
);
832 fparg
.dbl
= GETARG(double);
834 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
835 &expt
, &signflag
, &dtoaend
);
839 ndig
= dtoaend
- dtoaresult
;
840 cp
= convbuf
= __mbsconv(dtoaresult
, -1, loc
);
841 freedtoa(dtoaresult
);
845 if (expt
== INT_MAX
) { /* inf or nan */
847 cp
= (ch
>= 'a') ? L
"nan" : L
"NAN";
850 cp
= (ch
>= 'a') ? L
"inf" : L
"INF";
856 if (ch
== 'g' || ch
== 'G') {
857 if (expt
> -4 && expt
<= prec
) {
858 /* Make %[gG] smell like %[fF] */
868 * Make %[gG] smell like %[eE], but
869 * trim trailing zeroes if no # flag.
876 expsize
= exponent(expstr
, expt
- 1, expchar
);
877 size
= expsize
+ prec
;
878 if (prec
> 1 || flags
& ALT
)
881 /* space for digits before decimal point */
886 /* space for decimal pt and following digits */
887 if (prec
|| flags
& ALT
)
889 if ((flags
& GROUPING
) && expt
> 0)
890 size
+= grouping_init(&gs
, expt
, loc
);
893 #endif /* !NO_FLOATING_POINT */
897 * Assignment-like behavior is specified if the
898 * value overflows or is otherwise unrepresentable.
899 * C99 says to use `signed char' for %hhn conversions.
901 void *ptr
= GETARG(void *);
905 if (flags
& LLONGINT
)
906 *(long long *)ptr
= ret
;
907 else if (flags
& SIZET
)
908 *(ssize_t
*)ptr
= (ssize_t
)ret
;
909 else if (flags
& PTRDIFFT
)
910 *(ptrdiff_t *)ptr
= ret
;
911 else if (flags
& INTMAXT
)
912 *(intmax_t *)ptr
= ret
;
913 else if (flags
& LONGINT
)
915 else if (flags
& SHORTINT
)
917 else if (flags
& CHARINT
)
918 *(signed char *)ptr
= ret
;
921 continue; /* no output */
931 if (flags
& INTMAX_SIZE
)
939 * ``The argument shall be a pointer to void. The
940 * value of the pointer is converted to a sequence
941 * of printable characters, in an implementation-
949 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
952 flags
= flags
| INTMAXT
;
959 if (flags
& LONGINT
) {
960 if ((cp
= GETARG(wchar_t *)) == NULL
)
966 if ((mbp
= GETARG(char *)) == NULL
) {
970 convbuf
= __mbsconv(mbp
, prec
, loc
);
971 if (convbuf
== NULL
) {
972 fp
->_flags
|= __SERR
;
978 #if 0 // wcsnlen needs API review first
979 size
= (prec
>= 0) ? wcsnlen(cp
, prec
) : wcslen(cp
);
982 if (size
>= INT_MAX
) {
987 if(prec
>= 0 && prec
< size
)
1000 if (flags
& INTMAX_SIZE
)
1007 xdigs
= xdigs_upper
;
1010 xdigs
= xdigs_lower
;
1015 #endif /* VECTORS */
1016 if (flags
& INTMAX_SIZE
)
1021 /* leading 0x/X only if non-zero */
1023 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1027 /* unsigned conversions */
1028 nosign
: sign
= '\0';
1030 * ``... diouXx conversions ... if a precision is
1031 * specified, the 0 flag will be ignored.''
1033 * except for %#.0o and zero value
1035 number
: if ((dprec
= prec
) >= 0)
1039 * ``The result of converting a zero value with an
1040 * explicit precision of zero is no characters.''
1043 * ``The C Standard is clear enough as is. The call
1044 * printf("%#.0o", 0) should print 0.''
1045 * -- Defect Report #151
1048 if (flags
& INTMAX_SIZE
) {
1049 if (ujval
!= 0 || prec
!= 0 ||
1050 (flags
& ALT
&& base
== 8))
1051 cp
= __ujtoa(ujval
, cp
, base
,
1052 flags
& ALT
, xdigs
);
1054 if (ulval
!= 0 || prec
!= 0 ||
1055 (flags
& ALT
&& base
== 8))
1056 cp
= __ultoa(ulval
, cp
, base
,
1057 flags
& ALT
, xdigs
);
1059 size
= buf
+ BUF
- cp
;
1060 if (size
> BUF
) /* should never happen */
1061 LIBC_ABORT("size (%zd) > BUF (%d)", size
, BUF
);
1062 if ((flags
& GROUPING
) && size
!= 0)
1063 size
+= grouping_init(&gs
, size
, loc
);
1069 #endif /* VECTORS */
1070 default: /* "%?" prints ?, unless ? is NUL */
1073 /* pretend it was %c with argument ch */
1082 if (flags
& VECTOR
) {
1084 * Do the minimum amount of work necessary to construct
1085 * a format specifier that can be used to recursively
1086 * call vfprintf() for each element in the vector.
1088 int i
, j
; /* Counter. */
1089 int vcnt
; /* Number of elements in vector. */
1090 char *vfmt
; /* Pointer to format specifier. */
1092 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1093 int vwidth
= 0; /* Width specified via '*'. */
1094 int vprec
= 0; /* Precision specified via '*'. */
1095 char *vstr
; /* Used for asprintf(). */
1096 int vlen
; /* Length returned by asprintf(). */
1098 V_CHAR
, V_SHORT
, V_INT
,
1099 V_PCHAR
, V_PSHORT
, V_PINT
,
1102 V_LONGLONG
, V_PLONGLONG
,
1104 #endif /* V64TYPE */
1107 vval
.vectorarg
= GETARG(VECTORTYPE
);
1109 * Set vfmt. If vfmt_buf may not be big enough,
1110 * malloc() space, taking care to free it later.
1111 * (EXTRAHH is for possible extra "hh")
1113 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1116 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1118 /* Set the separator character, if not specified. */
1126 /* Create the format specifier. */
1127 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1129 case ',': case ';': case ':': case '_':
1130 case 'v': case 'h': case 'l':
1134 if (pct
[i
- 1] != '.')
1145 * Determine the number of elements in the vector and
1146 * finish up the format specifier.
1148 if (flags
& SHORTINT
) {
1162 } else if (flags
& LONGINT
) {
1164 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1166 } else if (flags
& LLONGINT
) {
1188 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1192 * The default case should never
1199 #endif /* V64TYPE */
1214 * The default case should never
1229 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1235 /* Get a vector element. */
1237 #define VPRINT(type, ind, args...) do { \
1240 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1243 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1246 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1249 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1252 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1255 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1258 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1261 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1264 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1267 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1271 PRINT((const CHAR *) vstr, vlen); \
1274 #else /* !V64TYPE */
1275 #define VPRINT(type, ind, args...) do { \
1278 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1281 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1284 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1287 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1290 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1293 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1296 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1300 PRINT((const CHAR *) vstr, vlen); \
1303 #endif /* V64TYPE */
1305 /* Actually print. */
1308 /* First element. */
1310 for (i
= 1; i
< vcnt
; i
++) {
1319 /* First element. */
1320 VPRINT(vtype
, 0, prec
);
1321 for (i
= 1; i
< vcnt
; i
++) {
1327 VPRINT(vtype
, i
, prec
);
1332 /* First element. */
1333 VPRINT(vtype
, 0, width
);
1334 for (i
= 1; i
< vcnt
; i
++) {
1340 VPRINT(vtype
, i
, width
);
1343 /* First element. */
1344 VPRINT(vtype
, 0, width
, prec
);
1345 for (i
= 1; i
< vcnt
; i
++) {
1351 VPRINT(vtype
, i
, width
, prec
);
1357 if (vfmt
!= vfmt_buf
)
1362 #endif /* VECTORS */
1364 * All reasonable formats wind up here. At this point, `cp'
1365 * points to a string which (if not flags&LADJUST) should be
1366 * padded out to `width' places. If flags&ZEROPAD, it should
1367 * first be prefixed by any sign or other prefix; otherwise,
1368 * it should be blank padded before the prefix is emitted.
1369 * After any left-hand padding and prefixing, emit zeroes
1370 * required by a decimal [diouxX] precision, then print the
1371 * string proper, then emit zeroes required by any leftover
1372 * floating precision; finally, if LADJUST, pad with blanks.
1374 * Compute actual size, so we know how much to pad.
1375 * size excludes decimal prec; realsz includes it.
1377 realsz
= dprec
> size
? dprec
: size
;
1383 prsize
= width
> realsz
? width
: realsz
;
1384 if (ret
+ prsize
> INT_MAX
) {
1390 /* right-adjusting blank padding */
1391 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1392 PAD(width
- realsz
, blanks
);
1398 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1403 /* right-adjusting zero padding */
1404 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1405 PAD(width
- realsz
, zeroes
);
1407 /* the string or number proper */
1408 #ifndef NO_FLOATING_POINT
1409 if ((flags
& FPT
) == 0) {
1411 /* leading zeroes from decimal precision */
1412 PAD(dprec
- size
, zeroes
);
1414 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, loc
) < 0)
1419 #ifndef NO_FLOATING_POINT
1420 } else { /* glue together f_p fragments */
1421 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1424 if (prec
|| flags
& ALT
)
1425 PRINT(&decimal_point
, 1);
1427 /* already handled initial 0's */
1431 n
= grouping_print(&gs
, &io
,
1432 cp
, convbuf
+ ndig
, loc
);
1437 PRINTANDPAD(cp
, convbuf
+ ndig
,
1441 if (prec
|| flags
& ALT
)
1442 PRINT(&decimal_point
, 1);
1444 PRINTANDPAD(cp
, convbuf
+ ndig
, prec
, zeroes
);
1445 } else { /* %[eE] or sufficiently long %[gG] */
1446 if (prec
> 1 || flags
& ALT
) {
1448 buf
[1] = decimal_point
;
1451 PAD(prec
- ndig
, zeroes
);
1454 PRINT(expstr
, expsize
);
1458 /* left-adjusting padding (always blank) */
1459 if (flags
& LADJUST
)
1460 PAD(width
- realsz
, blanks
);
1462 /* finally, adjust ret */
1465 FLUSH(); /* copy out the I/O vectors */
1474 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1476 return (ret
< 0 || ret
>= INT_MAX
) ? -1 : (int)ret
;