]>
git.saurik.com Git - apple/libc.git/blob - stdio/vfprintf-fbsd.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 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.68 2004/08/26 06:25:28 des Exp $");
43 #include "xlocale_private.h"
46 * Actual printf innards.
48 * This code is large and complicated...
51 #include "namespace.h"
52 #include <sys/types.h>
66 #include "un-namespace.h"
68 #include "libc_private.h"
73 typedef __attribute__ ((vector_size(16))) unsigned char VECTORTYPE
;
84 long long longlongarg
;
85 unsigned long long ulonglongarg
;
92 signed char *pschararg
;
96 long long *plonglongarg
;
97 ptrdiff_t *pptrdiffarg
;
100 #ifndef NO_FLOATING_POINT
102 long double longdoublearg
;
107 VECTORTYPE vectorarg
;
108 unsigned char vuchararg
[16];
109 signed char vchararg
[16];
110 unsigned short vushortarg
[8];
111 signed short vshortarg
[8];
112 unsigned int vuintarg
[4];
113 signed int vintarg
[4];
116 double vdoublearg
[2];
117 unsigned long long vulonglongarg
[2];
118 long long vlonglongarg
[2];
124 * Type ids for argument type table.
127 T_UNUSED
, TP_SHORT
, T_INT
, T_U_INT
, TP_INT
,
128 T_LONG
, T_U_LONG
, TP_LONG
, T_LLONG
, T_U_LLONG
, TP_LLONG
,
129 T_PTRDIFFT
, TP_PTRDIFFT
, T_SIZET
, TP_SIZET
,
130 T_INTMAXT
, T_UINTMAXT
, TP_INTMAXT
, TP_VOID
, TP_CHAR
, TP_SCHAR
,
132 T_DOUBLE
, T_LONG_DOUBLE
, T_WINT
, TP_WCHAR
, T_VECTOR
133 #else /* ! VECTORS */
134 T_DOUBLE
, T_LONG_DOUBLE
, T_WINT
, TP_WCHAR
138 static int __sprint(FILE *, struct __suio
*);
139 static int __sbprintf(FILE *, locale_t
, const char *, va_list) __printflike(3, 0);
140 static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char,
142 static char *__ultoa(u_long
, char *, int, int, const char *, int, char,
144 static char *__wcsconv(wchar_t *, int, locale_t
);
145 static void __find_arguments(const char *, va_list, union arg
**);
146 static void __grow_type_table(int, enum typeid **, int *);
149 * Flush out all the vectors defined by the given uio,
150 * then reset it so that it can be reused.
153 __sprint(FILE *fp
, struct __suio
*uio
)
157 if (uio
->uio_resid
== 0) {
161 err
= __sfvwrite(fp
, uio
);
168 * Helper function for `fprintf to unbuffered unix file': creates a
169 * temporary buffer. We only work on write-only files; this avoids
170 * worries about ungetc buffers and so forth.
173 __sbprintf(FILE *fp
, locale_t loc
, const char *fmt
, va_list ap
)
177 unsigned char buf
[BUFSIZ
];
179 /* copy the important variables */
180 fake
._flags
= fp
->_flags
& ~__SNBF
;
181 fake
._file
= fp
->_file
;
182 fake
._cookie
= fp
->_cookie
;
183 fake
._write
= fp
->_write
;
184 fake
._extra
= fp
->_extra
;
186 /* set up the buffer */
187 fake
._bf
._base
= fake
._p
= buf
;
188 fake
._bf
._size
= fake
._w
= sizeof(buf
);
189 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
191 /* do the work, then copy any error status */
192 ret
= __vfprintf(&fake
, loc
, fmt
, ap
);
193 if (ret
>= 0 && __fflush(&fake
))
195 if (fake
._flags
& __SERR
)
196 fp
->_flags
|= __SERR
;
201 * Macros for converting digits to letters and vice versa
203 #define to_digit(c) ((c) - '0')
204 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
205 #define to_char(n) ((n) + '0')
208 * Convert an unsigned long to ASCII for printf purposes, returning
209 * a pointer to the first character of the string representation.
210 * Octal numbers can be forced to have a leading zero; hex numbers
211 * use the given digits.
214 __ultoa(u_long val
, char *endp
, int base
, int octzero
, const char *xdigs
,
215 int needgrp
, char thousep
, const char *grp
)
222 * Handle the three cases separately, in the hope of getting
223 * better/faster code.
227 if (val
< 10) { /* many numbers are 1 digit */
228 *--cp
= to_char(val
);
233 * On many machines, unsigned arithmetic is harder than
234 * signed arithmetic, so we do at most one unsigned mod and
235 * divide; this is sufficient to reduce the range of
236 * the incoming value to where signed arithmetic works.
238 if (val
> LONG_MAX
) {
239 *--cp
= to_char(val
% 10);
245 *--cp
= to_char(sval
% 10);
248 * If (*grp == CHAR_MAX) then no more grouping
249 * should be performed.
251 if (needgrp
&& ndig
== *grp
&& *grp
!= CHAR_MAX
256 * If (*(grp+1) == '\0') then we have to
257 * use *grp character (last grouping rule)
260 if (*(grp
+1) != '\0')
269 *--cp
= to_char(val
& 7);
272 if (octzero
&& *cp
!= '0')
278 *--cp
= xdigs
[val
& 15];
289 /* Identical to __ultoa, but for intmax_t. */
291 __ujtoa(uintmax_t val
, char *endp
, int base
, int octzero
, const char *xdigs
,
292 int needgrp
, char thousep
, const char *grp
)
298 /* quick test for small values; __ultoa is typically much faster */
299 /* (perhaps instead we should run until small, then call __ultoa?) */
300 if (val
<= ULONG_MAX
)
301 return (__ultoa((u_long
)val
, endp
, base
, octzero
, xdigs
,
302 needgrp
, thousep
, grp
));
306 *--cp
= to_char(val
% 10);
310 if (val
> INTMAX_MAX
) {
311 *--cp
= to_char(val
% 10);
317 *--cp
= to_char(sval
% 10);
320 * If (*grp == CHAR_MAX) then no more grouping
321 * should be performed.
323 if (needgrp
&& *grp
!= CHAR_MAX
&& ndig
== *grp
328 * If (*(grp+1) == '\0') then we have to
329 * use *grp character (last grouping rule)
332 if (*(grp
+1) != '\0')
341 *--cp
= to_char(val
& 7);
344 if (octzero
&& *cp
!= '0')
350 *--cp
= xdigs
[val
& 15];
362 * Convert a wide character string argument for the %ls format to a multibyte
363 * string representation. ``prec'' specifies the maximum number of bytes
364 * to output. If ``prec'' is greater than or equal to zero, we can't assume
365 * that the wide char. string ends in a null character.
368 __wcsconv(wchar_t *wcsarg
, int prec
, locale_t loc
)
370 static const mbstate_t initial
;
372 char buf
[MB_LEN_MAX
];
375 size_t clen
= 0, nbytes
;
378 * Determine the number of bytes to output and allocate space for
386 clen
= wcrtomb_l(buf
, *p
++, &mbs
, loc
);
387 if (clen
== 0 || clen
== (size_t)-1 ||
388 nbytes
+ clen
> prec
)
395 nbytes
= wcsrtombs_l(NULL
, (const wchar_t **)&p
, 0, &mbs
, loc
);
396 if (nbytes
== (size_t)-1)
399 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
403 * Fill the output buffer with the multibyte representations of as
404 * many wide characters as will fit.
409 while (mbp
- convbuf
< nbytes
) {
410 clen
= wcrtomb_l(mbp
, *p
++, &mbs
, loc
);
411 if (clen
== 0 || clen
== (size_t)-1)
415 if (clen
== (size_t)-1) {
427 __private_extern__
const char *__fix_nogrouping(const char *);
430 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
436 ret
= __vfprintf(fp
, __current_locale(), fmt0
, ap
);
442 vfprintf_l(FILE * __restrict fp
, locale_t loc
, const char * __restrict fmt0
,
448 NORMALIZE_LOCALE(loc
);
450 ret
= __vfprintf(fp
, loc
, fmt0
, ap
);
455 #ifndef NO_FLOATING_POINT
458 #define freedtoa __freedtoa
467 static int exponent(char *, int, int);
469 #endif /* !NO_FLOATING_POINT */
472 * The size of the buffer we use as scratch space for integer
473 * conversions, among other things. Technically, we would need the
474 * most space for base 10 conversions with thousands' grouping
475 * characters between each pair of digits. 100 bytes is a
476 * conservative overestimate even for a 128-bit uintmax_t.
480 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
483 * Flags used during conversion.
485 #define ALT 0x001 /* alternate form */
486 #define LADJUST 0x004 /* left adjustment */
487 #define LONGDBL 0x008 /* long double */
488 #define LONGINT 0x010 /* long integer */
489 #define LLONGINT 0x020 /* long long integer */
490 #define SHORTINT 0x040 /* short integer */
491 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
492 #define FPT 0x100 /* Floating point number */
493 #define GROUPING 0x200 /* use grouping ("'" flag) */
494 /* C99 additional size modifiers: */
495 #define SIZET 0x400 /* size_t */
496 #define PTRDIFFT 0x800 /* ptrdiff_t */
497 #define INTMAXT 0x1000 /* intmax_t */
498 #define CHARINT 0x2000 /* print char using int format */
500 #define VECTOR 0x4000 /* Altivec or SSE vector */
504 * Non-MT-safe version
506 __private_extern__
int
507 __vfprintf(FILE *fp
, locale_t loc
, const char *fmt0
, va_list ap
)
509 char *fmt
; /* format string */
510 int ch
; /* character from fmt */
511 int n
, n2
; /* handy integer (short term usage) */
512 char *cp
; /* handy char pointer (short term usage) */
513 struct __siov
*iovp
; /* for PRINT macro */
514 int flags
; /* flags as above */
515 int ret
; /* return value accumulator */
516 int width
; /* width from format (%8d), or 0 */
517 int prec
; /* precision from format; <0 for N/A */
518 char sign
; /* sign prefix (' ', '+', '-', or \0) */
519 char thousands_sep
; /* locale specific thousands separator */
520 const char *grouping
; /* locale specific numeric grouping rules */
521 #ifndef NO_FLOATING_POINT
523 * We can decompose the printed representation of floating
524 * point numbers into several parts, some of which may be empty:
526 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
529 * A: 'sign' holds this value if present; '\0' otherwise
530 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
531 * C: cp points to the string MMMNNN. Leading and trailing
532 * zeros are not in the string and must be added.
533 * D: expchar holds this character; '\0' if no exponent, e.g. %f
534 * F: at least two digits for decimal, at least one digit for hex
536 char *decimal_point
; /* locale specific decimal point */
537 int signflag
; /* true if float is negative */
538 union { /* floating point arguments %[aAeEfFgG] */
542 int expt
; /* integer value of exponent */
543 char expchar
; /* exponent character: [eEpP\0] */
544 char *dtoaend
; /* pointer to end of converted digits */
545 int expsize
; /* character count for expstr */
546 int lead
; /* sig figs before decimal or group sep */
547 int ndig
; /* actual number of digits returned by dtoa */
548 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
549 char *dtoaresult
; /* buffer allocated by dtoa */
550 int nseps
; /* number of group separators with ' */
551 int nrepeats
; /* number of repeats of the last group */
554 union arg vval
; /* Vector argument. */
555 char *pct
; /* Pointer to '%' at beginning of specifier. */
556 char vsep
; /* Vector separator character. */
558 u_long ulval
; /* integer arguments %[diouxX] */
559 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
560 int base
; /* base for [diouxX] conversion */
561 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
562 int realsz
; /* field size expanded by dprec, sign, etc */
563 int size
; /* size of converted field or string */
564 int prsize
; /* max size of printed field */
565 const char *xdigs
; /* digits for %[xX] conversion */
567 struct __suio uio
; /* output information: summary */
568 struct __siov iov
[NIOV
];/* ... and individual io vectors */
569 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
570 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
571 union arg
*argtable
; /* args, built due to positional arg */
572 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
573 int nextarg
; /* 1-based argument index */
574 va_list orgap
; /* original argument pointer */
575 char *convbuf
; /* wide to multibyte conversion result */
578 * Choose PADSIZE to trade efficiency vs. size. If larger printf
579 * fields occur frequently, increase PADSIZE and make the initialisers
582 #define PADSIZE 16 /* pad chunk size */
583 static char blanks
[PADSIZE
] =
584 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
585 static char zeroes
[PADSIZE
] =
586 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
588 static const char xdigs_lower
[16] = "0123456789abcdef";
589 static const char xdigs_upper
[16] = "0123456789ABCDEF";
592 * BEWARE, these `goto error' on error, and PAD uses `n'.
594 #define PRINT(ptr, len) { \
595 iovp->iov_base = (ptr); \
596 iovp->iov_len = (len); \
597 uio.uio_resid += (len); \
599 if (++uio.uio_iovcnt >= NIOV) { \
600 if (__sprint(fp, &uio)) \
605 #define PAD(howmany, with) { \
606 if ((n = (howmany)) > 0) { \
607 while (n > PADSIZE) { \
608 PRINT(with, PADSIZE); \
614 #define PRINTANDPAD(p, ep, len, with) do { \
620 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
623 if (uio.uio_resid && __sprint(fp, &uio)) \
625 uio.uio_iovcnt = 0; \
630 * Get the argument indexed by nextarg. If the argument table is
631 * built, use it to get the argument. If its not, get the next
632 * argument (and arguments must be gotten sequentially).
634 #define GETARG(type) \
635 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
636 (nextarg++, va_arg(ap, type)))
639 * To extend shorts properly, we need both signed and unsigned
640 * argument extraction methods.
643 (flags&LONGINT ? GETARG(long) : \
644 flags&SHORTINT ? (long)(short)GETARG(int) : \
645 flags&CHARINT ? (long)(signed char)GETARG(int) : \
648 (flags&LONGINT ? GETARG(u_long) : \
649 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
650 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
651 (u_long)GETARG(u_int))
652 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
654 (flags&INTMAXT ? GETARG(intmax_t) : \
655 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
656 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
657 (intmax_t)GETARG(long long))
659 (flags&INTMAXT ? GETARG(uintmax_t) : \
660 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
661 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
662 (uintmax_t)GETARG(unsigned long long))
665 * Get * arguments, including the form *nn$. Preserve the nextarg
666 * that the argument can be gotten once the type is determined.
668 #define GETASTER(val) \
671 while (is_digit(*cp)) { \
672 n2 = 10 * n2 + to_digit(*cp); \
676 int hold = nextarg; \
677 if (argtable == NULL) { \
678 argtable = statargtable; \
679 __find_arguments (fmt0, orgap, &argtable); \
682 val = GETARG (int); \
686 val = GETARG (int); \
689 thousands_sep
= '\0';
692 #ifndef NO_FLOATING_POINT
694 decimal_point
= localeconv_l(loc
)->decimal_point
;
696 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
697 if (prepwrite(fp
) != 0) {
703 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
704 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
706 return (__sbprintf(fp
, loc
, fmt0
, ap
));
712 uio
.uio_iov
= iovp
= iov
;
718 * Scan the format for conversions (`%' character).
721 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
723 if ((n
= fmt
- cp
) != 0) {
724 if ((unsigned)ret
+ n
> INT_MAX
) {
736 fmt
++; /* skip over '%' */
745 vsep
= 'X'; /* Illegal value, changed to defaults later. */
749 reswitch
: switch (ch
) {
752 * ``If the space and + flags both appear, the space
753 * flag will be ignored.''
763 case ',': case ';': case ':': case '_':
769 * ``A negative field width argument is taken as a
770 * - flag followed by a positive field width.''
772 * They don't exclude field widths read from args.
787 thousands_sep
= *(localeconv_l(loc
)->thousands_sep
);
788 grouping
= __fix_nogrouping(localeconv_l(loc
)->grouping
);
791 if ((ch
= *fmt
++) == '*') {
796 while (is_digit(ch
)) {
797 prec
= 10 * prec
+ to_digit(ch
);
803 * ``Note that 0 is taken as a flag, not as the
804 * beginning of a field width.''
809 case '1': case '2': case '3': case '4':
810 case '5': case '6': case '7': case '8': case '9':
813 n
= 10 * n
+ to_digit(ch
);
815 } while (is_digit(ch
));
818 if (argtable
== NULL
) {
819 argtable
= statargtable
;
820 __find_arguments (fmt0
, orgap
,
827 #ifndef NO_FLOATING_POINT
833 if (flags
& SHORTINT
) {
843 if (flags
& LONGINT
) {
850 flags
|= LLONGINT
; /* not necessarily */
866 if (flags
& LONGINT
) {
867 static const mbstate_t initial
;
872 mbseqlen
= wcrtomb_l(cp
= buf
,
873 (wchar_t)GETARG(wint_t), &mbs
, loc
);
874 if (mbseqlen
== (size_t)-1) {
875 fp
->_flags
|= __SERR
;
878 size
= (int)mbseqlen
;
880 *(cp
= buf
) = GETARG(int);
894 if (flags
& INTMAX_SIZE
) {
896 if ((intmax_t)ujval
< 0) {
902 if ((long)ulval
< 0) {
909 #ifndef NO_FLOATING_POINT
913 if (flags
& VECTOR
) {
929 if (dtoaresult
!= NULL
)
930 freedtoa(dtoaresult
);
932 fparg
.dbl
= GETARG(double);
934 __hdtoa(fparg
.dbl
, xdigs
, prec
,
935 &expt
, &signflag
, &dtoaend
);
936 #else /* !LDBL_COMPAT */
937 if (flags
& LONGDBL
) {
938 fparg
.ldbl
= GETARG(long double);
940 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
941 &expt
, &signflag
, &dtoaend
);
943 fparg
.dbl
= GETARG(double);
945 __hdtoa(fparg
.dbl
, xdigs
, prec
,
946 &expt
, &signflag
, &dtoaend
);
948 #endif /* LDBL_COMPAT */
957 if (flags
& VECTOR
) {
963 if (prec
< 0) /* account for digit before decpt */
971 if (flags
& VECTOR
) {
981 if (flags
& VECTOR
) {
986 expchar
= ch
- ('g' - 'e');
992 if (dtoaresult
!= NULL
)
993 freedtoa(dtoaresult
);
995 fparg
.dbl
= GETARG(double);
997 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
998 &expt
, &signflag
, &dtoaend
);
1001 #else /* !LDBL_COMPAT */
1002 if (flags
& LONGDBL
) {
1003 fparg
.ldbl
= GETARG(long double);
1005 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
1006 &expt
, &signflag
, &dtoaend
);
1008 fparg
.dbl
= GETARG(double);
1010 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
1011 &expt
, &signflag
, &dtoaend
);
1015 #endif /* LDBL_COMPAT */
1019 if (expt
== INT_MAX
) { /* inf or nan */
1021 cp
= (ch
>= 'a') ? "nan" : "NAN";
1024 cp
= (ch
>= 'a') ? "inf" : "INF";
1029 ndig
= dtoaend
- cp
;
1030 if (ch
== 'g' || ch
== 'G') {
1031 if (expt
> -4 && expt
<= prec
) {
1032 /* Make %[gG] smell like %[fF] */
1042 * Make %[gG] smell like %[eE], but
1043 * trim trailing zeroes if no # flag.
1050 expsize
= exponent(expstr
, expt
- 1, expchar
);
1051 size
= expsize
+ prec
;
1052 if (prec
> 1 || flags
& ALT
)
1055 /* space for digits before decimal point */
1060 /* space for decimal pt and following digits */
1061 if (prec
|| flags
& ALT
)
1063 if (grouping
&& expt
> 0) {
1064 /* space for thousands' grouping */
1065 nseps
= nrepeats
= 0;
1067 while (*grouping
!= CHAR_MAX
) {
1068 if (lead
<= *grouping
)
1071 if (*(grouping
+1)) {
1077 size
+= nseps
+ nrepeats
;
1082 #endif /* !NO_FLOATING_POINT */
1085 * Assignment-like behavior is specified if the
1086 * value overflows or is otherwise unrepresentable.
1087 * C99 says to use `signed char' for %hhn conversions.
1089 if (flags
& LLONGINT
)
1090 *GETARG(long long *) = ret
;
1091 else if (flags
& SIZET
)
1092 *GETARG(ssize_t
*) = (ssize_t
)ret
;
1093 else if (flags
& PTRDIFFT
)
1094 *GETARG(ptrdiff_t *) = ret
;
1095 else if (flags
& INTMAXT
)
1096 *GETARG(intmax_t *) = ret
;
1097 else if (flags
& LONGINT
)
1098 *GETARG(long *) = ret
;
1099 else if (flags
& SHORTINT
)
1100 *GETARG(short *) = ret
;
1101 else if (flags
& CHARINT
)
1102 *GETARG(signed char *) = ret
;
1104 *GETARG(int *) = ret
;
1105 continue; /* no output */
1113 #endif /* VECTORS */
1114 if (flags
& INTMAX_SIZE
)
1122 * ``The argument shall be a pointer to void. The
1123 * value of the pointer is converted to a sequence
1124 * of printable characters, in an implementation-
1131 #endif /* VECTORS */
1132 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
1134 xdigs
= xdigs_lower
;
1135 flags
= flags
| INTMAXT
;
1142 if (flags
& LONGINT
) {
1145 if (convbuf
!= NULL
)
1147 if ((wcp
= GETARG(wchar_t *)) == NULL
)
1150 convbuf
= __wcsconv(wcp
, prec
, loc
);
1151 if (convbuf
== NULL
) {
1152 fp
->_flags
|= __SERR
;
1157 } else if ((cp
= GETARG(char *)) == NULL
)
1161 * can't use strlen; can only look for the
1162 * NUL in the first `prec' characters, and
1163 * strlen() will go further.
1165 char *p
= memchr(cp
, 0, (size_t)prec
);
1184 #endif /* VECTORS */
1185 if (flags
& INTMAX_SIZE
)
1192 xdigs
= xdigs_upper
;
1195 xdigs
= xdigs_lower
;
1200 #endif /* VECTORS */
1201 if (flags
& INTMAX_SIZE
)
1206 /* leading 0x/X only if non-zero */
1208 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1212 /* unsigned conversions */
1213 nosign
: sign
= '\0';
1215 * ``... diouXx conversions ... if a precision is
1216 * specified, the 0 flag will be ignored.''
1219 number
: if ((dprec
= prec
) >= 0)
1223 * ``The result of converting a zero value with an
1224 * explicit precision of zero is no characters.''
1226 * except for %#.0o and zero value
1229 if (flags
& INTMAX_SIZE
) {
1230 if (ujval
!= 0 || prec
!= 0)
1231 cp
= __ujtoa(ujval
, cp
, base
,
1233 flags
& GROUPING
, thousands_sep
,
1236 if (ulval
!= 0 || prec
!= 0 || (flags
& ALT
))
1237 cp
= __ultoa(ulval
, cp
, base
,
1239 flags
& GROUPING
, thousands_sep
,
1242 size
= buf
+ BUF
- cp
;
1243 if (size
> BUF
) /* should never happen */
1250 #endif /* VECTORS */
1251 default: /* "%?" prints ?, unless ? is NUL */
1254 /* pretend it was %c with argument ch */
1263 if (flags
& VECTOR
) {
1265 * Do the minimum amount of work necessary to construct
1266 * a format specifier that can be used to recursively
1267 * call vfprintf() for each element in the vector.
1269 int i
, j
; /* Counter. */
1270 int vcnt
; /* Number of elements in vector. */
1271 char *vfmt
; /* Pointer to format specifier. */
1273 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1274 int vwidth
= 0; /* Width specified via '*'. */
1275 int vprec
= 0; /* Precision specified via '*'. */
1276 char *vstr
; /* Used for asprintf(). */
1277 int vlen
; /* Length returned by asprintf(). */
1279 V_CHAR
, V_SHORT
, V_INT
,
1280 V_PCHAR
, V_PSHORT
, V_PINT
,
1283 V_LONGLONG
, V_PLONGLONG
,
1285 #endif /* V64TYPE */
1288 vval
.vectorarg
= GETARG(VECTORTYPE
);
1290 * Set vfmt. If vfmt_buf may not be big enough,
1291 * malloc() space, taking care to free it later.
1292 * (EXTRAHH is for possible extra "hh")
1294 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1297 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1299 /* Set the separator character, if not specified. */
1307 /* Create the format specifier. */
1308 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1310 case ',': case ';': case ':': case '_':
1311 case 'v': case 'h': case 'l':
1315 if (pct
[i
- 1] != '.')
1326 * Determine the number of elements in the vector and
1327 * finish up the format specifier.
1329 if (flags
& SHORTINT
) {
1343 } else if (flags
& LONGINT
) {
1345 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1347 } else if (flags
& LLONGINT
) {
1369 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1373 * The default case should never
1380 #endif /* V64TYPE */
1395 * The default case should never
1410 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1416 /* Get a vector element. */
1418 #define VPRINT(type, ind, args...) do { \
1421 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1424 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vuchararg[ind]); \
1427 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1430 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vushortarg[ind]); \
1433 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1436 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vuintarg[ind]); \
1439 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1442 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vulonglongarg[ind]); \
1445 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1448 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1452 PRINT(vstr, vlen); \
1456 #else /* !V64TYPE */
1457 #define VPRINT(type, ind, args...) do { \
1460 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1463 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vuchararg[ind]); \
1466 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1469 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vushortarg[ind]); \
1472 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1475 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vuintarg[ind]); \
1478 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1482 PRINT(vstr, vlen); \
1486 #endif /* V64TYPE */
1488 /* Actually print. */
1491 /* First element. */
1493 for (i
= 1; i
< vcnt
; i
++) {
1502 /* First element. */
1503 VPRINT(vtype
, 0, prec
);
1504 for (i
= 1; i
< vcnt
; i
++) {
1510 VPRINT(vtype
, i
, prec
);
1515 /* First element. */
1516 VPRINT(vtype
, 0, width
);
1517 for (i
= 1; i
< vcnt
; i
++) {
1523 VPRINT(vtype
, i
, width
);
1526 /* First element. */
1527 VPRINT(vtype
, 0, width
, prec
);
1528 for (i
= 1; i
< vcnt
; i
++) {
1534 VPRINT(vtype
, i
, width
, prec
);
1540 if (vfmt
!= vfmt_buf
)
1545 #endif /* VECTORS */
1547 * All reasonable formats wind up here. At this point, `cp'
1548 * points to a string which (if not flags&LADJUST) should be
1549 * padded out to `width' places. If flags&ZEROPAD, it should
1550 * first be prefixed by any sign or other prefix; otherwise,
1551 * it should be blank padded before the prefix is emitted.
1552 * After any left-hand padding and prefixing, emit zeroes
1553 * required by a decimal [diouxX] precision, then print the
1554 * string proper, then emit zeroes required by any leftover
1555 * floating precision; finally, if LADJUST, pad with blanks.
1557 * Compute actual size, so we know how much to pad.
1558 * size excludes decimal prec; realsz includes it.
1560 realsz
= dprec
> size
? dprec
: size
;
1566 prsize
= width
> realsz
? width
: realsz
;
1567 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1572 /* right-adjusting blank padding */
1573 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1574 PAD(width
- realsz
, blanks
);
1580 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1585 /* right-adjusting zero padding */
1586 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1587 PAD(width
- realsz
, zeroes
);
1589 /* leading zeroes from decimal precision */
1590 PAD(dprec
- size
, zeroes
);
1592 /* the string or number proper */
1593 #ifndef NO_FLOATING_POINT
1594 if ((flags
& FPT
) == 0) {
1596 } else { /* glue together f_p fragments */
1597 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1600 if (prec
|| flags
& ALT
)
1601 PRINT(decimal_point
, strlen(decimal_point
));
1603 /* already handled initial 0's */
1606 PRINTANDPAD(cp
, dtoaend
, lead
, zeroes
);
1609 while (nseps
>0 || nrepeats
>0) {
1616 PRINT(&thousands_sep
,
1618 PRINTANDPAD(cp
,dtoaend
,
1625 if (prec
|| flags
& ALT
)
1626 PRINT(decimal_point
, strlen(decimal_point
));
1628 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1629 } else { /* %[eE] or sufficiently long %[gG] */
1630 if (prec
> 1 || flags
& ALT
) {
1633 PRINT(decimal_point
, strlen(decimal_point
));
1635 PAD(prec
- ndig
, zeroes
);
1638 PRINT(expstr
, expsize
);
1644 /* left-adjusting padding (always blank) */
1645 if (flags
& LADJUST
)
1646 PAD(width
- realsz
, blanks
);
1648 /* finally, adjust ret */
1651 FLUSH(); /* copy out the I/O vectors */
1657 #ifndef NO_FLOATING_POINT
1658 if (dtoaresult
!= NULL
)
1659 freedtoa(dtoaresult
);
1661 if (convbuf
!= NULL
)
1665 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1672 * Find all arguments when a positional parameter is encountered. Returns a
1673 * table, indexed by argument number, of pointers to each arguments. The
1674 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1675 * It will be replaces with a malloc-ed one if it overflows.
1678 __find_arguments (const char *fmt0
, va_list ap
, union arg
**argtable
)
1680 char *fmt
; /* format string */
1681 int ch
; /* character from fmt */
1682 int n
, n2
; /* handy integer (short term usage) */
1683 char *cp
; /* handy char pointer (short term usage) */
1684 int flags
; /* flags as above */
1685 int width
; /* width from format (%8d), or 0 */
1686 enum typeid *typetable
; /* table of types */
1687 enum typeid stattypetable
[STATIC_ARG_TBL_SIZE
];
1688 int tablesize
; /* current size of type table */
1689 int tablemax
; /* largest used index in table */
1690 int nextarg
; /* 1-based argument index */
1693 * Add an argument type to the table, expanding if necessary.
1695 #define ADDTYPE(type) \
1696 ((nextarg >= tablesize) ? \
1697 __grow_type_table(nextarg, &typetable, &tablesize) : (void)0, \
1698 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1699 typetable[nextarg++] = type)
1702 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1703 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1704 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1705 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1706 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1709 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1710 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1711 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1712 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1713 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1716 * Add * arguments to the type array.
1718 #define ADDASTER() \
1721 while (is_digit(*cp)) { \
1722 n2 = 10 * n2 + to_digit(*cp); \
1726 int hold = nextarg; \
1735 typetable
= stattypetable
;
1736 tablesize
= STATIC_ARG_TBL_SIZE
;
1739 for (n
= 0; n
< STATIC_ARG_TBL_SIZE
; n
++)
1740 typetable
[n
] = T_UNUSED
;
1743 * Scan the format for conversions (`%' character).
1746 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
1750 fmt
++; /* skip over '%' */
1756 reswitch
: switch (ch
) {
1768 if ((ch
= *fmt
++) == '*') {
1772 while (is_digit(ch
)) {
1778 case '1': case '2': case '3': case '4':
1779 case '5': case '6': case '7': case '8': case '9':
1782 n
= 10 * n
+ to_digit(ch
);
1784 } while (is_digit(ch
));
1791 #ifndef NO_FLOATING_POINT
1797 if (flags
& SHORTINT
) {
1807 if (flags
& LONGINT
) {
1814 flags
|= LLONGINT
; /* not necessarily */
1826 if (flags
& LONGINT
)
1833 #endif /* VECTORS */
1848 #ifndef NO_FLOATING_POINT
1861 #endif /* VECTORS */
1862 if (flags
& LONGDBL
)
1863 ADDTYPE(T_LONG_DOUBLE
);
1867 #endif /* !NO_FLOATING_POINT */
1869 if (flags
& INTMAXT
)
1870 ADDTYPE(TP_INTMAXT
);
1871 else if (flags
& PTRDIFFT
)
1872 ADDTYPE(TP_PTRDIFFT
);
1873 else if (flags
& SIZET
)
1875 else if (flags
& LLONGINT
)
1877 else if (flags
& LONGINT
)
1879 else if (flags
& SHORTINT
)
1881 else if (flags
& CHARINT
)
1885 continue; /* no output */
1894 #endif /* VECTORS */
1902 #endif /* VECTORS */
1909 if (flags
& LONGINT
)
1924 #endif /* VECTORS */
1927 default: /* "%?" prints ?, unless ? is NUL */
1935 * Build the argument table.
1937 if (tablemax
>= STATIC_ARG_TBL_SIZE
) {
1938 *argtable
= (union arg
*)
1939 malloc (sizeof (union arg
) * (tablemax
+ 1));
1942 (*argtable
) [0].intarg
= 0;
1943 for (n
= 1; n
<= tablemax
; n
++) {
1944 switch (typetable
[n
]) {
1945 case T_UNUSED
: /* whoops! */
1946 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1949 (*argtable
) [n
].pschararg
= va_arg (ap
, signed char *);
1952 (*argtable
) [n
].pshortarg
= va_arg (ap
, short *);
1955 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1958 (*argtable
) [n
].uintarg
= va_arg (ap
, unsigned int);
1961 (*argtable
) [n
].pintarg
= va_arg (ap
, int *);
1964 (*argtable
) [n
].longarg
= va_arg (ap
, long);
1967 (*argtable
) [n
].ulongarg
= va_arg (ap
, unsigned long);
1970 (*argtable
) [n
].plongarg
= va_arg (ap
, long *);
1973 (*argtable
) [n
].longlongarg
= va_arg (ap
, long long);
1976 (*argtable
) [n
].ulonglongarg
= va_arg (ap
, unsigned long long);
1979 (*argtable
) [n
].plonglongarg
= va_arg (ap
, long long *);
1982 (*argtable
) [n
].ptrdiffarg
= va_arg (ap
, ptrdiff_t);
1985 (*argtable
) [n
].pptrdiffarg
= va_arg (ap
, ptrdiff_t *);
1988 (*argtable
) [n
].sizearg
= va_arg (ap
, size_t);
1991 (*argtable
) [n
].psizearg
= va_arg (ap
, size_t *);
1994 (*argtable
) [n
].intmaxarg
= va_arg (ap
, intmax_t);
1997 (*argtable
) [n
].uintmaxarg
= va_arg (ap
, uintmax_t);
2000 (*argtable
) [n
].pintmaxarg
= va_arg (ap
, intmax_t *);
2002 #ifndef NO_FLOATING_POINT
2004 (*argtable
) [n
].doublearg
= va_arg (ap
, double);
2007 (*argtable
) [n
].longdoublearg
= va_arg (ap
, long double);
2012 (*argtable
) [n
].vectorarg
= va_arg (ap
, VECTORTYPE
);
2014 #endif /* VECTORS */
2016 (*argtable
) [n
].pchararg
= va_arg (ap
, char *);
2019 (*argtable
) [n
].pvoidarg
= va_arg (ap
, void *);
2022 (*argtable
) [n
].wintarg
= va_arg (ap
, wint_t);
2025 (*argtable
) [n
].pwchararg
= va_arg (ap
, wchar_t *);
2030 if ((typetable
!= NULL
) && (typetable
!= stattypetable
))
2035 * Increase the size of the type table.
2038 __grow_type_table (int nextarg
, enum typeid **typetable
, int *tablesize
)
2040 enum typeid *const oldtable
= *typetable
;
2041 const int oldsize
= *tablesize
;
2042 enum typeid *newtable
;
2043 int n
, newsize
= oldsize
* 2;
2045 if (newsize
< nextarg
+ 1)
2046 newsize
= nextarg
+ 1;
2047 if (oldsize
== STATIC_ARG_TBL_SIZE
) {
2048 if ((newtable
= malloc(newsize
* sizeof(enum typeid))) == NULL
)
2049 abort(); /* XXX handle better */
2050 bcopy(oldtable
, newtable
, oldsize
* sizeof(enum typeid));
2052 newtable
= reallocf(oldtable
, newsize
* sizeof(enum typeid));
2053 if (newtable
== NULL
)
2054 abort(); /* XXX handle better */
2056 for (n
= oldsize
; n
< newsize
; n
++)
2057 newtable
[n
] = T_UNUSED
;
2059 *typetable
= newtable
;
2060 *tablesize
= newsize
;
2064 #ifndef NO_FLOATING_POINT
2067 exponent(char *p0
, int exp
, int fmtch
)
2070 char expbuf
[MAXEXPDIG
];
2080 t
= expbuf
+ MAXEXPDIG
;
2083 *--t
= to_char(exp
% 10);
2084 } while ((exp
/= 10) > 9);
2085 *--t
= to_char(exp
);
2086 for (; t
< expbuf
+ MAXEXPDIG
; *p
++ = *t
++);
2090 * Exponents for decimal floating point conversions
2091 * (%[eEgG]) must be at least two characters long,
2092 * whereas exponents for hexadecimal conversions can
2093 * be only one character long.
2095 if (fmtch
== 'e' || fmtch
== 'E')
2097 *p
++ = to_char(exp
);
2101 #endif /* !NO_FLOATING_POINT */