146076Sbostic /*- 246076Sbostic * Copyright (c) 1990 The Regents of the University of California. 334233Sbostic * All rights reserved. 434226Sbostic * 546076Sbostic * This code is derived from software contributed to Berkeley by 646076Sbostic * Chris Torek. 746076Sbostic * 842631Sbostic * %sccs.include.redist.c% 934226Sbostic */ 1034226Sbostic 1134233Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*50438Sbostic static char sccsid[] = "@(#)vfprintf.c 5.48 (Berkeley) 07/19/91"; 1334233Sbostic #endif /* LIBC_SCCS and not lint */ 1434233Sbostic 1546076Sbostic /* 1646076Sbostic * Actual printf innards. 1746076Sbostic * 1846076Sbostic * This code is large and complicated... 1946076Sbostic */ 2046076Sbostic 2134261Sbostic #include <sys/types.h> 2246611Sbostic #include <math.h> 2346076Sbostic #include <stdio.h> 2446076Sbostic #include <string.h> 2546076Sbostic #if __STDC__ 2646076Sbostic #include <stdarg.h> 2746076Sbostic #else 2834233Sbostic #include <varargs.h> 2946076Sbostic #endif 3046076Sbostic #include "local.h" 3146076Sbostic #include "fvwrite.h" 3234226Sbostic 33*50438Sbostic /* Define FLOATING_POINT to get floating point. */ 3446076Sbostic #define FLOATING_POINT 3534226Sbostic 3646076Sbostic /* 3746076Sbostic * Flush out all the vectors defined by the given uio, 3846076Sbostic * then reset it so that it can be reused. 3946076Sbostic */ 4046180Storek static int 4146076Sbostic __sprint(fp, uio) 4246076Sbostic FILE *fp; 4346076Sbostic register struct __suio *uio; 4446076Sbostic { 4546076Sbostic register int err; 4646076Sbostic 4746076Sbostic if (uio->uio_resid == 0) { 4846076Sbostic uio->uio_iovcnt = 0; 4946076Sbostic return (0); 5046076Sbostic } 5146076Sbostic err = __sfvwrite(fp, uio); 5246076Sbostic uio->uio_resid = 0; 5346076Sbostic uio->uio_iovcnt = 0; 5446076Sbostic return (err); 5546076Sbostic } 5646076Sbostic 5746076Sbostic /* 5846076Sbostic * Helper function for `fprintf to unbuffered unix file': creates a 5946076Sbostic * temporary buffer. We only work on write-only files; this avoids 6046076Sbostic * worries about ungetc buffers and so forth. 6146076Sbostic */ 6246180Storek static int 6346076Sbostic __sbprintf(fp, fmt, ap) 6446076Sbostic register FILE *fp; 6546180Storek const char *fmt; 6646076Sbostic va_list ap; 6746076Sbostic { 6846076Sbostic int ret; 6946076Sbostic FILE fake; 7046076Sbostic unsigned char buf[BUFSIZ]; 7146076Sbostic 7246076Sbostic /* copy the important variables */ 7346076Sbostic fake._flags = fp->_flags & ~__SNBF; 7446076Sbostic fake._file = fp->_file; 7546076Sbostic fake._cookie = fp->_cookie; 7646076Sbostic fake._write = fp->_write; 7746076Sbostic 7846076Sbostic /* set up the buffer */ 7946076Sbostic fake._bf._base = fake._p = buf; 8046076Sbostic fake._bf._size = fake._w = sizeof(buf); 8146076Sbostic fake._lbfsize = 0; /* not actually used, but Just In Case */ 8246076Sbostic 8346076Sbostic /* do the work, then copy any error status */ 8446076Sbostic ret = vfprintf(&fake, fmt, ap); 8546076Sbostic if (ret >= 0 && fflush(&fake)) 8646076Sbostic ret = EOF; 8746076Sbostic if (fake._flags & __SERR) 8846076Sbostic fp->_flags |= __SERR; 8946076Sbostic return (ret); 9046076Sbostic } 9146076Sbostic 9246076Sbostic 9346076Sbostic #ifdef FLOATING_POINT 9446180Storek #include "floatio.h" 9546076Sbostic 9634328Sbostic #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ 9746076Sbostic #define DEFPREC 6 9834328Sbostic 9946076Sbostic static int cvt(); 10034236Sbostic 10146076Sbostic #else /* no FLOATING_POINT */ 10234235Sbostic 10346076Sbostic #define BUF 40 10434331Sbostic 10546076Sbostic #endif /* FLOATING_POINT */ 10634318Sbostic 10746076Sbostic 10846076Sbostic /* 10946076Sbostic * Macros for converting digits to letters and vice versa 11046076Sbostic */ 11146076Sbostic #define to_digit(c) ((c) - '0') 11246076Sbostic #define is_digit(c) ((unsigned)to_digit(c) <= 9) 11346076Sbostic #define to_char(n) ((n) + '0') 11446076Sbostic 11546076Sbostic /* 11646076Sbostic * Flags used during conversion. 11746076Sbostic */ 11834318Sbostic #define LONGINT 0x01 /* long integer */ 11934318Sbostic #define LONGDBL 0x02 /* long double; unimplemented */ 12034318Sbostic #define SHORTINT 0x04 /* short integer */ 12134318Sbostic #define ALT 0x08 /* alternate form */ 12234318Sbostic #define LADJUST 0x10 /* left adjustment */ 12334427Sbostic #define ZEROPAD 0x20 /* zero (as opposed to blank) pad */ 12434427Sbostic #define HEXPREFIX 0x40 /* add 0x or 0X prefix */ 12534318Sbostic 12646180Storek int 12746076Sbostic vfprintf(fp, fmt0, ap) 12846076Sbostic FILE *fp; 12946180Storek const char *fmt0; 13046076Sbostic #if tahoe 13146076Sbostic register /* technically illegal, since we do not know what type va_list is */ 13246076Sbostic #endif 13346076Sbostic va_list ap; 13434226Sbostic { 13546076Sbostic register char *fmt; /* format string */ 13634427Sbostic register int ch; /* character from fmt */ 13746076Sbostic register int n; /* handy integer (short term usage) */ 13846076Sbostic register char *cp; /* handy char pointer (short term usage) */ 13946076Sbostic register struct __siov *iovp;/* for PRINT macro */ 14046076Sbostic register int flags; /* flags as above */ 14146076Sbostic int ret; /* return value accumulator */ 14246076Sbostic int width; /* width from format (%8d), or 0 */ 14346076Sbostic int prec; /* precision from format (%.3d), or -1 */ 14446076Sbostic char sign; /* sign prefix (' ', '+', '-', or \0) */ 14546076Sbostic #ifdef FLOATING_POINT 14646076Sbostic char softsign; /* temporary negative sign for floats */ 14734427Sbostic double _double; /* double precision arguments %[eEfgG] */ 14846076Sbostic int fpprec; /* `extra' floating precision in [eEfgG] */ 14946076Sbostic #endif 15034427Sbostic u_long _ulong; /* integer arguments %[diouxX] */ 15146076Sbostic enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 15246076Sbostic int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 15334624Sbostic int fieldsz; /* field size expanded by sign, etc */ 15446076Sbostic int realsz; /* field size expanded by dprec */ 15534427Sbostic int size; /* size of converted field or string */ 15646076Sbostic char *xdigs; /* digits for [xX] conversion */ 15746076Sbostic #define NIOV 8 15846076Sbostic struct __suio uio; /* output information: summary */ 15946076Sbostic struct __siov iov[NIOV];/* ... and individual io vectors */ 16034427Sbostic char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ 16146076Sbostic char ox[2]; /* space for 0x hex-prefix */ 16234226Sbostic 16346076Sbostic /* 16446076Sbostic * Choose PADSIZE to trade efficiency vs size. If larger 16546076Sbostic * printf fields occur frequently, increase PADSIZE (and make 16646076Sbostic * the initialisers below longer). 16746076Sbostic */ 16846076Sbostic #define PADSIZE 16 /* pad chunk size */ 16946076Sbostic static char blanks[PADSIZE] = 17046076Sbostic {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 17146076Sbostic static char zeroes[PADSIZE] = 17246076Sbostic {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 17346076Sbostic 17446076Sbostic /* 17546076Sbostic * BEWARE, these `goto error' on error, and PAD uses `n'. 17646076Sbostic */ 17746076Sbostic #define PRINT(ptr, len) { \ 17846076Sbostic iovp->iov_base = (ptr); \ 17946076Sbostic iovp->iov_len = (len); \ 18046076Sbostic uio.uio_resid += (len); \ 18146076Sbostic iovp++; \ 18246076Sbostic if (++uio.uio_iovcnt >= NIOV) { \ 18346076Sbostic if (__sprint(fp, &uio)) \ 18446076Sbostic goto error; \ 18546076Sbostic iovp = iov; \ 18646076Sbostic } \ 18746076Sbostic } 18846076Sbostic #define PAD(howmany, with) { \ 18946076Sbostic if ((n = (howmany)) > 0) { \ 19046076Sbostic while (n > PADSIZE) { \ 19146076Sbostic PRINT(with, PADSIZE); \ 19246076Sbostic n -= PADSIZE; \ 19346076Sbostic } \ 19446076Sbostic PRINT(with, n); \ 19546076Sbostic } \ 19646076Sbostic } 19746076Sbostic #define FLUSH() { \ 19846076Sbostic if (uio.uio_resid && __sprint(fp, &uio)) \ 19946076Sbostic goto error; \ 20046076Sbostic uio.uio_iovcnt = 0; \ 20146076Sbostic iovp = iov; \ 20246076Sbostic } 20346076Sbostic 20446076Sbostic /* 20546076Sbostic * To extend shorts properly, we need both signed and unsigned 20646076Sbostic * argument extraction methods. 20746076Sbostic */ 20846076Sbostic #define SARG() \ 20946076Sbostic (flags&LONGINT ? va_arg(ap, long) : \ 21046076Sbostic flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 21146076Sbostic (long)va_arg(ap, int)) 21246076Sbostic #define UARG() \ 21346076Sbostic (flags&LONGINT ? va_arg(ap, u_long) : \ 21446076Sbostic flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 21546076Sbostic (u_long)va_arg(ap, u_int)) 21646076Sbostic 21746076Sbostic /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 21846076Sbostic if (cantwrite(fp)) 21934428Sbostic return (EOF); 22034428Sbostic 22146076Sbostic /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 22246076Sbostic if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 22346076Sbostic fp->_file >= 0) 22446076Sbostic return (__sbprintf(fp, fmt0, ap)); 22546076Sbostic 22646076Sbostic fmt = (char *)fmt0; 22746076Sbostic uio.uio_iov = iovp = iov; 22846076Sbostic uio.uio_resid = 0; 22946076Sbostic uio.uio_iovcnt = 0; 23046076Sbostic ret = 0; 23146076Sbostic 23246076Sbostic /* 23346076Sbostic * Scan the format for conversions (`%' character). 23446076Sbostic */ 23546076Sbostic for (;;) { 23646076Sbostic for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 23746076Sbostic /* void */; 23846076Sbostic if ((n = fmt - cp) != 0) { 23946076Sbostic PRINT(cp, n); 24046076Sbostic ret += n; 24146076Sbostic } 24246076Sbostic if (ch == '\0') 24346076Sbostic goto done; 24446076Sbostic fmt++; /* skip over '%' */ 24546076Sbostic 24646076Sbostic flags = 0; 24746076Sbostic dprec = 0; 24846076Sbostic #ifdef FLOATING_POINT 24946076Sbostic fpprec = 0; 25034318Sbostic #endif 25146076Sbostic width = 0; 25234233Sbostic prec = -1; 25334318Sbostic sign = '\0'; 25434226Sbostic 25546076Sbostic rflag: ch = *fmt++; 25646076Sbostic reswitch: switch (ch) { 25734318Sbostic case ' ': 25834669Sbostic /* 25934669Sbostic * ``If the space and + flags both appear, the space 26034669Sbostic * flag will be ignored.'' 26134669Sbostic * -- ANSI X3J11 26234669Sbostic */ 26334669Sbostic if (!sign) 26434669Sbostic sign = ' '; 26534318Sbostic goto rflag; 26634233Sbostic case '#': 26734318Sbostic flags |= ALT; 26834318Sbostic goto rflag; 26934233Sbostic case '*': 27034235Sbostic /* 27134235Sbostic * ``A negative field width argument is taken as a 27246076Sbostic * - flag followed by a positive field width.'' 27334235Sbostic * -- ANSI X3J11 27434235Sbostic * They don't exclude field widths read from args. 27534235Sbostic */ 27646076Sbostic if ((width = va_arg(ap, int)) >= 0) 27734318Sbostic goto rflag; 27834235Sbostic width = -width; 27934427Sbostic /* FALLTHROUGH */ 28034235Sbostic case '-': 28134318Sbostic flags |= LADJUST; 28234318Sbostic goto rflag; 28334233Sbostic case '+': 28434314Sbostic sign = '+'; 28534318Sbostic goto rflag; 28634233Sbostic case '.': 28746076Sbostic if ((ch = *fmt++) == '*') { 28846076Sbostic n = va_arg(ap, int); 28946076Sbostic prec = n < 0 ? -1 : n; 29046076Sbostic goto rflag; 29134226Sbostic } 29246076Sbostic n = 0; 29346076Sbostic while (is_digit(ch)) { 29446076Sbostic n = 10 * n + to_digit(ch); 29546076Sbostic ch = *fmt++; 29646076Sbostic } 29734318Sbostic prec = n < 0 ? -1 : n; 29846076Sbostic goto reswitch; 29934233Sbostic case '0': 30034427Sbostic /* 30134427Sbostic * ``Note that 0 is taken as a flag, not as the 30234427Sbostic * beginning of a field width.'' 30334427Sbostic * -- ANSI X3J11 30434427Sbostic */ 30534427Sbostic flags |= ZEROPAD; 30634427Sbostic goto rflag; 30734233Sbostic case '1': case '2': case '3': case '4': 30834233Sbostic case '5': case '6': case '7': case '8': case '9': 30934318Sbostic n = 0; 31034233Sbostic do { 31146076Sbostic n = 10 * n + to_digit(ch); 31246076Sbostic ch = *fmt++; 31346076Sbostic } while (is_digit(ch)); 31434318Sbostic width = n; 31546076Sbostic goto reswitch; 31646076Sbostic #ifdef FLOATING_POINT 31734235Sbostic case 'L': 31834329Sbostic flags |= LONGDBL; 31934318Sbostic goto rflag; 32046076Sbostic #endif 32134235Sbostic case 'h': 32234318Sbostic flags |= SHORTINT; 32334318Sbostic goto rflag; 32434233Sbostic case 'l': 32534318Sbostic flags |= LONGINT; 32634318Sbostic goto rflag; 32734314Sbostic case 'c': 32846076Sbostic *(cp = buf) = va_arg(ap, int); 32934314Sbostic size = 1; 33034427Sbostic sign = '\0'; 33146076Sbostic break; 33234624Sbostic case 'D': 33334624Sbostic flags |= LONGINT; 33434624Sbostic /*FALLTHROUGH*/ 33534314Sbostic case 'd': 33634318Sbostic case 'i': 33746076Sbostic _ulong = SARG(); 33834318Sbostic if ((long)_ulong < 0) { 33934318Sbostic _ulong = -_ulong; 34034314Sbostic sign = '-'; 34134241Sbostic } 34246076Sbostic base = DEC; 34334327Sbostic goto number; 34446076Sbostic #ifdef FLOATING_POINT 34534261Sbostic case 'e': 34634236Sbostic case 'E': 34734235Sbostic case 'f': 34834261Sbostic case 'g': 34934243Sbostic case 'G': 35046076Sbostic _double = va_arg(ap, double); 35146126Storek /* do this before tricky precision changes */ 35247607Sbostic if (isinf(_double)) { 35347607Sbostic if (_double < 0) 35447607Sbostic sign = '-'; 35547607Sbostic cp = "Inf"; 35647607Sbostic size = 3; 35746126Storek break; 35846126Storek } 35947607Sbostic if (isnan(_double)) { 36047607Sbostic cp = "NaN"; 36147607Sbostic size = 3; 36247607Sbostic break; 36347607Sbostic } 36434328Sbostic /* 36534669Sbostic * don't do unrealistic precision; just pad it with 36634669Sbostic * zeroes later, so buffer size stays rational. 36734328Sbostic */ 36834328Sbostic if (prec > MAXFRACT) { 36946076Sbostic if (ch != 'g' && ch != 'G' || (flags&ALT)) 37034475Sbostic fpprec = prec - MAXFRACT; 37134328Sbostic prec = MAXFRACT; 37246076Sbostic } else if (prec == -1) 37334624Sbostic prec = DEFPREC; 37434669Sbostic /* 37546076Sbostic * cvt may have to round up before the "start" of 37646076Sbostic * its buffer, i.e. ``intf("%.2f", (double)9.999);''; 37746076Sbostic * if the first character is still NUL, it did. 37846076Sbostic * softsign avoids negative 0 if _double < 0 but 37946076Sbostic * no significant digits will be shown. 38034669Sbostic */ 38146076Sbostic cp = buf; 38246076Sbostic *cp = '\0'; 38346076Sbostic size = cvt(_double, prec, flags, &softsign, ch, 38446076Sbostic cp, buf + sizeof(buf)); 38534669Sbostic if (softsign) 38634669Sbostic sign = '-'; 38746076Sbostic if (*cp == '\0') 38846076Sbostic cp++; 38946076Sbostic break; 39046076Sbostic #endif /* FLOATING_POINT */ 39134235Sbostic case 'n': 39234427Sbostic if (flags & LONGINT) 39346076Sbostic *va_arg(ap, long *) = ret; 39434427Sbostic else if (flags & SHORTINT) 39546076Sbostic *va_arg(ap, short *) = ret; 39634318Sbostic else 39746076Sbostic *va_arg(ap, int *) = ret; 39846076Sbostic continue; /* no output */ 39934624Sbostic case 'O': 40034624Sbostic flags |= LONGINT; 40134624Sbostic /*FALLTHROUGH*/ 40234226Sbostic case 'o': 40346076Sbostic _ulong = UARG(); 40446076Sbostic base = OCT; 40534327Sbostic goto nosign; 40634235Sbostic case 'p': 40734320Sbostic /* 40834321Sbostic * ``The argument shall be a pointer to void. The 40934321Sbostic * value of the pointer is converted to a sequence 41034321Sbostic * of printable characters, in an implementation- 41134321Sbostic * defined manner.'' 41234321Sbostic * -- ANSI X3J11 41334320Sbostic */ 41434427Sbostic /* NOSTRICT */ 41546076Sbostic _ulong = (u_long)va_arg(ap, void *); 41646076Sbostic base = HEX; 41746076Sbostic xdigs = "0123456789abcdef"; 41846076Sbostic flags |= HEXPREFIX; 41946076Sbostic ch = 'x'; 42034327Sbostic goto nosign; 42134226Sbostic case 's': 42246076Sbostic if ((cp = va_arg(ap, char *)) == NULL) 42346076Sbostic cp = "(null)"; 42434321Sbostic if (prec >= 0) { 42534321Sbostic /* 42634321Sbostic * can't use strlen; can only look for the 42734321Sbostic * NUL in the first `prec' characters, and 42834321Sbostic * strlen() will go further. 42934321Sbostic */ 43046076Sbostic char *p = memchr(cp, 0, prec); 43134321Sbostic 43246076Sbostic if (p != NULL) { 43346076Sbostic size = p - cp; 43434321Sbostic if (size > prec) 43534321Sbostic size = prec; 43634427Sbostic } else 43734321Sbostic size = prec; 43834427Sbostic } else 43946076Sbostic size = strlen(cp); 44034427Sbostic sign = '\0'; 44146076Sbostic break; 44234624Sbostic case 'U': 44334624Sbostic flags |= LONGINT; 44434624Sbostic /*FALLTHROUGH*/ 44534226Sbostic case 'u': 44646076Sbostic _ulong = UARG(); 44746076Sbostic base = DEC; 44834327Sbostic goto nosign; 44934226Sbostic case 'X': 45046076Sbostic xdigs = "0123456789ABCDEF"; 45146076Sbostic goto hex; 45234226Sbostic case 'x': 45346076Sbostic xdigs = "0123456789abcdef"; 45446076Sbostic hex: _ulong = UARG(); 45546076Sbostic base = HEX; 45634326Sbostic /* leading 0x/X only if non-zero */ 45734427Sbostic if (flags & ALT && _ulong != 0) 45834427Sbostic flags |= HEXPREFIX; 45934327Sbostic 46034327Sbostic /* unsigned conversions */ 46134427Sbostic nosign: sign = '\0'; 46234326Sbostic /* 46334330Sbostic * ``... diouXx conversions ... if a precision is 46434330Sbostic * specified, the 0 flag will be ignored.'' 46534330Sbostic * -- ANSI X3J11 46634330Sbostic */ 46734427Sbostic number: if ((dprec = prec) >= 0) 46834427Sbostic flags &= ~ZEROPAD; 46934427Sbostic 47034330Sbostic /* 47134326Sbostic * ``The result of converting a zero value with an 47234326Sbostic * explicit precision of zero is no characters.'' 47334326Sbostic * -- ANSI X3J11 47434326Sbostic */ 47546076Sbostic cp = buf + BUF; 47634427Sbostic if (_ulong != 0 || prec != 0) { 47746076Sbostic /* 47846076Sbostic * unsigned mod is hard, and unsigned mod 47946076Sbostic * by a constant is easier than that by 48046076Sbostic * a variable; hence this switch. 48146076Sbostic */ 48246076Sbostic switch (base) { 48346076Sbostic case OCT: 48446076Sbostic do { 48546076Sbostic *--cp = to_char(_ulong & 7); 48646076Sbostic _ulong >>= 3; 48746076Sbostic } while (_ulong); 48846076Sbostic /* handle octal leading 0 */ 48946076Sbostic if (flags & ALT && *cp != '0') 49046076Sbostic *--cp = '0'; 49146076Sbostic break; 49234327Sbostic 49346076Sbostic case DEC: 49446076Sbostic /* many numbers are 1 digit */ 49546076Sbostic while (_ulong >= 10) { 49646076Sbostic *--cp = to_char(_ulong % 10); 49746076Sbostic _ulong /= 10; 49846076Sbostic } 49946076Sbostic *--cp = to_char(_ulong); 50046076Sbostic break; 50134327Sbostic 50246076Sbostic case HEX: 50346076Sbostic do { 50446076Sbostic *--cp = xdigs[_ulong & 15]; 50546076Sbostic _ulong >>= 4; 50646076Sbostic } while (_ulong); 50746076Sbostic break; 50834327Sbostic 50946076Sbostic default: 51046076Sbostic cp = "bug in vfprintf: bad base"; 51146180Storek size = strlen(cp); 51246076Sbostic goto skipsize; 51346076Sbostic } 51434327Sbostic } 51546076Sbostic size = buf + BUF - cp; 51646076Sbostic skipsize: 51734226Sbostic break; 51846076Sbostic default: /* "%?" prints ?, unless ? is NUL */ 51946076Sbostic if (ch == '\0') 52046076Sbostic goto done; 52146076Sbostic /* pretend it was %c with argument ch */ 52246076Sbostic cp = buf; 52346076Sbostic *cp = ch; 52446076Sbostic size = 1; 52546076Sbostic sign = '\0'; 52646076Sbostic break; 52734226Sbostic } 52846076Sbostic 52946076Sbostic /* 53046076Sbostic * All reasonable formats wind up here. At this point, 53146076Sbostic * `cp' points to a string which (if not flags&LADJUST) 53246076Sbostic * should be padded out to `width' places. If 53346076Sbostic * flags&ZEROPAD, it should first be prefixed by any 53446076Sbostic * sign or other prefix; otherwise, it should be blank 53546076Sbostic * padded before the prefix is emitted. After any 53646076Sbostic * left-hand padding and prefixing, emit zeroes 53746076Sbostic * required by a decimal [diouxX] precision, then print 53846076Sbostic * the string proper, then emit zeroes required by any 53946076Sbostic * leftover floating precision; finally, if LADJUST, 54046076Sbostic * pad with blanks. 54146076Sbostic */ 54246076Sbostic 54346076Sbostic /* 54446076Sbostic * compute actual size, so we know how much to pad. 54546076Sbostic * fieldsz excludes decimal prec; realsz includes it 54646076Sbostic */ 54746076Sbostic #ifdef FLOATING_POINT 54846076Sbostic fieldsz = size + fpprec; 54946076Sbostic #else 55046076Sbostic fieldsz = size; 55146076Sbostic #endif 55246076Sbostic if (sign) 55346076Sbostic fieldsz++; 55446076Sbostic else if (flags & HEXPREFIX) 55546076Sbostic fieldsz += 2; 55646076Sbostic realsz = dprec > fieldsz ? dprec : fieldsz; 55746076Sbostic 55846076Sbostic /* right-adjusting blank padding */ 55946076Sbostic if ((flags & (LADJUST|ZEROPAD)) == 0) 56046076Sbostic PAD(width - realsz, blanks); 56146076Sbostic 56246076Sbostic /* prefix */ 56346076Sbostic if (sign) { 56446076Sbostic PRINT(&sign, 1); 56546076Sbostic } else if (flags & HEXPREFIX) { 56646076Sbostic ox[0] = '0'; 56746076Sbostic ox[1] = ch; 56846076Sbostic PRINT(ox, 2); 56946076Sbostic } 57046076Sbostic 57146076Sbostic /* right-adjusting zero padding */ 57246076Sbostic if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 57346076Sbostic PAD(width - realsz, zeroes); 57446076Sbostic 57546076Sbostic /* leading zeroes from decimal precision */ 57646076Sbostic PAD(dprec - fieldsz, zeroes); 57746076Sbostic 57846076Sbostic /* the string or number proper */ 57946076Sbostic PRINT(cp, size); 58046076Sbostic 58146076Sbostic #ifdef FLOATING_POINT 58246076Sbostic /* trailing f.p. zeroes */ 58346076Sbostic PAD(fpprec, zeroes); 58446076Sbostic #endif 58546076Sbostic 58646076Sbostic /* left-adjusting padding (always blank) */ 58746076Sbostic if (flags & LADJUST) 58846076Sbostic PAD(width - realsz, blanks); 58946076Sbostic 59046076Sbostic /* finally, adjust ret */ 59146076Sbostic ret += width > realsz ? width : realsz; 59246076Sbostic 59346076Sbostic FLUSH(); /* copy out the I/O vectors */ 59434226Sbostic } 59546076Sbostic done: 59646076Sbostic FLUSH(); 59746076Sbostic error: 59846076Sbostic return (__sferror(fp) ? EOF : ret); 59934427Sbostic /* NOTREACHED */ 60034226Sbostic } 60134242Sbostic 60246076Sbostic #ifdef FLOATING_POINT 60346180Storek #include <math.h> 60446180Storek 60546076Sbostic static char *exponent(); 60646076Sbostic static char *round(); 60746076Sbostic 60846180Storek static int 60934669Sbostic cvt(number, prec, flags, signp, fmtch, startp, endp) 61034242Sbostic double number; 61134261Sbostic register int prec; 61234323Sbostic int flags; 61346076Sbostic char *signp; 61446076Sbostic int fmtch; 61546076Sbostic char *startp, *endp; 61634242Sbostic { 61734331Sbostic register char *p, *t; 61834672Sbostic register double fract; 61934624Sbostic int dotrim, expcnt, gformat; 62046180Storek double integer, tmp; 62134242Sbostic 62246076Sbostic dotrim = expcnt = gformat = 0; 62346076Sbostic if (number < 0) { 62446076Sbostic number = -number; 62546076Sbostic *signp = '-'; 62646076Sbostic } else 62746076Sbostic *signp = 0; 62844426Sbostic 62934624Sbostic fract = modf(number, &integer); 63034242Sbostic 63134624Sbostic /* get an extra slot for rounding. */ 63234624Sbostic t = ++startp; 63334624Sbostic 63434624Sbostic /* 63534624Sbostic * get integer portion of number; put into the end of the buffer; the 63634624Sbostic * .01 is added for modf(356.0 / 10, &integer) returning .59999999... 63734624Sbostic */ 63834624Sbostic for (p = endp - 1; integer; ++expcnt) { 63934624Sbostic tmp = modf(integer / 10, &integer); 64046076Sbostic *p-- = to_char((int)((tmp + .01) * 10)); 64134248Sbostic } 64246076Sbostic switch (fmtch) { 64334261Sbostic case 'f': 64434624Sbostic /* reverse integer into beginning of buffer */ 64534624Sbostic if (expcnt) 64634624Sbostic for (; ++p < endp; *t++ = *p); 64734624Sbostic else 64834624Sbostic *t++ = '0'; 64934248Sbostic /* 65034624Sbostic * if precision required or alternate flag set, add in a 65134624Sbostic * decimal point. 65234248Sbostic */ 65334624Sbostic if (prec || flags&ALT) 65434624Sbostic *t++ = '.'; 65534624Sbostic /* if requires more precision and some fraction left */ 65634624Sbostic if (fract) { 65734624Sbostic if (prec) 65834624Sbostic do { 65934624Sbostic fract = modf(fract * 10, &tmp); 66046076Sbostic *t++ = to_char((int)tmp); 66134624Sbostic } while (--prec && fract); 66234624Sbostic if (fract) 66334669Sbostic startp = round(fract, (int *)NULL, startp, 66434669Sbostic t - 1, (char)0, signp); 66534624Sbostic } 66634624Sbostic for (; prec--; *t++ = '0'); 66734624Sbostic break; 66834624Sbostic case 'e': 66934624Sbostic case 'E': 67034624Sbostic eformat: if (expcnt) { 67134624Sbostic *t++ = *++p; 67234624Sbostic if (prec || flags&ALT) 67334331Sbostic *t++ = '.'; 67434624Sbostic /* if requires more precision and some integer left */ 67534624Sbostic for (; prec && ++p < endp; --prec) 67634624Sbostic *t++ = *p; 67734624Sbostic /* 67834624Sbostic * if done precision and more of the integer component, 67934624Sbostic * round using it; adjust fract so we don't re-round 68034624Sbostic * later. 68134624Sbostic */ 68234624Sbostic if (!prec && ++p < endp) { 68334248Sbostic fract = 0; 68434669Sbostic startp = round((double)0, &expcnt, startp, 68534669Sbostic t - 1, *p, signp); 68634248Sbostic } 68734624Sbostic /* adjust expcnt for digit in front of decimal */ 68834624Sbostic --expcnt; 68934242Sbostic } 69034624Sbostic /* until first fractional digit, decrement exponent */ 69134624Sbostic else if (fract) { 69234624Sbostic /* adjust expcnt for digit in front of decimal */ 69334624Sbostic for (expcnt = -1;; --expcnt) { 69434624Sbostic fract = modf(fract * 10, &tmp); 69534624Sbostic if (tmp) 69634624Sbostic break; 69734248Sbostic } 69846076Sbostic *t++ = to_char((int)tmp); 69934624Sbostic if (prec || flags&ALT) 70034331Sbostic *t++ = '.'; 70134242Sbostic } 70234248Sbostic else { 70334624Sbostic *t++ = '0'; 70434624Sbostic if (prec || flags&ALT) 70534331Sbostic *t++ = '.'; 70634248Sbostic } 70734624Sbostic /* if requires more precision and some fraction left */ 70834624Sbostic if (fract) { 70934624Sbostic if (prec) 71034624Sbostic do { 71134624Sbostic fract = modf(fract * 10, &tmp); 71246076Sbostic *t++ = to_char((int)tmp); 71334624Sbostic } while (--prec && fract); 71434624Sbostic if (fract) 71534669Sbostic startp = round(fract, &expcnt, startp, 71634669Sbostic t - 1, (char)0, signp); 71734584Sbostic } 71834624Sbostic /* if requires more precision */ 71934624Sbostic for (; prec--; *t++ = '0'); 72034624Sbostic 72134624Sbostic /* unless alternate flag, trim any g/G format trailing 0's */ 72234624Sbostic if (gformat && !(flags&ALT)) { 72334624Sbostic while (t > startp && *--t == '0'); 72434624Sbostic if (*t == '.') 72534624Sbostic --t; 72634624Sbostic ++t; 72734624Sbostic } 72834624Sbostic t = exponent(t, expcnt, fmtch); 72934624Sbostic break; 73034624Sbostic case 'g': 73134624Sbostic case 'G': 73234624Sbostic /* a precision of 0 is treated as a precision of 1. */ 73334624Sbostic if (!prec) 73434624Sbostic ++prec; 73534624Sbostic /* 73634624Sbostic * ``The style used depends on the value converted; style e 73734624Sbostic * will be used only if the exponent resulting from the 73834624Sbostic * conversion is less than -4 or greater than the precision.'' 73934624Sbostic * -- ANSI X3J11 74034624Sbostic */ 74134624Sbostic if (expcnt > prec || !expcnt && fract && fract < .0001) { 74234624Sbostic /* 74334624Sbostic * g/G format counts "significant digits, not digits of 74434624Sbostic * precision; for the e/E format, this just causes an 74534624Sbostic * off-by-one problem, i.e. g/G considers the digit 74634624Sbostic * before the decimal point significant and e/E doesn't 74734624Sbostic * count it as precision. 74834624Sbostic */ 74934624Sbostic --prec; 75034624Sbostic fmtch -= 2; /* G->E, g->e */ 75134624Sbostic gformat = 1; 75234624Sbostic goto eformat; 75334624Sbostic } 75434624Sbostic /* 75534624Sbostic * reverse integer into beginning of buffer, 75634624Sbostic * note, decrement precision 75734624Sbostic */ 75834624Sbostic if (expcnt) 75934624Sbostic for (; ++p < endp; *t++ = *p, --prec); 76034624Sbostic else 76134624Sbostic *t++ = '0'; 76234624Sbostic /* 76334624Sbostic * if precision required or alternate flag set, add in a 76434624Sbostic * decimal point. If no digits yet, add in leading 0. 76534624Sbostic */ 76634624Sbostic if (prec || flags&ALT) { 76734624Sbostic dotrim = 1; 76834624Sbostic *t++ = '.'; 76934624Sbostic } 77034624Sbostic else 77134624Sbostic dotrim = 0; 77234624Sbostic /* if requires more precision and some fraction left */ 77334624Sbostic if (fract) { 77434624Sbostic if (prec) { 77534624Sbostic do { 77634624Sbostic fract = modf(fract * 10, &tmp); 77746076Sbostic *t++ = to_char((int)tmp); 77834624Sbostic } while(!tmp); 77934624Sbostic while (--prec && fract) { 78034624Sbostic fract = modf(fract * 10, &tmp); 78146076Sbostic *t++ = to_char((int)tmp); 78234248Sbostic } 78334248Sbostic } 78434624Sbostic if (fract) 78534669Sbostic startp = round(fract, (int *)NULL, startp, 78634669Sbostic t - 1, (char)0, signp); 78734624Sbostic } 78834624Sbostic /* alternate format, adds 0's for precision, else trim 0's */ 78934624Sbostic if (flags&ALT) 79034624Sbostic for (; prec--; *t++ = '0'); 79134624Sbostic else if (dotrim) { 79234624Sbostic while (t > startp && *--t == '0'); 79334624Sbostic if (*t != '.') 79434624Sbostic ++t; 79534624Sbostic } 79634624Sbostic } 79746076Sbostic return (t - startp); 79834624Sbostic } 79934248Sbostic 80034624Sbostic static char * 80134669Sbostic round(fract, exp, start, end, ch, signp) 80234624Sbostic double fract; 80334669Sbostic int *exp; 80434624Sbostic register char *start, *end; 80534669Sbostic char ch, *signp; 80634624Sbostic { 80734624Sbostic double tmp; 80834248Sbostic 80934624Sbostic if (fract) 81046180Storek (void)modf(fract * 10, &tmp); 81134624Sbostic else 81246076Sbostic tmp = to_digit(ch); 81334624Sbostic if (tmp > 4) 81434624Sbostic for (;; --end) { 81534624Sbostic if (*end == '.') 81634624Sbostic --end; 81734624Sbostic if (++*end <= '9') 81834624Sbostic break; 81934624Sbostic *end = '0'; 82034624Sbostic if (end == start) { 82134669Sbostic if (exp) { /* e/E; increment exponent */ 82234669Sbostic *end = '1'; 82334669Sbostic ++*exp; 82434669Sbostic } 82534669Sbostic else { /* f; add extra digit */ 82646076Sbostic *--end = '1'; 82746076Sbostic --start; 82834669Sbostic } 82934624Sbostic break; 83034242Sbostic } 83134242Sbostic } 83234669Sbostic /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */ 83334669Sbostic else if (*signp == '-') 83434669Sbostic for (;; --end) { 83534669Sbostic if (*end == '.') 83634669Sbostic --end; 83734669Sbostic if (*end != '0') 83834669Sbostic break; 83934669Sbostic if (end == start) 84034669Sbostic *signp = 0; 84134669Sbostic } 84246076Sbostic return (start); 84334624Sbostic } 84434248Sbostic 84534624Sbostic static char * 84634624Sbostic exponent(p, exp, fmtch) 84734624Sbostic register char *p; 84834624Sbostic register int exp; 84946076Sbostic int fmtch; 85034624Sbostic { 85134624Sbostic register char *t; 85234624Sbostic char expbuf[MAXEXP]; 85334248Sbostic 85434624Sbostic *p++ = fmtch; 85534624Sbostic if (exp < 0) { 85634624Sbostic exp = -exp; 85734624Sbostic *p++ = '-'; 85834242Sbostic } 85934624Sbostic else 86034624Sbostic *p++ = '+'; 86134624Sbostic t = expbuf + MAXEXP; 86234624Sbostic if (exp > 9) { 86334624Sbostic do { 86446076Sbostic *--t = to_char(exp % 10); 86534624Sbostic } while ((exp /= 10) > 9); 86646076Sbostic *--t = to_char(exp); 86734624Sbostic for (; t < expbuf + MAXEXP; *p++ = *t++); 86834624Sbostic } 86934624Sbostic else { 87034624Sbostic *p++ = '0'; 87146076Sbostic *p++ = to_char(exp); 87234624Sbostic } 87346076Sbostic return (p); 87434242Sbostic } 87546076Sbostic #endif /* FLOATING_POINT */ 876