]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfwprintf.c
395bc637b87c97dabdbad87926f357ab659656de
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 #include <sys/cdefs.h>
39 #if defined(LIBC_SCCS) && !defined(lint)
40 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
41 #endif /* LIBC_SCCS and not lint */
42 __FBSDID("FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.58 2003/04/14 11:24:53 das Exp");
44 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.12 2003/04/19 23:53:19 das Exp $");
47 * Actual wprintf innards.
49 * Avoid making gratuitous changes to this source file; it should be kept
50 * as close as possible to vfprintf.c for ease of maintenance.
53 #include "namespace.h"
54 #include <sys/types.h>
67 #include "un-namespace.h"
69 #include "libc_private.h"
73 /* Define FLOATING_POINT to get floating point. */
74 #define FLOATING_POINT
81 long long longlongarg
;
82 unsigned long long ulonglongarg
;
89 signed char *pschararg
;
93 long long *plonglongarg
;
94 ptrdiff_t *pptrdiffarg
;
99 long double longdoublearg
;
106 * Type ids for argument type table.
109 T_UNUSED
, TP_SHORT
, T_INT
, T_U_INT
, TP_INT
,
110 T_LONG
, T_U_LONG
, TP_LONG
, T_LLONG
, T_U_LLONG
, TP_LLONG
,
111 T_PTRDIFFT
, TP_PTRDIFFT
, T_SIZET
, TP_SIZET
,
112 T_INTMAXT
, T_UINTMAXT
, TP_INTMAXT
, TP_VOID
, TP_CHAR
, TP_SCHAR
,
113 T_DOUBLE
, T_LONG_DOUBLE
, T_WINT
, TP_WCHAR
116 static int __sbprintf(FILE *, const wchar_t *, va_list);
117 static wchar_t *__ujtoa(uintmax_t, wchar_t *, int, int, const wchar_t *, int,
119 static wchar_t *__ultoa(u_long
, wchar_t *, int, int, const wchar_t *, int,
121 static wchar_t *__mbsconv(char *, int);
122 static void __find_arguments(const wchar_t *, va_list, union arg
**);
123 static void __grow_type_table(int, enum typeid **, int *);
126 * Helper function for `fprintf to unbuffered unix file': creates a
127 * temporary buffer. We only work on write-only files; this avoids
128 * worries about ungetc buffers and so forth.
131 __sbprintf(FILE *fp
, const wchar_t *fmt
, va_list ap
)
135 unsigned char buf
[BUFSIZ
];
137 /* copy the important variables */
138 fake
._flags
= fp
->_flags
& ~__SNBF
;
139 fake
._file
= fp
->_file
;
140 fake
._cookie
= fp
->_cookie
;
141 fake
._write
= fp
->_write
;
142 fake
._extra
= fp
->_extra
;
144 /* set up the buffer */
145 fake
._bf
._base
= fake
._p
= buf
;
146 fake
._bf
._size
= fake
._w
= sizeof(buf
);
147 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
149 /* do the work, then copy any error status */
150 ret
= __vfwprintf(&fake
, fmt
, ap
);
151 if (ret
>= 0 && __fflush(&fake
))
153 if (fake
._flags
& __SERR
)
154 fp
->_flags
|= __SERR
;
159 * Macros for converting digits to letters and vice versa
161 #define to_digit(c) ((c) - '0')
162 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
163 #define to_char(n) ((n) + '0')
166 * Convert an unsigned long to ASCII for printf purposes, returning
167 * a pointer to the first character of the string representation.
168 * Octal numbers can be forced to have a leading zero; hex numbers
169 * use the given digits.
172 __ultoa(u_long val
, wchar_t *endp
, int base
, int octzero
, const wchar_t *xdigs
,
173 int needgrp
, char thousep
, const char *grp
)
180 * Handle the three cases separately, in the hope of getting
181 * better/faster code.
185 if (val
< 10) { /* many numbers are 1 digit */
186 *--cp
= to_char(val
);
191 * On many machines, unsigned arithmetic is harder than
192 * signed arithmetic, so we do at most one unsigned mod and
193 * divide; this is sufficient to reduce the range of
194 * the incoming value to where signed arithmetic works.
196 if (val
> LONG_MAX
) {
197 *--cp
= to_char(val
% 10);
203 *--cp
= to_char(sval
% 10);
206 * If (*grp == CHAR_MAX) then no more grouping
207 * should be performed.
209 if (needgrp
&& ndig
== *grp
&& *grp
!= CHAR_MAX
214 * If (*(grp+1) == '\0') then we have to
215 * use *grp character (last grouping rule)
218 if (*(grp
+1) != '\0')
227 *--cp
= to_char(val
& 7);
230 if (octzero
&& *cp
!= '0')
236 *--cp
= xdigs
[val
& 15];
247 /* Identical to __ultoa, but for intmax_t. */
249 __ujtoa(uintmax_t val
, wchar_t *endp
, int base
, int octzero
,
250 const wchar_t *xdigs
, int needgrp
, char thousep
, const char *grp
)
256 /* quick test for small values; __ultoa is typically much faster */
257 /* (perhaps instead we should run until small, then call __ultoa?) */
258 if (val
<= ULONG_MAX
)
259 return (__ultoa((u_long
)val
, endp
, base
, octzero
, xdigs
,
260 needgrp
, thousep
, grp
));
264 *--cp
= to_char(val
% 10);
268 if (val
> INTMAX_MAX
) {
269 *--cp
= to_char(val
% 10);
275 *--cp
= to_char(sval
% 10);
278 * If (*grp == CHAR_MAX) then no more grouping
279 * should be performed.
281 if (needgrp
&& *grp
!= CHAR_MAX
&& ndig
== *grp
286 * If (*(grp+1) == '\0') then we have to
287 * use *grp character (last grouping rule)
290 if (*(grp
+1) != '\0')
299 *--cp
= to_char(val
& 7);
302 if (octzero
&& *cp
!= '0')
308 *--cp
= xdigs
[val
& 15];
320 * Convert a multibyte character string argument for the %s format to a wide
321 * string representation. ``prec'' specifies the maximum number of bytes
322 * to output. If ``prec'' is greater than or equal to zero, we can't assume
323 * that the multibyte char. string ends in a null character.
326 __mbsconv(char *mbsarg
, int prec
)
328 wchar_t *convbuf
, *wcp
;
330 size_t insize
, nchars
, nconv
;
337 * Supplied argument is a multibyte string; convert it to wide
342 * String is not guaranteed to be NUL-terminated. Find the
343 * number of characters to print.
345 memset(&mbs
, 0, sizeof(mbs
));
348 while (nchars
!= (size_t)prec
) {
349 nconv
= mbrlen(p
, MB_CUR_MAX
, &mbs
);
350 if (nconv
== 0 || nconv
== (size_t)-1 ||
357 if (nconv
== (size_t)-1 || nconv
== (size_t)-2)
360 insize
= strlen(mbsarg
);
363 * Allocate buffer for the result and perform the conversion,
364 * converting at most `size' bytes of the input multibyte string to
365 * wide characters for printing.
367 convbuf
= malloc((insize
+ 1) * sizeof(*convbuf
));
372 memset(&mbs
, 0, sizeof(mbs
));
373 while (insize
!= 0) {
374 nconv
= mbrtowc(wcp
, p
, insize
, &mbs
);
375 if (nconv
== 0 || nconv
== (size_t)-1 || nconv
== (size_t)-2)
381 if (nconv
== (size_t)-1 || nconv
== (size_t)-2) {
394 vfwprintf(FILE * __restrict fp
, const wchar_t * __restrict fmt0
, va_list ap
)
400 ret
= __vfwprintf(fp
, fmt0
, ap
);
405 #ifdef FLOATING_POINT
408 #define freedtoa __freedtoa
417 static int exponent(wchar_t *, int, wchar_t);
419 #endif /* FLOATING_POINT */
422 * The size of the buffer we use as scratch space for integer
423 * conversions, among other things. Technically, we would need the
424 * most space for base 10 conversions with thousands' grouping
425 * characters between each pair of digits. 100 bytes is a
426 * conservative overestimate even for a 128-bit uintmax_t.
430 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
433 * Flags used during conversion.
435 #define ALT 0x001 /* alternate form */
436 #define LADJUST 0x004 /* left adjustment */
437 #define LONGDBL 0x008 /* long double */
438 #define LONGINT 0x010 /* long integer */
439 #define LLONGINT 0x020 /* long long integer */
440 #define SHORTINT 0x040 /* short integer */
441 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
442 #define FPT 0x100 /* Floating point number */
443 #define GROUPING 0x200 /* use grouping ("'" flag) */
444 /* C99 additional size modifiers: */
445 #define SIZET 0x400 /* size_t */
446 #define PTRDIFFT 0x800 /* ptrdiff_t */
447 #define INTMAXT 0x1000 /* intmax_t */
448 #define CHARINT 0x2000 /* print char using int format */
451 * Non-MT-safe version
454 __vfwprintf(FILE *fp
, const wchar_t *fmt0
, va_list ap
)
456 wchar_t *fmt
; /* format string */
457 wchar_t ch
; /* character from fmt */
458 int n
, n2
, n3
; /* handy integer (short term usage) */
459 wchar_t *cp
; /* handy char pointer (short term usage) */
460 int flags
; /* flags as above */
461 int ret
; /* return value accumulator */
462 int width
; /* width from format (%8d), or 0 */
463 int prec
; /* precision from format; <0 for N/A */
464 wchar_t sign
; /* sign prefix (' ', '+', '-', or \0) */
465 char thousands_sep
; /* locale specific thousands separator */
466 const char *grouping
; /* locale specific numeric grouping rules */
467 #ifdef FLOATING_POINT
469 * We can decompose the printed representation of floating
470 * point numbers into several parts, some of which may be empty:
472 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
475 * A: 'sign' holds this value if present; '\0' otherwise
476 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
477 * C: cp points to the string MMMNNN. Leading and trailing
478 * zeros are not in the string and must be added.
479 * D: expchar holds this character; '\0' if no exponent, e.g. %f
480 * F: at least two digits for decimal, at least one digit for hex
482 char *decimal_point
; /* locale specific decimal point */
483 int signflag
; /* true if float is negative */
484 union { /* floating point arguments %[aAeEfFgG] */
488 int expt
; /* integer value of exponent */
489 char expchar
; /* exponent character: [eEpP\0] */
490 char *dtoaend
; /* pointer to end of converted digits */
491 int expsize
; /* character count for expstr */
492 int lead
; /* sig figs before decimal or group sep */
493 int ndig
; /* actual number of digits returned by dtoa */
494 wchar_t expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
495 char *dtoaresult
; /* buffer allocated by dtoa */
496 int nseps
; /* number of group separators with ' */
497 int nrepeats
; /* number of repeats of the last group */
499 u_long ulval
; /* integer arguments %[diouxX] */
500 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
501 int base
; /* base for [diouxX] conversion */
502 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
503 int realsz
; /* field size expanded by dprec, sign, etc */
504 int size
; /* size of converted field or string */
505 int prsize
; /* max size of printed field */
506 const wchar_t *xdigs
; /* digits for [xX] conversion */
507 wchar_t buf
[BUF
]; /* buffer with space for digits of uintmax_t */
508 wchar_t ox
[2]; /* space for 0x hex-prefix */
509 union arg
*argtable
; /* args, built due to positional arg */
510 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
511 int nextarg
; /* 1-based argument index */
512 va_list orgap
; /* original argument pointer */
513 wchar_t *convbuf
; /* multibyte to wide conversion result */
516 * Choose PADSIZE to trade efficiency vs. size. If larger printf
517 * fields occur frequently, increase PADSIZE and make the initialisers
520 #define PADSIZE 16 /* pad chunk size */
521 static wchar_t blanks
[PADSIZE
] =
522 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
523 static wchar_t zeroes
[PADSIZE
] =
524 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
526 static const wchar_t xdigs_lower
[16] = L
"0123456789abcdef";
527 static const wchar_t xdigs_upper
[16] = L
"0123456789ABCDEF";
530 * BEWARE, these `goto error' on error, PRINT uses `n2' and
533 #define PRINT(ptr, len) do { \
534 for (n3 = 0; n3 < (len); n3++) \
535 __fputwc((ptr)[n3], fp); \
537 #define PAD(howmany, with) do { \
538 if ((n = (howmany)) > 0) { \
539 while (n > PADSIZE) { \
540 PRINT(with, PADSIZE); \
546 #define PRINTANDPAD(p, ep, len, with) do { \
552 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
556 * Get the argument indexed by nextarg. If the argument table is
557 * built, use it to get the argument. If its not, get the next
558 * argument (and arguments must be gotten sequentially).
560 #define GETARG(type) \
561 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
562 (nextarg++, va_arg(ap, type)))
565 * To extend shorts properly, we need both signed and unsigned
566 * argument extraction methods.
569 (flags&LONGINT ? GETARG(long) : \
570 flags&SHORTINT ? (long)(short)GETARG(int) : \
571 flags&CHARINT ? (long)(signed char)GETARG(int) : \
574 (flags&LONGINT ? GETARG(u_long) : \
575 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
576 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
577 (u_long)GETARG(u_int))
578 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
580 (flags&INTMAXT ? GETARG(intmax_t) : \
581 flags&SIZET ? (intmax_t)GETARG(size_t) : \
582 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
583 (intmax_t)GETARG(long long))
585 (flags&INTMAXT ? GETARG(uintmax_t) : \
586 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
587 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
588 (uintmax_t)GETARG(unsigned long long))
591 * Get * arguments, including the form *nn$. Preserve the nextarg
592 * that the argument can be gotten once the type is determined.
594 #define GETASTER(val) \
597 while (is_digit(*cp)) { \
598 n2 = 10 * n2 + to_digit(*cp); \
602 int hold = nextarg; \
603 if (argtable == NULL) { \
604 argtable = statargtable; \
605 __find_arguments (fmt0, orgap, &argtable); \
608 val = GETARG (int); \
612 val = GETARG (int); \
616 thousands_sep
= '\0';
618 #ifdef FLOATING_POINT
619 decimal_point
= localeconv()->decimal_point
;
622 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
626 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
627 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
629 return (__sbprintf(fp
, fmt0
, ap
));
631 fmt
= (wchar_t *)fmt0
;
638 * Scan the format for conversions (`%' character).
641 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
643 if ((n
= fmt
- cp
) != 0) {
644 if ((unsigned)ret
+ n
> INT_MAX
) {
653 fmt
++; /* skip over '%' */
663 reswitch
: switch (ch
) {
666 * ``If the space and + flags both appear, the space
667 * flag will be ignored.''
678 * ``A negative field width argument is taken as a
679 * - flag followed by a positive field width.''
681 * They don't exclude field widths read from args.
696 thousands_sep
= *(localeconv()->thousands_sep
);
697 grouping
= localeconv()->grouping
;
700 if ((ch
= *fmt
++) == '*') {
705 while (is_digit(ch
)) {
706 prec
= 10 * prec
+ to_digit(ch
);
712 * ``Note that 0 is taken as a flag, not as the
713 * beginning of a field width.''
718 case '1': case '2': case '3': case '4':
719 case '5': case '6': case '7': case '8': case '9':
722 n
= 10 * n
+ to_digit(ch
);
724 } while (is_digit(ch
));
727 if (argtable
== NULL
) {
728 argtable
= statargtable
;
729 __find_arguments (fmt0
, orgap
,
736 #ifdef FLOATING_POINT
742 if (flags
& SHORTINT
) {
752 if (flags
& LONGINT
) {
759 flags
|= LLONGINT
; /* not necessarily */
772 *(cp
= buf
) = (wchar_t)GETARG(wint_t);
774 *(cp
= buf
) = (wchar_t)btowc(GETARG(int));
783 if (flags
& INTMAX_SIZE
) {
785 if ((intmax_t)ujval
< 0) {
791 if ((long)ulval
< 0) {
798 #ifdef FLOATING_POINT
812 * XXX We don't actually have a conversion
813 * XXX routine for this yet.
815 if (flags
& LONGDBL
) {
816 fparg
.ldbl
= (double)GETARG(long double);
818 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
819 &expt
, &signflag
, &dtoaend
);
821 fparg
.dbl
= GETARG(double);
823 __hdtoa(fparg
.dbl
, xdigs
, prec
,
824 &expt
, &signflag
, &dtoaend
);
828 cp
= convbuf
= __mbsconv(dtoaresult
, -1);
829 freedtoa(dtoaresult
);
835 if (prec
< 0) /* account for digit before decpt */
846 expchar
= ch
- ('g' - 'e');
854 if (flags
& LONGDBL
) {
855 fparg
.ldbl
= GETARG(long double);
857 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
858 &expt
, &signflag
, &dtoaend
);
860 fparg
.dbl
= GETARG(double);
862 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
863 &expt
, &signflag
, &dtoaend
);
867 ndig
= dtoaend
- dtoaresult
;
868 cp
= convbuf
= __mbsconv(dtoaresult
, -1);
869 freedtoa(dtoaresult
);
872 if (expt
== INT_MAX
) { /* inf or nan */
874 cp
= (ch
>= 'a') ? L
"nan" : L
"NAN";
877 cp
= (ch
>= 'a') ? L
"inf" : L
"INF";
882 if (ch
== 'g' || ch
== 'G') {
883 if (expt
> -4 && expt
<= prec
) {
884 /* Make %[gG] smell like %[fF] */
894 * Make %[gG] smell like %[eE], but
895 * trim trailing zeroes if no # flag.
902 expsize
= exponent(expstr
, expt
- 1, expchar
);
903 size
= expsize
+ prec
;
904 if (prec
> 1 || flags
& ALT
)
907 /* space for digits before decimal point */
912 /* space for decimal pt and following digits */
913 if (prec
|| flags
& ALT
)
915 if (grouping
&& expt
> 0) {
916 /* space for thousands' grouping */
917 nseps
= nrepeats
= 0;
919 while (*grouping
!= CHAR_MAX
) {
920 if (lead
<= *grouping
)
929 size
+= nseps
+ nrepeats
;
934 #endif /* FLOATING_POINT */
937 * Assignment-like behavior is specified if the
938 * value overflows or is otherwise unrepresentable.
939 * C99 says to use `signed char' for %hhn conversions.
941 if (flags
& LLONGINT
)
942 *GETARG(long long *) = ret
;
943 else if (flags
& SIZET
)
944 *GETARG(ssize_t
*) = (ssize_t
)ret
;
945 else if (flags
& PTRDIFFT
)
946 *GETARG(ptrdiff_t *) = ret
;
947 else if (flags
& INTMAXT
)
948 *GETARG(intmax_t *) = ret
;
949 else if (flags
& LONGINT
)
950 *GETARG(long *) = ret
;
951 else if (flags
& SHORTINT
)
952 *GETARG(short *) = ret
;
953 else if (flags
& CHARINT
)
954 *GETARG(signed char *) = ret
;
956 *GETARG(int *) = ret
;
957 continue; /* no output */
962 if (flags
& INTMAX_SIZE
)
970 * ``The argument shall be a pointer to void. The
971 * value of the pointer is converted to a sequence
972 * of printable characters, in an implementation-
976 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
979 flags
= flags
| INTMAXT
;
986 if (flags
& LONGINT
) {
987 if ((cp
= GETARG(wchar_t *)) == NULL
)
994 if ((mbp
= GETARG(char *)) == NULL
)
997 convbuf
= __mbsconv(mbp
, prec
);
998 if (convbuf
== NULL
) {
999 fp
->_flags
|= __SERR
;
1008 * can't use wcslen; can only look for the
1009 * NUL in the first `prec' characters, and
1010 * wcslen() will go further.
1012 wchar_t *p
= wmemchr(cp
, 0, (size_t)prec
);
1028 if (flags
& INTMAX_SIZE
)
1035 xdigs
= xdigs_upper
;
1038 xdigs
= xdigs_lower
;
1040 if (flags
& INTMAX_SIZE
)
1045 /* leading 0x/X only if non-zero */
1047 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1051 /* unsigned conversions */
1052 nosign
: sign
= '\0';
1054 * ``... diouXx conversions ... if a precision is
1055 * specified, the 0 flag will be ignored.''
1058 number
: if ((dprec
= prec
) >= 0)
1062 * ``The result of converting a zero value with an
1063 * explicit precision of zero is no characters.''
1067 if (flags
& INTMAX_SIZE
) {
1068 if (ujval
!= 0 || prec
!= 0)
1069 cp
= __ujtoa(ujval
, cp
, base
,
1071 flags
& GROUPING
, thousands_sep
,
1074 if (ulval
!= 0 || prec
!= 0)
1075 cp
= __ultoa(ulval
, cp
, base
,
1077 flags
& GROUPING
, thousands_sep
,
1080 size
= buf
+ BUF
- cp
;
1081 if (size
> BUF
) /* should never happen */
1084 default: /* "%?" prints ?, unless ? is NUL */
1087 /* pretend it was %c with argument ch */
1096 * All reasonable formats wind up here. At this point, `cp'
1097 * points to a string which (if not flags&LADJUST) should be
1098 * padded out to `width' places. If flags&ZEROPAD, it should
1099 * first be prefixed by any sign or other prefix; otherwise,
1100 * it should be blank padded before the prefix is emitted.
1101 * After any left-hand padding and prefixing, emit zeroes
1102 * required by a decimal [diouxX] precision, then print the
1103 * string proper, then emit zeroes required by any leftover
1104 * floating precision; finally, if LADJUST, pad with blanks.
1106 * Compute actual size, so we know how much to pad.
1107 * size excludes decimal prec; realsz includes it.
1109 realsz
= dprec
> size
? dprec
: size
;
1115 prsize
= width
> realsz
? width
: realsz
;
1116 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1121 /* right-adjusting blank padding */
1122 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1123 PAD(width
- realsz
, blanks
);
1128 } else if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1133 /* right-adjusting zero padding */
1134 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1135 PAD(width
- realsz
, zeroes
);
1137 /* leading zeroes from decimal precision */
1138 PAD(dprec
- size
, zeroes
);
1140 /* the string or number proper */
1141 #ifdef FLOATING_POINT
1142 if ((flags
& FPT
) == 0) {
1144 } else { /* glue together f_p fragments */
1145 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1148 if (prec
|| flags
& ALT
)
1149 PRINT(decimal_point
, 1);
1151 /* already handled initial 0's */
1154 PRINTANDPAD(cp
, convbuf
+ ndig
, lead
, zeroes
);
1157 while (nseps
>0 || nrepeats
>0) {
1164 PRINT(&thousands_sep
,
1171 if (cp
> convbuf
+ ndig
)
1172 cp
= convbuf
+ ndig
;
1174 if (prec
|| flags
& ALT
) {
1175 buf
[0] = *decimal_point
;
1179 PRINTANDPAD(cp
, convbuf
+ ndig
, prec
, zeroes
);
1180 } else { /* %[eE] or sufficiently long %[gG] */
1181 if (prec
> 1 || flags
& ALT
) {
1183 buf
[1] = *decimal_point
;
1186 PAD(prec
- ndig
, zeroes
);
1189 PRINT(expstr
, expsize
);
1195 /* left-adjusting padding (always blank) */
1196 if (flags
& LADJUST
)
1197 PAD(width
- realsz
, blanks
);
1199 /* finally, adjust ret */
1204 if (convbuf
!= NULL
)
1208 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1215 * Find all arguments when a positional parameter is encountered. Returns a
1216 * table, indexed by argument number, of pointers to each arguments. The
1217 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1218 * It will be replaces with a malloc-ed one if it overflows.
1221 __find_arguments (const wchar_t *fmt0
, va_list ap
, union arg
**argtable
)
1223 wchar_t *fmt
; /* format string */
1224 wchar_t ch
; /* character from fmt */
1225 int n
, n2
; /* handy integer (short term usage) */
1226 wchar_t *cp
; /* handy char pointer (short term usage) */
1227 int flags
; /* flags as above */
1228 int width
; /* width from format (%8d), or 0 */
1229 enum typeid *typetable
; /* table of types */
1230 enum typeid stattypetable
[STATIC_ARG_TBL_SIZE
];
1231 int tablesize
; /* current size of type table */
1232 int tablemax
; /* largest used index in table */
1233 int nextarg
; /* 1-based argument index */
1236 * Add an argument type to the table, expanding if necessary.
1238 #define ADDTYPE(type) \
1239 ((nextarg >= tablesize) ? \
1240 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1241 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1242 typetable[nextarg++] = type)
1245 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1246 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1247 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1248 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1249 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1252 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1253 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1254 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1255 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1256 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1259 * Add * arguments to the type array.
1261 #define ADDASTER() \
1264 while (is_digit(*cp)) { \
1265 n2 = 10 * n2 + to_digit(*cp); \
1269 int hold = nextarg; \
1277 fmt
= (wchar_t *)fmt0
;
1278 typetable
= stattypetable
;
1279 tablesize
= STATIC_ARG_TBL_SIZE
;
1282 memset (typetable
, T_UNUSED
, STATIC_ARG_TBL_SIZE
);
1285 * Scan the format for conversions (`%' character).
1288 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
1292 fmt
++; /* skip over '%' */
1298 reswitch
: switch (ch
) {
1310 if ((ch
= *fmt
++) == '*') {
1314 while (is_digit(ch
)) {
1320 case '1': case '2': case '3': case '4':
1321 case '5': case '6': case '7': case '8': case '9':
1324 n
= 10 * n
+ to_digit(ch
);
1326 } while (is_digit(ch
));
1333 #ifdef FLOATING_POINT
1339 if (flags
& SHORTINT
) {
1349 if (flags
& LONGINT
) {
1356 flags
|= LLONGINT
; /* not necessarily */
1368 if (flags
& LONGINT
)
1380 #ifdef FLOATING_POINT
1390 if (flags
& LONGDBL
)
1391 ADDTYPE(T_LONG_DOUBLE
);
1395 #endif /* FLOATING_POINT */
1397 if (flags
& INTMAXT
)
1398 ADDTYPE(TP_INTMAXT
);
1399 else if (flags
& PTRDIFFT
)
1400 ADDTYPE(TP_PTRDIFFT
);
1401 else if (flags
& SIZET
)
1403 else if (flags
& LLONGINT
)
1405 else if (flags
& LONGINT
)
1407 else if (flags
& SHORTINT
)
1409 else if (flags
& CHARINT
)
1413 continue; /* no output */
1427 if (flags
& LONGINT
)
1440 default: /* "%?" prints ?, unless ? is NUL */
1448 * Build the argument table.
1450 if (tablemax
>= STATIC_ARG_TBL_SIZE
) {
1451 *argtable
= (union arg
*)
1452 malloc (sizeof (union arg
) * (tablemax
+ 1));
1455 (*argtable
) [0].intarg
= 0;
1456 for (n
= 1; n
<= tablemax
; n
++) {
1457 switch (typetable
[n
]) {
1458 case T_UNUSED
: /* whoops! */
1459 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1462 (*argtable
) [n
].pschararg
= va_arg (ap
, signed char *);
1465 (*argtable
) [n
].pshortarg
= va_arg (ap
, short *);
1468 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1471 (*argtable
) [n
].uintarg
= va_arg (ap
, unsigned int);
1474 (*argtable
) [n
].pintarg
= va_arg (ap
, int *);
1477 (*argtable
) [n
].longarg
= va_arg (ap
, long);
1480 (*argtable
) [n
].ulongarg
= va_arg (ap
, unsigned long);
1483 (*argtable
) [n
].plongarg
= va_arg (ap
, long *);
1486 (*argtable
) [n
].longlongarg
= va_arg (ap
, long long);
1489 (*argtable
) [n
].ulonglongarg
= va_arg (ap
, unsigned long long);
1492 (*argtable
) [n
].plonglongarg
= va_arg (ap
, long long *);
1495 (*argtable
) [n
].ptrdiffarg
= va_arg (ap
, ptrdiff_t);
1498 (*argtable
) [n
].pptrdiffarg
= va_arg (ap
, ptrdiff_t *);
1501 (*argtable
) [n
].sizearg
= va_arg (ap
, size_t);
1504 (*argtable
) [n
].psizearg
= va_arg (ap
, ssize_t
*);
1507 (*argtable
) [n
].intmaxarg
= va_arg (ap
, intmax_t);
1510 (*argtable
) [n
].uintmaxarg
= va_arg (ap
, uintmax_t);
1513 (*argtable
) [n
].pintmaxarg
= va_arg (ap
, intmax_t *);
1515 #ifdef FLOATING_POINT
1517 (*argtable
) [n
].doublearg
= va_arg (ap
, double);
1520 (*argtable
) [n
].longdoublearg
= va_arg (ap
, long double);
1524 (*argtable
) [n
].pchararg
= va_arg (ap
, char *);
1527 (*argtable
) [n
].pvoidarg
= va_arg (ap
, void *);
1530 (*argtable
) [n
].wintarg
= va_arg (ap
, wint_t);
1533 (*argtable
) [n
].pwchararg
= va_arg (ap
, wchar_t *);
1538 if ((typetable
!= NULL
) && (typetable
!= stattypetable
))
1543 * Increase the size of the type table.
1546 __grow_type_table (int nextarg
, enum typeid **typetable
, int *tablesize
)
1548 enum typeid *const oldtable
= *typetable
;
1549 const int oldsize
= *tablesize
;
1550 enum typeid *newtable
;
1551 int newsize
= oldsize
* 2;
1553 if (newsize
< nextarg
+ 1)
1554 newsize
= nextarg
+ 1;
1555 if (oldsize
== STATIC_ARG_TBL_SIZE
) {
1556 if ((newtable
= malloc(newsize
)) == NULL
)
1557 abort(); /* XXX handle better */
1558 bcopy(oldtable
, newtable
, oldsize
);
1560 if ((newtable
= reallocf(oldtable
, newsize
)) == NULL
)
1561 abort(); /* XXX handle better */
1563 memset(&newtable
[oldsize
], T_UNUSED
, newsize
- oldsize
);
1565 *typetable
= newtable
;
1566 *tablesize
= newsize
;
1570 #ifdef FLOATING_POINT
1573 exponent(wchar_t *p0
, int exp
, wchar_t fmtch
)
1576 wchar_t expbuf
[MAXEXPDIG
];
1586 t
= expbuf
+ MAXEXPDIG
;
1589 *--t
= to_char(exp
% 10);
1590 } while ((exp
/= 10) > 9);
1591 *--t
= to_char(exp
);
1592 for (; t
< expbuf
+ MAXEXPDIG
; *p
++ = *t
++);
1596 * Exponents for decimal floating point conversions
1597 * (%[eEgG]) must be at least two characters long,
1598 * whereas exponents for hexadecimal conversions can
1599 * be only one character long.
1601 if (fmtch
== 'e' || fmtch
== 'E')
1603 *p
++ = to_char(exp
);
1607 #endif /* FLOATING_POINT */