]>
git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/vfprintf.c
e61530111ddc10277327b4d76558fa1e91165822
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.59 2003/04/19 23:53:19 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"
69 /* Define FLOATING_POINT to get floating point. */
70 #define FLOATING_POINT
77 long long longlongarg
;
78 unsigned long long ulonglongarg
;
85 signed char *pschararg
;
89 long long *plonglongarg
;
90 ptrdiff_t *pptrdiffarg
;
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 __sprint(FILE *, struct __suio
*);
113 static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0);
114 static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char,
116 static char *__ultoa(u_long
, char *, int, int, const char *, int, char,
118 static char *__wcsconv(wchar_t *, int);
119 static void __find_arguments(const char *, va_list, union arg
**);
120 static void __grow_type_table(int, enum typeid **, int *);
123 * Flush out all the vectors defined by the given uio,
124 * then reset it so that it can be reused.
127 __sprint(FILE *fp
, struct __suio
*uio
)
131 if (uio
->uio_resid
== 0) {
135 err
= __sfvwrite(fp
, uio
);
142 * Helper function for `fprintf to unbuffered unix file': creates a
143 * temporary buffer. We only work on write-only files; this avoids
144 * worries about ungetc buffers and so forth.
147 __sbprintf(FILE *fp
, const char *fmt
, va_list ap
)
151 unsigned char buf
[BUFSIZ
];
153 /* copy the important variables */
154 fake
._flags
= fp
->_flags
& ~__SNBF
;
155 fake
._file
= fp
->_file
;
156 fake
._cookie
= fp
->_cookie
;
157 fake
._write
= fp
->_write
;
158 fake
._extra
= fp
->_extra
;
160 /* set up the buffer */
161 fake
._bf
._base
= fake
._p
= buf
;
162 fake
._bf
._size
= fake
._w
= sizeof(buf
);
163 fake
._lbfsize
= 0; /* not actually used, but Just In Case */
165 /* do the work, then copy any error status */
166 ret
= __vfprintf(&fake
, fmt
, ap
);
167 if (ret
>= 0 && __fflush(&fake
))
169 if (fake
._flags
& __SERR
)
170 fp
->_flags
|= __SERR
;
175 * Macros for converting digits to letters and vice versa
177 #define to_digit(c) ((c) - '0')
178 #define is_digit(c) ((unsigned)to_digit(c) <= 9)
179 #define to_char(n) ((n) + '0')
182 * Convert an unsigned long to ASCII for printf purposes, returning
183 * a pointer to the first character of the string representation.
184 * Octal numbers can be forced to have a leading zero; hex numbers
185 * use the given digits.
188 __ultoa(u_long val
, char *endp
, int base
, int octzero
, const char *xdigs
,
189 int needgrp
, char thousep
, const char *grp
)
196 * Handle the three cases separately, in the hope of getting
197 * better/faster code.
201 if (val
< 10) { /* many numbers are 1 digit */
202 *--cp
= to_char(val
);
207 * On many machines, unsigned arithmetic is harder than
208 * signed arithmetic, so we do at most one unsigned mod and
209 * divide; this is sufficient to reduce the range of
210 * the incoming value to where signed arithmetic works.
212 if (val
> LONG_MAX
) {
213 *--cp
= to_char(val
% 10);
219 *--cp
= to_char(sval
% 10);
222 * If (*grp == CHAR_MAX) then no more grouping
223 * should be performed.
225 if (needgrp
&& ndig
== *grp
&& *grp
!= CHAR_MAX
230 * If (*(grp+1) == '\0') then we have to
231 * use *grp character (last grouping rule)
234 if (*(grp
+1) != '\0')
243 *--cp
= to_char(val
& 7);
246 if (octzero
&& *cp
!= '0')
252 *--cp
= xdigs
[val
& 15];
263 /* Identical to __ultoa, but for intmax_t. */
265 __ujtoa(uintmax_t val
, char *endp
, int base
, int octzero
, const char *xdigs
,
266 int needgrp
, char thousep
, const char *grp
)
272 /* quick test for small values; __ultoa is typically much faster */
273 /* (perhaps instead we should run until small, then call __ultoa?) */
274 if (val
<= ULONG_MAX
)
275 return (__ultoa((u_long
)val
, endp
, base
, octzero
, xdigs
,
276 needgrp
, thousep
, grp
));
280 *--cp
= to_char(val
% 10);
284 if (val
> INTMAX_MAX
) {
285 *--cp
= to_char(val
% 10);
291 *--cp
= to_char(sval
% 10);
294 * If (*grp == CHAR_MAX) then no more grouping
295 * should be performed.
297 if (needgrp
&& *grp
!= CHAR_MAX
&& ndig
== *grp
302 * If (*(grp+1) == '\0') then we have to
303 * use *grp character (last grouping rule)
306 if (*(grp
+1) != '\0')
315 *--cp
= to_char(val
& 7);
318 if (octzero
&& *cp
!= '0')
324 *--cp
= xdigs
[val
& 15];
336 * Convert a wide character string argument for the %ls format to a multibyte
337 * string representation. ``prec'' specifies the maximum number of bytes
338 * to output. If ``prec'' is greater than or equal to zero, we can't assume
339 * that the wide char. string ends in a null character.
342 __wcsconv(wchar_t *wcsarg
, int prec
)
344 char buf
[MB_LEN_MAX
];
351 * Determine the number of bytes to output and allocate space for
354 memset(&mbs
, 0, sizeof(mbs
));
359 clen
= wcrtomb(buf
, *p
++, &mbs
);
360 if (clen
== 0 || clen
== (size_t)-1 ||
361 nbytes
+ clen
> prec
)
367 nbytes
= wcsrtombs(NULL
, (const wchar_t **)&p
, 0, &mbs
);
368 if (nbytes
== (size_t)-1)
371 if ((convbuf
= malloc(nbytes
+ 1)) == NULL
)
375 * Fill the output buffer with the multibyte representations of as
376 * many wide characters as will fit.
380 memset(&mbs
, 0, sizeof(mbs
));
381 while (mbp
- convbuf
< nbytes
) {
382 clen
= wcrtomb(mbp
, *p
++, &mbs
);
383 if (clen
== 0 || clen
== (size_t)-1)
387 if (clen
== (size_t)-1) {
400 vfprintf(FILE * __restrict fp
, const char * __restrict fmt0
, va_list ap
)
406 ret
= __vfprintf(fp
, fmt0
, ap
);
411 #ifdef FLOATING_POINT
414 #define freedtoa __freedtoa
423 static int exponent(char *, int, int);
425 #endif /* FLOATING_POINT */
428 * The size of the buffer we use as scratch space for integer
429 * conversions, among other things. Technically, we would need the
430 * most space for base 10 conversions with thousands' grouping
431 * characters between each pair of digits. 100 bytes is a
432 * conservative overestimate even for a 128-bit uintmax_t.
436 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
439 * Flags used during conversion.
441 #define ALT 0x001 /* alternate form */
442 #define LADJUST 0x004 /* left adjustment */
443 #define LONGDBL 0x008 /* long double */
444 #define LONGINT 0x010 /* long integer */
445 #define LLONGINT 0x020 /* long long integer */
446 #define SHORTINT 0x040 /* short integer */
447 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
448 #define FPT 0x100 /* Floating point number */
449 #define GROUPING 0x200 /* use grouping ("'" flag) */
450 /* C99 additional size modifiers: */
451 #define SIZET 0x400 /* size_t */
452 #define PTRDIFFT 0x800 /* ptrdiff_t */
453 #define INTMAXT 0x1000 /* intmax_t */
454 #define CHARINT 0x2000 /* print char using int format */
457 * Non-MT-safe version
460 __vfprintf(FILE *fp
, const char *fmt0
, va_list ap
)
462 char *fmt
; /* format string */
463 int ch
; /* character from fmt */
464 int n
, n2
; /* handy integer (short term usage) */
465 char *cp
; /* handy char pointer (short term usage) */
466 struct __siov
*iovp
; /* for PRINT macro */
467 int flags
; /* flags as above */
468 int ret
; /* return value accumulator */
469 int width
; /* width from format (%8d), or 0 */
470 int prec
; /* precision from format; <0 for N/A */
471 char sign
; /* sign prefix (' ', '+', '-', or \0) */
472 char thousands_sep
; /* locale specific thousands separator */
473 const char *grouping
; /* locale specific numeric grouping rules */
474 #ifdef FLOATING_POINT
476 * We can decompose the printed representation of floating
477 * point numbers into several parts, some of which may be empty:
479 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
482 * A: 'sign' holds this value if present; '\0' otherwise
483 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
484 * C: cp points to the string MMMNNN. Leading and trailing
485 * zeros are not in the string and must be added.
486 * D: expchar holds this character; '\0' if no exponent, e.g. %f
487 * F: at least two digits for decimal, at least one digit for hex
489 char *decimal_point
; /* locale specific decimal point */
490 int signflag
; /* true if float is negative */
491 union { /* floating point arguments %[aAeEfFgG] */
495 int expt
; /* integer value of exponent */
496 char expchar
; /* exponent character: [eEpP\0] */
497 char *dtoaend
; /* pointer to end of converted digits */
498 int expsize
; /* character count for expstr */
499 int lead
; /* sig figs before decimal or group sep */
500 int ndig
; /* actual number of digits returned by dtoa */
501 char expstr
[MAXEXPDIG
+2]; /* buffer for exponent string: e+ZZZ */
502 char *dtoaresult
; /* buffer allocated by dtoa */
503 int nseps
; /* number of group separators with ' */
504 int nrepeats
; /* number of repeats of the last group */
506 u_long ulval
; /* integer arguments %[diouxX] */
507 uintmax_t ujval
; /* %j, %ll, %q, %t, %z integers */
508 int base
; /* base for [diouxX] conversion */
509 int dprec
; /* a copy of prec if [diouxX], 0 otherwise */
510 int realsz
; /* field size expanded by dprec, sign, etc */
511 int size
; /* size of converted field or string */
512 int prsize
; /* max size of printed field */
513 const char *xdigs
; /* digits for %[xX] conversion */
515 struct __suio uio
; /* output information: summary */
516 struct __siov iov
[NIOV
];/* ... and individual io vectors */
517 char buf
[BUF
]; /* buffer with space for digits of uintmax_t */
518 char ox
[2]; /* space for 0x; ox[1] is either x, X, or \0 */
519 union arg
*argtable
; /* args, built due to positional arg */
520 union arg statargtable
[STATIC_ARG_TBL_SIZE
];
521 int nextarg
; /* 1-based argument index */
522 va_list orgap
; /* original argument pointer */
523 char *convbuf
; /* wide to multibyte conversion result */
526 * Choose PADSIZE to trade efficiency vs. size. If larger printf
527 * fields occur frequently, increase PADSIZE and make the initialisers
530 #define PADSIZE 16 /* pad chunk size */
531 static char blanks
[PADSIZE
] =
532 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
533 static char zeroes
[PADSIZE
] =
534 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
536 static const char xdigs_lower
[16] = "0123456789abcdef";
537 static const char xdigs_upper
[16] = "0123456789ABCDEF";
540 * BEWARE, these `goto error' on error, and PAD uses `n'.
542 #define PRINT(ptr, len) { \
543 iovp->iov_base = (ptr); \
544 iovp->iov_len = (len); \
545 uio.uio_resid += (len); \
547 if (++uio.uio_iovcnt >= NIOV) { \
548 if (__sprint(fp, &uio)) \
553 #define PAD(howmany, with) { \
554 if ((n = (howmany)) > 0) { \
555 while (n > PADSIZE) { \
556 PRINT(with, PADSIZE); \
562 #define PRINTANDPAD(p, ep, len, with) do { \
568 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
571 if (uio.uio_resid && __sprint(fp, &uio)) \
573 uio.uio_iovcnt = 0; \
578 * Get the argument indexed by nextarg. If the argument table is
579 * built, use it to get the argument. If its not, get the next
580 * argument (and arguments must be gotten sequentially).
582 #define GETARG(type) \
583 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
584 (nextarg++, va_arg(ap, type)))
587 * To extend shorts properly, we need both signed and unsigned
588 * argument extraction methods.
591 (flags&LONGINT ? GETARG(long) : \
592 flags&SHORTINT ? (long)(short)GETARG(int) : \
593 flags&CHARINT ? (long)(signed char)GETARG(int) : \
596 (flags&LONGINT ? GETARG(u_long) : \
597 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
598 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
599 (u_long)GETARG(u_int))
600 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT)
602 (flags&INTMAXT ? GETARG(intmax_t) : \
603 flags&SIZET ? (intmax_t)GETARG(size_t) : \
604 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
605 (intmax_t)GETARG(long long))
607 (flags&INTMAXT ? GETARG(uintmax_t) : \
608 flags&SIZET ? (uintmax_t)GETARG(size_t) : \
609 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
610 (uintmax_t)GETARG(unsigned long long))
613 * Get * arguments, including the form *nn$. Preserve the nextarg
614 * that the argument can be gotten once the type is determined.
616 #define GETASTER(val) \
619 while (is_digit(*cp)) { \
620 n2 = 10 * n2 + to_digit(*cp); \
624 int hold = nextarg; \
625 if (argtable == NULL) { \
626 argtable = statargtable; \
627 __find_arguments (fmt0, orgap, &argtable); \
630 val = GETARG (int); \
634 val = GETARG (int); \
638 thousands_sep
= '\0';
641 #ifdef FLOATING_POINT
643 decimal_point
= localeconv()->decimal_point
;
645 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
649 /* optimise fprintf(stderr) (and other unbuffered Unix files) */
650 if ((fp
->_flags
& (__SNBF
|__SWR
|__SRW
)) == (__SNBF
|__SWR
) &&
652 return (__sbprintf(fp
, fmt0
, ap
));
658 uio
.uio_iov
= iovp
= iov
;
664 * Scan the format for conversions (`%' character).
667 for (cp
= fmt
; (ch
= *fmt
) != '\0' && ch
!= '%'; fmt
++)
669 if ((n
= fmt
- cp
) != 0) {
670 if ((unsigned)ret
+ n
> INT_MAX
) {
679 fmt
++; /* skip over '%' */
689 reswitch
: switch (ch
) {
692 * ``If the space and + flags both appear, the space
693 * flag will be ignored.''
704 * ``A negative field width argument is taken as a
705 * - flag followed by a positive field width.''
707 * They don't exclude field widths read from args.
722 thousands_sep
= *(localeconv()->thousands_sep
);
723 grouping
= localeconv()->grouping
;
726 if ((ch
= *fmt
++) == '*') {
731 while (is_digit(ch
)) {
732 prec
= 10 * prec
+ to_digit(ch
);
738 * ``Note that 0 is taken as a flag, not as the
739 * beginning of a field width.''
744 case '1': case '2': case '3': case '4':
745 case '5': case '6': case '7': case '8': case '9':
748 n
= 10 * n
+ to_digit(ch
);
750 } while (is_digit(ch
));
753 if (argtable
== NULL
) {
754 argtable
= statargtable
;
755 __find_arguments (fmt0
, orgap
,
762 #ifdef FLOATING_POINT
768 if (flags
& SHORTINT
) {
778 if (flags
& LONGINT
) {
785 flags
|= LLONGINT
; /* not necessarily */
797 if (flags
& LONGINT
) {
801 memset(&mbs
, 0, sizeof(mbs
));
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 #ifdef FLOATING_POINT
849 * XXX We don't actually have a conversion
850 * XXX routine for this yet.
852 if (flags
& LONGDBL
) {
853 fparg
.ldbl
= (double)GETARG(long double);
855 __hldtoa(fparg
.ldbl
, xdigs
, prec
,
856 &expt
, &signflag
, &dtoaend
);
858 fparg
.dbl
= GETARG(double);
860 __hdtoa(fparg
.dbl
, xdigs
, prec
,
861 &expt
, &signflag
, &dtoaend
);
868 if (prec
< 0) /* account for digit before decpt */
879 expchar
= ch
- ('g' - 'e');
885 if (dtoaresult
!= NULL
)
886 freedtoa(dtoaresult
);
887 if (flags
& LONGDBL
) {
888 fparg
.ldbl
= GETARG(long double);
890 __ldtoa(&fparg
.ldbl
, expchar
? 2 : 3, prec
,
891 &expt
, &signflag
, &dtoaend
);
893 fparg
.dbl
= GETARG(double);
895 dtoa(fparg
.dbl
, expchar
? 2 : 3, prec
,
896 &expt
, &signflag
, &dtoaend
);
902 if (expt
== INT_MAX
) { /* inf or nan */
904 cp
= (ch
>= 'a') ? "nan" : "NAN";
907 cp
= (ch
>= 'a') ? "inf" : "INF";
913 if (ch
== 'g' || ch
== 'G') {
914 if (expt
> -4 && expt
<= prec
) {
915 /* Make %[gG] smell like %[fF] */
925 * Make %[gG] smell like %[eE], but
926 * trim trailing zeroes if no # flag.
933 expsize
= exponent(expstr
, expt
- 1, expchar
);
934 size
= expsize
+ prec
;
935 if (prec
> 1 || flags
& ALT
)
938 /* space for digits before decimal point */
943 /* space for decimal pt and following digits */
944 if (prec
|| flags
& ALT
)
946 if (grouping
&& expt
> 0) {
947 /* space for thousands' grouping */
948 nseps
= nrepeats
= 0;
950 while (*grouping
!= CHAR_MAX
) {
951 if (lead
<= *grouping
)
960 size
+= nseps
+ nrepeats
;
965 #endif /* FLOATING_POINT */
968 * Assignment-like behavior is specified if the
969 * value overflows or is otherwise unrepresentable.
970 * C99 says to use `signed char' for %hhn conversions.
972 if (flags
& LLONGINT
)
973 *GETARG(long long *) = ret
;
974 else if (flags
& SIZET
)
975 *GETARG(ssize_t
*) = (ssize_t
)ret
;
976 else if (flags
& PTRDIFFT
)
977 *GETARG(ptrdiff_t *) = ret
;
978 else if (flags
& INTMAXT
)
979 *GETARG(intmax_t *) = ret
;
980 else if (flags
& LONGINT
)
981 *GETARG(long *) = ret
;
982 else if (flags
& SHORTINT
)
983 *GETARG(short *) = ret
;
984 else if (flags
& CHARINT
)
985 *GETARG(signed char *) = ret
;
987 *GETARG(int *) = ret
;
988 continue; /* no output */
993 if (flags
& INTMAX_SIZE
)
1001 * ``The argument shall be a pointer to void. The
1002 * value of the pointer is converted to a sequence
1003 * of printable characters, in an implementation-
1007 ujval
= (uintmax_t)(uintptr_t)GETARG(void *);
1009 xdigs
= xdigs_lower
;
1010 flags
= flags
| INTMAXT
;
1017 if (flags
& LONGINT
) {
1020 if (convbuf
!= NULL
)
1022 if ((wcp
= GETARG(wchar_t *)) == NULL
)
1025 convbuf
= __wcsconv(wcp
, prec
);
1026 if (convbuf
== NULL
) {
1027 fp
->_flags
|= __SERR
;
1032 } else if ((cp
= GETARG(char *)) == NULL
)
1036 * can't use strlen; can only look for the
1037 * NUL in the first `prec' characters, and
1038 * strlen() will go further.
1040 char *p
= memchr(cp
, 0, (size_t)prec
);
1056 if (flags
& INTMAX_SIZE
)
1063 xdigs
= xdigs_upper
;
1066 xdigs
= xdigs_lower
;
1068 if (flags
& INTMAX_SIZE
)
1073 /* leading 0x/X only if non-zero */
1075 (flags
& INTMAX_SIZE
? ujval
!= 0 : ulval
!= 0))
1079 /* unsigned conversions */
1080 nosign
: sign
= '\0';
1082 * ``... diouXx conversions ... if a precision is
1083 * specified, the 0 flag will be ignored.''
1086 number
: if ((dprec
= prec
) >= 0)
1090 * ``The result of converting a zero value with an
1091 * explicit precision of zero is no characters.''
1095 if (flags
& INTMAX_SIZE
) {
1096 if (ujval
!= 0 || prec
!= 0)
1097 cp
= __ujtoa(ujval
, cp
, base
,
1099 flags
& GROUPING
, thousands_sep
,
1102 if (ulval
!= 0 || prec
!= 0)
1103 cp
= __ultoa(ulval
, cp
, base
,
1105 flags
& GROUPING
, thousands_sep
,
1108 size
= buf
+ BUF
- cp
;
1109 if (size
> BUF
) /* should never happen */
1112 default: /* "%?" prints ?, unless ? is NUL */
1115 /* pretend it was %c with argument ch */
1124 * All reasonable formats wind up here. At this point, `cp'
1125 * points to a string which (if not flags&LADJUST) should be
1126 * padded out to `width' places. If flags&ZEROPAD, it should
1127 * first be prefixed by any sign or other prefix; otherwise,
1128 * it should be blank padded before the prefix is emitted.
1129 * After any left-hand padding and prefixing, emit zeroes
1130 * required by a decimal [diouxX] precision, then print the
1131 * string proper, then emit zeroes required by any leftover
1132 * floating precision; finally, if LADJUST, pad with blanks.
1134 * Compute actual size, so we know how much to pad.
1135 * size excludes decimal prec; realsz includes it.
1137 realsz
= dprec
> size
? dprec
: size
;
1143 prsize
= width
> realsz
? width
: realsz
;
1144 if ((unsigned)ret
+ prsize
> INT_MAX
) {
1149 /* right-adjusting blank padding */
1150 if ((flags
& (LADJUST
|ZEROPAD
)) == 0)
1151 PAD(width
- realsz
, blanks
);
1156 } else if (ox
[1]) { /* ox[1] is either x, X, or \0 */
1161 /* right-adjusting zero padding */
1162 if ((flags
& (LADJUST
|ZEROPAD
)) == ZEROPAD
)
1163 PAD(width
- realsz
, zeroes
);
1165 /* leading zeroes from decimal precision */
1166 PAD(dprec
- size
, zeroes
);
1168 /* the string or number proper */
1169 #ifdef FLOATING_POINT
1170 if ((flags
& FPT
) == 0) {
1172 } else { /* glue together f_p fragments */
1173 if (!expchar
) { /* %[fF] or sufficiently short %[gG] */
1176 if (prec
|| flags
& ALT
)
1177 PRINT(decimal_point
, 1);
1179 /* already handled initial 0's */
1182 PRINTANDPAD(cp
, dtoaend
, lead
, zeroes
);
1185 while (nseps
>0 || nrepeats
>0) {
1192 PRINT(&thousands_sep
,
1194 PRINTANDPAD(cp
,dtoaend
,
1201 if (prec
|| flags
& ALT
)
1202 PRINT(decimal_point
,1);
1204 PRINTANDPAD(cp
, dtoaend
, prec
, zeroes
);
1205 } else { /* %[eE] or sufficiently long %[gG] */
1206 if (prec
> 1 || flags
& ALT
) {
1208 buf
[1] = *decimal_point
;
1211 PAD(prec
- ndig
, zeroes
);
1214 PRINT(expstr
, expsize
);
1220 /* left-adjusting padding (always blank) */
1221 if (flags
& LADJUST
)
1222 PAD(width
- realsz
, blanks
);
1224 /* finally, adjust ret */
1227 FLUSH(); /* copy out the I/O vectors */
1232 #ifdef FLOATING_POINT
1233 if (dtoaresult
!= NULL
)
1234 freedtoa(dtoaresult
);
1236 if (convbuf
!= NULL
)
1240 if ((argtable
!= NULL
) && (argtable
!= statargtable
))
1247 * Find all arguments when a positional parameter is encountered. Returns a
1248 * table, indexed by argument number, of pointers to each arguments. The
1249 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1250 * It will be replaces with a malloc-ed one if it overflows.
1253 __find_arguments (const char *fmt0
, va_list ap
, union arg
**argtable
)
1255 char *fmt
; /* format string */
1256 int ch
; /* character from fmt */
1257 int n
, n2
; /* handy integer (short term usage) */
1258 char *cp
; /* handy char pointer (short term usage) */
1259 int flags
; /* flags as above */
1260 int width
; /* width from format (%8d), or 0 */
1261 enum typeid *typetable
; /* table of types */
1262 enum typeid stattypetable
[STATIC_ARG_TBL_SIZE
];
1263 int tablesize
; /* current size of type table */
1264 int tablemax
; /* largest used index in table */
1265 int nextarg
; /* 1-based argument index */
1268 * Add an argument type to the table, expanding if necessary.
1270 #define ADDTYPE(type) \
1271 ((nextarg >= tablesize) ? \
1272 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \
1273 (nextarg > tablemax) ? tablemax = nextarg : 0, \
1274 typetable[nextarg++] = type)
1277 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \
1278 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1279 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1280 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
1281 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT))))))
1284 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \
1285 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \
1286 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \
1287 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
1288 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT))))))
1291 * Add * arguments to the type array.
1293 #define ADDASTER() \
1296 while (is_digit(*cp)) { \
1297 n2 = 10 * n2 + to_digit(*cp); \
1301 int hold = nextarg; \
1310 typetable
= stattypetable
;
1311 tablesize
= STATIC_ARG_TBL_SIZE
;
1314 memset (typetable
, T_UNUSED
, STATIC_ARG_TBL_SIZE
);
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 #ifdef FLOATING_POINT
1371 if (flags
& SHORTINT
) {
1381 if (flags
& LONGINT
) {
1388 flags
|= LLONGINT
; /* not necessarily */
1400 if (flags
& LONGINT
)
1412 #ifdef FLOATING_POINT
1422 if (flags
& LONGDBL
)
1423 ADDTYPE(T_LONG_DOUBLE
);
1427 #endif /* FLOATING_POINT */
1429 if (flags
& INTMAXT
)
1430 ADDTYPE(TP_INTMAXT
);
1431 else if (flags
& PTRDIFFT
)
1432 ADDTYPE(TP_PTRDIFFT
);
1433 else if (flags
& SIZET
)
1435 else if (flags
& LLONGINT
)
1437 else if (flags
& LONGINT
)
1439 else if (flags
& SHORTINT
)
1441 else if (flags
& CHARINT
)
1445 continue; /* no output */
1459 if (flags
& LONGINT
)
1472 default: /* "%?" prints ?, unless ? is NUL */
1480 * Build the argument table.
1482 if (tablemax
>= STATIC_ARG_TBL_SIZE
) {
1483 *argtable
= (union arg
*)
1484 malloc (sizeof (union arg
) * (tablemax
+ 1));
1487 (*argtable
) [0].intarg
= 0;
1488 for (n
= 1; n
<= tablemax
; n
++) {
1489 switch (typetable
[n
]) {
1490 case T_UNUSED
: /* whoops! */
1491 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1494 (*argtable
) [n
].pschararg
= va_arg (ap
, signed char *);
1497 (*argtable
) [n
].pshortarg
= va_arg (ap
, short *);
1500 (*argtable
) [n
].intarg
= va_arg (ap
, int);
1503 (*argtable
) [n
].uintarg
= va_arg (ap
, unsigned int);
1506 (*argtable
) [n
].pintarg
= va_arg (ap
, int *);
1509 (*argtable
) [n
].longarg
= va_arg (ap
, long);
1512 (*argtable
) [n
].ulongarg
= va_arg (ap
, unsigned long);
1515 (*argtable
) [n
].plongarg
= va_arg (ap
, long *);
1518 (*argtable
) [n
].longlongarg
= va_arg (ap
, long long);
1521 (*argtable
) [n
].ulonglongarg
= va_arg (ap
, unsigned long long);
1524 (*argtable
) [n
].plonglongarg
= va_arg (ap
, long long *);
1527 (*argtable
) [n
].ptrdiffarg
= va_arg (ap
, ptrdiff_t);
1530 (*argtable
) [n
].pptrdiffarg
= va_arg (ap
, ptrdiff_t *);
1533 (*argtable
) [n
].sizearg
= va_arg (ap
, size_t);
1536 (*argtable
) [n
].psizearg
= va_arg (ap
, ssize_t
*);
1539 (*argtable
) [n
].intmaxarg
= va_arg (ap
, intmax_t);
1542 (*argtable
) [n
].uintmaxarg
= va_arg (ap
, uintmax_t);
1545 (*argtable
) [n
].pintmaxarg
= va_arg (ap
, intmax_t *);
1547 #ifdef FLOATING_POINT
1549 (*argtable
) [n
].doublearg
= va_arg (ap
, double);
1552 (*argtable
) [n
].longdoublearg
= va_arg (ap
, long double);
1556 (*argtable
) [n
].pchararg
= va_arg (ap
, char *);
1559 (*argtable
) [n
].pvoidarg
= va_arg (ap
, void *);
1562 (*argtable
) [n
].wintarg
= va_arg (ap
, wint_t);
1565 (*argtable
) [n
].pwchararg
= va_arg (ap
, wchar_t *);
1570 if ((typetable
!= NULL
) && (typetable
!= stattypetable
))
1575 * Increase the size of the type table.
1578 __grow_type_table (int nextarg
, enum typeid **typetable
, int *tablesize
)
1580 enum typeid *const oldtable
= *typetable
;
1581 const int oldsize
= *tablesize
;
1582 enum typeid *newtable
;
1583 int newsize
= oldsize
* 2;
1585 if (newsize
< nextarg
+ 1)
1586 newsize
= nextarg
+ 1;
1587 if (oldsize
== STATIC_ARG_TBL_SIZE
) {
1588 if ((newtable
= malloc(newsize
)) == NULL
)
1589 abort(); /* XXX handle better */
1590 bcopy(oldtable
, newtable
, oldsize
);
1592 if ((newtable
= reallocf(oldtable
, newsize
)) == NULL
)
1593 abort(); /* XXX handle better */
1595 memset(&newtable
[oldsize
], T_UNUSED
, newsize
- oldsize
);
1597 *typetable
= newtable
;
1598 *tablesize
= newsize
;
1602 #ifdef FLOATING_POINT
1605 exponent(char *p0
, int exp
, int fmtch
)
1608 char 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 /* FLOATING_POINT */