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 #define OS_CRASH_ENABLE_EXPERIMENTAL_LIBTRACE 1
35 #if defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.90 2009/02/28 06:06:57 das Exp $");
41 #include "xlocale_private.h"
44 * Actual printf innards.
46 * This code is large and complicated...
49 #include "namespace.h"
50 #include <sys/types.h>
61 #if 0 // xprintf pending API review
67 #include "un-namespace.h"
69 #include <os/assumes.h>
70 #include <mach-o/dyld_priv.h>
71 #include <mach/vm_region.h>
73 #include "libc_private.h"
76 #include "printflocal.h"
78 static int __sprint(FILE *, locale_t
, struct __suio
*);
80 static int __sbprintf(FILE *, locale_t
, const char *, va_list) __printflike(3, 0);
82 static char *__wcsconv(wchar_t *, int, locale_t
);
84 __private_extern__
const char *__fix_nogrouping(const char *);
87 #include "printfcommon.h"
89 struct grouping_state
{
90 char *thousands_sep
; /* locale-specific thousands separator */
91 int thousep_len
; /* length of thousands_sep */
92 const char *grouping
; /* locale-specific numeric grouping rules */
93 int lead
; /* sig figs before decimal or group sep */
94 int nseps
; /* number of group separators with ' */
95 int nrepeats
; /* number of repeats of the last group */
99 * Initialize the thousands' grouping state in preparation to print a
100 * number with ndigits digits. This routine returns the total number
101 * of bytes that will be needed.
104 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
106 struct lconv
*locale
;
108 locale
= localeconv_l(loc
);
109 gs
->grouping
= __fix_nogrouping(locale
->grouping
);
110 gs
->thousands_sep
= locale
->thousands_sep
;
111 gs
->thousep_len
= strlen(gs
->thousands_sep
);
113 gs
->nseps
= gs
->nrepeats
= 0;
115 while (*gs
->grouping
!= CHAR_MAX
) {
116 if (gs
->lead
<= *gs
->grouping
)
118 gs
->lead
-= *gs
->grouping
;
119 if (*(gs
->grouping
+1)) {
125 return ((gs
->nseps
+ gs
->nrepeats
) * gs
->thousep_len
);
129 * Print a number with thousands' separators.
132 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
133 const CHAR
*cp
, const CHAR
*ep
, locale_t loc
)
135 const CHAR
*cp0
= cp
;
137 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, loc
))
140 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
141 if (gs
->nrepeats
> 0)
147 if (io_print(iop
, gs
->thousands_sep
, gs
->thousep_len
, loc
))
149 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, loc
))
159 * Flush out all the vectors defined by the given uio,
160 * then reset it so that it can be reused.
163 __sprint(FILE *fp
, locale_t loc __unused
, struct __suio
*uio
)
167 if (uio
->uio_resid
== 0) {
171 err
= __sfvwrite(fp
, uio
);
179 * Helper function for `fprintf to unbuffered unix file': creates a
180 * temporary buffer. We only work on write-only files; this avoids
181 * worries about ungetc buffers and so forth.
184 __sbprintf(FILE *fp
, locale_t loc
, const char *fmt
, va_list ap
)
188 unsigned char buf
[BUFSIZ
];
193 /* XXX This is probably not needed. */
194 if (prepwrite(fp
) != 0)
197 /* copy the important variables */
198 fake
._flags
= fp
->_flags
& ~__SNBF
;
199 fake
._file
= fp
->_file
;
200 fake
._cookie
= fp
->_cookie
;
201 fake
._write
= fp
->_write
;
202 fake
._orientation
= fp
->_orientation
;
203 fake
._mbstate
= fp
->_mbstate
;
205 /* set up the buffer */
206 fake
._bf
._base
= fake
._p
= buf
;
207 fake
._bf
._size
= fake
._w
= sizeof(buf
);
208 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
210 /* do the work, then copy any error status */
211 ret
= __vfprintf(&fake
, loc
, fmt
, ap
);
212 if (ret
>= 0 && __fflush(&fake
))
214 if (fake
._flags
& __SERR
)
215 fp
->_flags
|= __SERR
;
221 * Convert a wide character string argument for the %ls format to a multibyte
222 * string representation. If not -1, prec specifies the maximum number of
223 * bytes to output, and also means that we can't assume that the wide char.
224 * string ends is null-terminated.
227 __wcsconv(wchar_t *wcsarg
, int prec
, locale_t loc
)
229 static const mbstate_t initial
;
231 char buf
[MB_LEN_MAX
];
236 /* Allocate space for the maximum number of bytes we could output. */
240 nbytes
= wcsrtombs_l(NULL
, (const wchar_t **)&p
, 0, &mbs
, loc
);
241 if (nbytes
== (size_t)-1)
245 * Optimisation: if the output precision is small enough,
246 * just allocate enough memory for the maximum instead of
247 * scanning the string.
256 clen
= wcrtomb_l(buf
, *p
++, &mbs
, loc
);
257 if (clen
== 0 || clen
== (size_t)-1 ||
258 nbytes
+ clen
> prec
)
264 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
267 /* Fill the output buffer. */
270 if ((nbytes
= wcsrtombs_l(convbuf
, (const wchar_t **)&p
,
271 nbytes
, &mbs
, loc
)) == (size_t)-1) {
275 convbuf
[nbytes
] = '\0';
283 vfprintf_l(FILE * __restrict fp
, locale_t loc
, const char * __restrict fmt0
, va_list ap
)
289 ret
= __xvprintf(XPRINTF_PLAIN
, NULL
, fp
, loc
, fmt0
, ap
);
295 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
301 ret
= __xvprintf(XPRINTF_PLAIN
, NULL
, fp
, __current_locale(), fmt0
, ap
);
307 * The size of the buffer we use as scratch space for integer
308 * conversions, among other things. We need enough space to
309 * write a uintmax_t in octal (plus one byte).
311 #if UINTMAX_MAX <= UINT64_MAX
314 #error "BUF must be large enough to format a uintmax_t"
317 __private_extern__
bool
318 __printf_is_memory_read_only(void *addr
, size_t __unused size
)
320 vm_address_t address
= addr
;
321 vm_size_t vmsize
= 0;
322 vm_region_basic_info_data_64_t info
;
323 mach_msg_type_number_t info_cnt
= VM_REGION_BASIC_INFO_COUNT_64
;
324 memory_object_name_t object
= MACH_PORT_NULL
;
325 kern_return_t kr
= KERN_SUCCESS
;
327 kr
= vm_region_64(mach_task_self(),
330 VM_REGION_BASIC_INFO_64
,
331 (vm_region_info_t
) &info
,
334 return (kr
== KERN_SUCCESS
) && !(info
.protection
& VM_PROT_WRITE
);
338 * Non-MT-safe version
340 __private_extern__
int
341 __vfprintf(FILE *fp
, locale_t loc
, const char *fmt0
, va_list ap
)
343 char *fmt
; /* format string */
344 int ch
; /* character from fmt */
345 ssize_t n
, n2
; /* handy integer (short term usage) */
346 char *cp
; /* handy char pointer (short term usage) */
347 int flags
; /* flags as above */
348 ssize_t ret
; /* return value accumulator */
349 ssize_t width
; /* width from format (%8d), or 0 */
350 ssize_t prec
; /* precision from format; <0 for N/A */
351 char sign
; /* sign prefix (' ', '+', '-', or \0) */
352 struct grouping_state gs
; /* thousands' grouping info */
354 #ifndef ALLOW_DYNAMIC_PERCENT_N
355 bool static_format_checked
= false;
356 #endif // ALLOW_DYNAMIC_PERCENT_N
358 #ifndef NO_FLOATING_POINT
360 * We can decompose the printed representation of floating
361 * point numbers into several parts, some of which may be empty:
363 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
366 * A: 'sign' holds this value if present; '\0' otherwise
367 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
368 * C: cp points to the string MMMNNN. Leading and trailing
369 * zeros are not in the string and must be added.
370 * D: expchar holds this character; '\0' if no exponent, e.g. %f
371 * F: at least two digits for decimal, at least one digit for hex
373 char *decimal_point
; /* locale specific decimal point */
374 int decpt_len
; /* length of decimal_point */
375 int signflag
; /* true if float is negative */
376 union { /* floating point arguments %[aAeEfFgG] */
380 int expt
; /* integer value of exponent */
381 char expchar
; /* exponent character: [eEpP\0] */
382 char *dtoaend
; /* pointer to end of converted digits */
383 int expsize
; /* character count for expstr */
384 int ndig
; /* actual number of digits returned by dtoa */
385 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
386 char *dtoaresult
; /* buffer allocated by dtoa */
389 union arg vval
; /* Vector argument. */
390 char *pct
; /* Pointer to '%' at beginning of specifier. */
391 char vsep
; /* Vector separator character. */
393 u_long ulval
; /* integer arguments %[diouxX] */
394 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
395 int base
; /* base for [diouxX] conversion */
396 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
397 ssize_t realsz
; /* field size expanded by dprec, sign, etc */
398 ssize_t size
; /* size of converted field or string */
399 ssize_t prsize
; /* max size of printed field */
400 const char *xdigs
; /* digits for %[xX] conversion */
401 struct io_state io
; /* I/O buffering state */
402 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
403 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
404 union arg
*argtable
; /* args, built due to positional arg */
405 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
406 int nextarg
; /* 1-based argument index */
407 va_list orgap
; /* original argument pointer */
408 char *convbuf
; /* wide to multibyte conversion result */
410 static const char xdigs_lower
[16] = "0123456789abcdef";
411 static const char xdigs_upper
[16] = "0123456789ABCDEF";
413 /* BEWARE, these `goto error' on error. */
414 #define PRINT(ptr, len) { \
415 if (io_print(&io, (ptr), (len), loc)) \
418 #define PAD(howmany, with) { \
419 if (io_pad(&io, (howmany), (with), loc)) \
422 #define PRINTANDPAD(p, ep, len, with) { \
423 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
427 if (io_flush(&io, loc)) \
432 * Get the argument indexed by nextarg. If the argument table is
433 * built, use it to get the argument. If its not, get the next
434 * argument (and arguments must be gotten sequentially).
436 #define GETARG(type) \
437 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
438 (nextarg++, va_arg(ap, type)))
441 * To extend shorts properly, we need both signed and unsigned
442 * argument extraction methods.
445 (flags&LONGINT ? GETARG(long) : \
446 flags&SHORTINT ? (long)(short)GETARG(int) : \
447 flags&CHARINT ? (long)(signed char)GETARG(int) : \
450 (flags&LONGINT ? GETARG(u_long) : \
451 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
452 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
453 (u_long)GETARG(u_int))
454 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
456 (flags&INTMAXT ? GETARG(intmax_t) : \
457 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
458 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
459 (intmax_t)GETARG(long long))
461 (flags&INTMAXT ? GETARG(uintmax_t) : \
462 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
463 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
464 (uintmax_t)GETARG(unsigned long long))
467 * Get * arguments, including the form *nn$. Preserve the nextarg
468 * that the argument can be gotten once the type is determined.
470 #define GETASTER(val) \
473 while (is_digit(*cp)) { \
474 n2 = 10 * n2 + to_digit(*cp); \
478 int hold = nextarg; \
479 if (argtable == NULL) { \
480 argtable = statargtable; \
481 if (__find_arguments (fmt0, orgap, &argtable)) { \
487 val = GETARG (int); \
491 val = GETARG (int); \
494 /* The following has been moved to __v2printf() */
496 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
497 if (prepwrite(fp
) != 0) {
511 #ifndef NO_FLOATING_POINT
513 decimal_point
= localeconv_l(loc
)->decimal_point
;
514 /* The overwhelmingly common case is decpt_len == 1. */
515 decpt_len
= (decimal_point
[1] == '\0' ? 1 : strlen(decimal_point
));
519 * Scan the format for conversions (`%' character).
522 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
524 if ((n
= fmt
- cp
) != 0) {
525 if (ret
+ n
>= INT_MAX
) {
538 fmt
++; /* skip over '%' */
548 vsep
= 'X'; /* Illegal value, changed to defaults later. */
552 reswitch
: switch (ch
) {
555 * ``If the space and + flags both appear, the space
556 * flag will be ignored.''
566 case ',': case ';': case ':': case '_':
572 * ``A negative field width argument is taken as a
573 * - flag followed by a positive field width.''
575 * They don't exclude field widths read from args.
592 if ((ch
= *fmt
++) == '*') {
597 while (is_digit(ch
)) {
598 prec
= 10 * prec
+ to_digit(ch
);
604 * ``Note that 0 is taken as a flag, not as the
605 * beginning of a field width.''
610 case '1': case '2': case '3': case '4':
611 case '5': case '6': case '7': case '8': case '9':
614 n
= 10 * n
+ to_digit(ch
);
616 } while (is_digit(ch
));
619 if (argtable
== NULL
) {
620 argtable
= statargtable
;
621 if (__find_arguments (fmt0
, orgap
,
631 #ifndef NO_FLOATING_POINT
637 if (flags
& SHORTINT
) {
647 if (flags
& LONGINT
) {
654 flags
|= LLONGINT
; /* not necessarily */
670 if (flags
& LONGINT
) {
671 static const mbstate_t initial
;
676 mbseqlen
= wcrtomb_l(cp
= buf
,
677 (wchar_t)GETARG(wint_t), &mbs
, loc
);
678 if (mbseqlen
== (size_t)-1) {
679 fp
->_flags
|= __SERR
;
682 size
= (int)mbseqlen
;
684 *(cp
= buf
) = GETARG(int);
698 if (flags
& INTMAX_SIZE
) {
700 if ((intmax_t)ujval
< 0) {
706 if ((long)ulval
< 0) {
713 #ifndef NO_FLOATING_POINT
717 if (flags
& VECTOR
) {
733 if (dtoaresult
!= NULL
)
734 freedtoa(dtoaresult
);
735 if (flags
& LONGDBL
) {
736 fparg
.ldbl
= GETARG(long double);
738 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
739 &expt
, &signflag
, &dtoaend
);
741 fparg
.dbl
= GETARG(double);
743 __hdtoa(fparg
.dbl
, xdigs
, prec
,
744 &expt
, &signflag
, &dtoaend
);
754 if (flags
& VECTOR
) {
760 if (prec
< 0) /* account for digit before decpt */
768 if (flags
& VECTOR
) {
778 if (flags
& VECTOR
) {
783 expchar
= ch
- ('g' - 'e');
789 if (dtoaresult
!= NULL
)
790 freedtoa(dtoaresult
);
791 if (flags
& LONGDBL
) {
792 fparg
.ldbl
= GETARG(long double);
794 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
795 &expt
, &signflag
, &dtoaend
);
797 fparg
.dbl
= GETARG(double);
799 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
800 &expt
, &signflag
, &dtoaend
);
807 if (expt
== INT_MAX
) { /* inf or nan */
809 cp
= (ch
>= 'a') ? "nan" : "NAN";
812 cp
= (ch
>= 'a') ? "inf" : "INF";
819 if (ch
== 'g' || ch
== 'G') {
820 if (expt
> -4 && expt
<= prec
) {
821 /* Make %[gG] smell like %[fF] */
831 * Make %[gG] smell like %[eE], but
832 * trim trailing zeroes if no # flag.
839 expsize
= exponent(expstr
, expt
- 1, expchar
);
840 size
= expsize
+ prec
;
841 if (prec
> 1 || flags
& ALT
)
844 /* space for digits before decimal point */
849 /* space for decimal pt and following digits */
850 if (prec
|| flags
& ALT
)
851 size
+= prec
+ decpt_len
;
852 if ((flags
& GROUPING
) && expt
> 0)
853 size
+= grouping_init(&gs
, expt
, loc
);
856 #endif /* !NO_FLOATING_POINT */
860 * Assignment-like behavior is specified if the
861 * value overflows or is otherwise unrepresentable.
862 * C99 says to use `signed char' for %hhn conversions.
864 void *ptr
= GETARG(void *);
868 #ifndef ALLOW_DYNAMIC_PERCENT_N
869 if (!static_format_checked
) {
870 static_format_checked
= __printf_is_memory_read_only((void*)fmt0
, strlen(fmt0
));
872 if (!static_format_checked
) {
873 os_crash("%%n used in a non-immutable format string: %s", fmt0
);
875 #endif // ALLOW_DYNAMIC_PERCENT_N
877 if (flags
& LLONGINT
)
878 *(long long *)ptr
= ret
;
879 else if (flags
& SIZET
)
880 *(ssize_t
*)ptr
= (ssize_t
)ret
;
881 else if (flags
& PTRDIFFT
)
882 *(ptrdiff_t *)ptr
= ret
;
883 else if (flags
& INTMAXT
)
884 *(intmax_t *)ptr
= ret
;
885 else if (flags
& LONGINT
)
887 else if (flags
& SHORTINT
)
889 else if (flags
& CHARINT
)
890 *(signed char *)ptr
= ret
;
893 continue; /* no output */
903 if (flags
& INTMAX_SIZE
)
911 * ``The argument shall be a pointer to void. The
912 * value of the pointer is converted to a sequence
913 * of printable characters, in an implementation-
921 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
924 flags
= flags
| INTMAXT
;
931 if (flags
& LONGINT
) {
935 if ((wcp
= GETARG(wchar_t *)) == NULL
) {
939 convbuf
= __wcsconv(wcp
, prec
, loc
);
940 if (convbuf
== NULL
) {
941 fp
->_flags
|= __SERR
;
946 } else if ((cp
= GETARG(char *)) == NULL
)
949 size_t cp_len
= (prec
>= 0) ? strnlen(cp
, prec
) : strlen(cp
);
950 if (cp_len
< INT_MAX
) {
967 if (flags
& INTMAX_SIZE
)
983 if (flags
& INTMAX_SIZE
)
988 /* leading 0x/X only if non-zero */
990 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
994 /* unsigned conversions */
997 * ``... diouXx conversions ... if a precision is
998 * specified, the 0 flag will be ignored.''
1000 * except for %#.0o and zero value
1002 number
: if ((dprec
= prec
) >= 0)
1006 * ``The result of converting a zero value with an
1007 * explicit precision of zero is no characters.''
1010 * ``The C Standard is clear enough as is. The call
1011 * printf("%#.0o", 0) should print 0.''
1012 * -- Defect Report #151
1015 if (flags
& INTMAX_SIZE
) {
1016 if (ujval
!= 0 || prec
!= 0 ||
1017 (flags
& ALT
&& base
== 8))
1018 cp
= __ujtoa(ujval
, cp
, base
,
1019 flags
& ALT
, xdigs
);
1021 if (ulval
!= 0 || prec
!= 0 ||
1022 (flags
& ALT
&& base
== 8))
1023 cp
= __ultoa(ulval
, cp
, base
,
1024 flags
& ALT
, xdigs
);
1026 size
= buf
+ BUF
- cp
;
1027 if (size
> BUF
) /* should never happen */
1028 LIBC_ABORT("size (%zd) > BUF (%d)", size
, BUF
);
1029 if ((flags
& GROUPING
) && size
!= 0)
1030 size
+= grouping_init(&gs
, size
, loc
);
1036 #endif /* VECTORS */
1037 default: /* "%?" prints ?, unless ? is NUL */
1040 /* pretend it was %c with argument ch */
1049 if (flags
& VECTOR
) {
1051 * Do the minimum amount of work necessary to construct
1052 * a format specifier that can be used to recursively
1053 * call vfprintf() for each element in the vector.
1055 int i
, j
; /* Counter. */
1056 int vcnt
; /* Number of elements in vector. */
1057 char *vfmt
; /* Pointer to format specifier. */
1059 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1060 int vwidth
= 0; /* Width specified via '*'. */
1061 int vprec
= 0; /* Precision specified via '*'. */
1062 char *vstr
; /* Used for asprintf(). */
1063 int vlen
; /* Length returned by asprintf(). */
1065 V_CHAR
, V_SHORT
, V_INT
,
1066 V_PCHAR
, V_PSHORT
, V_PINT
,
1069 V_LONGLONG
, V_PLONGLONG
,
1071 #endif /* V64TYPE */
1074 vval
.vectorarg
= GETARG(VECTORTYPE
);
1076 * Set vfmt. If vfmt_buf may not be big enough,
1077 * malloc() space, taking care to free it later.
1078 * (EXTRAHH is for possible extra "hh")
1080 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1083 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1085 /* Set the separator character, if not specified. */
1093 /* Create the format specifier. */
1094 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1096 case ',': case ';': case ':': case '_':
1097 case 'v': case 'h': case 'l':
1101 if (pct
[i
- 1] != '.')
1112 * Determine the number of elements in the vector and
1113 * finish up the format specifier.
1115 if (flags
& SHORTINT
) {
1129 } else if (flags
& LONGINT
) {
1131 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1133 } else if (flags
& LLONGINT
) {
1155 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1159 * The default case should never
1166 #endif /* V64TYPE */
1181 * The default case should never
1196 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1202 /* Get a vector element. */
1204 #define VPRINT(type, ind, args...) do { \
1207 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1210 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1213 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1216 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1219 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1222 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1225 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1228 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1231 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1234 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1238 PRINT(vstr, vlen); \
1242 #else /* !V64TYPE */
1243 #define VPRINT(type, ind, args...) do { \
1246 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1249 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1252 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1255 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1258 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1261 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1264 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1268 PRINT(vstr, vlen); \
1272 #endif /* V64TYPE */
1274 /* Actually print. */
1277 /* First element. */
1279 for (i
= 1; i
< vcnt
; i
++) {
1288 /* First element. */
1289 VPRINT(vtype
, 0, prec
);
1290 for (i
= 1; i
< vcnt
; i
++) {
1296 VPRINT(vtype
, i
, prec
);
1301 /* First element. */
1302 VPRINT(vtype
, 0, width
);
1303 for (i
= 1; i
< vcnt
; i
++) {
1309 VPRINT(vtype
, i
, width
);
1312 /* First element. */
1313 VPRINT(vtype
, 0, width
, prec
);
1314 for (i
= 1; i
< vcnt
; i
++) {
1320 VPRINT(vtype
, i
, width
, prec
);
1326 if (vfmt
!= vfmt_buf
)
1331 #endif /* VECTORS */
1333 * All reasonable formats wind up here. At this point, `cp'
1334 * points to a string which (if not flags&LADJUST) should be
1335 * padded out to `width' places. If flags&ZEROPAD, it should
1336 * first be prefixed by any sign or other prefix; otherwise,
1337 * it should be blank padded before the prefix is emitted.
1338 * After any left-hand padding and prefixing, emit zeroes
1339 * required by a decimal [diouxX] precision, then print the
1340 * string proper, then emit zeroes required by any leftover
1341 * floating precision; finally, if LADJUST, pad with blanks.
1343 * Compute actual size, so we know how much to pad.
1344 * size excludes decimal prec; realsz includes it.
1346 realsz
= dprec
> size
? dprec
: size
;
1352 prsize
= width
> realsz
? width
: realsz
;
1353 if (ret
+ prsize
>= INT_MAX
) {
1359 /* right-adjusting blank padding */
1360 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1361 PAD(width
- realsz
, blanks
);
1367 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1372 /* right-adjusting zero padding */
1373 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1374 PAD(width
- realsz
, zeroes
);
1376 /* the string or number proper */
1377 #ifndef NO_FLOATING_POINT
1378 if ((flags
& FPT
) == 0) {
1380 /* leading zeroes from decimal precision */
1381 PAD(dprec
- size
, zeroes
);
1383 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, loc
) < 0)
1388 #ifndef NO_FLOATING_POINT
1389 } else { /* glue together f_p fragments */
1390 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1393 if (prec
|| flags
& ALT
)
1394 PRINT(decimal_point
,decpt_len
);
1396 /* already handled initial 0's */
1400 n
= grouping_print(&gs
, &io
,
1406 PRINTANDPAD(cp
, dtoaend
,
1410 if (prec
|| flags
& ALT
)
1411 PRINT(decimal_point
,decpt_len
);
1413 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1414 } else { /* %[eE] or sufficiently long %[gG] */
1415 if (prec
> 1 || flags
& ALT
) {
1417 PRINT(decimal_point
, decpt_len
);
1419 PAD(prec
- ndig
, zeroes
);
1422 PRINT(expstr
, expsize
);
1426 /* left-adjusting padding (always blank) */
1427 if (flags
& LADJUST
)
1428 PAD(width
- realsz
, blanks
);
1430 /* finally, adjust ret */
1433 FLUSH(); /* copy out the I/O vectors */
1439 #ifndef NO_FLOATING_POINT
1440 if (dtoaresult
!= NULL
)
1441 freedtoa(dtoaresult
);
1446 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1448 return (ret
< 0 || ret
>= INT_MAX
) ? -1 : (int)ret
;