]>
git.saurik.com Git - apple/libc.git/blob - stdio/vfwprintf-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
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
40 #endif /* LIBC_SCCS and not lint */
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.23 2004/08/26 06:25:28 des Exp $");
45 #include "xlocale_private.h"
48 * Actual wprintf innards.
50 * Avoid making gratuitous changes to this source file; it should be kept
51 * as close as possible to vfprintf.c for ease of maintenance.
54 #include "namespace.h"
55 #include <sys/types.h>
69 #include "un-namespace.h"
71 #include "libc_private.h"
76 typedef __attribute__ ((vector_size(16))) unsigned char VECTORTYPE
;
87 long long longlongarg
;
88 unsigned long long ulonglongarg
;
95 signed char *pschararg
;
99 long long *plonglongarg
;
100 ptrdiff_t *pptrdiffarg
;
102 intmax_t *pintmaxarg
;
103 #ifndef NO_FLOATING_POINT
105 long double longdoublearg
;
110 VECTORTYPE vectorarg
;
111 unsigned char vuchararg
[16];
112 signed char vchararg
[16];
113 unsigned short vushortarg
[8];
114 signed short vshortarg
[8];
115 unsigned int vuintarg
[4];
116 signed int vintarg
[4];
119 double vdoublearg
[2];
120 unsigned long long vulonglongarg
[2];
121 long long vlonglongarg
[2];
127 * Type ids for argument type table.
130 T_UNUSED
, TP_SHORT
, T_INT
, T_U_INT
, TP_INT
,
131 T_LONG
, T_U_LONG
, TP_LONG
, T_LLONG
, T_U_LLONG
, TP_LLONG
,
132 T_PTRDIFFT
, TP_PTRDIFFT
, T_SIZET
, TP_SIZET
,
133 T_INTMAXT
, T_UINTMAXT
, TP_INTMAXT
, TP_VOID
, TP_CHAR
, TP_SCHAR
,
135 T_DOUBLE
, T_LONG_DOUBLE
, T_WINT
, TP_WCHAR
, T_VECTOR
136 #else /* ! VECTORS */
137 T_DOUBLE
, T_LONG_DOUBLE
, T_WINT
, TP_WCHAR
141 static int __sbprintf(FILE *, locale_t
, const wchar_t *, va_list);
142 static wint_t __xfputwc(wchar_t, FILE *, locale_t
);
143 static wchar_t *__ujtoa(uintmax_t, wchar_t *, int, int, const char *, int,
145 static wchar_t *__ultoa(u_long
, wchar_t *, int, int, const char *, int,
147 static wchar_t *__mbsconv(char *, int, locale_t
);
148 static void __find_arguments(const wchar_t *, va_list, union arg
**);
149 static void __grow_type_table(int, enum typeid **, int *);
152 * Helper function for `fprintf to unbuffered unix file': creates a
153 * temporary buffer. We only work on write-only files; this avoids
154 * worries about ungetc buffers and so forth.
157 __sbprintf(FILE *fp
, locale_t loc
, const wchar_t *fmt
, va_list ap
)
161 unsigned char buf
[BUFSIZ
];
163 /* copy the important variables */
164 fake
._flags
= fp
->_flags
& ~__SNBF
;
165 fake
._file
= fp
->_file
;
166 fake
._cookie
= fp
->_cookie
;
167 fake
._write
= fp
->_write
;
168 fake
._extra
= fp
->_extra
;
170 /* set up the buffer */
171 fake
._bf
._base
= fake
._p
= buf
;
172 fake
._bf
._size
= fake
._w
= sizeof(buf
);
173 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
175 /* do the work, then copy any error status */
176 ret
= __vfwprintf(&fake
, loc
, fmt
, ap
);
177 if (ret
>= 0 && __fflush(&fake
))
179 if (fake
._flags
& __SERR
)
180 fp
->_flags
|= __SERR
;
185 * Like __fputwc, but handles fake string (__SSTR) files properly.
186 * File must already be locked.
189 __xfputwc(wchar_t wc
, FILE *fp
, locale_t loc
)
191 static const mbstate_t initial
;
193 char buf
[MB_LEN_MAX
];
198 if ((fp
->_flags
& __SSTR
) == 0)
199 return (__fputwc(wc
, fp
, loc
));
202 if ((len
= wcrtomb_l(buf
, wc
, &mbs
, loc
)) == (size_t)-1) {
203 fp
->_flags
|= __SERR
;
211 return (__sfvwrite(fp
, &uio
) != EOF
? (wint_t)wc
: WEOF
);
215 * Macros for converting digits to letters and vice versa
217 #define to_digit(c) ((c) - '0')
218 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
219 #define to_char(n) ((n) + '0')
222 * Convert an unsigned long to ASCII for printf purposes, returning
223 * a pointer to the first character of the string representation.
224 * Octal numbers can be forced to have a leading zero; hex numbers
225 * use the given digits.
228 __ultoa(u_long val
, wchar_t *endp
, int base
, int octzero
, const char *xdigs
,
229 int needgrp
, char thousep
, const char *grp
)
236 * Handle the three cases separately, in the hope of getting
237 * better/faster code.
241 if (val
< 10) { /* many numbers are 1 digit */
242 *--cp
= to_char(val
);
247 * On many machines, unsigned arithmetic is harder than
248 * signed arithmetic, so we do at most one unsigned mod and
249 * divide; this is sufficient to reduce the range of
250 * the incoming value to where signed arithmetic works.
252 if (val
> LONG_MAX
) {
253 *--cp
= to_char(val
% 10);
259 *--cp
= to_char(sval
% 10);
262 * If (*grp == CHAR_MAX) then no more grouping
263 * should be performed.
265 if (needgrp
&& ndig
== *grp
&& *grp
!= CHAR_MAX
270 * If (*(grp+1) == '\0') then we have to
271 * use *grp character (last grouping rule)
274 if (*(grp
+1) != '\0')
283 *--cp
= to_char(val
& 7);
286 if (octzero
&& *cp
!= '0')
292 *--cp
= xdigs
[val
& 15];
298 LIBC_ABORT("base = %d", base
);
303 /* Identical to __ultoa, but for intmax_t. */
305 __ujtoa(uintmax_t val
, wchar_t *endp
, int base
, int octzero
,
306 const char *xdigs
, int needgrp
, char thousep
, const char *grp
)
312 /* quick test for small values; __ultoa is typically much faster */
313 /* (perhaps instead we should run until small, then call __ultoa?) */
314 if (val
<= ULONG_MAX
)
315 return (__ultoa((u_long
)val
, endp
, base
, octzero
, xdigs
,
316 needgrp
, thousep
, grp
));
320 *--cp
= to_char(val
% 10);
324 if (val
> INTMAX_MAX
) {
325 *--cp
= to_char(val
% 10);
331 *--cp
= to_char(sval
% 10);
334 * If (*grp == CHAR_MAX) then no more grouping
335 * should be performed.
337 if (needgrp
&& *grp
!= CHAR_MAX
&& ndig
== *grp
342 * If (*(grp+1) == '\0') then we have to
343 * use *grp character (last grouping rule)
346 if (*(grp
+1) != '\0')
355 *--cp
= to_char(val
& 7);
358 if (octzero
&& *cp
!= '0')
364 *--cp
= xdigs
[val
& 15];
370 LIBC_ABORT("base = %d", base
);
376 * Convert a multibyte character string argument for the %s format to a wide
377 * string representation. ``prec'' specifies the maximum number of bytes
378 * to output. If ``prec'' is greater than or equal to zero, we can't assume
379 * that the multibyte char. string ends in a null character.
382 __mbsconv(char *mbsarg
, int prec
, locale_t loc
)
384 static const mbstate_t initial
;
386 wchar_t *convbuf
, *wcp
;
388 size_t insize
, nchars
, nconv
= 0;
389 int mb_cur_max
= MB_CUR_MAX_L(loc
);
395 * Supplied argument is a multibyte string; convert it to wide
400 * String is not guaranteed to be NUL-terminated. Find the
401 * number of characters to print.
406 while (nchars
!= (size_t)prec
) {
407 nconv
= mbrlen_l(p
, mb_cur_max
, &mbs
, loc
);
408 if (nconv
== 0 || nconv
== (size_t)-1 ||
415 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
418 insize
= strlen(mbsarg
);
421 * Allocate buffer for the result and perform the conversion,
422 * converting at most `size' bytes of the input multibyte string to
423 * wide characters for printing.
425 convbuf
= malloc((insize
+ 1) * sizeof(*convbuf
));
431 while (insize
!= 0) {
432 nconv
= mbrtowc_l(wcp
, p
, insize
, &mbs
, loc
);
433 if (nconv
== 0 || nconv
== (size_t)-1 || nconv
== (size_t)-2)
439 if (nconv
== (size_t)-1 || nconv
== (size_t)-2) {
451 __private_extern__
const char *__fix_nogrouping(const char *);
454 vfwprintf(FILE * __restrict fp
, const wchar_t * __restrict fmt0
, va_list ap
)
460 ret
= __vfwprintf(fp
, __current_locale(), fmt0
, ap
);
466 vfwprintf_l(FILE * __restrict fp
, locale_t loc
, const wchar_t * __restrict fmt0
,
472 NORMALIZE_LOCALE(loc
);
474 ret
= __vfwprintf(fp
, loc
, fmt0
, ap
);
479 #ifndef NO_FLOATING_POINT
482 #define freedtoa __freedtoa
491 static int exponent(wchar_t *, int, wchar_t);
493 #endif /* !NO_FLOATING_POINT */
496 * The size of the buffer we use as scratch space for integer
497 * conversions, among other things. Technically, we would need the
498 * most space for base 10 conversions with thousands' grouping
499 * characters between each pair of digits. 100 bytes is a
500 * conservative overestimate even for a 128-bit uintmax_t.
504 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
507 * Flags used during conversion.
509 #define ALT 0x001 /* alternate form */
510 #define LADJUST 0x004 /* left adjustment */
511 #define LONGDBL 0x008 /* long double */
512 #define LONGINT 0x010 /* long integer */
513 #define LLONGINT 0x020 /* long long integer */
514 #define SHORTINT 0x040 /* short integer */
515 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
516 #define FPT 0x100 /* Floating point number */
517 #define GROUPING 0x200 /* use grouping ("'" flag) */
518 /* C99 additional size modifiers: */
519 #define SIZET 0x400 /* size_t */
520 #define PTRDIFFT 0x800 /* ptrdiff_t */
521 #define INTMAXT 0x1000 /* intmax_t */
522 #define CHARINT 0x2000 /* print char using int format */
524 #define VECTOR 0x4000 /* Altivec or SSE vector */
528 * Non-MT-safe version
530 __private_extern__
int
531 __vfwprintf(FILE *fp
, locale_t loc
, const wchar_t *fmt0
, va_list ap
)
533 wchar_t *fmt
; /* format string */
534 wchar_t ch
; /* character from fmt */
535 int n
, n2
, n3
; /* handy integer (short term usage) */
536 wchar_t *cp
; /* handy char pointer (short term usage) */
537 int flags
; /* flags as above */
538 int ret
; /* return value accumulator */
539 int width
; /* width from format (%8d), or 0 */
540 int prec
; /* precision from format; <0 for N/A */
541 wchar_t sign
; /* sign prefix (' ', '+', '-', or \0) */
542 char thousands_sep
; /* locale specific thousands separator */
543 const char *grouping
; /* locale specific numeric grouping rules */
544 #ifndef NO_FLOATING_POINT
546 * We can decompose the printed representation of floating
547 * point numbers into several parts, some of which may be empty:
549 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
552 * A: 'sign' holds this value if present; '\0' otherwise
553 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
554 * C: cp points to the string MMMNNN. Leading and trailing
555 * zeros are not in the string and must be added.
556 * D: expchar holds this character; '\0' if no exponent, e.g. %f
557 * F: at least two digits for decimal, at least one digit for hex
559 wchar_t decimal_point
; /* locale specific decimal point */
560 char *decimal_point_mb
; /* multibyte decimal point */
561 int signflag
; /* true if float is negative */
562 union { /* floating point arguments %[aAeEfFgG] */
566 int expt
; /* integer value of exponent */
567 char expchar
; /* exponent character: [eEpP\0] */
568 char *dtoaend
; /* pointer to end of converted digits */
569 int expsize
; /* character count for expstr */
570 int lead
; /* sig figs before decimal or group sep */
571 int ndig
; /* actual number of digits returned by dtoa */
572 wchar_t expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
573 char *dtoaresult
; /* buffer allocated by dtoa */
574 int nseps
; /* number of group separators with ' */
575 int nrepeats
; /* number of repeats of the last group */
578 union arg vval
; /* Vector argument. */
579 wchar_t *pct
; /* Pointer to '%' at beginning of specifier. */
580 wchar_t vsep
; /* Vector separator character. */
582 u_long ulval
; /* integer arguments %[diouxX] */
583 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
584 int base
; /* base for [diouxX] conversion */
585 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
586 int realsz
; /* field size expanded by dprec, sign, etc */
587 int size
; /* size of converted field or string */
588 int prsize
; /* max size of printed field */
589 const char *xdigs
; /* digits for [xX] conversion */
590 wchar_t buf
[BUF
]; /* buffer with space for digits of uintmax_t */
591 wchar_t ox
[2]; /* space for 0x hex-prefix */
592 union arg
*argtable
; /* args, built due to positional arg */
593 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
594 int nextarg
; /* 1-based argument index */
595 va_list orgap
; /* original argument pointer */
596 wchar_t *convbuf
; /* multibyte to wide conversion result */
599 * Choose PADSIZE to trade efficiency vs. size. If larger printf
600 * fields occur frequently, increase PADSIZE and make the initialisers
603 #define PADSIZE 16 /* pad chunk size */
604 static wchar_t blanks
[PADSIZE
] =
605 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
606 static wchar_t zeroes
[PADSIZE
] =
607 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
609 static const char xdigs_lower
[16] = "0123456789abcdef";
610 static const char xdigs_upper
[16] = "0123456789ABCDEF";
613 * BEWARE, these `goto error' on error, PRINT uses `n2' and
616 #define PRINT(ptr, len) do { \
617 for (n3 = 0; n3 < (len); n3++) \
618 __xfputwc((ptr)[n3], fp, loc); \
620 #define PAD(howmany, with) do { \
621 if ((n = (howmany)) > 0) { \
622 while (n > PADSIZE) { \
623 PRINT(with, PADSIZE); \
629 #define PRINTANDPAD(p, ep, len, with) do { \
635 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
639 * Get the argument indexed by nextarg. If the argument table is
640 * built, use it to get the argument. If its not, get the next
641 * argument (and arguments must be gotten sequentially).
643 #define GETARG(type) \
644 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
645 (nextarg++, va_arg(ap, type)))
648 * To extend shorts properly, we need both signed and unsigned
649 * argument extraction methods.
652 (flags&LONGINT ? GETARG(long) : \
653 flags&SHORTINT ? (long)(short)GETARG(int) : \
654 flags&CHARINT ? (long)(signed char)GETARG(int) : \
657 (flags&LONGINT ? GETARG(u_long) : \
658 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
659 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
660 (u_long)GETARG(u_int))
661 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
663 (flags&INTMAXT ? GETARG(intmax_t) : \
664 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
665 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
666 (intmax_t)GETARG(long long))
668 (flags&INTMAXT ? GETARG(uintmax_t) : \
669 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
670 flags&PTRDIFFT ? (uintmax_t)(unsigned)GETARG(ptrdiff_t) : \
671 (uintmax_t)GETARG(unsigned long long))
674 * Get * arguments, including the form *nn$. Preserve the nextarg
675 * that the argument can be gotten once the type is determined.
677 #define GETASTER(val) \
680 while (is_digit(*cp)) { \
681 n2 = 10 * n2 + to_digit(*cp); \
685 int hold = nextarg; \
686 if (argtable == NULL) { \
687 argtable = statargtable; \
688 __find_arguments (fmt0, orgap, &argtable); \
691 val = GETARG (int); \
695 val = GETARG (int); \
698 thousands_sep
= '\0';
700 #ifndef NO_FLOATING_POINT
701 decimal_point_mb
= localeconv_l(loc
)->decimal_point
;
702 mbtowc_l(&decimal_point
, decimal_point_mb
, strlen(decimal_point_mb
), loc
);
705 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
706 if (prepwrite(fp
) != 0) {
712 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
713 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
715 return (__sbprintf(fp
, loc
, fmt0
, ap
));
717 fmt
= (wchar_t *)fmt0
;
724 * Scan the format for conversions (`%' character).
727 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
729 if ((n
= fmt
- cp
) != 0) {
730 if ((unsigned)ret
+ n
> INT_MAX
) {
742 fmt
++; /* skip over '%' */
751 vsep
= 'X'; /* Illegal value, changed to defaults later. */
755 reswitch
: switch (ch
) {
758 * ``If the space and + flags both appear, the space
759 * flag will be ignored.''
769 case ',': case ';': case ':': case '_':
775 * ``A negative field width argument is taken as a
776 * - flag followed by a positive field width.''
778 * They don't exclude field widths read from args.
793 thousands_sep
= *(localeconv_l(loc
)->thousands_sep
);
794 grouping
= __fix_nogrouping(localeconv_l(loc
)->grouping
);
797 if ((ch
= *fmt
++) == '*') {
802 while (is_digit(ch
)) {
803 prec
= 10 * prec
+ to_digit(ch
);
809 * ``Note that 0 is taken as a flag, not as the
810 * beginning of a field width.''
815 case '1': case '2': case '3': case '4':
816 case '5': case '6': case '7': case '8': case '9':
819 n
= 10 * n
+ to_digit(ch
);
821 } while (is_digit(ch
));
824 if (argtable
== NULL
) {
825 argtable
= statargtable
;
826 __find_arguments (fmt0
, orgap
,
833 #ifndef NO_FLOATING_POINT
839 if (flags
& SHORTINT
) {
849 if (flags
& LONGINT
) {
856 flags
|= LLONGINT
; /* not necessarily */
873 *(cp
= buf
) = (wchar_t)GETARG(wint_t);
875 *(cp
= buf
) = (wchar_t)btowc_l(GETARG(int), loc
);
888 if (flags
& INTMAX_SIZE
) {
890 if ((intmax_t)ujval
< 0) {
896 if ((long)ulval
< 0) {
903 #ifndef NO_FLOATING_POINT
907 if (flags
& VECTOR
) {
924 fparg
.dbl
= GETARG(double);
926 __hdtoa(fparg
.dbl
, xdigs
, prec
,
927 &expt
, &signflag
, &dtoaend
);
928 #else /* !LDBL_COMPAT */
929 if (flags
& LONGDBL
) {
930 fparg
.ldbl
= GETARG(long double);
932 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
933 &expt
, &signflag
, &dtoaend
);
935 fparg
.dbl
= GETARG(double);
937 __hdtoa(fparg
.dbl
, xdigs
, prec
,
938 &expt
, &signflag
, &dtoaend
);
940 #endif /* LDBL_COMPAT */
942 prec
= dtoaend
- dtoaresult
;
947 ndig
= dtoaend
- dtoaresult
;
948 cp
= convbuf
= __mbsconv(dtoaresult
, -1, loc
);
949 freedtoa(dtoaresult
);
954 if (flags
& VECTOR
) {
960 if (prec
< 0) /* account for digit before decpt */
968 if (flags
& VECTOR
) {
978 if (flags
& VECTOR
) {
983 expchar
= ch
- ('g' - 'e');
992 fparg
.dbl
= GETARG(double);
994 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
995 &expt
, &signflag
, &dtoaend
);
998 #else /* !LDBL_COMPAT */
999 if (flags
& LONGDBL
) {
1000 fparg
.ldbl
= GETARG(long double);
1002 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
1003 &expt
, &signflag
, &dtoaend
);
1005 fparg
.dbl
= GETARG(double);
1007 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
1008 &expt
, &signflag
, &dtoaend
);
1012 #endif /* LDBL_COMPAT */
1013 ndig
= dtoaend
- dtoaresult
;
1014 cp
= convbuf
= __mbsconv(dtoaresult
, -1, loc
);
1015 freedtoa(dtoaresult
);
1019 if (expt
== INT_MAX
) { /* inf or nan */
1021 cp
= (ch
>= 'a') ? L
"nan" : L
"NAN";
1024 cp
= (ch
>= 'a') ? L
"inf" : L
"INF";
1029 if (ch
== 'g' || ch
== 'G') {
1030 if (expt
> -4 && expt
<= prec
) {
1031 /* Make %[gG] smell like %[fF] */
1041 * Make %[gG] smell like %[eE], but
1042 * trim trailing zeroes if no # flag.
1049 expsize
= exponent(expstr
, expt
- 1, expchar
);
1050 size
= expsize
+ prec
;
1051 if (prec
> 1 || flags
& ALT
)
1054 /* space for digits before decimal point */
1059 /* space for decimal pt and following digits */
1060 if (prec
|| flags
& ALT
)
1062 if (grouping
&& expt
> 0) {
1063 /* space for thousands' grouping */
1064 nseps
= nrepeats
= 0;
1066 while (*grouping
!= CHAR_MAX
) {
1067 if (lead
<= *grouping
)
1070 if (*(grouping
+1)) {
1076 size
+= nseps
+ nrepeats
;
1081 #endif /* !NO_FLOATING_POINT */
1084 * Assignment-like behavior is specified if the
1085 * value overflows or is otherwise unrepresentable.
1086 * C99 says to use `signed char' for %hhn conversions.
1088 if (flags
& LLONGINT
)
1089 *GETARG(long long *) = ret
;
1090 else if (flags
& SIZET
)
1091 *GETARG(ssize_t
*) = (ssize_t
)ret
;
1092 else if (flags
& PTRDIFFT
)
1093 *GETARG(ptrdiff_t *) = ret
;
1094 else if (flags
& INTMAXT
)
1095 *GETARG(intmax_t *) = ret
;
1096 else if (flags
& LONGINT
)
1097 *GETARG(long *) = ret
;
1098 else if (flags
& SHORTINT
)
1099 *GETARG(short *) = ret
;
1100 else if (flags
& CHARINT
)
1101 *GETARG(signed char *) = ret
;
1103 *GETARG(int *) = ret
;
1104 continue; /* no output */
1112 #endif /* VECTORS */
1113 if (flags
& INTMAX_SIZE
)
1121 * ``The argument shall be a pointer to void. The
1122 * value of the pointer is converted to a sequence
1123 * of printable characters, in an implementation-
1130 #endif /* VECTORS */
1131 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
1133 xdigs
= xdigs_lower
;
1134 flags
= flags
| INTMAXT
;
1141 if (flags
& LONGINT
) {
1142 if ((cp
= GETARG(wchar_t *)) == NULL
)
1147 if (convbuf
!= NULL
)
1149 if ((mbp
= GETARG(char *)) == NULL
)
1152 convbuf
= __mbsconv(mbp
, prec
, loc
);
1153 if (convbuf
== NULL
) {
1154 fp
->_flags
|= __SERR
;
1163 * can't use wcslen; can only look for the
1164 * NUL in the first `prec' characters, and
1165 * wcslen() will go further.
1167 wchar_t *p
= wmemchr(cp
, 0, (size_t)prec
);
1186 #endif /* VECTORS */
1187 if (flags
& INTMAX_SIZE
)
1194 xdigs
= xdigs_upper
;
1197 xdigs
= xdigs_lower
;
1202 #endif /* VECTORS */
1203 if (flags
& INTMAX_SIZE
)
1208 /* leading 0x/X only if non-zero */
1210 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1214 /* unsigned conversions */
1215 nosign
: sign
= '\0';
1217 * ``... diouXx conversions ... if a precision is
1218 * specified, the 0 flag will be ignored.''
1221 number
: if ((dprec
= prec
) >= 0)
1225 * ``The result of converting a zero value with an
1226 * explicit precision of zero is no characters.''
1228 * except for %#.0o and zero value
1231 if (flags
& INTMAX_SIZE
) {
1232 if (ujval
!= 0 || prec
!= 0)
1233 cp
= __ujtoa(ujval
, cp
, base
,
1235 flags
& GROUPING
, thousands_sep
,
1238 if (ulval
!= 0 || prec
!= 0 || (flags
& ALT
))
1239 cp
= __ultoa(ulval
, cp
, base
,
1241 flags
& GROUPING
, thousands_sep
,
1244 size
= buf
+ BUF
- cp
;
1245 if (size
> BUF
) /* should never happen */
1246 LIBC_ABORT("size %d > BUF %d", size
, BUF
);
1252 #endif /* VECTORS */
1253 default: /* "%?" prints ?, unless ? is NUL */
1256 /* pretend it was %c with argument ch */
1265 if (flags
& VECTOR
) {
1267 * Do the minimum amount of work necessary to construct
1268 * a format specifier that can be used to recursively
1269 * call vfprintf() for each element in the vector.
1271 int i
, j
; /* Counter. */
1272 int vcnt
; /* Number of elements in vector. */
1273 char *vfmt
; /* Pointer to format specifier. */
1275 char vfmt_buf
[32 + EXTRAHH
]; /* Static buffer for format spec. */
1276 int vwidth
= 0; /* Width specified via '*'. */
1277 int vprec
= 0; /* Precision specified via '*'. */
1278 char *vstr
; /* Used for asprintf(). */
1279 int vlen
; /* Length returned by asprintf(). */
1281 V_CHAR
, V_SHORT
, V_INT
,
1282 V_PCHAR
, V_PSHORT
, V_PINT
,
1285 V_LONGLONG
, V_PLONGLONG
,
1287 #endif /* V64TYPE */
1290 vval
.vectorarg
= GETARG(VECTORTYPE
);
1292 * Set vfmt. If vfmt_buf may not be big enough,
1293 * malloc() space, taking care to free it later.
1294 * (EXTRAHH is for possible extra "hh")
1296 if (&fmt
[-1] - pct
+ EXTRAHH
< sizeof(vfmt_buf
))
1299 vfmt
= (char *)malloc(&fmt
[-1] - pct
+ EXTRAHH
+ 1);
1301 /* Set the separator character, if not specified. */
1309 /* Create the format specifier. */
1310 for (i
= j
= 0; i
< &fmt
[-1] - pct
; i
++) {
1312 case ',': case ';': case ':': case '_':
1313 case 'v': case 'h': case 'l':
1317 if (pct
[i
- 1] != '.')
1328 * Determine the number of elements in the vector and
1329 * finish up the format specifier.
1331 if (flags
& SHORTINT
) {
1345 } else if (flags
& LONGINT
) {
1347 vtype
= (ch
== 'p') ? V_PINT
: V_INT
;
1349 } else if (flags
& LLONGINT
) {
1371 vtype
= (ch
== 'p') ? V_PLONGLONG
: V_LONGLONG
;
1375 * The default case should never
1382 #endif /* V64TYPE */
1397 * The default case should never
1412 vtype
= (ch
== 'p') ? V_PCHAR
: V_CHAR
;
1418 /* Get a vector element. */
1420 #define VPRINT(type, ind, args...) do { \
1423 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1426 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vuchararg[ind]); \
1429 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1432 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vushortarg[ind]); \
1435 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1438 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vuintarg[ind]); \
1441 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \
1444 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vulonglongarg[ind]); \
1447 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1450 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \
1454 PRINT(vstr, vlen); \
1457 #else /* !V64TYPE */
1458 #define VPRINT(type, ind, args...) do { \
1461 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \
1464 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vuchararg[ind]); \
1467 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \
1470 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vushortarg[ind]); \
1473 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \
1476 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(long)vval.vuintarg[ind]); \
1479 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \
1483 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
, 1);
1603 /* already handled initial 0's */
1606 PRINTANDPAD(cp
, convbuf
+ ndig
, lead
, zeroes
);
1609 while (nseps
>0 || nrepeats
>0) {
1616 PRINT(&thousands_sep
,
1623 if (cp
> convbuf
+ ndig
)
1624 cp
= convbuf
+ ndig
;
1626 if (prec
|| flags
& ALT
) {
1627 PRINT(&decimal_point
, 1);
1630 PRINTANDPAD(cp
, convbuf
+ ndig
, prec
, zeroes
);
1631 } else { /* %[eE] or sufficiently long %[gG] */
1632 if (prec
> 1 || flags
& ALT
) {
1634 buf
[1] = decimal_point
;
1637 PAD(prec
- ndig
, zeroes
);
1640 PRINT(expstr
, expsize
);
1646 /* left-adjusting padding (always blank) */
1647 if (flags
& LADJUST
)
1648 PAD(width
- realsz
, blanks
);
1650 /* finally, adjust ret */
1656 if (convbuf
!= NULL
)
1660 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1667 * Find all arguments when a positional parameter is encountered. Returns a
1668 * table, indexed by argument number, of pointers to each arguments. The
1669 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1670 * It will be replaces with a malloc-ed one if it overflows.
1673 __find_arguments (const wchar_t *fmt0
, va_list ap
, union arg
**argtable
)
1675 wchar_t *fmt
; /* format string */
1676 wchar_t ch
; /* character from fmt */
1677 int n
, n2
; /* handy integer (short term usage) */
1678 wchar_t *cp
; /* handy char pointer (short term usage) */
1679 int flags
; /* flags as above */
1680 int width
; /* width from format (%8d), or 0 */
1681 enum typeid *typetable
; /* table of types */
1682 enum typeid stattypetable
[STATIC_ARG_TBL_SIZE
];
1683 int tablesize
; /* current size of type table */
1684 int tablemax
; /* largest used index in table */
1685 int nextarg
; /* 1-based argument index */
1688 * Add an argument type to the table, expanding if necessary.
1690 #define ADDTYPE(type) \
1691 ((nextarg >= tablesize) ? \
1692 __grow_type_table(nextarg, &typetable, &tablesize) : (void)0, \
1693 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1694 typetable[nextarg++] = type)
1697 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1698 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1699 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1700 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1701 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1704 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1705 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1706 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1707 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1708 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1711 * Add * arguments to the type array.
1713 #define ADDASTER() \
1716 while (is_digit(*cp)) { \
1717 n2 = 10 * n2 + to_digit(*cp); \
1721 int hold = nextarg; \
1729 fmt
= (wchar_t *)fmt0
;
1730 typetable
= stattypetable
;
1731 tablesize
= STATIC_ARG_TBL_SIZE
;
1734 for (n
= 0; n
< STATIC_ARG_TBL_SIZE
; n
++)
1735 typetable
[n
] = T_UNUSED
;
1738 * Scan the format for conversions (`%' character).
1741 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
1745 fmt
++; /* skip over '%' */
1751 reswitch
: switch (ch
) {
1763 if ((ch
= *fmt
++) == '*') {
1767 while (is_digit(ch
)) {
1773 case '1': case '2': case '3': case '4':
1774 case '5': case '6': case '7': case '8': case '9':
1777 n
= 10 * n
+ to_digit(ch
);
1779 } while (is_digit(ch
));
1786 #ifndef NO_FLOATING_POINT
1792 if (flags
& SHORTINT
) {
1802 if (flags
& LONGINT
) {
1809 flags
|= LLONGINT
; /* not necessarily */
1821 if (flags
& LONGINT
)
1828 #endif /* VECTORS */
1840 #endif /* VECTORS */
1843 #ifndef NO_FLOATING_POINT
1856 #endif /* VECTORS */
1857 if (flags
& LONGDBL
)
1858 ADDTYPE(T_LONG_DOUBLE
);
1862 #endif /* !NO_FLOATING_POINT */
1864 if (flags
& INTMAXT
)
1865 ADDTYPE(TP_INTMAXT
);
1866 else if (flags
& PTRDIFFT
)
1867 ADDTYPE(TP_PTRDIFFT
);
1868 else if (flags
& SIZET
)
1870 else if (flags
& LLONGINT
)
1872 else if (flags
& LONGINT
)
1874 else if (flags
& SHORTINT
)
1876 else if (flags
& CHARINT
)
1880 continue; /* no output */
1889 #endif /* VECTORS */
1897 #endif /* VECTORS */
1904 if (flags
& LONGINT
)
1919 #endif /* VECTORS */
1922 default: /* "%?" prints ?, unless ? is NUL */
1930 * Build the argument table.
1932 if (tablemax
>= STATIC_ARG_TBL_SIZE
) {
1933 *argtable
= (union arg
*)
1934 malloc (sizeof (union arg
) * (tablemax
+ 1));
1937 (*argtable
) [0].intarg
= 0;
1938 for (n
= 1; n
<= tablemax
; n
++) {
1939 switch (typetable
[n
]) {
1940 case T_UNUSED
: /* whoops! */
1941 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1944 (*argtable
) [n
].pschararg
= va_arg (ap
, signed char *);
1947 (*argtable
) [n
].pshortarg
= va_arg (ap
, short *);
1950 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1953 (*argtable
) [n
].uintarg
= va_arg (ap
, unsigned int);
1956 (*argtable
) [n
].pintarg
= va_arg (ap
, int *);
1959 (*argtable
) [n
].longarg
= va_arg (ap
, long);
1962 (*argtable
) [n
].ulongarg
= va_arg (ap
, unsigned long);
1965 (*argtable
) [n
].plongarg
= va_arg (ap
, long *);
1968 (*argtable
) [n
].longlongarg
= va_arg (ap
, long long);
1971 (*argtable
) [n
].ulonglongarg
= va_arg (ap
, unsigned long long);
1974 (*argtable
) [n
].plonglongarg
= va_arg (ap
, long long *);
1977 (*argtable
) [n
].ptrdiffarg
= va_arg (ap
, ptrdiff_t);
1980 (*argtable
) [n
].pptrdiffarg
= va_arg (ap
, ptrdiff_t *);
1983 (*argtable
) [n
].sizearg
= va_arg (ap
, size_t);
1986 (*argtable
) [n
].psizearg
= va_arg (ap
, size_t *);
1989 (*argtable
) [n
].intmaxarg
= va_arg (ap
, intmax_t);
1992 (*argtable
) [n
].uintmaxarg
= va_arg (ap
, uintmax_t);
1995 (*argtable
) [n
].pintmaxarg
= va_arg (ap
, intmax_t *);
1997 #ifndef NO_FLOATING_POINT
1999 (*argtable
) [n
].doublearg
= va_arg (ap
, double);
2002 (*argtable
) [n
].longdoublearg
= va_arg (ap
, long double);
2007 (*argtable
) [n
].vectorarg
= va_arg (ap
, VECTORTYPE
);
2009 #endif /* VECTORS */
2011 (*argtable
) [n
].pchararg
= va_arg (ap
, char *);
2014 (*argtable
) [n
].pvoidarg
= va_arg (ap
, void *);
2017 (*argtable
) [n
].wintarg
= va_arg (ap
, wint_t);
2020 (*argtable
) [n
].pwchararg
= va_arg (ap
, wchar_t *);
2025 if ((typetable
!= NULL
) && (typetable
!= stattypetable
))
2030 * Increase the size of the type table.
2033 __grow_type_table (int nextarg
, enum typeid **typetable
, int *tablesize
)
2035 enum typeid *const oldtable
= *typetable
;
2036 const int oldsize
= *tablesize
;
2037 enum typeid *newtable
;
2038 int n
, newsize
= oldsize
* 2;
2040 if (newsize
< nextarg
+ 1)
2041 newsize
= nextarg
+ 1;
2042 if (oldsize
== STATIC_ARG_TBL_SIZE
) {
2043 if ((newtable
= malloc(newsize
* sizeof(enum typeid))) == NULL
)
2044 LIBC_ABORT("malloc: %s", strerror(errno
)); /* XXX handle better */
2045 bcopy(oldtable
, newtable
, oldsize
* sizeof(enum typeid));
2047 newtable
= reallocf(oldtable
, newsize
* sizeof(enum typeid));
2048 if (newtable
== NULL
)
2049 LIBC_ABORT("reallocf: %s", strerror(errno
)); /* XXX handle better */
2051 for (n
= oldsize
; n
< newsize
; n
++)
2052 newtable
[n
] = T_UNUSED
;
2054 *typetable
= newtable
;
2055 *tablesize
= newsize
;
2059 #ifndef NO_FLOATING_POINT
2062 exponent(wchar_t *p0
, int exp
, wchar_t fmtch
)
2065 wchar_t expbuf
[MAXEXPDIG
];
2075 t
= expbuf
+ MAXEXPDIG
;
2078 *--t
= to_char(exp
% 10);
2079 } while ((exp
/= 10) > 9);
2080 *--t
= to_char(exp
);
2081 for (; t
< expbuf
+ MAXEXPDIG
; *p
++ = *t
++);
2085 * Exponents for decimal floating point conversions
2086 * (%[eEgG]) must be at least two characters long,
2087 * whereas exponents for hexadecimal conversions can
2088 * be only one character long.
2090 if (fmtch
== 'e' || fmtch
== 'E')
2092 *p
++ = to_char(exp
);
2096 #endif /* !NO_FLOATING_POINT */