Deleted Added
sdiff udiff text old ( 113142 ) new ( 113146 )
full compact
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Chris Torek.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 24 unchanged lines hidden (view full) ---

33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#if defined(LIBC_SCCS) && !defined(lint)
38static 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: head/lib/libc/stdio/vfprintf.c 113146 2003-04-05 22:11:42Z das $");
42
43/*
44 * Actual printf innards.
45 *
46 * This code is large and complicated...
47 */
48
49#include "namespace.h"

--- 56 unchanged lines hidden (view full) ---

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
110};
111
112static int __sprint(FILE *, struct __suio *);
113static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0);
114static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char,
115 const char *);
116static char *__ultoa(u_long, char *, int, int, const char *, int, char,
117 const char *);
118static char *__wcsconv(wchar_t *, int);
119static void __find_arguments(const char *, va_list, union arg **);
120static void __grow_type_table(int, enum typeid **, int *);
121
122/*
123 * Flush out all the vectors defined by the given uio,
124 * then reset it so that it can be reused.

--- 55 unchanged lines hidden (view full) ---

180
181/*
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.
186 */
187static char *
188__ultoa(u_long val, char *endp, int base, int octzero, const char *xdigs,
189 int needgrp, char thousep, const char *grp)
190{
191 char *cp = endp;
192 long sval;
193 int ndig;
194
195 /*
196 * Handle the three cases separately, in the hope of getting

--- 60 unchanged lines hidden (view full) ---

257 default: /* oops */
258 abort();
259 }
260 return (cp);
261}
262
263/* Identical to __ultoa, but for intmax_t. */
264static char *
265__ujtoa(uintmax_t val, char *endp, int base, int octzero, const char *xdigs,
266 int needgrp, char thousep, const char *grp)
267{
268 char *cp = endp;
269 intmax_t sval;
270 int ndig;
271
272 /* quick test for small values; __ultoa is typically much faster */
273 /* (perhaps instead we should run until small, then call __ultoa?) */

--- 128 unchanged lines hidden (view full) ---

402
403 FLOCKFILE(fp);
404 ret = __vfprintf(fp, fmt0, ap);
405 FUNLOCKFILE(fp);
406 return (ret);
407}
408
409#ifdef FLOATING_POINT
410
411#define dtoa __dtoa
412#define freedtoa __freedtoa
413
414#include <float.h>
415#include <math.h>
416#include "floatio.h"
417#include "gdtoa.h"
418
419#define DEFPREC 6
420
421static int exponent(char *, int, int);
422
423#endif /* FLOATING_POINT */
424
425/*
426 * The size of the buffer we use as scratch space for integer
427 * conversions, among other things. Technically, we would need the
428 * most space for base 10 conversions with thousands' grouping
429 * characters between each pair of digits. 100 bytes is a
430 * conservative overestimate even for a 128-bit uintmax_t.
431 */
432#define BUF 100
433
434#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
435
436/*
437 * Flags used during conversion.
438 */
439#define ALT 0x001 /* alternate form */
440#define LADJUST 0x004 /* left adjustment */
441#define LONGDBL 0x008 /* long double */
442#define LONGINT 0x010 /* long integer */
443#define LLONGINT 0x020 /* long long integer */
444#define SHORTINT 0x040 /* short integer */
445#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */
446#define FPT 0x100 /* Floating point number */
447#define GROUPING 0x200 /* use grouping ("'" flag) */

--- 12 unchanged lines hidden (view full) ---

460 char *fmt; /* format string */
461 int ch; /* character from fmt */
462 int n, n2; /* handy integer (short term usage) */
463 char *cp; /* handy char pointer (short term usage) */
464 struct __siov *iovp; /* for PRINT macro */
465 int flags; /* flags as above */
466 int ret; /* return value accumulator */
467 int width; /* width from format (%8d), or 0 */
468 int prec; /* precision from format; <0 for N/A */
469 char sign; /* sign prefix (' ', '+', '-', or \0) */
470 char thousands_sep; /* locale specific thousands separator */
471 const char *grouping; /* locale specific numeric grouping rules */
472#ifdef FLOATING_POINT
473 /*
474 * We can decompose the printed representation of floating
475 * point numbers into several parts, some of which may be empty:
476 *
477 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
478 * A B ---C--- D E F
479 *
480 * A: 'sign' holds this value if present; '\0' otherwise
481 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
482 * C: cp points to the string MMMNNN. Leading and trailing
483 * zeros are not in the string and must be added.
484 * D: expchar holds this character; '\0' if no exponent, e.g. %f
485 * F: at least two digits for decimal, at least one digit for hex
486 */
487 char *decimal_point; /* locale specific decimal point */
488 int signflag; /* true if float is negative */
489 union { /* floating point arguments %[aAeEfFgG] */
490 double dbl;
491 long double ldbl;
492 } fparg;
493 int expt; /* integer value of exponent */
494 char expchar; /* exponent character: [eEpP\0] */
495 char *dtoaend; /* pointer to end of converted digits */
496 int expsize; /* character count for expstr */
497 int lead; /* sig figs before decimal or group sep */
498 int ndig; /* actual number of digits returned by dtoa */
499 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
500 char *dtoaresult; /* buffer allocated by dtoa */
501 int nseps; /* number of group separators with ' */
502 int nrepeats; /* number of repeats of the last group */
503#endif
504 u_long ulval; /* integer arguments %[diouxX] */
505 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */
506 int base; /* base for [diouxX] conversion */
507 int dprec; /* a copy of prec if [diouxX], 0 otherwise */
508 int realsz; /* field size expanded by dprec, sign, etc */
509 int size; /* size of converted field or string */
510 int prsize; /* max size of printed field */
511 const char *xdigs; /* digits for %[xX] conversion */
512#define NIOV 8
513 struct __suio uio; /* output information: summary */
514 struct __siov iov[NIOV];/* ... and individual io vectors */
515 char buf[BUF]; /* buffer with space for digits of uintmax_t */
516 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
517 union arg *argtable; /* args, built due to positional arg */
518 union arg statargtable [STATIC_ARG_TBL_SIZE];
519 int nextarg; /* 1-based argument index */
520 va_list orgap; /* original argument pointer */
521 char *convbuf; /* wide to multibyte conversion result */
522
523 /*
524 * Choose PADSIZE to trade efficiency vs. size. If larger printf
525 * fields occur frequently, increase PADSIZE and make the initialisers
526 * below longer.
527 */
528#define PADSIZE 16 /* pad chunk size */
529 static char blanks[PADSIZE] =
530 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
531 static char zeroes[PADSIZE] =
532 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
533
534 static const char xdigs_lower[16] = "0123456789abcdef";
535 static const char xdigs_upper[16] = "0123456789ABCDEF";
536
537 /*
538 * BEWARE, these `goto error' on error, and PAD uses `n'.
539 */
540#define PRINT(ptr, len) { \
541 iovp->iov_base = (ptr); \
542 iovp->iov_len = (len); \
543 uio.uio_resid += (len); \
544 iovp++; \

--- 123 unchanged lines hidden (view full) ---

668 goto done;
669 fmt++; /* skip over '%' */
670
671 flags = 0;
672 dprec = 0;
673 width = 0;
674 prec = -1;
675 sign = '\0';
676 ox[1] = '\0';
677
678rflag: ch = *fmt++;
679reswitch: switch (ch) {
680 case ' ':
681 /*-
682 * ``If the space and + flags both appear, the space
683 * flag will be ignored.''
684 * -- ANSI X3J11

--- 138 unchanged lines hidden (view full) ---

823 }
824 }
825 base = 10;
826 goto number;
827#ifdef FLOATING_POINT
828#ifdef HEXFLOAT
829 case 'a':
830 case 'A':
831 if (ch == 'a') {
832 ox[1] = 'x';
833 xdigs = xdigs_lower;
834 expchar = 'p';
835 } else {
836 ox[1] = 'X';
837 xdigs = xdigs_upper;
838 expchar = 'P';
839 }
840 /*
841 * XXX We don't actually have a conversion
842 * XXX routine for this yet.
843 */
844 if (flags & LONGDBL) {
845 fparg.ldbl = (double)GETARG(long double);
846 dtoaresult = cp =
847 __hldtoa(fparg.ldbl, xdigs, prec,
848 &expt, &signflag, &dtoaend);
849 } else {
850 fparg.dbl = GETARG(double);
851 dtoaresult = cp =
852 __hdtoa(fparg.dbl, xdigs, prec,
853 &expt, &signflag, &dtoaend);
854 }
855 goto fp_begin;
856#endif
857 case 'e':
858 case 'E':
859 expchar = ch;
860 if (prec < 0) /* account for digit before decpt */
861 prec = DEFPREC + 1;
862 else
863 prec++;
864 goto fp_begin;
865 case 'f':
866 case 'F':
867 expchar = '\0';
868 goto fp_begin;
869 case 'g':
870 case 'G':
871 expchar = ch - ('g' - 'e');
872 if (prec == 0)
873 prec = 1;
874fp_begin:
875 if (prec < 0)
876 prec = DEFPREC;
877 if (dtoaresult != NULL)
878 freedtoa(dtoaresult);
879 if (flags & LONGDBL) {
880 fparg.ldbl = GETARG(long double);
881 dtoaresult = cp =
882 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
883 &expt, &signflag, &dtoaend);
884 } else {
885 fparg.dbl = GETARG(double);
886 dtoaresult = cp =
887 dtoa(fparg.dbl, expchar ? 2 : 3, prec,
888 &expt, &signflag, &dtoaend);
889 if (expt == 9999)
890 expt = INT_MAX;
891 }
892 if (signflag)
893 sign = '-';
894 if (expt == INT_MAX) { /* inf or nan */
895 if (*cp == 'N') {
896 cp = (ch >= 'a') ? "nan" : "NAN";
897 sign = '\0';
898 } else
899 cp = (ch >= 'a') ? "inf" : "INF";
900 size = 3;
901 break;
902 }
903 flags |= FPT;
904 ndig = dtoaend - cp;
905 if (ch == 'g' || ch == 'G') {
906 if (expt > -4 && expt <= prec) {
907 /* Make %[gG] smell like %[fF] */
908 expchar = '\0';
909 if (flags & ALT)
910 prec -= expt;
911 else
912 prec = ndig - expt;
913 if (prec < 0)
914 prec = 0;
915 }
916 }
917 if (expchar) {
918 expsize = exponent(expstr, expt - 1, expchar);
919 size = expsize + prec;
920 if (prec || flags & ALT)
921 ++size;
922 } else {
923 if (expt > 0) {
924 size = expt;
925 if (prec || flags & ALT)
926 size += prec + 1;
927 } else /* "0.X" */
928 size = prec + 2;
929 if (grouping && expt > 0) {
930 /* space for thousands' grouping */
931 nseps = nrepeats = 0;
932 lead = expt;
933 while (*grouping != CHAR_MAX) {
934 if (lead <= *grouping)
935 break;
936 lead -= *grouping;
937 if (*(grouping+1)) {
938 nseps++;
939 grouping++;
940 } else
941 nrepeats++;
942 }
943 size += nseps + nrepeats;
944 } else
945 lead = (expt < ndig) ? expt : ndig;
946 }
947 break;
948#endif /* FLOATING_POINT */
949 case 'n':
950 /*
951 * Assignment-like behavior is specified if the
952 * value overflows or is otherwise unrepresentable.
953 * C99 says to use `signed char' for %hhn conversions.
954 */

--- 29 unchanged lines hidden (view full) ---

984 * ``The argument shall be a pointer to void. The
985 * value of the pointer is converted to a sequence
986 * of printable characters, in an implementation-
987 * defined manner.''
988 * -- ANSI X3J11
989 */
990 ujval = (uintmax_t)(uintptr_t)GETARG(void *);
991 base = 16;
992 xdigs = xdigs_lower;
993 flags = flags | INTMAXT;
994 ox[1] = 'x';
995 goto nosign;
996 case 'S':
997 flags |= LONGINT;
998 /*FALLTHROUGH*/
999 case 's':
1000 if (flags & LONGINT) {
1001 wchar_t *wcp;
1002

--- 35 unchanged lines hidden (view full) ---

1038 case 'u':
1039 if (flags & INTMAX_SIZE)
1040 ujval = UJARG();
1041 else
1042 ulval = UARG();
1043 base = 10;
1044 goto nosign;
1045 case 'X':
1046 xdigs = xdigs_upper;
1047 goto hex;
1048 case 'x':
1049 xdigs = xdigs_lower;
1050hex:
1051 if (flags & INTMAX_SIZE)
1052 ujval = UJARG();
1053 else
1054 ulval = UARG();
1055 base = 16;
1056 /* leading 0x/X only if non-zero */
1057 if (flags & ALT &&
1058 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1059 ox[1] = ch;
1060
1061 flags &= ~GROUPING;
1062 /* unsigned conversions */
1063nosign: sign = '\0';
1064 /*-
1065 * ``... diouXx conversions ... if a precision is
1066 * specified, the 0 flag will be ignored.''
1067 * -- ANSI X3J11

--- 47 unchanged lines hidden (view full) ---

1115 * floating precision; finally, if LADJUST, pad with blanks.
1116 *
1117 * Compute actual size, so we know how much to pad.
1118 * size excludes decimal prec; realsz includes it.
1119 */
1120 realsz = dprec > size ? dprec : size;
1121 if (sign)
1122 realsz++;
1123 else if (ox[1])
1124 realsz += 2;
1125
1126 prsize = width > realsz ? width : realsz;
1127 if ((unsigned)ret + prsize > INT_MAX) {
1128 ret = EOF;
1129 goto error;
1130 }
1131
1132 /* right-adjusting blank padding */
1133 if ((flags & (LADJUST|ZEROPAD)) == 0)
1134 PAD(width - realsz, blanks);
1135
1136 /* prefix */
1137 if (sign) {
1138 PRINT(&sign, 1);
1139 } else if (ox[1]) { /* ox[1] is either x, X, or \0 */
1140 ox[0] = '0';
1141 PRINT(ox, 2);
1142 }
1143
1144 /* right-adjusting zero padding */
1145 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1146 PAD(width - realsz, zeroes);
1147
1148 /* leading zeroes from decimal precision */
1149 PAD(dprec - size, zeroes);
1150
1151 /* the string or number proper */
1152#ifdef FLOATING_POINT
1153 if ((flags & FPT) == 0) {
1154 PRINT(cp, size);
1155 } else { /* glue together f_p fragments */
1156 if (!expchar) { /* %[fF] or sufficiently short %[gG] */
1157 if (expt <= 0) {
1158 buf[0] = '0';
1159 buf[1] = *decimal_point;
1160 PRINT(buf, 2);
1161 PAD(-expt, zeroes);
1162 if (ndig > 0)
1163 PRINT(cp, ndig);
1164 } else {
1165 PRINT(cp, lead);
1166 cp += lead;
1167 if (grouping) {
1168 while (nseps>0 || nrepeats>0) {
1169 if (nrepeats > 0)
1170 nrepeats--;
1171 else {
1172 grouping--;
1173 nseps--;
1174 }
1175 PRINT(&thousands_sep,
1176 1);
1177 PRINT(cp, *grouping);
1178 cp += *grouping;
1179 }
1180 } else {
1181 PAD(expt - lead, zeroes);
1182 }
1183 if (prec || flags & ALT)
1184 PRINT(decimal_point,1);
1185 if (ndig > lead)
1186 PRINT(cp, ndig - lead);
1187 }
1188 PAD(prec - ndig + expt, zeroes);
1189 } else { /* %[eE] or sufficiently long %[gG] */
1190 if (prec || flags & ALT) {
1191 buf[0] = *cp++;
1192 buf[1] = *decimal_point;
1193 PRINT(buf, 2);
1194 PRINT(cp, ndig-1);
1195 PAD(prec - ndig, zeroes);
1196 } else /* XeYYY */
1197 PRINT(cp, 1);
1198
1199 PRINT(expstr, expsize);
1200 }
1201 }
1202#else
1203 PRINT(cp, size);
1204#endif
1205 /* left-adjusting padding (always blank) */
1206 if (flags & LADJUST)

--- 4 unchanged lines hidden (view full) ---

1211
1212 FLUSH(); /* copy out the I/O vectors */
1213 }
1214done:
1215 FLUSH();
1216error:
1217#ifdef FLOATING_POINT
1218 if (dtoaresult != NULL)
1219 freedtoa(dtoaresult);
1220#endif
1221 if (convbuf != NULL)
1222 free(convbuf);
1223 if (__sferror(fp))
1224 ret = EOF;
1225 if ((argtable != NULL) && (argtable != statargtable))
1226 free (argtable);
1227 return (ret);

--- 353 unchanged lines hidden (view full) ---

1581
1582 *typetable = newtable;
1583 *tablesize = newsize;
1584}
1585
1586
1587#ifdef FLOATING_POINT
1588
1589static int
1590exponent(char *p0, int exp, int fmtch)
1591{
1592 char *p, *t;
1593 char expbuf[MAXEXPDIG];
1594
1595 p = p0;
1596 *p++ = fmtch;

--- 7 unchanged lines hidden (view full) ---

1604 if (exp > 9) {
1605 do {
1606 *--t = to_char(exp % 10);
1607 } while ((exp /= 10) > 9);
1608 *--t = to_char(exp);
1609 for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1610 }
1611 else {
1612 /*
1613 * Exponents for decimal floating point conversions
1614 * (%[eEgG]) must be at least two characters long,
1615 * whereas exponents for hexadecimal conversions can
1616 * be only one character long.
1617 */
1618 if (fmtch == 'e' || fmtch == 'E')
1619 *p++ = '0';
1620 *p++ = to_char(exp);
1621 }
1622 return (p - p0);
1623}
1624#endif /* FLOATING_POINT */