]>
git.saurik.com Git - apple/libc.git/blob - stdio/vfprintf-fbsd.c
3182836585deaa1089d55f250b6b7aeb736582a5
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.90 2009/02/28 06:06:57 das Exp $");
39 #include "xlocale_private.h"
42 * Actual printf innards.
44 * This code is large and complicated...
47 #include "namespace.h"
48 #include <sys/types.h>
59 #if 0 // xprintf pending API review
65 #include "un-namespace.h"
67 #include "libc_private.h"
70 #include "printflocal.h"
72 static int __sprint(FILE *, locale_t
, struct __suio
*);
73 static int __sbprintf(FILE *, locale_t
, const char *, va_list) __printflike(3, 0);
74 static char *__wcsconv(wchar_t *, int, locale_t
);
76 __private_extern__
const char *__fix_nogrouping(const char *);
79 #include "printfcommon.h"
81 struct grouping_state
{
82 char *thousands_sep
; /* locale-specific thousands separator */
83 int thousep_len
; /* length of thousands_sep */
84 const char *grouping
; /* locale-specific numeric grouping rules */
85 int lead
; /* sig figs before decimal or group sep */
86 int nseps
; /* number of group separators with ' */
87 int nrepeats
; /* number of repeats of the last group */
91 * Initialize the thousands' grouping state in preparation to print a
92 * number with ndigits digits. This routine returns the total number
93 * of bytes that will be needed.
96 grouping_init(struct grouping_state
*gs
, int ndigits
, locale_t loc
)
100 locale
= localeconv_l(loc
);
101 gs
->grouping
= __fix_nogrouping(locale
->grouping
);
102 gs
->thousands_sep
= locale
->thousands_sep
;
103 gs
->thousep_len
= strlen(gs
->thousands_sep
);
105 gs
->nseps
= gs
->nrepeats
= 0;
107 while (*gs
->grouping
!= CHAR_MAX
) {
108 if (gs
->lead
<= *gs
->grouping
)
110 gs
->lead
-= *gs
->grouping
;
111 if (*(gs
->grouping
+1)) {
117 return ((gs
->nseps
+ gs
->nrepeats
) * gs
->thousep_len
);
121 * Print a number with thousands' separators.
124 grouping_print(struct grouping_state
*gs
, struct io_state
*iop
,
125 const CHAR
*cp
, const CHAR
*ep
, locale_t loc
)
127 const CHAR
*cp0
= cp
;
129 if (io_printandpad(iop
, cp
, ep
, gs
->lead
, zeroes
, loc
))
132 while (gs
->nseps
> 0 || gs
->nrepeats
> 0) {
133 if (gs
->nrepeats
> 0)
139 if (io_print(iop
, gs
->thousands_sep
, gs
->thousep_len
, loc
))
141 if (io_printandpad(iop
, cp
, ep
, *gs
->grouping
, zeroes
, loc
))
151 * Flush out all the vectors defined by the given uio,
152 * then reset it so that it can be reused.
155 __sprint(FILE *fp
, locale_t loc __unused
, struct __suio
*uio
)
159 if (uio
->uio_resid
== 0) {
163 err
= __sfvwrite(fp
, uio
);
170 * Helper function for `fprintf to unbuffered unix file': creates a
171 * temporary buffer. We only work on write-only files; this avoids
172 * worries about ungetc buffers and so forth.
175 __sbprintf(FILE *fp
, locale_t loc
, const char *fmt
, va_list ap
)
179 unsigned char buf
[BUFSIZ
];
184 /* XXX This is probably not needed. */
185 if (prepwrite(fp
) != 0)
188 /* copy the important variables */
189 fake
._flags
= fp
->_flags
& ~__SNBF
;
190 fake
._file
= fp
->_file
;
191 fake
._cookie
= fp
->_cookie
;
192 fake
._write
= fp
->_write
;
193 fake
._orientation
= fp
->_orientation
;
194 fake
._mbstate
= fp
->_mbstate
;
196 /* set up the buffer */
197 fake
._bf
._base
= fake
._p
= buf
;
198 fake
._bf
._size
= fake
._w
= sizeof(buf
);
199 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
201 /* do the work, then copy any error status */
202 ret
= __vfprintf(&fake
, loc
, fmt
, ap
);
203 if (ret
>= 0 && __fflush(&fake
))
205 if (fake
._flags
& __SERR
)
206 fp
->_flags
|= __SERR
;
211 * Convert a wide character string argument for the %ls format to a multibyte
212 * string representation. If not -1, prec specifies the maximum number of
213 * bytes to output, and also means that we can't assume that the wide char.
214 * string ends is null-terminated.
217 __wcsconv(wchar_t *wcsarg
, int prec
, locale_t loc
)
219 static const mbstate_t initial
;
221 char buf
[MB_LEN_MAX
];
226 /* Allocate space for the maximum number of bytes we could output. */
230 nbytes
= wcsrtombs_l(NULL
, (const wchar_t **)&p
, 0, &mbs
, loc
);
231 if (nbytes
== (size_t)-1)
235 * Optimisation: if the output precision is small enough,
236 * just allocate enough memory for the maximum instead of
237 * scanning the string.
246 clen
= wcrtomb_l(buf
, *p
++, &mbs
, loc
);
247 if (clen
== 0 || clen
== (size_t)-1 ||
248 nbytes
+ clen
> prec
)
254 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
257 /* Fill the output buffer. */
260 if ((nbytes
= wcsrtombs_l(convbuf
, (const wchar_t **)&p
,
261 nbytes
, &mbs
, loc
)) == (size_t)-1) {
265 convbuf
[nbytes
] = '\0';
273 vfprintf_l(FILE * __restrict fp
, locale_t loc
, const char * __restrict fmt0
, va_list ap
)
278 NORMALIZE_LOCALE(loc
);
280 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
281 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
283 ret
= __sbprintf(fp
, loc
, fmt0
, ap
);
285 ret
= __vfprintf(fp
, loc
, fmt0
, ap
);
291 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
294 return vfprintf_l(fp
, __current_locale(), fmt0
, ap
);
298 * The size of the buffer we use as scratch space for integer
299 * conversions, among other things. We need enough space to
300 * write a uintmax_t in octal (plus one byte).
302 #if UINTMAX_MAX <= UINT64_MAX
305 #error "BUF must be large enough to format a uintmax_t"
309 * Non-MT-safe version
311 __private_extern__
int
312 __vfprintf(FILE *fp
, locale_t loc
, const char *fmt0
, va_list ap
)
314 char *fmt
; /* format string */
315 int ch
; /* character from fmt */
316 int n
, n2
; /* handy integer (short term usage) */
317 char *cp
; /* handy char pointer (short term usage) */
318 int flags
; /* flags as above */
319 int ret
; /* return value accumulator */
320 int width
; /* width from format (%8d), or 0 */
321 int prec
; /* precision from format; <0 for N/A */
322 char sign
; /* sign prefix (' ', '+', '-', or \0) */
323 struct grouping_state gs
; /* thousands' grouping info */
325 #ifndef NO_FLOATING_POINT
327 * We can decompose the printed representation of floating
328 * point numbers into several parts, some of which may be empty:
330 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
333 * A: 'sign' holds this value if present; '\0' otherwise
334 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
335 * C: cp points to the string MMMNNN. Leading and trailing
336 * zeros are not in the string and must be added.
337 * D: expchar holds this character; '\0' if no exponent, e.g. %f
338 * F: at least two digits for decimal, at least one digit for hex
340 char *decimal_point
; /* locale specific decimal point */
341 int decpt_len
; /* length of decimal_point */
342 int signflag
; /* true if float is negative */
343 union { /* floating point arguments %[aAeEfFgG] */
347 int expt
; /* integer value of exponent */
348 char expchar
; /* exponent character: [eEpP\0] */
349 char *dtoaend
; /* pointer to end of converted digits */
350 int expsize
; /* character count for expstr */
351 int ndig
; /* actual number of digits returned by dtoa */
352 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
353 char *dtoaresult
; /* buffer allocated by dtoa */
356 union arg vval
; /* Vector argument. */
357 char *pct
; /* Pointer to '%' at beginning of specifier. */
358 char vsep
; /* Vector separator character. */
360 u_long ulval
; /* integer arguments %[diouxX] */
361 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
362 int base
; /* base for [diouxX] conversion */
363 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
364 int realsz
; /* field size expanded by dprec, sign, etc */
365 int size
; /* size of converted field or string */
366 int prsize
; /* max size of printed field */
367 const char *xdigs
; /* digits for %[xX] conversion */
368 struct io_state io
; /* I/O buffering state */
369 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
370 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
371 union arg
*argtable
; /* args, built due to positional arg */
372 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
373 int nextarg
; /* 1-based argument index */
374 va_list orgap
; /* original argument pointer */
375 char *convbuf
; /* wide to multibyte conversion result */
377 static const char xdigs_lower
[16] = "0123456789abcdef";
378 static const char xdigs_upper
[16] = "0123456789ABCDEF";
380 /* BEWARE, these `goto error' on error. */
381 #define PRINT(ptr, len) { \
382 if (io_print(&io, (ptr), (len), loc)) \
385 #define PAD(howmany, with) { \
386 if (io_pad(&io, (howmany), (with), loc)) \
389 #define PRINTANDPAD(p, ep, len, with) { \
390 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \
394 if (io_flush(&io, loc)) \
399 * Get the argument indexed by nextarg. If the argument table is
400 * built, use it to get the argument. If its not, get the next
401 * argument (and arguments must be gotten sequentially).
403 #define GETARG(type) \
404 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
405 (nextarg++, va_arg(ap, type)))
408 * To extend shorts properly, we need both signed and unsigned
409 * argument extraction methods.
412 (flags&LONGINT ? GETARG(long) : \
413 flags&SHORTINT ? (long)(short)GETARG(int) : \
414 flags&CHARINT ? (long)(signed char)GETARG(int) : \
417 (flags&LONGINT ? GETARG(u_long) : \
418 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
419 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
420 (u_long)GETARG(u_int))
421 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
423 (flags&INTMAXT ? GETARG(intmax_t) : \
424 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
425 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
426 (intmax_t)GETARG(long long))
428 (flags&INTMAXT ? GETARG(uintmax_t) : \
429 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
430 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \
431 (uintmax_t)GETARG(unsigned long long))
434 * Get * arguments, including the form *nn$. Preserve the nextarg
435 * that the argument can be gotten once the type is determined.
437 #define GETASTER(val) \
440 while (is_digit(*cp)) { \
441 n2 = 10 * n2 + to_digit(*cp); \
445 int hold = nextarg; \
446 if (argtable == NULL) { \
447 argtable = statargtable; \
448 if (__find_arguments (fmt0, orgap, &argtable)) { \
454 val = GETARG (int); \
458 val = GETARG (int); \
461 #if 0 // xprintf pending API review
462 if (__use_xprintf
== 0 && getenv("USE_XPRINTF"))
464 if (__use_xprintf
> 0)
465 return (__xvprintf(fp
, loc
, fmt0
, ap
));
468 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
469 if (prepwrite(fp
) != 0) {
482 #ifndef NO_FLOATING_POINT
484 decimal_point
= localeconv_l(loc
)->decimal_point
;
485 /* The overwhelmingly common case is decpt_len == 1. */
486 decpt_len
= (decimal_point
[1] == '\0' ? 1 : strlen(decimal_point
));
490 * Scan the format for conversions (`%' character).
493 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
495 if ((n
= fmt
- cp
) != 0) {
496 if ((unsigned)ret
+ n
> INT_MAX
) {
508 fmt
++; /* skip over '%' */
518 vsep
= 'X'; /* Illegal value, changed to defaults later. */
522 reswitch
: switch (ch
) {
525 * ``If the space and + flags both appear, the space
526 * flag will be ignored.''
536 case ',': case ';': case ':': case '_':
542 * ``A negative field width argument is taken as a
543 * - flag followed by a positive field width.''
545 * They don't exclude field widths read from args.
562 if ((ch
= *fmt
++) == '*') {
567 while (is_digit(ch
)) {
568 prec
= 10 * prec
+ to_digit(ch
);
574 * ``Note that 0 is taken as a flag, not as the
575 * beginning of a field width.''
580 case '1': case '2': case '3': case '4':
581 case '5': case '6': case '7': case '8': case '9':
584 n
= 10 * n
+ to_digit(ch
);
586 } while (is_digit(ch
));
589 if (argtable
== NULL
) {
590 argtable
= statargtable
;
591 if (__find_arguments (fmt0
, orgap
,
601 #ifndef NO_FLOATING_POINT
607 if (flags
& SHORTINT
) {
617 if (flags
& LONGINT
) {
624 flags
|= LLONGINT
; /* not necessarily */
640 if (flags
& LONGINT
) {
641 static const mbstate_t initial
;
646 mbseqlen
= wcrtomb_l(cp
= buf
,
647 (wchar_t)GETARG(wint_t), &mbs
, loc
);
648 if (mbseqlen
== (size_t)-1) {
649 fp
->_flags
|= __SERR
;
652 size
= (int)mbseqlen
;
654 *(cp
= buf
) = GETARG(int);
668 if (flags
& INTMAX_SIZE
) {
670 if ((intmax_t)ujval
< 0) {
676 if ((long)ulval
< 0) {
683 #ifndef NO_FLOATING_POINT
687 if (flags
& VECTOR
) {
703 if (dtoaresult
!= NULL
)
704 freedtoa(dtoaresult
);
706 fparg
.dbl
= GETARG(double);
708 __hdtoa(fparg
.dbl
, xdigs
, prec
,
709 &expt
, &signflag
, &dtoaend
);
710 #else /* !LDBL_COMPAT */
711 if (flags
& LONGDBL
) {
712 fparg
.ldbl
= GETARG(long double);
714 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
715 &expt
, &signflag
, &dtoaend
);
717 fparg
.dbl
= GETARG(double);
719 __hdtoa(fparg
.dbl
, xdigs
, prec
,
720 &expt
, &signflag
, &dtoaend
);
722 #endif /* LDBL_COMPAT */
731 if (flags
& VECTOR
) {
737 if (prec
< 0) /* account for digit before decpt */
745 if (flags
& VECTOR
) {
755 if (flags
& VECTOR
) {
760 expchar
= ch
- ('g' - 'e');
766 if (dtoaresult
!= NULL
)
767 freedtoa(dtoaresult
);
769 fparg
.dbl
= GETARG(double);
771 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
772 &expt
, &signflag
, &dtoaend
);
775 #else /* !LDBL_COMPAT */
776 if (flags
& LONGDBL
) {
777 fparg
.ldbl
= GETARG(long double);
779 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
780 &expt
, &signflag
, &dtoaend
);
782 fparg
.dbl
= GETARG(double);
784 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
785 &expt
, &signflag
, &dtoaend
);
789 #endif /* LDBL_COMPAT */
793 if (expt
== INT_MAX
) { /* inf or nan */
795 cp
= (ch
>= 'a') ? "nan" : "NAN";
798 cp
= (ch
>= 'a') ? "inf" : "INF";
805 if (ch
== 'g' || ch
== 'G') {
806 if (expt
> -4 && expt
<= prec
) {
807 /* Make %[gG] smell like %[fF] */
817 * Make %[gG] smell like %[eE], but
818 * trim trailing zeroes if no # flag.
825 expsize
= exponent(expstr
, expt
- 1, expchar
);
826 size
= expsize
+ prec
;
827 if (prec
> 1 || flags
& ALT
)
830 /* space for digits before decimal point */
835 /* space for decimal pt and following digits */
836 if (prec
|| flags
& ALT
)
837 size
+= prec
+ decpt_len
;
838 if ((flags
& GROUPING
) && expt
> 0)
839 size
+= grouping_init(&gs
, expt
, loc
);
842 #endif /* !NO_FLOATING_POINT */
846 * Assignment-like behavior is specified if the
847 * value overflows or is otherwise unrepresentable.
848 * C99 says to use `signed char' for %hhn conversions.
850 void *ptr
= GETARG(void *);
853 else if (flags
& LLONGINT
)
854 *(long long *)ptr
= ret
;
855 else if (flags
& SIZET
)
856 *(ssize_t
*)ptr
= (ssize_t
)ret
;
857 else if (flags
& PTRDIFFT
)
858 *(ptrdiff_t *)ptr
= ret
;
859 else if (flags
& INTMAXT
)
860 *(intmax_t *)ptr
= ret
;
861 else if (flags
& LONGINT
)
863 else if (flags
& SHORTINT
)
865 else if (flags
& CHARINT
)
866 *(signed char *)ptr
= ret
;
869 continue; /* no output */
879 if (flags
& INTMAX_SIZE
)
887 * ``The argument shall be a pointer to void. The
888 * value of the pointer is converted to a sequence
889 * of printable characters, in an implementation-
897 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
900 flags
= flags
| INTMAXT
;
907 if (flags
& LONGINT
) {
912 if ((wcp
= GETARG(wchar_t *)) == NULL
)
915 convbuf
= __wcsconv(wcp
, prec
, loc
);
916 if (convbuf
== NULL
) {
917 fp
->_flags
|= __SERR
;
922 } else if ((cp
= GETARG(char *)) == NULL
)
924 size
= (prec
>= 0) ? strnlen(cp
, prec
) : strlen(cp
);
935 if (flags
& INTMAX_SIZE
)
951 if (flags
& INTMAX_SIZE
)
956 /* leading 0x/X only if non-zero */
958 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
962 /* unsigned conversions */
965 * ``... diouXx conversions ... if a precision is
966 * specified, the 0 flag will be ignored.''
968 * except for %#.0o and zero value
970 number
: if ((dprec
= prec
) >= 0)
974 * ``The result of converting a zero value with an
975 * explicit precision of zero is no characters.''
978 * ``The C Standard is clear enough as is. The call
979 * printf("%#.0o", 0) should print 0.''
980 * -- Defect Report #151
983 if (flags
& INTMAX_SIZE
) {
984 if (ujval
!= 0 || prec
!= 0 ||
985 (flags
& ALT
&& base
== 8))
986 cp
= __ujtoa(ujval
, cp
, base
,
989 if (ulval
!= 0 || prec
!= 0 ||
990 (flags
& ALT
&& base
== 8))
991 cp
= __ultoa(ulval
, cp
, base
,
994 size
= buf
+ BUF
- cp
;
995 if (size
> BUF
) /* should never happen */
996 LIBC_ABORT("size (%d) > BUF (%d)", size
, BUF
);
997 if ((flags
& GROUPING
) && size
!= 0)
998 size
+= grouping_init(&gs
, size
, loc
);
1004 #endif /* VECTORS */
1005 default: /* "%?" prints ?, unless ? is NUL */
1008 /* pretend it was %c with argument ch */
1017 if (flags
& VECTOR
) {
1019 * Do the minimum amount of work necessary to construct
1020 * a format specifier that can be used to recursively
1021 * call vfprintf() for each element in the vector.
1023 int i
, j
; /* Counter. */
1024 int vcnt
; /* Number of elements in vector. */
1025 char *vfmt
; /* Pointer to format specifier. */
1027 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1028 int vwidth
= 0; /* Width specified via '*'. */
1029 int vprec
= 0; /* Precision specified via '*'. */
1030 char *vstr
; /* Used for asprintf(). */
1031 int vlen
; /* Length returned by asprintf(). */
1033 V_CHAR
, V_SHORT
, V_INT
,
1034 V_PCHAR
, V_PSHORT
, V_PINT
,
1037 V_LONGLONG
, V_PLONGLONG
,
1039 #endif /* V64TYPE */
1042 vval
.vectorarg
= GETARG(VECTORTYPE
);
1044 * Set vfmt. If vfmt_buf may not be big enough,
1045 * malloc() space, taking care to free it later.
1046 * (EXTRAHH is for possible extra "hh")
1048 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1051 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1053 /* Set the separator character, if not specified. */
1061 /* Create the format specifier. */
1062 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1064 case ',': case ';': case ':': case '_':
1065 case 'v': case 'h': case 'l':
1069 if (pct
[i
- 1] != '.')
1080 * Determine the number of elements in the vector and
1081 * finish up the format specifier.
1083 if (flags
& SHORTINT
) {
1097 } else if (flags
& LONGINT
) {
1099 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1101 } else if (flags
& LLONGINT
) {
1123 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1127 * The default case should never
1134 #endif /* V64TYPE */
1149 * The default case should never
1164 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1170 /* Get a vector element. */
1172 #define VPRINT(type, ind, args...) do { \
1175 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1178 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1181 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1184 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1187 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1190 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1193 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1196 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \
1199 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1202 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1206 PRINT(vstr, vlen); \
1210 #else /* !V64TYPE */
1211 #define VPRINT(type, ind, args...) do { \
1214 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1217 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \
1220 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1223 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \
1226 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1229 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \
1232 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1236 PRINT(vstr, vlen); \
1240 #endif /* V64TYPE */
1242 /* Actually print. */
1245 /* First element. */
1247 for (i
= 1; i
< vcnt
; i
++) {
1256 /* First element. */
1257 VPRINT(vtype
, 0, prec
);
1258 for (i
= 1; i
< vcnt
; i
++) {
1264 VPRINT(vtype
, i
, prec
);
1269 /* First element. */
1270 VPRINT(vtype
, 0, width
);
1271 for (i
= 1; i
< vcnt
; i
++) {
1277 VPRINT(vtype
, i
, width
);
1280 /* First element. */
1281 VPRINT(vtype
, 0, width
, prec
);
1282 for (i
= 1; i
< vcnt
; i
++) {
1288 VPRINT(vtype
, i
, width
, prec
);
1294 if (vfmt
!= vfmt_buf
)
1299 #endif /* VECTORS */
1301 * All reasonable formats wind up here. At this point, `cp'
1302 * points to a string which (if not flags&LADJUST) should be
1303 * padded out to `width' places. If flags&ZEROPAD, it should
1304 * first be prefixed by any sign or other prefix; otherwise,
1305 * it should be blank padded before the prefix is emitted.
1306 * After any left-hand padding and prefixing, emit zeroes
1307 * required by a decimal [diouxX] precision, then print the
1308 * string proper, then emit zeroes required by any leftover
1309 * floating precision; finally, if LADJUST, pad with blanks.
1311 * Compute actual size, so we know how much to pad.
1312 * size excludes decimal prec; realsz includes it.
1314 realsz
= dprec
> size
? dprec
: size
;
1320 prsize
= width
> realsz
? width
: realsz
;
1321 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1326 /* right-adjusting blank padding */
1327 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1328 PAD(width
- realsz
, blanks
);
1334 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1339 /* right-adjusting zero padding */
1340 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1341 PAD(width
- realsz
, zeroes
);
1343 /* the string or number proper */
1344 #ifndef NO_FLOATING_POINT
1345 if ((flags
& FPT
) == 0) {
1347 /* leading zeroes from decimal precision */
1348 PAD(dprec
- size
, zeroes
);
1350 if (grouping_print(&gs
, &io
, cp
, buf
+BUF
, loc
) < 0)
1355 #ifndef NO_FLOATING_POINT
1356 } else { /* glue together f_p fragments */
1357 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1360 if (prec
|| flags
& ALT
)
1361 PRINT(decimal_point
,decpt_len
);
1363 /* already handled initial 0's */
1367 n
= grouping_print(&gs
, &io
,
1373 PRINTANDPAD(cp
, dtoaend
,
1377 if (prec
|| flags
& ALT
)
1378 PRINT(decimal_point
,decpt_len
);
1380 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1381 } else { /* %[eE] or sufficiently long %[gG] */
1382 if (prec
> 1 || flags
& ALT
) {
1384 PRINT(decimal_point
, decpt_len
);
1386 PAD(prec
- ndig
, zeroes
);
1389 PRINT(expstr
, expsize
);
1393 /* left-adjusting padding (always blank) */
1394 if (flags
& LADJUST
)
1395 PAD(width
- realsz
, blanks
);
1397 /* finally, adjust ret */
1400 FLUSH(); /* copy out the I/O vectors */
1406 #ifndef NO_FLOATING_POINT
1407 if (dtoaresult
!= NULL
)
1408 freedtoa(dtoaresult
);
1410 if (convbuf
!= NULL
)
1414 if ((argtable
!= NULL
) && (argtable
!= statargtable
))