]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfwprintf.c
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 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.20 2004/05/02 20:09:14 obrien Exp $");
46 * Actual wprintf innards.
48 * Avoid making gratuitous changes to this source file; it should be kept
49 * as close as possible to vfprintf.c for ease of maintenance.
52 #include "namespace.h"
53 #include <sys/types.h>
66 #include "un-namespace.h"
68 #include "libc_private.h"
77 long long longlongarg
;
78 unsigned long long ulonglongarg
;
85 signed char *pschararg
;
89 long long *plonglongarg
;
90 ptrdiff_t *pptrdiffarg
;
93 #ifndef NO_FLOATING_POINT
95 long double longdoublearg
;
102 * Type ids for argument type table.
105 T_UNUSED
, TP_SHORT
, T_INT
, T_U_INT
, TP_INT
,
106 T_LONG
, T_U_LONG
, TP_LONG
, T_LLONG
, T_U_LLONG
, TP_LLONG
,
107 T_PTRDIFFT
, TP_PTRDIFFT
, T_SIZET
, TP_SIZET
,
108 T_INTMAXT
, T_UINTMAXT
, TP_INTMAXT
, TP_VOID
, TP_CHAR
, TP_SCHAR
,
109 T_DOUBLE
, T_LONG_DOUBLE
, T_WINT
, TP_WCHAR
112 static int __sbprintf(FILE *, const wchar_t *, va_list);
113 static wint_t __xfputwc(wchar_t, FILE *);
114 static wchar_t *__ujtoa(uintmax_t, wchar_t *, int, int, const char *, int,
116 static wchar_t *__ultoa(u_long
, wchar_t *, int, int, const char *, int,
118 static wchar_t *__mbsconv(char *, int);
119 static void __find_arguments(const wchar_t *, va_list, union arg
**);
120 static void __grow_type_table(int, enum typeid **, int *);
123 * Helper function for `fprintf to unbuffered unix file': creates a
124 * temporary buffer. We only work on write-only files; this avoids
125 * worries about ungetc buffers and so forth.
128 __sbprintf(FILE *fp
, const wchar_t *fmt
, va_list ap
)
132 unsigned char buf
[BUFSIZ
];
134 /* copy the important variables */
135 fake
._flags
= fp
->_flags
& ~__SNBF
;
136 fake
._file
= fp
->_file
;
137 fake
._cookie
= fp
->_cookie
;
138 fake
._write
= fp
->_write
;
139 fake
._extra
= fp
->_extra
;
141 /* set up the buffer */
142 fake
._bf
._base
= fake
._p
= buf
;
143 fake
._bf
._size
= fake
._w
= sizeof(buf
);
144 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
146 /* do the work, then copy any error status */
147 ret
= __vfwprintf(&fake
, fmt
, ap
);
148 if (ret
>= 0 && __fflush(&fake
))
150 if (fake
._flags
& __SERR
)
151 fp
->_flags
|= __SERR
;
156 * Like __fputwc, but handles fake string (__SSTR) files properly.
157 * File must already be locked.
160 __xfputwc(wchar_t wc
, FILE *fp
)
162 static const mbstate_t initial
;
164 char buf
[MB_LEN_MAX
];
169 if ((fp
->_flags
& __SSTR
) == 0)
170 return (__fputwc(wc
, fp
));
173 if ((len
= wcrtomb(buf
, wc
, &mbs
)) == (size_t)-1) {
174 fp
->_flags
|= __SERR
;
182 return (__sfvwrite(fp
, &uio
) != EOF
? (wint_t)wc
: WEOF
);
186 * Macros for converting digits to letters and vice versa
188 #define to_digit(c) ((c) - '0')
189 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
190 #define to_char(n) ((n) + '0')
193 * Convert an unsigned long to ASCII for printf purposes, returning
194 * a pointer to the first character of the string representation.
195 * Octal numbers can be forced to have a leading zero; hex numbers
196 * use the given digits.
199 __ultoa(u_long val
, wchar_t *endp
, int base
, int octzero
, const char *xdigs
,
200 int needgrp
, char thousep
, const char *grp
)
207 * Handle the three cases separately, in the hope of getting
208 * better/faster code.
212 if (val
< 10) { /* many numbers are 1 digit */
213 *--cp
= to_char(val
);
218 * On many machines, unsigned arithmetic is harder than
219 * signed arithmetic, so we do at most one unsigned mod and
220 * divide; this is sufficient to reduce the range of
221 * the incoming value to where signed arithmetic works.
223 if (val
> LONG_MAX
) {
224 *--cp
= to_char(val
% 10);
230 *--cp
= to_char(sval
% 10);
233 * If (*grp == CHAR_MAX) then no more grouping
234 * should be performed.
236 if (needgrp
&& ndig
== *grp
&& *grp
!= CHAR_MAX
241 * If (*(grp+1) == '\0') then we have to
242 * use *grp character (last grouping rule)
245 if (*(grp
+1) != '\0')
254 *--cp
= to_char(val
& 7);
257 if (octzero
&& *cp
!= '0')
263 *--cp
= xdigs
[val
& 15];
274 /* Identical to __ultoa, but for intmax_t. */
276 __ujtoa(uintmax_t val
, wchar_t *endp
, int base
, int octzero
,
277 const char *xdigs
, int needgrp
, char thousep
, const char *grp
)
283 /* quick test for small values; __ultoa is typically much faster */
284 /* (perhaps instead we should run until small, then call __ultoa?) */
285 if (val
<= ULONG_MAX
)
286 return (__ultoa((u_long
)val
, endp
, base
, octzero
, xdigs
,
287 needgrp
, thousep
, grp
));
291 *--cp
= to_char(val
% 10);
295 if (val
> INTMAX_MAX
) {
296 *--cp
= to_char(val
% 10);
302 *--cp
= to_char(sval
% 10);
305 * If (*grp == CHAR_MAX) then no more grouping
306 * should be performed.
308 if (needgrp
&& *grp
!= CHAR_MAX
&& ndig
== *grp
313 * If (*(grp+1) == '\0') then we have to
314 * use *grp character (last grouping rule)
317 if (*(grp
+1) != '\0')
326 *--cp
= to_char(val
& 7);
329 if (octzero
&& *cp
!= '0')
335 *--cp
= xdigs
[val
& 15];
347 * Convert a multibyte character string argument for the %s format to a wide
348 * string representation. ``prec'' specifies the maximum number of bytes
349 * to output. If ``prec'' is greater than or equal to zero, we can't assume
350 * that the multibyte char. string ends in a null character.
353 __mbsconv(char *mbsarg
, int prec
)
355 static const mbstate_t initial
;
357 wchar_t *convbuf
, *wcp
;
359 size_t insize
, nchars
, nconv
;
365 * Supplied argument is a multibyte string; convert it to wide
370 * String is not guaranteed to be NUL-terminated. Find the
371 * number of characters to print.
376 while (nchars
!= (size_t)prec
) {
377 nconv
= mbrlen(p
, MB_CUR_MAX
, &mbs
);
378 if (nconv
== 0 || nconv
== (size_t)-1 ||
385 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
388 insize
= strlen(mbsarg
);
391 * Allocate buffer for the result and perform the conversion,
392 * converting at most `size' bytes of the input multibyte string to
393 * wide characters for printing.
395 convbuf
= malloc((insize
+ 1) * sizeof(*convbuf
));
401 while (insize
!= 0) {
402 nconv
= mbrtowc(wcp
, p
, insize
, &mbs
);
403 if (nconv
== 0 || nconv
== (size_t)-1 || nconv
== (size_t)-2)
409 if (nconv
== (size_t)-1 || nconv
== (size_t)-2) {
422 vfwprintf(FILE * __restrict fp
, const wchar_t * __restrict fmt0
, va_list ap
)
428 ret
= __vfwprintf(fp
, fmt0
, ap
);
433 #ifndef NO_FLOATING_POINT
436 #define freedtoa __freedtoa
445 static int exponent(wchar_t *, int, wchar_t);
447 #endif /* !NO_FLOATING_POINT */
450 * The size of the buffer we use as scratch space for integer
451 * conversions, among other things. Technically, we would need the
452 * most space for base 10 conversions with thousands' grouping
453 * characters between each pair of digits. 100 bytes is a
454 * conservative overestimate even for a 128-bit uintmax_t.
458 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
461 * Flags used during conversion.
463 #define ALT 0x001 /* alternate form */
464 #define LADJUST 0x004 /* left adjustment */
465 #define LONGDBL 0x008 /* long double */
466 #define LONGINT 0x010 /* long integer */
467 #define LLONGINT 0x020 /* long long integer */
468 #define SHORTINT 0x040 /* short integer */
469 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
470 #define FPT 0x100 /* Floating point number */
471 #define GROUPING 0x200 /* use grouping ("'" flag) */
472 /* C99 additional size modifiers: */
473 #define SIZET 0x400 /* size_t */
474 #define PTRDIFFT 0x800 /* ptrdiff_t */
475 #define INTMAXT 0x1000 /* intmax_t */
476 #define CHARINT 0x2000 /* print char using int format */
479 * Non-MT-safe version
482 __vfwprintf(FILE *fp
, const wchar_t *fmt0
, va_list ap
)
484 wchar_t *fmt
; /* format string */
485 wchar_t ch
; /* character from fmt */
486 int n
, n2
, n3
; /* handy integer (short term usage) */
487 wchar_t *cp
; /* handy char pointer (short term usage) */
488 int flags
; /* flags as above */
489 int ret
; /* return value accumulator */
490 int width
; /* width from format (%8d), or 0 */
491 int prec
; /* precision from format; <0 for N/A */
492 wchar_t sign
; /* sign prefix (' ', '+', '-', or \0) */
493 char thousands_sep
; /* locale specific thousands separator */
494 const char *grouping
; /* locale specific numeric grouping rules */
495 #ifndef NO_FLOATING_POINT
497 * We can decompose the printed representation of floating
498 * point numbers into several parts, some of which may be empty:
500 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
503 * A: 'sign' holds this value if present; '\0' otherwise
504 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
505 * C: cp points to the string MMMNNN. Leading and trailing
506 * zeros are not in the string and must be added.
507 * D: expchar holds this character; '\0' if no exponent, e.g. %f
508 * F: at least two digits for decimal, at least one digit for hex
510 char *decimal_point
; /* locale specific decimal point */
511 int signflag
; /* true if float is negative */
512 union { /* floating point arguments %[aAeEfFgG] */
516 int expt
; /* integer value of exponent */
517 char expchar
; /* exponent character: [eEpP\0] */
518 char *dtoaend
; /* pointer to end of converted digits */
519 int expsize
; /* character count for expstr */
520 int lead
; /* sig figs before decimal or group sep */
521 int ndig
; /* actual number of digits returned by dtoa */
522 wchar_t expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
523 char *dtoaresult
; /* buffer allocated by dtoa */
524 int nseps
; /* number of group separators with ' */
525 int nrepeats
; /* number of repeats of the last group */
527 u_long ulval
; /* integer arguments %[diouxX] */
528 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
529 int base
; /* base for [diouxX] conversion */
530 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
531 int realsz
; /* field size expanded by dprec, sign, etc */
532 int size
; /* size of converted field or string */
533 int prsize
; /* max size of printed field */
534 const char *xdigs
; /* digits for [xX] conversion */
535 wchar_t buf
[BUF
]; /* buffer with space for digits of uintmax_t */
536 wchar_t ox
[2]; /* space for 0x hex-prefix */
537 union arg
*argtable
; /* args, built due to positional arg */
538 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
539 int nextarg
; /* 1-based argument index */
540 va_list orgap
; /* original argument pointer */
541 wchar_t *convbuf
; /* multibyte to wide conversion result */
544 * Choose PADSIZE to trade efficiency vs. size. If larger printf
545 * fields occur frequently, increase PADSIZE and make the initialisers
548 #define PADSIZE 16 /* pad chunk size */
549 static wchar_t blanks
[PADSIZE
] =
550 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
551 static wchar_t zeroes
[PADSIZE
] =
552 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
554 static const char xdigs_lower
[16] = "0123456789abcdef";
555 static const char xdigs_upper
[16] = "0123456789ABCDEF";
558 * BEWARE, these `goto error' on error, PRINT uses `n2' and
561 #define PRINT(ptr, len) do { \
562 for (n3 = 0; n3 < (len); n3++) \
563 __xfputwc((ptr)[n3], fp); \
565 #define PAD(howmany, with) do { \
566 if ((n = (howmany)) > 0) { \
567 while (n > PADSIZE) { \
568 PRINT(with, PADSIZE); \
574 #define PRINTANDPAD(p, ep, len, with) do { \
580 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
584 * Get the argument indexed by nextarg. If the argument table is
585 * built, use it to get the argument. If its not, get the next
586 * argument (and arguments must be gotten sequentially).
588 #define GETARG(type) \
589 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
590 (nextarg++, va_arg(ap, type)))
593 * To extend shorts properly, we need both signed and unsigned
594 * argument extraction methods.
597 (flags&LONGINT ? GETARG(long) : \
598 flags&SHORTINT ? (long)(short)GETARG(int) : \
599 flags&CHARINT ? (long)(signed char)GETARG(int) : \
602 (flags&LONGINT ? GETARG(u_long) : \
603 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
604 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
605 (u_long)GETARG(u_int))
606 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
608 (flags&INTMAXT ? GETARG(intmax_t) : \
609 flags&SIZET ? (intmax_t)GETARG(size_t) : \
610 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
611 (intmax_t)GETARG(long long))
613 (flags&INTMAXT ? GETARG(uintmax_t) : \
614 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
615 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
616 (uintmax_t)GETARG(unsigned long long))
619 * Get * arguments, including the form *nn$. Preserve the nextarg
620 * that the argument can be gotten once the type is determined.
622 #define GETASTER(val) \
625 while (is_digit(*cp)) { \
626 n2 = 10 * n2 + to_digit(*cp); \
630 int hold = nextarg; \
631 if (argtable == NULL) { \
632 argtable = statargtable; \
633 __find_arguments (fmt0, orgap, &argtable); \
636 val = GETARG (int); \
640 val = GETARG (int); \
644 thousands_sep
= '\0';
646 #ifndef NO_FLOATING_POINT
647 decimal_point
= localeconv()->decimal_point
;
650 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
654 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
655 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
657 return (__sbprintf(fp
, fmt0
, ap
));
659 fmt
= (wchar_t *)fmt0
;
666 * Scan the format for conversions (`%' character).
669 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
671 if ((n
= fmt
- cp
) != 0) {
672 if ((unsigned)ret
+ n
> INT_MAX
) {
681 fmt
++; /* skip over '%' */
691 reswitch
: switch (ch
) {
694 * ``If the space and + flags both appear, the space
695 * flag will be ignored.''
706 * ``A negative field width argument is taken as a
707 * - flag followed by a positive field width.''
709 * They don't exclude field widths read from args.
724 thousands_sep
= *(localeconv()->thousands_sep
);
725 grouping
= localeconv()->grouping
;
728 if ((ch
= *fmt
++) == '*') {
733 while (is_digit(ch
)) {
734 prec
= 10 * prec
+ to_digit(ch
);
740 * ``Note that 0 is taken as a flag, not as the
741 * beginning of a field width.''
746 case '1': case '2': case '3': case '4':
747 case '5': case '6': case '7': case '8': case '9':
750 n
= 10 * n
+ to_digit(ch
);
752 } while (is_digit(ch
));
755 if (argtable
== NULL
) {
756 argtable
= statargtable
;
757 __find_arguments (fmt0
, orgap
,
764 #ifndef NO_FLOATING_POINT
770 if (flags
& SHORTINT
) {
780 if (flags
& LONGINT
) {
787 flags
|= LLONGINT
; /* not necessarily */
800 *(cp
= buf
) = (wchar_t)GETARG(wint_t);
802 *(cp
= buf
) = (wchar_t)btowc(GETARG(int));
811 if (flags
& INTMAX_SIZE
) {
813 if ((intmax_t)ujval
< 0) {
819 if ((long)ulval
< 0) {
826 #ifndef NO_FLOATING_POINT
840 if (flags
& LONGDBL
) {
841 fparg
.ldbl
= GETARG(long double);
843 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
844 &expt
, &signflag
, &dtoaend
);
846 fparg
.dbl
= GETARG(double);
848 __hdtoa(fparg
.dbl
, xdigs
, prec
,
849 &expt
, &signflag
, &dtoaend
);
852 prec
= dtoaend
- dtoaresult
;
857 ndig
= dtoaend
- dtoaresult
;
858 cp
= convbuf
= __mbsconv(dtoaresult
, -1);
859 freedtoa(dtoaresult
);
864 if (prec
< 0) /* account for digit before decpt */
875 expchar
= ch
- ('g' - 'e');
883 if (flags
& LONGDBL
) {
884 fparg
.ldbl
= GETARG(long double);
886 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
887 &expt
, &signflag
, &dtoaend
);
889 fparg
.dbl
= GETARG(double);
891 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
892 &expt
, &signflag
, &dtoaend
);
896 ndig
= dtoaend
- dtoaresult
;
897 cp
= convbuf
= __mbsconv(dtoaresult
, -1);
898 freedtoa(dtoaresult
);
902 if (expt
== INT_MAX
) { /* inf or nan */
904 cp
= (ch
>= 'a') ? L
"nan" : L
"NAN";
907 cp
= (ch
>= 'a') ? L
"inf" : L
"INF";
912 if (ch
== 'g' || ch
== 'G') {
913 if (expt
> -4 && expt
<= prec
) {
914 /* Make %[gG] smell like %[fF] */
924 * Make %[gG] smell like %[eE], but
925 * trim trailing zeroes if no # flag.
932 expsize
= exponent(expstr
, expt
- 1, expchar
);
933 size
= expsize
+ prec
;
934 if (prec
> 1 || flags
& ALT
)
937 /* space for digits before decimal point */
942 /* space for decimal pt and following digits */
943 if (prec
|| flags
& ALT
)
945 if (grouping
&& expt
> 0) {
946 /* space for thousands' grouping */
947 nseps
= nrepeats
= 0;
949 while (*grouping
!= CHAR_MAX
) {
950 if (lead
<= *grouping
)
959 size
+= nseps
+ nrepeats
;
964 #endif /* !NO_FLOATING_POINT */
967 * Assignment-like behavior is specified if the
968 * value overflows or is otherwise unrepresentable.
969 * C99 says to use `signed char' for %hhn conversions.
971 if (flags
& LLONGINT
)
972 *GETARG(long long *) = ret
;
973 else if (flags
& SIZET
)
974 *GETARG(ssize_t
*) = (ssize_t
)ret
;
975 else if (flags
& PTRDIFFT
)
976 *GETARG(ptrdiff_t *) = ret
;
977 else if (flags
& INTMAXT
)
978 *GETARG(intmax_t *) = ret
;
979 else if (flags
& LONGINT
)
980 *GETARG(long *) = ret
;
981 else if (flags
& SHORTINT
)
982 *GETARG(short *) = ret
;
983 else if (flags
& CHARINT
)
984 *GETARG(signed char *) = ret
;
986 *GETARG(int *) = ret
;
987 continue; /* no output */
992 if (flags
& INTMAX_SIZE
)
1000 * ``The argument shall be a pointer to void. The
1001 * value of the pointer is converted to a sequence
1002 * of printable characters, in an implementation-
1006 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
1008 xdigs
= xdigs_lower
;
1009 flags
= flags
| INTMAXT
;
1016 if (flags
& LONGINT
) {
1017 if ((cp
= GETARG(wchar_t *)) == NULL
)
1022 if (convbuf
!= NULL
)
1024 if ((mbp
= GETARG(char *)) == NULL
)
1027 convbuf
= __mbsconv(mbp
, prec
);
1028 if (convbuf
== NULL
) {
1029 fp
->_flags
|= __SERR
;
1038 * can't use wcslen; can only look for the
1039 * NUL in the first `prec' characters, and
1040 * wcslen() will go further.
1042 wchar_t *p
= wmemchr(cp
, 0, (size_t)prec
);
1058 if (flags
& INTMAX_SIZE
)
1065 xdigs
= xdigs_upper
;
1068 xdigs
= xdigs_lower
;
1070 if (flags
& INTMAX_SIZE
)
1075 /* leading 0x/X only if non-zero */
1077 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1081 /* unsigned conversions */
1082 nosign
: sign
= '\0';
1084 * ``... diouXx conversions ... if a precision is
1085 * specified, the 0 flag will be ignored.''
1088 number
: if ((dprec
= prec
) >= 0)
1092 * ``The result of converting a zero value with an
1093 * explicit precision of zero is no characters.''
1097 if (flags
& INTMAX_SIZE
) {
1098 if (ujval
!= 0 || prec
!= 0)
1099 cp
= __ujtoa(ujval
, cp
, base
,
1101 flags
& GROUPING
, thousands_sep
,
1104 if (ulval
!= 0 || prec
!= 0)
1105 cp
= __ultoa(ulval
, cp
, base
,
1107 flags
& GROUPING
, thousands_sep
,
1110 size
= buf
+ BUF
- cp
;
1111 if (size
> BUF
) /* should never happen */
1114 default: /* "%?" prints ?, unless ? is NUL */
1117 /* pretend it was %c with argument ch */
1126 * All reasonable formats wind up here. At this point, `cp'
1127 * points to a string which (if not flags&LADJUST) should be
1128 * padded out to `width' places. If flags&ZEROPAD, it should
1129 * first be prefixed by any sign or other prefix; otherwise,
1130 * it should be blank padded before the prefix is emitted.
1131 * After any left-hand padding and prefixing, emit zeroes
1132 * required by a decimal [diouxX] precision, then print the
1133 * string proper, then emit zeroes required by any leftover
1134 * floating precision; finally, if LADJUST, pad with blanks.
1136 * Compute actual size, so we know how much to pad.
1137 * size excludes decimal prec; realsz includes it.
1139 realsz
= dprec
> size
? dprec
: size
;
1145 prsize
= width
> realsz
? width
: realsz
;
1146 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1151 /* right-adjusting blank padding */
1152 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1153 PAD(width
- realsz
, blanks
);
1159 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1164 /* right-adjusting zero padding */
1165 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1166 PAD(width
- realsz
, zeroes
);
1168 /* leading zeroes from decimal precision */
1169 PAD(dprec
- size
, zeroes
);
1171 /* the string or number proper */
1172 #ifndef NO_FLOATING_POINT
1173 if ((flags
& FPT
) == 0) {
1175 } else { /* glue together f_p fragments */
1176 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1179 if (prec
|| flags
& ALT
)
1180 PRINT(decimal_point
, 1);
1182 /* already handled initial 0's */
1185 PRINTANDPAD(cp
, convbuf
+ ndig
, lead
, zeroes
);
1188 while (nseps
>0 || nrepeats
>0) {
1195 PRINT(&thousands_sep
,
1202 if (cp
> convbuf
+ ndig
)
1203 cp
= convbuf
+ ndig
;
1205 if (prec
|| flags
& ALT
) {
1206 buf
[0] = *decimal_point
;
1210 PRINTANDPAD(cp
, convbuf
+ ndig
, prec
, zeroes
);
1211 } else { /* %[eE] or sufficiently long %[gG] */
1212 if (prec
> 1 || flags
& ALT
) {
1214 buf
[1] = *decimal_point
;
1217 PAD(prec
- ndig
, zeroes
);
1220 PRINT(expstr
, expsize
);
1226 /* left-adjusting padding (always blank) */
1227 if (flags
& LADJUST
)
1228 PAD(width
- realsz
, blanks
);
1230 /* finally, adjust ret */
1235 if (convbuf
!= NULL
)
1239 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1246 * Find all arguments when a positional parameter is encountered. Returns a
1247 * table, indexed by argument number, of pointers to each arguments. The
1248 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1249 * It will be replaces with a malloc-ed one if it overflows.
1252 __find_arguments (const wchar_t *fmt0
, va_list ap
, union arg
**argtable
)
1254 wchar_t *fmt
; /* format string */
1255 wchar_t ch
; /* character from fmt */
1256 int n
, n2
; /* handy integer (short term usage) */
1257 wchar_t *cp
; /* handy char pointer (short term usage) */
1258 int flags
; /* flags as above */
1259 int width
; /* width from format (%8d), or 0 */
1260 enum typeid *typetable
; /* table of types */
1261 enum typeid stattypetable
[STATIC_ARG_TBL_SIZE
];
1262 int tablesize
; /* current size of type table */
1263 int tablemax
; /* largest used index in table */
1264 int nextarg
; /* 1-based argument index */
1267 * Add an argument type to the table, expanding if necessary.
1269 #define ADDTYPE(type) \
1270 ((nextarg >= tablesize) ? \
1271 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1272 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1273 typetable[nextarg++] = type)
1276 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1277 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1278 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1279 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1280 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1283 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1284 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1285 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1286 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1287 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1290 * Add * arguments to the type array.
1292 #define ADDASTER() \
1295 while (is_digit(*cp)) { \
1296 n2 = 10 * n2 + to_digit(*cp); \
1300 int hold = nextarg; \
1308 fmt
= (wchar_t *)fmt0
;
1309 typetable
= stattypetable
;
1310 tablesize
= STATIC_ARG_TBL_SIZE
;
1313 for (n
= 0; n
< STATIC_ARG_TBL_SIZE
; n
++)
1314 typetable
[n
] = T_UNUSED
;
1317 * Scan the format for conversions (`%' character).
1320 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
1324 fmt
++; /* skip over '%' */
1330 reswitch
: switch (ch
) {
1342 if ((ch
= *fmt
++) == '*') {
1346 while (is_digit(ch
)) {
1352 case '1': case '2': case '3': case '4':
1353 case '5': case '6': case '7': case '8': case '9':
1356 n
= 10 * n
+ to_digit(ch
);
1358 } while (is_digit(ch
));
1365 #ifndef NO_FLOATING_POINT
1371 if (flags
& SHORTINT
) {
1381 if (flags
& LONGINT
) {
1388 flags
|= LLONGINT
; /* not necessarily */
1400 if (flags
& LONGINT
)
1412 #ifndef NO_FLOATING_POINT
1420 if (flags
& LONGDBL
)
1421 ADDTYPE(T_LONG_DOUBLE
);
1425 #endif /* !NO_FLOATING_POINT */
1427 if (flags
& INTMAXT
)
1428 ADDTYPE(TP_INTMAXT
);
1429 else if (flags
& PTRDIFFT
)
1430 ADDTYPE(TP_PTRDIFFT
);
1431 else if (flags
& SIZET
)
1433 else if (flags
& LLONGINT
)
1435 else if (flags
& LONGINT
)
1437 else if (flags
& SHORTINT
)
1439 else if (flags
& CHARINT
)
1443 continue; /* no output */
1457 if (flags
& LONGINT
)
1470 default: /* "%?" prints ?, unless ? is NUL */
1478 * Build the argument table.
1480 if (tablemax
>= STATIC_ARG_TBL_SIZE
) {
1481 *argtable
= (union arg
*)
1482 malloc (sizeof (union arg
) * (tablemax
+ 1));
1485 (*argtable
) [0].intarg
= 0;
1486 for (n
= 1; n
<= tablemax
; n
++) {
1487 switch (typetable
[n
]) {
1488 case T_UNUSED
: /* whoops! */
1489 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1492 (*argtable
) [n
].pschararg
= va_arg (ap
, signed char *);
1495 (*argtable
) [n
].pshortarg
= va_arg (ap
, short *);
1498 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1501 (*argtable
) [n
].uintarg
= va_arg (ap
, unsigned int);
1504 (*argtable
) [n
].pintarg
= va_arg (ap
, int *);
1507 (*argtable
) [n
].longarg
= va_arg (ap
, long);
1510 (*argtable
) [n
].ulongarg
= va_arg (ap
, unsigned long);
1513 (*argtable
) [n
].plongarg
= va_arg (ap
, long *);
1516 (*argtable
) [n
].longlongarg
= va_arg (ap
, long long);
1519 (*argtable
) [n
].ulonglongarg
= va_arg (ap
, unsigned long long);
1522 (*argtable
) [n
].plonglongarg
= va_arg (ap
, long long *);
1525 (*argtable
) [n
].ptrdiffarg
= va_arg (ap
, ptrdiff_t);
1528 (*argtable
) [n
].pptrdiffarg
= va_arg (ap
, ptrdiff_t *);
1531 (*argtable
) [n
].sizearg
= va_arg (ap
, size_t);
1534 (*argtable
) [n
].psizearg
= va_arg (ap
, ssize_t
*);
1537 (*argtable
) [n
].intmaxarg
= va_arg (ap
, intmax_t);
1540 (*argtable
) [n
].uintmaxarg
= va_arg (ap
, uintmax_t);
1543 (*argtable
) [n
].pintmaxarg
= va_arg (ap
, intmax_t *);
1545 #ifndef NO_FLOATING_POINT
1547 (*argtable
) [n
].doublearg
= va_arg (ap
, double);
1550 (*argtable
) [n
].longdoublearg
= va_arg (ap
, long double);
1554 (*argtable
) [n
].pchararg
= va_arg (ap
, char *);
1557 (*argtable
) [n
].pvoidarg
= va_arg (ap
, void *);
1560 (*argtable
) [n
].wintarg
= va_arg (ap
, wint_t);
1563 (*argtable
) [n
].pwchararg
= va_arg (ap
, wchar_t *);
1568 if ((typetable
!= NULL
) && (typetable
!= stattypetable
))
1573 * Increase the size of the type table.
1576 __grow_type_table (int nextarg
, enum typeid **typetable
, int *tablesize
)
1578 enum typeid *const oldtable
= *typetable
;
1579 const int oldsize
= *tablesize
;
1580 enum typeid *newtable
;
1581 int n
, newsize
= oldsize
* 2;
1583 if (newsize
< nextarg
+ 1)
1584 newsize
= nextarg
+ 1;
1585 if (oldsize
== STATIC_ARG_TBL_SIZE
) {
1586 if ((newtable
= malloc(newsize
* sizeof(enum typeid))) == NULL
)
1587 abort(); /* XXX handle better */
1588 bcopy(oldtable
, newtable
, oldsize
* sizeof(enum typeid));
1590 newtable
= reallocf(oldtable
, newsize
* sizeof(enum typeid));
1591 if (newtable
== NULL
)
1592 abort(); /* XXX handle better */
1594 for (n
= oldsize
; n
< newsize
; n
++)
1595 newtable
[n
] = T_UNUSED
;
1597 *typetable
= newtable
;
1598 *tablesize
= newsize
;
1602 #ifndef NO_FLOATING_POINT
1605 exponent(wchar_t *p0
, int exp
, wchar_t fmtch
)
1608 wchar_t expbuf
[MAXEXPDIG
];
1618 t
= expbuf
+ MAXEXPDIG
;
1621 *--t
= to_char(exp
% 10);
1622 } while ((exp
/= 10) > 9);
1623 *--t
= to_char(exp
);
1624 for (; t
< expbuf
+ MAXEXPDIG
; *p
++ = *t
++);
1628 * Exponents for decimal floating point conversions
1629 * (%[eEgG]) must be at least two characters long,
1630 * whereas exponents for hexadecimal conversions can
1631 * be only one character long.
1633 if (fmtch
== 'e' || fmtch
== 'E')
1635 *p
++ = to_char(exp
);
1639 #endif /* !NO_FLOATING_POINT */