]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfprintf.c
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfprintf.c,v 1.65 2004/05/02 10:55:05 das Exp $");
44 * Actual printf innards.
46 * This code is large and complicated...
49 #include "namespace.h"
50 #include <sys/types.h>
63 #include "un-namespace.h"
65 #include "libc_private.h"
74 long long longlongarg
;
75 unsigned long long ulonglongarg
;
82 signed char *pschararg
;
86 long long *plonglongarg
;
87 ptrdiff_t *pptrdiffarg
;
90 #ifndef NO_FLOATING_POINT
92 long double longdoublearg
;
99 * Type ids for argument type table.
102 T_UNUSED
, TP_SHORT
, T_INT
, T_U_INT
, TP_INT
,
103 T_LONG
, T_U_LONG
, TP_LONG
, T_LLONG
, T_U_LLONG
, TP_LLONG
,
104 T_PTRDIFFT
, TP_PTRDIFFT
, T_SIZET
, TP_SIZET
,
105 T_INTMAXT
, T_UINTMAXT
, TP_INTMAXT
, TP_VOID
, TP_CHAR
, TP_SCHAR
,
106 T_DOUBLE
, T_LONG_DOUBLE
, T_WINT
, TP_WCHAR
109 static int __sprint(FILE *, struct __suio
*);
110 static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0);
111 static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char,
113 static char *__ultoa(u_long
, char *, int, int, const char *, int, char,
115 static char *__wcsconv(wchar_t *, int);
116 static void __find_arguments(const char *, va_list, union arg
**);
117 static void __grow_type_table(int, enum typeid **, int *);
120 * Flush out all the vectors defined by the given uio,
121 * then reset it so that it can be reused.
124 __sprint(FILE *fp
, struct __suio
*uio
)
128 if (uio
->uio_resid
== 0) {
132 err
= __sfvwrite(fp
, uio
);
139 * Helper function for `fprintf to unbuffered unix file': creates a
140 * temporary buffer. We only work on write-only files; this avoids
141 * worries about ungetc buffers and so forth.
144 __sbprintf(FILE *fp
, const char *fmt
, va_list ap
)
148 unsigned char buf
[BUFSIZ
];
150 /* copy the important variables */
151 fake
._flags
= fp
->_flags
& ~__SNBF
;
152 fake
._file
= fp
->_file
;
153 fake
._cookie
= fp
->_cookie
;
154 fake
._write
= fp
->_write
;
155 fake
._extra
= fp
->_extra
;
157 /* set up the buffer */
158 fake
._bf
._base
= fake
._p
= buf
;
159 fake
._bf
._size
= fake
._w
= sizeof(buf
);
160 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
162 /* do the work, then copy any error status */
163 ret
= __vfprintf(&fake
, fmt
, ap
);
164 if (ret
>= 0 && __fflush(&fake
))
166 if (fake
._flags
& __SERR
)
167 fp
->_flags
|= __SERR
;
172 * Macros for converting digits to letters and vice versa
174 #define to_digit(c) ((c) - '0')
175 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
176 #define to_char(n) ((n) + '0')
179 * Convert an unsigned long to ASCII for printf purposes, returning
180 * a pointer to the first character of the string representation.
181 * Octal numbers can be forced to have a leading zero; hex numbers
182 * use the given digits.
185 __ultoa(u_long val
, char *endp
, int base
, int octzero
, const char *xdigs
,
186 int needgrp
, char thousep
, const char *grp
)
193 * Handle the three cases separately, in the hope of getting
194 * better/faster code.
198 if (val
< 10) { /* many numbers are 1 digit */
199 *--cp
= to_char(val
);
204 * On many machines, unsigned arithmetic is harder than
205 * signed arithmetic, so we do at most one unsigned mod and
206 * divide; this is sufficient to reduce the range of
207 * the incoming value to where signed arithmetic works.
209 if (val
> LONG_MAX
) {
210 *--cp
= to_char(val
% 10);
216 *--cp
= to_char(sval
% 10);
219 * If (*grp == CHAR_MAX) then no more grouping
220 * should be performed.
222 if (needgrp
&& ndig
== *grp
&& *grp
!= CHAR_MAX
227 * If (*(grp+1) == '\0') then we have to
228 * use *grp character (last grouping rule)
231 if (*(grp
+1) != '\0')
240 *--cp
= to_char(val
& 7);
243 if (octzero
&& *cp
!= '0')
249 *--cp
= xdigs
[val
& 15];
260 /* Identical to __ultoa, but for intmax_t. */
262 __ujtoa(uintmax_t val
, char *endp
, int base
, int octzero
, const char *xdigs
,
263 int needgrp
, char thousep
, const char *grp
)
269 /* quick test for small values; __ultoa is typically much faster */
270 /* (perhaps instead we should run until small, then call __ultoa?) */
271 if (val
<= ULONG_MAX
)
272 return (__ultoa((u_long
)val
, endp
, base
, octzero
, xdigs
,
273 needgrp
, thousep
, grp
));
277 *--cp
= to_char(val
% 10);
281 if (val
> INTMAX_MAX
) {
282 *--cp
= to_char(val
% 10);
288 *--cp
= to_char(sval
% 10);
291 * If (*grp == CHAR_MAX) then no more grouping
292 * should be performed.
294 if (needgrp
&& *grp
!= CHAR_MAX
&& ndig
== *grp
299 * If (*(grp+1) == '\0') then we have to
300 * use *grp character (last grouping rule)
303 if (*(grp
+1) != '\0')
312 *--cp
= to_char(val
& 7);
315 if (octzero
&& *cp
!= '0')
321 *--cp
= xdigs
[val
& 15];
333 * Convert a wide character string argument for the %ls format to a multibyte
334 * string representation. ``prec'' specifies the maximum number of bytes
335 * to output. If ``prec'' is greater than or equal to zero, we can't assume
336 * that the wide char. string ends in a null character.
339 __wcsconv(wchar_t *wcsarg
, int prec
)
341 static const mbstate_t initial
;
343 char buf
[MB_LEN_MAX
];
349 * Determine the number of bytes to output and allocate space for
357 clen
= wcrtomb(buf
, *p
++, &mbs
);
358 if (clen
== 0 || clen
== (size_t)-1 ||
359 nbytes
+ clen
> prec
)
366 nbytes
= wcsrtombs(NULL
, (const wchar_t **)&p
, 0, &mbs
);
367 if (nbytes
== (size_t)-1)
370 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
374 * Fill the output buffer with the multibyte representations of as
375 * many wide characters as will fit.
380 while (mbp
- convbuf
< nbytes
) {
381 clen
= wcrtomb(mbp
, *p
++, &mbs
);
382 if (clen
== 0 || clen
== (size_t)-1)
386 if (clen
== (size_t)-1) {
399 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
405 ret
= __vfprintf(fp
, fmt0
, ap
);
410 #ifndef NO_FLOATING_POINT
413 #define freedtoa __freedtoa
422 static int exponent(char *, int, int);
424 #endif /* !NO_FLOATING_POINT */
427 * The size of the buffer we use as scratch space for integer
428 * conversions, among other things. Technically, we would need the
429 * most space for base 10 conversions with thousands' grouping
430 * characters between each pair of digits. 100 bytes is a
431 * conservative overestimate even for a 128-bit uintmax_t.
435 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
438 * Flags used during conversion.
440 #define ALT 0x001 /* alternate form */
441 #define LADJUST 0x004 /* left adjustment */
442 #define LONGDBL 0x008 /* long double */
443 #define LONGINT 0x010 /* long integer */
444 #define LLONGINT 0x020 /* long long integer */
445 #define SHORTINT 0x040 /* short integer */
446 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
447 #define FPT 0x100 /* Floating point number */
448 #define GROUPING 0x200 /* use grouping ("'" flag) */
449 /* C99 additional size modifiers: */
450 #define SIZET 0x400 /* size_t */
451 #define PTRDIFFT 0x800 /* ptrdiff_t */
452 #define INTMAXT 0x1000 /* intmax_t */
453 #define CHARINT 0x2000 /* print char using int format */
456 * Non-MT-safe version
459 __vfprintf(FILE *fp
, const char *fmt0
, va_list ap
)
461 char *fmt
; /* format string */
462 int ch
; /* character from fmt */
463 int n
, n2
; /* handy integer (short term usage) */
464 char *cp
; /* handy char pointer (short term usage) */
465 struct __siov
*iovp
; /* for PRINT macro */
466 int flags
; /* flags as above */
467 int ret
; /* return value accumulator */
468 int width
; /* width from format (%8d), or 0 */
469 int prec
; /* precision from format; <0 for N/A */
470 char sign
; /* sign prefix (' ', '+', '-', or \0) */
471 char thousands_sep
; /* locale specific thousands separator */
472 const char *grouping
; /* locale specific numeric grouping rules */
473 #ifndef NO_FLOATING_POINT
475 * We can decompose the printed representation of floating
476 * point numbers into several parts, some of which may be empty:
478 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
481 * A: 'sign' holds this value if present; '\0' otherwise
482 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
483 * C: cp points to the string MMMNNN. Leading and trailing
484 * zeros are not in the string and must be added.
485 * D: expchar holds this character; '\0' if no exponent, e.g. %f
486 * F: at least two digits for decimal, at least one digit for hex
488 char *decimal_point
; /* locale specific decimal point */
489 int signflag
; /* true if float is negative */
490 union { /* floating point arguments %[aAeEfFgG] */
494 int expt
; /* integer value of exponent */
495 char expchar
; /* exponent character: [eEpP\0] */
496 char *dtoaend
; /* pointer to end of converted digits */
497 int expsize
; /* character count for expstr */
498 int lead
; /* sig figs before decimal or group sep */
499 int ndig
; /* actual number of digits returned by dtoa */
500 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
501 char *dtoaresult
; /* buffer allocated by dtoa */
502 int nseps
; /* number of group separators with ' */
503 int nrepeats
; /* number of repeats of the last group */
505 u_long ulval
; /* integer arguments %[diouxX] */
506 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
507 int base
; /* base for [diouxX] conversion */
508 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
509 int realsz
; /* field size expanded by dprec, sign, etc */
510 int size
; /* size of converted field or string */
511 int prsize
; /* max size of printed field */
512 const char *xdigs
; /* digits for %[xX] conversion */
514 struct __suio uio
; /* output information: summary */
515 struct __siov iov
[NIOV
];/* ... and individual io vectors */
516 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
517 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
518 union arg
*argtable
; /* args, built due to positional arg */
519 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
520 int nextarg
; /* 1-based argument index */
521 va_list orgap
; /* original argument pointer */
522 char *convbuf
; /* wide to multibyte conversion result */
525 * Choose PADSIZE to trade efficiency vs. size. If larger printf
526 * fields occur frequently, increase PADSIZE and make the initialisers
529 #define PADSIZE 16 /* pad chunk size */
530 static char blanks
[PADSIZE
] =
531 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
532 static char zeroes
[PADSIZE
] =
533 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
535 static const char xdigs_lower
[16] = "0123456789abcdef";
536 static const char xdigs_upper
[16] = "0123456789ABCDEF";
539 * BEWARE, these `goto error' on error, and PAD uses `n'.
541 #define PRINT(ptr, len) { \
542 iovp->iov_base = (ptr); \
543 iovp->iov_len = (len); \
544 uio.uio_resid += (len); \
546 if (++uio.uio_iovcnt >= NIOV) { \
547 if (__sprint(fp, &uio)) \
552 #define PAD(howmany, with) { \
553 if ((n = (howmany)) > 0) { \
554 while (n > PADSIZE) { \
555 PRINT(with, PADSIZE); \
561 #define PRINTANDPAD(p, ep, len, with) do { \
567 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
570 if (uio.uio_resid && __sprint(fp, &uio)) \
572 uio.uio_iovcnt = 0; \
577 * Get the argument indexed by nextarg. If the argument table is
578 * built, use it to get the argument. If its not, get the next
579 * argument (and arguments must be gotten sequentially).
581 #define GETARG(type) \
582 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
583 (nextarg++, va_arg(ap, type)))
586 * To extend shorts properly, we need both signed and unsigned
587 * argument extraction methods.
590 (flags&LONGINT ? GETARG(long) : \
591 flags&SHORTINT ? (long)(short)GETARG(int) : \
592 flags&CHARINT ? (long)(signed char)GETARG(int) : \
595 (flags&LONGINT ? GETARG(u_long) : \
596 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
597 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
598 (u_long)GETARG(u_int))
599 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
601 (flags&INTMAXT ? GETARG(intmax_t) : \
602 flags&SIZET ? (intmax_t)GETARG(size_t) : \
603 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
604 (intmax_t)GETARG(long long))
606 (flags&INTMAXT ? GETARG(uintmax_t) : \
607 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
608 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
609 (uintmax_t)GETARG(unsigned long long))
612 * Get * arguments, including the form *nn$. Preserve the nextarg
613 * that the argument can be gotten once the type is determined.
615 #define GETASTER(val) \
618 while (is_digit(*cp)) { \
619 n2 = 10 * n2 + to_digit(*cp); \
623 int hold = nextarg; \
624 if (argtable == NULL) { \
625 argtable = statargtable; \
626 __find_arguments (fmt0, orgap, &argtable); \
629 val = GETARG (int); \
633 val = GETARG (int); \
637 thousands_sep
= '\0';
640 #ifndef NO_FLOATING_POINT
642 decimal_point
= localeconv()->decimal_point
;
644 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
648 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
649 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
651 return (__sbprintf(fp
, fmt0
, ap
));
657 uio
.uio_iov
= iovp
= iov
;
663 * Scan the format for conversions (`%' character).
666 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
668 if ((n
= fmt
- cp
) != 0) {
669 if ((unsigned)ret
+ n
> INT_MAX
) {
678 fmt
++; /* skip over '%' */
688 reswitch
: switch (ch
) {
691 * ``If the space and + flags both appear, the space
692 * flag will be ignored.''
703 * ``A negative field width argument is taken as a
704 * - flag followed by a positive field width.''
706 * They don't exclude field widths read from args.
721 thousands_sep
= *(localeconv()->thousands_sep
);
722 grouping
= localeconv()->grouping
;
725 if ((ch
= *fmt
++) == '*') {
730 while (is_digit(ch
)) {
731 prec
= 10 * prec
+ to_digit(ch
);
737 * ``Note that 0 is taken as a flag, not as the
738 * beginning of a field width.''
743 case '1': case '2': case '3': case '4':
744 case '5': case '6': case '7': case '8': case '9':
747 n
= 10 * n
+ to_digit(ch
);
749 } while (is_digit(ch
));
752 if (argtable
== NULL
) {
753 argtable
= statargtable
;
754 __find_arguments (fmt0
, orgap
,
761 #ifndef NO_FLOATING_POINT
767 if (flags
& SHORTINT
) {
777 if (flags
& LONGINT
) {
784 flags
|= LLONGINT
; /* not necessarily */
796 if (flags
& LONGINT
) {
797 static const mbstate_t initial
;
802 mbseqlen
= wcrtomb(cp
= buf
,
803 (wchar_t)GETARG(wint_t), &mbs
);
804 if (mbseqlen
== (size_t)-1) {
805 fp
->_flags
|= __SERR
;
808 size
= (int)mbseqlen
;
810 *(cp
= buf
) = GETARG(int);
820 if (flags
& INTMAX_SIZE
) {
822 if ((intmax_t)ujval
< 0) {
828 if ((long)ulval
< 0) {
835 #ifndef NO_FLOATING_POINT
849 if (dtoaresult
!= NULL
)
850 freedtoa(dtoaresult
);
851 if (flags
& LONGDBL
) {
852 fparg
.ldbl
= GETARG(long double);
854 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
855 &expt
, &signflag
, &dtoaend
);
857 fparg
.dbl
= GETARG(double);
859 __hdtoa(fparg
.dbl
, xdigs
, prec
,
860 &expt
, &signflag
, &dtoaend
);
870 if (prec
< 0) /* account for digit before decpt */
881 expchar
= ch
- ('g' - 'e');
887 if (dtoaresult
!= NULL
)
888 freedtoa(dtoaresult
);
889 if (flags
& LONGDBL
) {
890 fparg
.ldbl
= GETARG(long double);
892 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
893 &expt
, &signflag
, &dtoaend
);
895 fparg
.dbl
= GETARG(double);
897 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
898 &expt
, &signflag
, &dtoaend
);
905 if (expt
== INT_MAX
) { /* inf or nan */
907 cp
= (ch
>= 'a') ? "nan" : "NAN";
910 cp
= (ch
>= 'a') ? "inf" : "INF";
916 if (ch
== 'g' || ch
== 'G') {
917 if (expt
> -4 && expt
<= prec
) {
918 /* Make %[gG] smell like %[fF] */
928 * Make %[gG] smell like %[eE], but
929 * trim trailing zeroes if no # flag.
936 expsize
= exponent(expstr
, expt
- 1, expchar
);
937 size
= expsize
+ prec
;
938 if (prec
> 1 || flags
& ALT
)
941 /* space for digits before decimal point */
946 /* space for decimal pt and following digits */
947 if (prec
|| flags
& ALT
)
949 if (grouping
&& expt
> 0) {
950 /* space for thousands' grouping */
951 nseps
= nrepeats
= 0;
953 while (*grouping
!= CHAR_MAX
) {
954 if (lead
<= *grouping
)
963 size
+= nseps
+ nrepeats
;
968 #endif /* !NO_FLOATING_POINT */
971 * Assignment-like behavior is specified if the
972 * value overflows or is otherwise unrepresentable.
973 * C99 says to use `signed char' for %hhn conversions.
975 if (flags
& LLONGINT
)
976 *GETARG(long long *) = ret
;
977 else if (flags
& SIZET
)
978 *GETARG(ssize_t
*) = (ssize_t
)ret
;
979 else if (flags
& PTRDIFFT
)
980 *GETARG(ptrdiff_t *) = ret
;
981 else if (flags
& INTMAXT
)
982 *GETARG(intmax_t *) = ret
;
983 else if (flags
& LONGINT
)
984 *GETARG(long *) = ret
;
985 else if (flags
& SHORTINT
)
986 *GETARG(short *) = ret
;
987 else if (flags
& CHARINT
)
988 *GETARG(signed char *) = ret
;
990 *GETARG(int *) = ret
;
991 continue; /* no output */
996 if (flags
& INTMAX_SIZE
)
1004 * ``The argument shall be a pointer to void. The
1005 * value of the pointer is converted to a sequence
1006 * of printable characters, in an implementation-
1010 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
1012 xdigs
= xdigs_lower
;
1013 flags
= flags
| INTMAXT
;
1020 if (flags
& LONGINT
) {
1023 if (convbuf
!= NULL
)
1025 if ((wcp
= GETARG(wchar_t *)) == NULL
)
1028 convbuf
= __wcsconv(wcp
, prec
);
1029 if (convbuf
== NULL
) {
1030 fp
->_flags
|= __SERR
;
1035 } else if ((cp
= GETARG(char *)) == NULL
)
1039 * can't use strlen; can only look for the
1040 * NUL in the first `prec' characters, and
1041 * strlen() will go further.
1043 char *p
= memchr(cp
, 0, (size_t)prec
);
1059 if (flags
& INTMAX_SIZE
)
1066 xdigs
= xdigs_upper
;
1069 xdigs
= xdigs_lower
;
1071 if (flags
& INTMAX_SIZE
)
1076 /* leading 0x/X only if non-zero */
1078 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1082 /* unsigned conversions */
1083 nosign
: sign
= '\0';
1085 * ``... diouXx conversions ... if a precision is
1086 * specified, the 0 flag will be ignored.''
1089 number
: if ((dprec
= prec
) >= 0)
1093 * ``The result of converting a zero value with an
1094 * explicit precision of zero is no characters.''
1098 if (flags
& INTMAX_SIZE
) {
1099 if (ujval
!= 0 || prec
!= 0)
1100 cp
= __ujtoa(ujval
, cp
, base
,
1102 flags
& GROUPING
, thousands_sep
,
1105 if (ulval
!= 0 || prec
!= 0)
1106 cp
= __ultoa(ulval
, cp
, base
,
1108 flags
& GROUPING
, thousands_sep
,
1111 size
= buf
+ BUF
- cp
;
1112 if (size
> BUF
) /* should never happen */
1115 default: /* "%?" prints ?, unless ? is NUL */
1118 /* pretend it was %c with argument ch */
1127 * All reasonable formats wind up here. At this point, `cp'
1128 * points to a string which (if not flags&LADJUST) should be
1129 * padded out to `width' places. If flags&ZEROPAD, it should
1130 * first be prefixed by any sign or other prefix; otherwise,
1131 * it should be blank padded before the prefix is emitted.
1132 * After any left-hand padding and prefixing, emit zeroes
1133 * required by a decimal [diouxX] precision, then print the
1134 * string proper, then emit zeroes required by any leftover
1135 * floating precision; finally, if LADJUST, pad with blanks.
1137 * Compute actual size, so we know how much to pad.
1138 * size excludes decimal prec; realsz includes it.
1140 realsz
= dprec
> size
? dprec
: size
;
1146 prsize
= width
> realsz
? width
: realsz
;
1147 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1152 /* right-adjusting blank padding */
1153 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1154 PAD(width
- realsz
, blanks
);
1160 if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1165 /* right-adjusting zero padding */
1166 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1167 PAD(width
- realsz
, zeroes
);
1169 /* leading zeroes from decimal precision */
1170 PAD(dprec
- size
, zeroes
);
1172 /* the string or number proper */
1173 #ifndef NO_FLOATING_POINT
1174 if ((flags
& FPT
) == 0) {
1176 } else { /* glue together f_p fragments */
1177 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1180 if (prec
|| flags
& ALT
)
1181 PRINT(decimal_point
, 1);
1183 /* already handled initial 0's */
1186 PRINTANDPAD(cp
, dtoaend
, lead
, zeroes
);
1189 while (nseps
>0 || nrepeats
>0) {
1196 PRINT(&thousands_sep
,
1198 PRINTANDPAD(cp
,dtoaend
,
1205 if (prec
|| flags
& ALT
)
1206 PRINT(decimal_point
,1);
1208 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1209 } else { /* %[eE] or sufficiently long %[gG] */
1210 if (prec
> 1 || flags
& ALT
) {
1212 buf
[1] = *decimal_point
;
1215 PAD(prec
- ndig
, zeroes
);
1218 PRINT(expstr
, expsize
);
1224 /* left-adjusting padding (always blank) */
1225 if (flags
& LADJUST
)
1226 PAD(width
- realsz
, blanks
);
1228 /* finally, adjust ret */
1231 FLUSH(); /* copy out the I/O vectors */
1236 #ifndef NO_FLOATING_POINT
1237 if (dtoaresult
!= NULL
)
1238 freedtoa(dtoaresult
);
1240 if (convbuf
!= NULL
)
1244 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1251 * Find all arguments when a positional parameter is encountered. Returns a
1252 * table, indexed by argument number, of pointers to each arguments. The
1253 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1254 * It will be replaces with a malloc-ed one if it overflows.
1257 __find_arguments (const char *fmt0
, va_list ap
, union arg
**argtable
)
1259 char *fmt
; /* format string */
1260 int ch
; /* character from fmt */
1261 int n
, n2
; /* handy integer (short term usage) */
1262 char *cp
; /* handy char pointer (short term usage) */
1263 int flags
; /* flags as above */
1264 int width
; /* width from format (%8d), or 0 */
1265 enum typeid *typetable
; /* table of types */
1266 enum typeid stattypetable
[STATIC_ARG_TBL_SIZE
];
1267 int tablesize
; /* current size of type table */
1268 int tablemax
; /* largest used index in table */
1269 int nextarg
; /* 1-based argument index */
1272 * Add an argument type to the table, expanding if necessary.
1274 #define ADDTYPE(type) \
1275 ((nextarg >= tablesize) ? \
1276 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1277 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1278 typetable[nextarg++] = type)
1281 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1282 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1283 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1284 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1285 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1288 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1289 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1290 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1291 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1292 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1295 * Add * arguments to the type array.
1297 #define ADDASTER() \
1300 while (is_digit(*cp)) { \
1301 n2 = 10 * n2 + to_digit(*cp); \
1305 int hold = nextarg; \
1314 typetable
= stattypetable
;
1315 tablesize
= STATIC_ARG_TBL_SIZE
;
1318 for (n
= 0; n
< STATIC_ARG_TBL_SIZE
; n
++)
1319 typetable
[n
] = T_UNUSED
;
1322 * Scan the format for conversions (`%' character).
1325 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
1329 fmt
++; /* skip over '%' */
1335 reswitch
: switch (ch
) {
1347 if ((ch
= *fmt
++) == '*') {
1351 while (is_digit(ch
)) {
1357 case '1': case '2': case '3': case '4':
1358 case '5': case '6': case '7': case '8': case '9':
1361 n
= 10 * n
+ to_digit(ch
);
1363 } while (is_digit(ch
));
1370 #ifndef NO_FLOATING_POINT
1376 if (flags
& SHORTINT
) {
1386 if (flags
& LONGINT
) {
1393 flags
|= LLONGINT
; /* not necessarily */
1405 if (flags
& LONGINT
)
1417 #ifndef NO_FLOATING_POINT
1425 if (flags
& LONGDBL
)
1426 ADDTYPE(T_LONG_DOUBLE
);
1430 #endif /* !NO_FLOATING_POINT */
1432 if (flags
& INTMAXT
)
1433 ADDTYPE(TP_INTMAXT
);
1434 else if (flags
& PTRDIFFT
)
1435 ADDTYPE(TP_PTRDIFFT
);
1436 else if (flags
& SIZET
)
1438 else if (flags
& LLONGINT
)
1440 else if (flags
& LONGINT
)
1442 else if (flags
& SHORTINT
)
1444 else if (flags
& CHARINT
)
1448 continue; /* no output */
1462 if (flags
& LONGINT
)
1475 default: /* "%?" prints ?, unless ? is NUL */
1483 * Build the argument table.
1485 if (tablemax
>= STATIC_ARG_TBL_SIZE
) {
1486 *argtable
= (union arg
*)
1487 malloc (sizeof (union arg
) * (tablemax
+ 1));
1490 (*argtable
) [0].intarg
= 0;
1491 for (n
= 1; n
<= tablemax
; n
++) {
1492 switch (typetable
[n
]) {
1493 case T_UNUSED
: /* whoops! */
1494 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1497 (*argtable
) [n
].pschararg
= va_arg (ap
, signed char *);
1500 (*argtable
) [n
].pshortarg
= va_arg (ap
, short *);
1503 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1506 (*argtable
) [n
].uintarg
= va_arg (ap
, unsigned int);
1509 (*argtable
) [n
].pintarg
= va_arg (ap
, int *);
1512 (*argtable
) [n
].longarg
= va_arg (ap
, long);
1515 (*argtable
) [n
].ulongarg
= va_arg (ap
, unsigned long);
1518 (*argtable
) [n
].plongarg
= va_arg (ap
, long *);
1521 (*argtable
) [n
].longlongarg
= va_arg (ap
, long long);
1524 (*argtable
) [n
].ulonglongarg
= va_arg (ap
, unsigned long long);
1527 (*argtable
) [n
].plonglongarg
= va_arg (ap
, long long *);
1530 (*argtable
) [n
].ptrdiffarg
= va_arg (ap
, ptrdiff_t);
1533 (*argtable
) [n
].pptrdiffarg
= va_arg (ap
, ptrdiff_t *);
1536 (*argtable
) [n
].sizearg
= va_arg (ap
, size_t);
1539 (*argtable
) [n
].psizearg
= va_arg (ap
, ssize_t
*);
1542 (*argtable
) [n
].intmaxarg
= va_arg (ap
, intmax_t);
1545 (*argtable
) [n
].uintmaxarg
= va_arg (ap
, uintmax_t);
1548 (*argtable
) [n
].pintmaxarg
= va_arg (ap
, intmax_t *);
1550 #ifndef NO_FLOATING_POINT
1552 (*argtable
) [n
].doublearg
= va_arg (ap
, double);
1555 (*argtable
) [n
].longdoublearg
= va_arg (ap
, long double);
1559 (*argtable
) [n
].pchararg
= va_arg (ap
, char *);
1562 (*argtable
) [n
].pvoidarg
= va_arg (ap
, void *);
1565 (*argtable
) [n
].wintarg
= va_arg (ap
, wint_t);
1568 (*argtable
) [n
].pwchararg
= va_arg (ap
, wchar_t *);
1573 if ((typetable
!= NULL
) && (typetable
!= stattypetable
))
1578 * Increase the size of the type table.
1581 __grow_type_table (int nextarg
, enum typeid **typetable
, int *tablesize
)
1583 enum typeid *const oldtable
= *typetable
;
1584 const int oldsize
= *tablesize
;
1585 enum typeid *newtable
;
1586 int n
, newsize
= oldsize
* 2;
1588 if (newsize
< nextarg
+ 1)
1589 newsize
= nextarg
+ 1;
1590 if (oldsize
== STATIC_ARG_TBL_SIZE
) {
1591 if ((newtable
= malloc(newsize
* sizeof(enum typeid))) == NULL
)
1592 abort(); /* XXX handle better */
1593 bcopy(oldtable
, newtable
, oldsize
* sizeof(enum typeid));
1595 newtable
= reallocf(oldtable
, newsize
* sizeof(enum typeid));
1596 if (newtable
== NULL
)
1597 abort(); /* XXX handle better */
1599 for (n
= oldsize
; n
< newsize
; n
++)
1600 newtable
[n
] = T_UNUSED
;
1602 *typetable
= newtable
;
1603 *tablesize
= newsize
;
1607 #ifndef NO_FLOATING_POINT
1610 exponent(char *p0
, int exp
, int fmtch
)
1613 char expbuf
[MAXEXPDIG
];
1623 t
= expbuf
+ MAXEXPDIG
;
1626 *--t
= to_char(exp
% 10);
1627 } while ((exp
/= 10) > 9);
1628 *--t
= to_char(exp
);
1629 for (; t
< expbuf
+ MAXEXPDIG
; *p
++ = *t
++);
1633 * Exponents for decimal floating point conversions
1634 * (%[eEgG]) must be at least two characters long,
1635 * whereas exponents for hexadecimal conversions can
1636 * be only one character long.
1638 if (fmtch
== 'e' || fmtch
== 'E')
1640 *p
++ = to_char(exp
);
1644 #endif /* !NO_FLOATING_POINT */