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*47607Sbostic static char sccsid[] = "@(#)vfprintf.c 5.47 (Berkeley) 03/22/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 3346076Sbostic /* 3446076Sbostic * Define FLOATING_POINT to get floating point. 3546076Sbostic * Define CSH to get a csh-specific version (grr). 3646076Sbostic */ 3746076Sbostic #ifndef CSH 3846076Sbostic #define FLOATING_POINT 3946076Sbostic #endif 4034226Sbostic 4146076Sbostic /* end of configuration stuff */ 4234624Sbostic 4346076Sbostic 4446076Sbostic #ifdef CSH 4546076Sbostic /* 4646076Sbostic * C shell hacks. Ick, gag. 4746076Sbostic */ 4846076Sbostic #undef BUFSIZ 4946076Sbostic #include "sh.h" 5046076Sbostic 5146180Storek #if __STDC__ 5246180Storek int 5346180Storek printf(const char *fmt, ...) { 5446180Storek FILE f; 5546646Sdonn va_list ap; 5646646Sdonn int ret; 5746180Storek 5846646Sdonn va_start(ap, fmt); 5946180Storek f._flags = __SWR; 6046646Sdonn f._write = NULL; 6146646Sdonn ret = vfprintf(&f, fmt, ap); 6246646Sdonn va_end(ap); 6346646Sdonn return ret; 6446180Storek } 6546180Storek #else 6646180Storek int 6746076Sbostic printf(fmt, args) 6846076Sbostic char *fmt; 6946076Sbostic { 7046076Sbostic FILE f; 7146076Sbostic 7246076Sbostic f._flags = __SWR; 7346442Storek f._write = NULL; 7446076Sbostic return (vfprintf(&f, fmt, &args)); 7546076Sbostic } 7646180Storek #endif 7746076Sbostic 7846180Storek int 7946442Storek __sprint(fp, uio) 8046442Storek FILE *fp; 8146076Sbostic register struct __suio *uio; 8246076Sbostic { 8346076Sbostic register char *p; 8446076Sbostic register int n, ch, iovcnt; 8546442Storek register struct __siov *iov; 8646076Sbostic 8746442Storek /* must allow sprintf to work, might as well allow others too */ 8846442Storek if (fp->_write || fp->_flags & __SSTR) { 8946442Storek if (uio->uio_resid == 0) { 9046442Storek uio->uio_iovcnt = 0; 9146442Storek return (0); 9246442Storek } 9346442Storek n = __sfvwrite(fp, uio); 9446442Storek uio->uio_resid = 0; 9546442Storek uio->uio_iovcnt = 0; 9646442Storek return (n); 9746442Storek } 9846442Storek iov = uio->uio_iov; 9946076Sbostic for (iovcnt = uio->uio_iovcnt; --iovcnt >= 0; iov++) { 10046076Sbostic for (p = iov->iov_base, n = iov->iov_len; --n >= 0;) { 10146076Sbostic #ifdef CSHPUTCHAR 10246076Sbostic ch = *p++; 10346076Sbostic CSHPUTCHAR; /* this horrid macro uses `ch' */ 10446076Sbostic #else 10546076Sbostic #undef putchar 10646076Sbostic putchar(*p++); 10746076Sbostic #endif 10846076Sbostic } 10946076Sbostic } 11046076Sbostic uio->uio_resid = 0; 11146076Sbostic uio->uio_iovcnt = 0; 11246076Sbostic return (0); 11346076Sbostic } 11446076Sbostic 11546076Sbostic #else /* CSH */ 11646076Sbostic 11746076Sbostic /* 11846076Sbostic * Flush out all the vectors defined by the given uio, 11946076Sbostic * then reset it so that it can be reused. 12046076Sbostic */ 12146180Storek static int 12246076Sbostic __sprint(fp, uio) 12346076Sbostic FILE *fp; 12446076Sbostic register struct __suio *uio; 12546076Sbostic { 12646076Sbostic register int err; 12746076Sbostic 12846076Sbostic if (uio->uio_resid == 0) { 12946076Sbostic uio->uio_iovcnt = 0; 13046076Sbostic return (0); 13146076Sbostic } 13246076Sbostic err = __sfvwrite(fp, uio); 13346076Sbostic uio->uio_resid = 0; 13446076Sbostic uio->uio_iovcnt = 0; 13546076Sbostic return (err); 13646076Sbostic } 13746076Sbostic 13846076Sbostic /* 13946076Sbostic * Helper function for `fprintf to unbuffered unix file': creates a 14046076Sbostic * temporary buffer. We only work on write-only files; this avoids 14146076Sbostic * worries about ungetc buffers and so forth. 14246076Sbostic */ 14346180Storek static int 14446076Sbostic __sbprintf(fp, fmt, ap) 14546076Sbostic register FILE *fp; 14646180Storek const char *fmt; 14746076Sbostic va_list ap; 14846076Sbostic { 14946076Sbostic int ret; 15046076Sbostic FILE fake; 15146076Sbostic unsigned char buf[BUFSIZ]; 15246076Sbostic 15346076Sbostic /* copy the important variables */ 15446076Sbostic fake._flags = fp->_flags & ~__SNBF; 15546076Sbostic fake._file = fp->_file; 15646076Sbostic fake._cookie = fp->_cookie; 15746076Sbostic fake._write = fp->_write; 15846076Sbostic 15946076Sbostic /* set up the buffer */ 16046076Sbostic fake._bf._base = fake._p = buf; 16146076Sbostic fake._bf._size = fake._w = sizeof(buf); 16246076Sbostic fake._lbfsize = 0; /* not actually used, but Just In Case */ 16346076Sbostic 16446076Sbostic /* do the work, then copy any error status */ 16546076Sbostic ret = vfprintf(&fake, fmt, ap); 16646076Sbostic if (ret >= 0 && fflush(&fake)) 16746076Sbostic ret = EOF; 16846076Sbostic if (fake._flags & __SERR) 16946076Sbostic fp->_flags |= __SERR; 17046076Sbostic return (ret); 17146076Sbostic } 17246076Sbostic 17346076Sbostic #endif /* CSH */ 17446076Sbostic 17546076Sbostic 17646076Sbostic #ifdef FLOATING_POINT 17746180Storek #include "floatio.h" 17846076Sbostic 17934328Sbostic #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ 18046076Sbostic #define DEFPREC 6 18134328Sbostic 18246076Sbostic static int cvt(); 18334236Sbostic 18446076Sbostic #else /* no FLOATING_POINT */ 18534235Sbostic 18646076Sbostic #define BUF 40 18734331Sbostic 18846076Sbostic #endif /* FLOATING_POINT */ 18934318Sbostic 19046076Sbostic 19146076Sbostic /* 19246076Sbostic * Macros for converting digits to letters and vice versa 19346076Sbostic */ 19446076Sbostic #define to_digit(c) ((c) - '0') 19546076Sbostic #define is_digit(c) ((unsigned)to_digit(c) <= 9) 19646076Sbostic #define to_char(n) ((n) + '0') 19746076Sbostic 19846076Sbostic /* 19946076Sbostic * Flags used during conversion. 20046076Sbostic */ 20134318Sbostic #define LONGINT 0x01 /* long integer */ 20234318Sbostic #define LONGDBL 0x02 /* long double; unimplemented */ 20334318Sbostic #define SHORTINT 0x04 /* short integer */ 20434318Sbostic #define ALT 0x08 /* alternate form */ 20534318Sbostic #define LADJUST 0x10 /* left adjustment */ 20634427Sbostic #define ZEROPAD 0x20 /* zero (as opposed to blank) pad */ 20734427Sbostic #define HEXPREFIX 0x40 /* add 0x or 0X prefix */ 20834318Sbostic 20946180Storek int 21046076Sbostic vfprintf(fp, fmt0, ap) 21146076Sbostic FILE *fp; 21246180Storek const char *fmt0; 21346076Sbostic #if tahoe 21446076Sbostic register /* technically illegal, since we do not know what type va_list is */ 21546076Sbostic #endif 21646076Sbostic va_list ap; 21734226Sbostic { 21846076Sbostic register char *fmt; /* format string */ 21934427Sbostic register int ch; /* character from fmt */ 22046076Sbostic register int n; /* handy integer (short term usage) */ 22146076Sbostic register char *cp; /* handy char pointer (short term usage) */ 22246076Sbostic register struct __siov *iovp;/* for PRINT macro */ 22346076Sbostic register int flags; /* flags as above */ 22446076Sbostic int ret; /* return value accumulator */ 22546076Sbostic int width; /* width from format (%8d), or 0 */ 22646076Sbostic int prec; /* precision from format (%.3d), or -1 */ 22746076Sbostic char sign; /* sign prefix (' ', '+', '-', or \0) */ 22846076Sbostic #ifdef FLOATING_POINT 22946076Sbostic char softsign; /* temporary negative sign for floats */ 23034427Sbostic double _double; /* double precision arguments %[eEfgG] */ 23146076Sbostic int fpprec; /* `extra' floating precision in [eEfgG] */ 23246076Sbostic #endif 23334427Sbostic u_long _ulong; /* integer arguments %[diouxX] */ 23446076Sbostic enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */ 23546076Sbostic int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 23634624Sbostic int fieldsz; /* field size expanded by sign, etc */ 23746076Sbostic int realsz; /* field size expanded by dprec */ 23834427Sbostic int size; /* size of converted field or string */ 23946076Sbostic char *xdigs; /* digits for [xX] conversion */ 24046076Sbostic #define NIOV 8 24146076Sbostic struct __suio uio; /* output information: summary */ 24246076Sbostic struct __siov iov[NIOV];/* ... and individual io vectors */ 24334427Sbostic char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ 24446076Sbostic char ox[2]; /* space for 0x hex-prefix */ 24534226Sbostic 24646076Sbostic /* 24746076Sbostic * Choose PADSIZE to trade efficiency vs size. If larger 24846076Sbostic * printf fields occur frequently, increase PADSIZE (and make 24946076Sbostic * the initialisers below longer). 25046076Sbostic */ 25146076Sbostic #define PADSIZE 16 /* pad chunk size */ 25246076Sbostic static char blanks[PADSIZE] = 25346076Sbostic {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 25446076Sbostic static char zeroes[PADSIZE] = 25546076Sbostic {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 25646076Sbostic 25746076Sbostic /* 25846076Sbostic * BEWARE, these `goto error' on error, and PAD uses `n'. 25946076Sbostic */ 26046076Sbostic #define PRINT(ptr, len) { \ 26146076Sbostic iovp->iov_base = (ptr); \ 26246076Sbostic iovp->iov_len = (len); \ 26346076Sbostic uio.uio_resid += (len); \ 26446076Sbostic iovp++; \ 26546076Sbostic if (++uio.uio_iovcnt >= NIOV) { \ 26646076Sbostic if (__sprint(fp, &uio)) \ 26746076Sbostic goto error; \ 26846076Sbostic iovp = iov; \ 26946076Sbostic } \ 27046076Sbostic } 27146076Sbostic #define PAD(howmany, with) { \ 27246076Sbostic if ((n = (howmany)) > 0) { \ 27346076Sbostic while (n > PADSIZE) { \ 27446076Sbostic PRINT(with, PADSIZE); \ 27546076Sbostic n -= PADSIZE; \ 27646076Sbostic } \ 27746076Sbostic PRINT(with, n); \ 27846076Sbostic } \ 27946076Sbostic } 28046076Sbostic #define FLUSH() { \ 28146076Sbostic if (uio.uio_resid && __sprint(fp, &uio)) \ 28246076Sbostic goto error; \ 28346076Sbostic uio.uio_iovcnt = 0; \ 28446076Sbostic iovp = iov; \ 28546076Sbostic } 28646076Sbostic 28746076Sbostic /* 28846076Sbostic * To extend shorts properly, we need both signed and unsigned 28946076Sbostic * argument extraction methods. 29046076Sbostic */ 29146076Sbostic #define SARG() \ 29246076Sbostic (flags&LONGINT ? va_arg(ap, long) : \ 29346076Sbostic flags&SHORTINT ? (long)(short)va_arg(ap, int) : \ 29446076Sbostic (long)va_arg(ap, int)) 29546076Sbostic #define UARG() \ 29646076Sbostic (flags&LONGINT ? va_arg(ap, u_long) : \ 29746076Sbostic flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \ 29846076Sbostic (u_long)va_arg(ap, u_int)) 29946076Sbostic 30046076Sbostic #ifndef CSH 30146076Sbostic /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 30246076Sbostic if (cantwrite(fp)) 30334428Sbostic return (EOF); 30434428Sbostic 30546076Sbostic /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 30646076Sbostic if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 30746076Sbostic fp->_file >= 0) 30846076Sbostic return (__sbprintf(fp, fmt0, ap)); 30946076Sbostic #endif /* CSH */ 31046076Sbostic 31146076Sbostic fmt = (char *)fmt0; 31246076Sbostic uio.uio_iov = iovp = iov; 31346076Sbostic uio.uio_resid = 0; 31446076Sbostic uio.uio_iovcnt = 0; 31546076Sbostic ret = 0; 31646076Sbostic 31746076Sbostic /* 31846076Sbostic * Scan the format for conversions (`%' character). 31946076Sbostic */ 32046076Sbostic for (;;) { 32146076Sbostic for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 32246076Sbostic /* void */; 32346076Sbostic if ((n = fmt - cp) != 0) { 32446076Sbostic PRINT(cp, n); 32546076Sbostic ret += n; 32646076Sbostic } 32746076Sbostic if (ch == '\0') 32846076Sbostic goto done; 32946076Sbostic fmt++; /* skip over '%' */ 33046076Sbostic 33146076Sbostic flags = 0; 33246076Sbostic dprec = 0; 33346076Sbostic #ifdef FLOATING_POINT 33446076Sbostic fpprec = 0; 33534318Sbostic #endif 33646076Sbostic width = 0; 33734233Sbostic prec = -1; 33834318Sbostic sign = '\0'; 33934226Sbostic 34046076Sbostic rflag: ch = *fmt++; 34146076Sbostic reswitch: switch (ch) { 34234318Sbostic case ' ': 34334669Sbostic /* 34434669Sbostic * ``If the space and + flags both appear, the space 34534669Sbostic * flag will be ignored.'' 34634669Sbostic * -- ANSI X3J11 34734669Sbostic */ 34834669Sbostic if (!sign) 34934669Sbostic sign = ' '; 35034318Sbostic goto rflag; 35134233Sbostic case '#': 35234318Sbostic flags |= ALT; 35334318Sbostic goto rflag; 35434233Sbostic case '*': 35534235Sbostic /* 35634235Sbostic * ``A negative field width argument is taken as a 35746076Sbostic * - flag followed by a positive field width.'' 35834235Sbostic * -- ANSI X3J11 35934235Sbostic * They don't exclude field widths read from args. 36034235Sbostic */ 36146076Sbostic if ((width = va_arg(ap, int)) >= 0) 36234318Sbostic goto rflag; 36334235Sbostic width = -width; 36434427Sbostic /* FALLTHROUGH */ 36534235Sbostic case '-': 36634318Sbostic flags |= LADJUST; 36734318Sbostic goto rflag; 36834233Sbostic case '+': 36934314Sbostic sign = '+'; 37034318Sbostic goto rflag; 37134233Sbostic case '.': 37246076Sbostic if ((ch = *fmt++) == '*') { 37346076Sbostic n = va_arg(ap, int); 37446076Sbostic prec = n < 0 ? -1 : n; 37546076Sbostic goto rflag; 37634226Sbostic } 37746076Sbostic n = 0; 37846076Sbostic while (is_digit(ch)) { 37946076Sbostic n = 10 * n + to_digit(ch); 38046076Sbostic ch = *fmt++; 38146076Sbostic } 38234318Sbostic prec = n < 0 ? -1 : n; 38346076Sbostic goto reswitch; 38434233Sbostic case '0': 38534427Sbostic /* 38634427Sbostic * ``Note that 0 is taken as a flag, not as the 38734427Sbostic * beginning of a field width.'' 38834427Sbostic * -- ANSI X3J11 38934427Sbostic */ 39034427Sbostic flags |= ZEROPAD; 39134427Sbostic goto rflag; 39234233Sbostic case '1': case '2': case '3': case '4': 39334233Sbostic case '5': case '6': case '7': case '8': case '9': 39434318Sbostic n = 0; 39534233Sbostic do { 39646076Sbostic n = 10 * n + to_digit(ch); 39746076Sbostic ch = *fmt++; 39846076Sbostic } while (is_digit(ch)); 39934318Sbostic width = n; 40046076Sbostic goto reswitch; 40146076Sbostic #ifdef FLOATING_POINT 40234235Sbostic case 'L': 40334329Sbostic flags |= LONGDBL; 40434318Sbostic goto rflag; 40546076Sbostic #endif 40634235Sbostic case 'h': 40734318Sbostic flags |= SHORTINT; 40834318Sbostic goto rflag; 40934233Sbostic case 'l': 41034318Sbostic flags |= LONGINT; 41134318Sbostic goto rflag; 41234314Sbostic case 'c': 41346076Sbostic *(cp = buf) = va_arg(ap, int); 41434314Sbostic size = 1; 41534427Sbostic sign = '\0'; 41646076Sbostic break; 41734624Sbostic case 'D': 41834624Sbostic flags |= LONGINT; 41934624Sbostic /*FALLTHROUGH*/ 42034314Sbostic case 'd': 42134318Sbostic case 'i': 42246076Sbostic _ulong = SARG(); 42334318Sbostic if ((long)_ulong < 0) { 42434318Sbostic _ulong = -_ulong; 42534314Sbostic sign = '-'; 42634241Sbostic } 42746076Sbostic base = DEC; 42834327Sbostic goto number; 42946076Sbostic #ifdef FLOATING_POINT 43034261Sbostic case 'e': 43134236Sbostic case 'E': 43234235Sbostic case 'f': 43334261Sbostic case 'g': 43434243Sbostic case 'G': 43546076Sbostic _double = va_arg(ap, double); 43646126Storek /* do this before tricky precision changes */ 437*47607Sbostic if (isinf(_double)) { 438*47607Sbostic if (_double < 0) 439*47607Sbostic sign = '-'; 440*47607Sbostic cp = "Inf"; 441*47607Sbostic size = 3; 44246126Storek break; 44346126Storek } 444*47607Sbostic if (isnan(_double)) { 445*47607Sbostic cp = "NaN"; 446*47607Sbostic size = 3; 447*47607Sbostic break; 448*47607Sbostic } 44934328Sbostic /* 45034669Sbostic * don't do unrealistic precision; just pad it with 45134669Sbostic * zeroes later, so buffer size stays rational. 45234328Sbostic */ 45334328Sbostic if (prec > MAXFRACT) { 45446076Sbostic if (ch != 'g' && ch != 'G' || (flags&ALT)) 45534475Sbostic fpprec = prec - MAXFRACT; 45634328Sbostic prec = MAXFRACT; 45746076Sbostic } else if (prec == -1) 45834624Sbostic prec = DEFPREC; 45934669Sbostic /* 46046076Sbostic * cvt may have to round up before the "start" of 46146076Sbostic * its buffer, i.e. ``intf("%.2f", (double)9.999);''; 46246076Sbostic * if the first character is still NUL, it did. 46346076Sbostic * softsign avoids negative 0 if _double < 0 but 46446076Sbostic * no significant digits will be shown. 46534669Sbostic */ 46646076Sbostic cp = buf; 46746076Sbostic *cp = '\0'; 46846076Sbostic size = cvt(_double, prec, flags, &softsign, ch, 46946076Sbostic cp, buf + sizeof(buf)); 47034669Sbostic if (softsign) 47134669Sbostic sign = '-'; 47246076Sbostic if (*cp == '\0') 47346076Sbostic cp++; 47446076Sbostic break; 47546076Sbostic #endif /* FLOATING_POINT */ 47634235Sbostic case 'n': 47734427Sbostic if (flags & LONGINT) 47846076Sbostic *va_arg(ap, long *) = ret; 47934427Sbostic else if (flags & SHORTINT) 48046076Sbostic *va_arg(ap, short *) = ret; 48134318Sbostic else 48246076Sbostic *va_arg(ap, int *) = ret; 48346076Sbostic continue; /* no output */ 48434624Sbostic case 'O': 48534624Sbostic flags |= LONGINT; 48634624Sbostic /*FALLTHROUGH*/ 48734226Sbostic case 'o': 48846076Sbostic _ulong = UARG(); 48946076Sbostic base = OCT; 49034327Sbostic goto nosign; 49134235Sbostic case 'p': 49234320Sbostic /* 49334321Sbostic * ``The argument shall be a pointer to void. The 49434321Sbostic * value of the pointer is converted to a sequence 49534321Sbostic * of printable characters, in an implementation- 49634321Sbostic * defined manner.'' 49734321Sbostic * -- ANSI X3J11 49834320Sbostic */ 49934427Sbostic /* NOSTRICT */ 50046076Sbostic _ulong = (u_long)va_arg(ap, void *); 50146076Sbostic base = HEX; 50246076Sbostic xdigs = "0123456789abcdef"; 50346076Sbostic flags |= HEXPREFIX; 50446076Sbostic ch = 'x'; 50534327Sbostic goto nosign; 50634226Sbostic case 's': 50746076Sbostic if ((cp = va_arg(ap, char *)) == NULL) 50846076Sbostic cp = "(null)"; 50934321Sbostic if (prec >= 0) { 51034321Sbostic /* 51134321Sbostic * can't use strlen; can only look for the 51234321Sbostic * NUL in the first `prec' characters, and 51334321Sbostic * strlen() will go further. 51434321Sbostic */ 51546076Sbostic char *p = memchr(cp, 0, prec); 51634321Sbostic 51746076Sbostic if (p != NULL) { 51846076Sbostic size = p - cp; 51934321Sbostic if (size > prec) 52034321Sbostic size = prec; 52134427Sbostic } else 52234321Sbostic size = prec; 52334427Sbostic } else 52446076Sbostic size = strlen(cp); 52534427Sbostic sign = '\0'; 52646076Sbostic break; 52734624Sbostic case 'U': 52834624Sbostic flags |= LONGINT; 52934624Sbostic /*FALLTHROUGH*/ 53034226Sbostic case 'u': 53146076Sbostic _ulong = UARG(); 53246076Sbostic base = DEC; 53334327Sbostic goto nosign; 53434226Sbostic case 'X': 53546076Sbostic xdigs = "0123456789ABCDEF"; 53646076Sbostic goto hex; 53734226Sbostic case 'x': 53846076Sbostic xdigs = "0123456789abcdef"; 53946076Sbostic hex: _ulong = UARG(); 54046076Sbostic base = HEX; 54134326Sbostic /* leading 0x/X only if non-zero */ 54234427Sbostic if (flags & ALT && _ulong != 0) 54334427Sbostic flags |= HEXPREFIX; 54434327Sbostic 54534327Sbostic /* unsigned conversions */ 54634427Sbostic nosign: sign = '\0'; 54734326Sbostic /* 54834330Sbostic * ``... diouXx conversions ... if a precision is 54934330Sbostic * specified, the 0 flag will be ignored.'' 55034330Sbostic * -- ANSI X3J11 55134330Sbostic */ 55234427Sbostic number: if ((dprec = prec) >= 0) 55334427Sbostic flags &= ~ZEROPAD; 55434427Sbostic 55534330Sbostic /* 55634326Sbostic * ``The result of converting a zero value with an 55734326Sbostic * explicit precision of zero is no characters.'' 55834326Sbostic * -- ANSI X3J11 55934326Sbostic */ 56046076Sbostic cp = buf + BUF; 56134427Sbostic if (_ulong != 0 || prec != 0) { 56246076Sbostic /* 56346076Sbostic * unsigned mod is hard, and unsigned mod 56446076Sbostic * by a constant is easier than that by 56546076Sbostic * a variable; hence this switch. 56646076Sbostic */ 56746076Sbostic switch (base) { 56846076Sbostic case OCT: 56946076Sbostic do { 57046076Sbostic *--cp = to_char(_ulong & 7); 57146076Sbostic _ulong >>= 3; 57246076Sbostic } while (_ulong); 57346076Sbostic /* handle octal leading 0 */ 57446076Sbostic if (flags & ALT && *cp != '0') 57546076Sbostic *--cp = '0'; 57646076Sbostic break; 57734327Sbostic 57846076Sbostic case DEC: 57946076Sbostic /* many numbers are 1 digit */ 58046076Sbostic while (_ulong >= 10) { 58146076Sbostic *--cp = to_char(_ulong % 10); 58246076Sbostic _ulong /= 10; 58346076Sbostic } 58446076Sbostic *--cp = to_char(_ulong); 58546076Sbostic break; 58634327Sbostic 58746076Sbostic case HEX: 58846076Sbostic do { 58946076Sbostic *--cp = xdigs[_ulong & 15]; 59046076Sbostic _ulong >>= 4; 59146076Sbostic } while (_ulong); 59246076Sbostic break; 59334327Sbostic 59446076Sbostic default: 59546076Sbostic cp = "bug in vfprintf: bad base"; 59646180Storek size = strlen(cp); 59746076Sbostic goto skipsize; 59846076Sbostic } 59934327Sbostic } 60046076Sbostic size = buf + BUF - cp; 60146076Sbostic skipsize: 60234226Sbostic break; 60346076Sbostic default: /* "%?" prints ?, unless ? is NUL */ 60446076Sbostic if (ch == '\0') 60546076Sbostic goto done; 60646076Sbostic /* pretend it was %c with argument ch */ 60746076Sbostic cp = buf; 60846076Sbostic *cp = ch; 60946076Sbostic size = 1; 61046076Sbostic sign = '\0'; 61146076Sbostic break; 61234226Sbostic } 61346076Sbostic 61446076Sbostic /* 61546076Sbostic * All reasonable formats wind up here. At this point, 61646076Sbostic * `cp' points to a string which (if not flags&LADJUST) 61746076Sbostic * should be padded out to `width' places. If 61846076Sbostic * flags&ZEROPAD, it should first be prefixed by any 61946076Sbostic * sign or other prefix; otherwise, it should be blank 62046076Sbostic * padded before the prefix is emitted. After any 62146076Sbostic * left-hand padding and prefixing, emit zeroes 62246076Sbostic * required by a decimal [diouxX] precision, then print 62346076Sbostic * the string proper, then emit zeroes required by any 62446076Sbostic * leftover floating precision; finally, if LADJUST, 62546076Sbostic * pad with blanks. 62646076Sbostic */ 62746076Sbostic 62846076Sbostic /* 62946076Sbostic * compute actual size, so we know how much to pad. 63046076Sbostic * fieldsz excludes decimal prec; realsz includes it 63146076Sbostic */ 63246076Sbostic #ifdef FLOATING_POINT 63346076Sbostic fieldsz = size + fpprec; 63446076Sbostic #else 63546076Sbostic fieldsz = size; 63646076Sbostic #endif 63746076Sbostic if (sign) 63846076Sbostic fieldsz++; 63946076Sbostic else if (flags & HEXPREFIX) 64046076Sbostic fieldsz += 2; 64146076Sbostic realsz = dprec > fieldsz ? dprec : fieldsz; 64246076Sbostic 64346076Sbostic /* right-adjusting blank padding */ 64446076Sbostic if ((flags & (LADJUST|ZEROPAD)) == 0) 64546076Sbostic PAD(width - realsz, blanks); 64646076Sbostic 64746076Sbostic /* prefix */ 64846076Sbostic if (sign) { 64946076Sbostic PRINT(&sign, 1); 65046076Sbostic } else if (flags & HEXPREFIX) { 65146076Sbostic ox[0] = '0'; 65246076Sbostic ox[1] = ch; 65346076Sbostic PRINT(ox, 2); 65446076Sbostic } 65546076Sbostic 65646076Sbostic /* right-adjusting zero padding */ 65746076Sbostic if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 65846076Sbostic PAD(width - realsz, zeroes); 65946076Sbostic 66046076Sbostic /* leading zeroes from decimal precision */ 66146076Sbostic PAD(dprec - fieldsz, zeroes); 66246076Sbostic 66346076Sbostic /* the string or number proper */ 66446076Sbostic PRINT(cp, size); 66546076Sbostic 66646076Sbostic #ifdef FLOATING_POINT 66746076Sbostic /* trailing f.p. zeroes */ 66846076Sbostic PAD(fpprec, zeroes); 66946076Sbostic #endif 67046076Sbostic 67146076Sbostic /* left-adjusting padding (always blank) */ 67246076Sbostic if (flags & LADJUST) 67346076Sbostic PAD(width - realsz, blanks); 67446076Sbostic 67546076Sbostic /* finally, adjust ret */ 67646076Sbostic ret += width > realsz ? width : realsz; 67746076Sbostic 67846076Sbostic FLUSH(); /* copy out the I/O vectors */ 67934226Sbostic } 68046076Sbostic done: 68146076Sbostic FLUSH(); 68246076Sbostic error: 68346076Sbostic return (__sferror(fp) ? EOF : ret); 68434427Sbostic /* NOTREACHED */ 68534226Sbostic } 68634242Sbostic 68746076Sbostic #ifdef FLOATING_POINT 68846180Storek #include <math.h> 68946180Storek 69046076Sbostic static char *exponent(); 69146076Sbostic static char *round(); 69246076Sbostic 69346180Storek static int 69434669Sbostic cvt(number, prec, flags, signp, fmtch, startp, endp) 69534242Sbostic double number; 69634261Sbostic register int prec; 69734323Sbostic int flags; 69846076Sbostic char *signp; 69946076Sbostic int fmtch; 70046076Sbostic char *startp, *endp; 70134242Sbostic { 70234331Sbostic register char *p, *t; 70334672Sbostic register double fract; 70434624Sbostic int dotrim, expcnt, gformat; 70546180Storek double integer, tmp; 70634242Sbostic 70746076Sbostic dotrim = expcnt = gformat = 0; 70846076Sbostic if (number < 0) { 70946076Sbostic number = -number; 71046076Sbostic *signp = '-'; 71146076Sbostic } else 71246076Sbostic *signp = 0; 71344426Sbostic 71434624Sbostic fract = modf(number, &integer); 71534242Sbostic 71634624Sbostic /* get an extra slot for rounding. */ 71734624Sbostic t = ++startp; 71834624Sbostic 71934624Sbostic /* 72034624Sbostic * get integer portion of number; put into the end of the buffer; the 72134624Sbostic * .01 is added for modf(356.0 / 10, &integer) returning .59999999... 72234624Sbostic */ 72334624Sbostic for (p = endp - 1; integer; ++expcnt) { 72434624Sbostic tmp = modf(integer / 10, &integer); 72546076Sbostic *p-- = to_char((int)((tmp + .01) * 10)); 72634248Sbostic } 72746076Sbostic switch (fmtch) { 72834261Sbostic case 'f': 72934624Sbostic /* reverse integer into beginning of buffer */ 73034624Sbostic if (expcnt) 73134624Sbostic for (; ++p < endp; *t++ = *p); 73234624Sbostic else 73334624Sbostic *t++ = '0'; 73434248Sbostic /* 73534624Sbostic * if precision required or alternate flag set, add in a 73634624Sbostic * decimal point. 73734248Sbostic */ 73834624Sbostic if (prec || flags&ALT) 73934624Sbostic *t++ = '.'; 74034624Sbostic /* if requires more precision and some fraction left */ 74134624Sbostic if (fract) { 74234624Sbostic if (prec) 74334624Sbostic do { 74434624Sbostic fract = modf(fract * 10, &tmp); 74546076Sbostic *t++ = to_char((int)tmp); 74634624Sbostic } while (--prec && fract); 74734624Sbostic if (fract) 74834669Sbostic startp = round(fract, (int *)NULL, startp, 74934669Sbostic t - 1, (char)0, signp); 75034624Sbostic } 75134624Sbostic for (; prec--; *t++ = '0'); 75234624Sbostic break; 75334624Sbostic case 'e': 75434624Sbostic case 'E': 75534624Sbostic eformat: if (expcnt) { 75634624Sbostic *t++ = *++p; 75734624Sbostic if (prec || flags&ALT) 75834331Sbostic *t++ = '.'; 75934624Sbostic /* if requires more precision and some integer left */ 76034624Sbostic for (; prec && ++p < endp; --prec) 76134624Sbostic *t++ = *p; 76234624Sbostic /* 76334624Sbostic * if done precision and more of the integer component, 76434624Sbostic * round using it; adjust fract so we don't re-round 76534624Sbostic * later. 76634624Sbostic */ 76734624Sbostic if (!prec && ++p < endp) { 76834248Sbostic fract = 0; 76934669Sbostic startp = round((double)0, &expcnt, startp, 77034669Sbostic t - 1, *p, signp); 77134248Sbostic } 77234624Sbostic /* adjust expcnt for digit in front of decimal */ 77334624Sbostic --expcnt; 77434242Sbostic } 77534624Sbostic /* until first fractional digit, decrement exponent */ 77634624Sbostic else if (fract) { 77734624Sbostic /* adjust expcnt for digit in front of decimal */ 77834624Sbostic for (expcnt = -1;; --expcnt) { 77934624Sbostic fract = modf(fract * 10, &tmp); 78034624Sbostic if (tmp) 78134624Sbostic break; 78234248Sbostic } 78346076Sbostic *t++ = to_char((int)tmp); 78434624Sbostic if (prec || flags&ALT) 78534331Sbostic *t++ = '.'; 78634242Sbostic } 78734248Sbostic else { 78834624Sbostic *t++ = '0'; 78934624Sbostic if (prec || flags&ALT) 79034331Sbostic *t++ = '.'; 79134248Sbostic } 79234624Sbostic /* if requires more precision and some fraction left */ 79334624Sbostic if (fract) { 79434624Sbostic if (prec) 79534624Sbostic do { 79634624Sbostic fract = modf(fract * 10, &tmp); 79746076Sbostic *t++ = to_char((int)tmp); 79834624Sbostic } while (--prec && fract); 79934624Sbostic if (fract) 80034669Sbostic startp = round(fract, &expcnt, startp, 80134669Sbostic t - 1, (char)0, signp); 80234584Sbostic } 80334624Sbostic /* if requires more precision */ 80434624Sbostic for (; prec--; *t++ = '0'); 80534624Sbostic 80634624Sbostic /* unless alternate flag, trim any g/G format trailing 0's */ 80734624Sbostic if (gformat && !(flags&ALT)) { 80834624Sbostic while (t > startp && *--t == '0'); 80934624Sbostic if (*t == '.') 81034624Sbostic --t; 81134624Sbostic ++t; 81234624Sbostic } 81334624Sbostic t = exponent(t, expcnt, fmtch); 81434624Sbostic break; 81534624Sbostic case 'g': 81634624Sbostic case 'G': 81734624Sbostic /* a precision of 0 is treated as a precision of 1. */ 81834624Sbostic if (!prec) 81934624Sbostic ++prec; 82034624Sbostic /* 82134624Sbostic * ``The style used depends on the value converted; style e 82234624Sbostic * will be used only if the exponent resulting from the 82334624Sbostic * conversion is less than -4 or greater than the precision.'' 82434624Sbostic * -- ANSI X3J11 82534624Sbostic */ 82634624Sbostic if (expcnt > prec || !expcnt && fract && fract < .0001) { 82734624Sbostic /* 82834624Sbostic * g/G format counts "significant digits, not digits of 82934624Sbostic * precision; for the e/E format, this just causes an 83034624Sbostic * off-by-one problem, i.e. g/G considers the digit 83134624Sbostic * before the decimal point significant and e/E doesn't 83234624Sbostic * count it as precision. 83334624Sbostic */ 83434624Sbostic --prec; 83534624Sbostic fmtch -= 2; /* G->E, g->e */ 83634624Sbostic gformat = 1; 83734624Sbostic goto eformat; 83834624Sbostic } 83934624Sbostic /* 84034624Sbostic * reverse integer into beginning of buffer, 84134624Sbostic * note, decrement precision 84234624Sbostic */ 84334624Sbostic if (expcnt) 84434624Sbostic for (; ++p < endp; *t++ = *p, --prec); 84534624Sbostic else 84634624Sbostic *t++ = '0'; 84734624Sbostic /* 84834624Sbostic * if precision required or alternate flag set, add in a 84934624Sbostic * decimal point. If no digits yet, add in leading 0. 85034624Sbostic */ 85134624Sbostic if (prec || flags&ALT) { 85234624Sbostic dotrim = 1; 85334624Sbostic *t++ = '.'; 85434624Sbostic } 85534624Sbostic else 85634624Sbostic dotrim = 0; 85734624Sbostic /* if requires more precision and some fraction left */ 85834624Sbostic if (fract) { 85934624Sbostic if (prec) { 86034624Sbostic do { 86134624Sbostic fract = modf(fract * 10, &tmp); 86246076Sbostic *t++ = to_char((int)tmp); 86334624Sbostic } while(!tmp); 86434624Sbostic while (--prec && fract) { 86534624Sbostic fract = modf(fract * 10, &tmp); 86646076Sbostic *t++ = to_char((int)tmp); 86734248Sbostic } 86834248Sbostic } 86934624Sbostic if (fract) 87034669Sbostic startp = round(fract, (int *)NULL, startp, 87134669Sbostic t - 1, (char)0, signp); 87234624Sbostic } 87334624Sbostic /* alternate format, adds 0's for precision, else trim 0's */ 87434624Sbostic if (flags&ALT) 87534624Sbostic for (; prec--; *t++ = '0'); 87634624Sbostic else if (dotrim) { 87734624Sbostic while (t > startp && *--t == '0'); 87834624Sbostic if (*t != '.') 87934624Sbostic ++t; 88034624Sbostic } 88134624Sbostic } 88246076Sbostic return (t - startp); 88334624Sbostic } 88434248Sbostic 88534624Sbostic static char * 88634669Sbostic round(fract, exp, start, end, ch, signp) 88734624Sbostic double fract; 88834669Sbostic int *exp; 88934624Sbostic register char *start, *end; 89034669Sbostic char ch, *signp; 89134624Sbostic { 89234624Sbostic double tmp; 89334248Sbostic 89434624Sbostic if (fract) 89546180Storek (void)modf(fract * 10, &tmp); 89634624Sbostic else 89746076Sbostic tmp = to_digit(ch); 89834624Sbostic if (tmp > 4) 89934624Sbostic for (;; --end) { 90034624Sbostic if (*end == '.') 90134624Sbostic --end; 90234624Sbostic if (++*end <= '9') 90334624Sbostic break; 90434624Sbostic *end = '0'; 90534624Sbostic if (end == start) { 90634669Sbostic if (exp) { /* e/E; increment exponent */ 90734669Sbostic *end = '1'; 90834669Sbostic ++*exp; 90934669Sbostic } 91034669Sbostic else { /* f; add extra digit */ 91146076Sbostic *--end = '1'; 91246076Sbostic --start; 91334669Sbostic } 91434624Sbostic break; 91534242Sbostic } 91634242Sbostic } 91734669Sbostic /* ``"%.3f", (double)-0.0004'' gives you a negative 0. */ 91834669Sbostic else if (*signp == '-') 91934669Sbostic for (;; --end) { 92034669Sbostic if (*end == '.') 92134669Sbostic --end; 92234669Sbostic if (*end != '0') 92334669Sbostic break; 92434669Sbostic if (end == start) 92534669Sbostic *signp = 0; 92634669Sbostic } 92746076Sbostic return (start); 92834624Sbostic } 92934248Sbostic 93034624Sbostic static char * 93134624Sbostic exponent(p, exp, fmtch) 93234624Sbostic register char *p; 93334624Sbostic register int exp; 93446076Sbostic int fmtch; 93534624Sbostic { 93634624Sbostic register char *t; 93734624Sbostic char expbuf[MAXEXP]; 93834248Sbostic 93934624Sbostic *p++ = fmtch; 94034624Sbostic if (exp < 0) { 94134624Sbostic exp = -exp; 94234624Sbostic *p++ = '-'; 94334242Sbostic } 94434624Sbostic else 94534624Sbostic *p++ = '+'; 94634624Sbostic t = expbuf + MAXEXP; 94734624Sbostic if (exp > 9) { 94834624Sbostic do { 94946076Sbostic *--t = to_char(exp % 10); 95034624Sbostic } while ((exp /= 10) > 9); 95146076Sbostic *--t = to_char(exp); 95234624Sbostic for (; t < expbuf + MAXEXP; *p++ = *t++); 95334624Sbostic } 95434624Sbostic else { 95534624Sbostic *p++ = '0'; 95646076Sbostic *p++ = to_char(exp); 95734624Sbostic } 95846076Sbostic return (p); 95934242Sbostic } 96046076Sbostic #endif /* FLOATING_POINT */ 961