134226Sbostic /* 234233Sbostic * Copyright (c) 1988 Regents of the University of California. 334233Sbostic * All rights reserved. 434226Sbostic * 534233Sbostic * Redistribution and use in source and binary forms are permitted 634233Sbostic * provided that this notice is preserved and that due credit is given 734233Sbostic * to the University of California at Berkeley. The name of the University 834233Sbostic * may not be used to endorse or promote products derived from this 934233Sbostic * software without specific prior written permission. This software 1034233Sbostic * is provided ``as is'' without express or implied warranty. 1134226Sbostic */ 1234226Sbostic 1334233Sbostic #if defined(LIBC_SCCS) && !defined(lint) 14*34328Sbostic static char sccsid[] = "@(#)vfprintf.c 5.24 (Berkeley) 05/18/88"; 1534233Sbostic #endif /* LIBC_SCCS and not lint */ 1634233Sbostic 1734261Sbostic #include <sys/types.h> 1834233Sbostic #include <varargs.h> 1934226Sbostic #include <stdio.h> 2034233Sbostic #include <ctype.h> 2134226Sbostic 22*34328Sbostic /* 11-bit exponent (VAX G floating point) is 308 decimal digits */ 23*34328Sbostic #define MAXEXP 308 24*34328Sbostic /* 128 bit fraction takes up 39 decimal digits; max reasonable precision */ 25*34328Sbostic #define MAXFRACT 39 2634226Sbostic 27*34328Sbostic #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ 28*34328Sbostic 2934323Sbostic #define PUTC(ch) {++cnt; putc((char)ch, fp);} 3034236Sbostic 3134318Sbostic #define ARG() \ 3234318Sbostic _ulong = flags&LONGINT ? va_arg(argp, long) : \ 3334318Sbostic flags&SHORTINT ? va_arg(argp, short) : va_arg(argp, int); 3434235Sbostic 3534318Sbostic /* have to deal with the negative buffer count kludge */ 3634318Sbostic #define NEGATIVE_COUNT_KLUDGE 3734318Sbostic 3834318Sbostic #define LONGINT 0x01 /* long integer */ 3934318Sbostic #define LONGDBL 0x02 /* long double; unimplemented */ 4034318Sbostic #define SHORTINT 0x04 /* short integer */ 4134318Sbostic #define ALT 0x08 /* alternate form */ 4234318Sbostic #define LADJUST 0x10 /* left adjustment */ 4334318Sbostic 4434323Sbostic _doprnt(fmt0, argp, fp) 4534323Sbostic u_char *fmt0; 4634233Sbostic va_list argp; 4734235Sbostic register FILE *fp; 4834226Sbostic { 4934323Sbostic register u_char *fmt; 5034323Sbostic register int ch, cnt, n; 5134323Sbostic register char *t; 5234235Sbostic double _double; 5334314Sbostic u_long _ulong; 54*34328Sbostic int base, flags, fpprec, prec, size, width; 55*34328Sbostic char padc, sign, *digs, buf[BUF], *_cvt(); 5634226Sbostic 5734323Sbostic fmt = fmt0; 5834243Sbostic digs = "0123456789abcdef"; 5934322Sbostic for (cnt = 0;; ++fmt) { 6034318Sbostic n = fp->_cnt; 6134318Sbostic for (t = fp->_ptr; (ch = *fmt) && ch != '%'; ++cnt, ++fmt) 6234318Sbostic if (--n < 0 6334318Sbostic #ifdef NEGATIVE_COUNT_KLUDGE 6434318Sbostic && (!(fp->_flag & _IOLBF) || -n >= fp->_bufsiz) 6534318Sbostic #endif 6634318Sbostic || ch == '\n' && fp->_flag&_IOLBF) { 6734318Sbostic fp->_cnt = n; 6834318Sbostic fp->_ptr = t; 6934318Sbostic (void)_flsbuf(ch, fp); 7034318Sbostic n = fp->_cnt; 7134318Sbostic t = fp->_ptr; 7234318Sbostic } 7334318Sbostic else 7434314Sbostic *t++ = ch; 7534318Sbostic fp->_cnt = n; 7634318Sbostic fp->_ptr = t; 7734318Sbostic if (!ch) 7834314Sbostic return(cnt); 7934314Sbostic 80*34328Sbostic flags = fpprec = width = 0; 8134233Sbostic prec = -1; 8234233Sbostic padc = ' '; 8334318Sbostic sign = '\0'; 8434226Sbostic 8534318Sbostic rflag: switch (*++fmt) { 8634318Sbostic case ' ': 8734318Sbostic sign = ' '; 8834318Sbostic goto rflag; 8934233Sbostic case '#': 9034318Sbostic flags |= ALT; 9134318Sbostic goto rflag; 9234233Sbostic case '*': 9334235Sbostic /* 9434235Sbostic * ``A negative field width argument is taken as a 9534235Sbostic * - flag followed by a positive field width.'' 9634235Sbostic * -- ANSI X3J11 9734235Sbostic * They don't exclude field widths read from args. 9834235Sbostic */ 9934235Sbostic if ((width = va_arg(argp, int)) >= 0) 10034318Sbostic goto rflag; 10134235Sbostic width = -width; 10234235Sbostic /*FALLTHROUGH*/ 10334235Sbostic case '-': 10434318Sbostic flags |= LADJUST; 10534318Sbostic goto rflag; 10634233Sbostic case '+': 10734314Sbostic sign = '+'; 10834318Sbostic goto rflag; 10934233Sbostic case '.': 11034235Sbostic if (*++fmt == '*') 11134318Sbostic n = va_arg(argp, int); 11234318Sbostic else if (isascii(*fmt) && isdigit(*fmt)) { 11334318Sbostic n = 0; 11434233Sbostic do { 11534318Sbostic n = 10 * n + *fmt - '0'; 11634318Sbostic } while (isascii(*++fmt) && isdigit(*fmt)); 11734233Sbostic --fmt; 11834226Sbostic } 11934235Sbostic else { 12034318Sbostic --fmt; 12134235Sbostic prec = 0; 12234318Sbostic goto rflag; 12334235Sbostic } 12434318Sbostic prec = n < 0 ? -1 : n; 12534318Sbostic goto rflag; 12634233Sbostic case '0': 12734233Sbostic padc = '0'; 12834235Sbostic /*FALLTHROUGH*/ 12934233Sbostic case '1': case '2': case '3': case '4': 13034233Sbostic case '5': case '6': case '7': case '8': case '9': 13134318Sbostic n = 0; 13234233Sbostic do { 13334318Sbostic n = 10 * n + *fmt - '0'; 13434318Sbostic } while (isascii(*++fmt) && isdigit(*fmt)); 13534318Sbostic width = n; 13634233Sbostic --fmt; 13734319Sbostic goto rflag; 13834235Sbostic case 'L': 13934318Sbostic /* 14034318Sbostic * C doesn't have a long double; use long for now. 14134318Sbostic * flags |= LONGDBL; 14234318Sbostic */ 14334318Sbostic flags |= LONGINT; 14434318Sbostic goto rflag; 14534235Sbostic case 'h': 14634318Sbostic flags |= SHORTINT; 14734318Sbostic goto rflag; 14834233Sbostic case 'l': 14934318Sbostic flags |= LONGINT; 15034318Sbostic goto rflag; 15134314Sbostic case 'c': 15234319Sbostic buf[0] = va_arg(argp, int); 15334314Sbostic size = 1; 15434319Sbostic t = buf; 15534314Sbostic goto pforw; 15634314Sbostic case 'd': 15734318Sbostic case 'i': 15834318Sbostic ARG(); 15934318Sbostic if ((long)_ulong < 0) { 16034318Sbostic _ulong = -_ulong; 16134314Sbostic sign = '-'; 16234241Sbostic } 16334241Sbostic base = 10; 16434327Sbostic goto number; 16534261Sbostic case 'e': 16634236Sbostic case 'E': 16734235Sbostic case 'f': 16834261Sbostic case 'g': 16934243Sbostic case 'G': 17034243Sbostic _double = va_arg(argp, double); 171*34328Sbostic /* 172*34328Sbostic * don't bother to do unrealistic precision; just 173*34328Sbostic * pad it with zeroes later. This keeps buffer size 174*34328Sbostic * rational. 175*34328Sbostic */ 176*34328Sbostic if (prec > MAXFRACT) { 177*34328Sbostic fpprec = prec - MAXFRACT; 178*34328Sbostic prec = MAXFRACT; 179*34328Sbostic } 18034327Sbostic size = _cvt(_double, prec, flags, *fmt, padc, &sign, 18134323Sbostic buf, buf + sizeof(buf)) - buf; 18234319Sbostic t = buf; 18334327Sbostic /* 18434327Sbostic * zero-padded sign put out here; blank padded sign 18534327Sbostic * placed in number in _cvt(). 18634327Sbostic */ 18734327Sbostic if (sign && padc == '0') { 18834327Sbostic PUTC(sign); 18934327Sbostic --width; 19034327Sbostic } 19134314Sbostic goto pforw; 19234235Sbostic case 'n': 19334318Sbostic if (flags&LONGDBL || flags&LONGINT) 19434318Sbostic *va_arg(argp, long *) = cnt; 19534318Sbostic else if (flags&SHORTINT) 19634318Sbostic *va_arg(argp, short *) = cnt; 19734318Sbostic else 19834318Sbostic *va_arg(argp, int *) = cnt; 19934235Sbostic break; 20034226Sbostic case 'o': 20134318Sbostic ARG(); 20234226Sbostic base = 8; 20334327Sbostic goto nosign; 20434235Sbostic case 'p': 20534320Sbostic /* 20634321Sbostic * ``The argument shall be a pointer to void. The 20734321Sbostic * value of the pointer is converted to a sequence 20834321Sbostic * of printable characters, in an implementation- 20934321Sbostic * defined manner.'' 21034321Sbostic * -- ANSI X3J11 21134320Sbostic */ 21234327Sbostic /*NOSTRICT*/ 21334320Sbostic _ulong = (u_long)va_arg(argp, void *); 21434320Sbostic base = 16; 21534327Sbostic goto nosign; 21634226Sbostic case 's': 21734314Sbostic if (!(t = va_arg(argp, char *))) 21834314Sbostic t = "(null)"; 21934321Sbostic if (prec >= 0) { 22034321Sbostic /* 22134321Sbostic * can't use strlen; can only look for the 22234321Sbostic * NUL in the first `prec' characters, and 22334321Sbostic * strlen() will go further. 22434321Sbostic */ 22534321Sbostic char *p, *memchr(); 22634321Sbostic 22734321Sbostic if (p = memchr(t, 0, prec)) { 22834321Sbostic size = p - t; 22934321Sbostic if (size > prec) 23034321Sbostic size = prec; 23134321Sbostic } 23234321Sbostic else 23334321Sbostic size = prec; 23434321Sbostic } 23534321Sbostic else 23634321Sbostic size = strlen(t); 237*34328Sbostic goto pforw; 23834226Sbostic case 'u': 23934318Sbostic ARG(); 24034226Sbostic base = 10; 24134327Sbostic goto nosign; 24234226Sbostic case 'X': 24334226Sbostic digs = "0123456789ABCDEF"; 24434233Sbostic /*FALLTHROUGH*/ 24534226Sbostic case 'x': 24634318Sbostic ARG(); 24734314Sbostic base = 16; 24834326Sbostic /* leading 0x/X only if non-zero */ 24934326Sbostic if (!_ulong) 25034324Sbostic flags &= ~ALT; 25134327Sbostic 25234327Sbostic /* unsigned conversions */ 25334327Sbostic nosign: sign = NULL; 25434326Sbostic /* 25534326Sbostic * ``The result of converting a zero value with an 25634326Sbostic * explicit precision of zero is no characters.'' 25734326Sbostic * -- ANSI X3J11 25834326Sbostic */ 25934327Sbostic number: if (!_ulong && !prec) 26034326Sbostic break; 26134327Sbostic 262*34328Sbostic t = buf + BUF - 1; 26334233Sbostic do { 26434314Sbostic *t-- = digs[_ulong % base]; 26534314Sbostic _ulong /= base; 26634314Sbostic } while(_ulong); 267*34328Sbostic for (size = buf + BUF - 1 - t; size < prec; ++size) 26834324Sbostic *t-- = '0'; 269*34328Sbostic digs = "0123456789abcdef"; 27034327Sbostic 27134327Sbostic /* alternate mode for hex and octal numbers */ 27234324Sbostic if (flags&ALT) 27334324Sbostic switch (base) { 27434324Sbostic case 16: 27534324Sbostic /* avoid "00000x35" */ 27634327Sbostic if (padc == ' ') { 27734327Sbostic *t-- = *fmt; 27834327Sbostic *t-- = '0'; 279*34328Sbostic size += 2; 28034327Sbostic } 28134327Sbostic else { 28234324Sbostic PUTC('0'); 28334324Sbostic PUTC(*fmt); 284*34328Sbostic width -= 2; 28534324Sbostic } 28634324Sbostic break; 28734324Sbostic case 8: 28834324Sbostic if (t[1] != '0') { 28934324Sbostic *t-- = '0'; 290*34328Sbostic ++size; 29134324Sbostic } 29234324Sbostic break; 29334314Sbostic } 29434327Sbostic 29534327Sbostic if (sign) { 29634327Sbostic /* avoid "0000-3" */ 297*34328Sbostic if (padc == ' ') { 29834327Sbostic *t-- = sign; 299*34328Sbostic ++size; 300*34328Sbostic } 301*34328Sbostic else { 30234327Sbostic PUTC(sign); 303*34328Sbostic --width; 304*34328Sbostic } 30534327Sbostic } 306*34328Sbostic ++t; 30734327Sbostic 308*34328Sbostic pforw: if (!(flags&LADJUST) && width) 309*34328Sbostic for (n = size + fpprec; n++ < width;) 31034263Sbostic PUTC(padc); 311*34328Sbostic if (fp->_cnt - (n = size) >= 0) { 312*34328Sbostic cnt += n; 313*34328Sbostic fp->_cnt -= n; 314*34328Sbostic bcopy(t, fp->_ptr, n); 315*34328Sbostic fp->_ptr += n; 316*34328Sbostic } 317*34328Sbostic else for (; n--; ++t) 31834314Sbostic PUTC(*t); 319*34328Sbostic while (fpprec--) 320*34328Sbostic PUTC('0'); 321*34328Sbostic if (flags&LADJUST) 322*34328Sbostic for (n = size + fpprec; ++n < width;) 323*34328Sbostic PUTC(' '); 32434226Sbostic break; 32534233Sbostic case '\0': /* "%?" prints ?, unless ? is NULL */ 32634314Sbostic return(cnt); 32734226Sbostic default: 32834263Sbostic PUTC(*fmt); 32934226Sbostic } 33034226Sbostic } 33134314Sbostic /*NOTREACHED*/ 33234226Sbostic } 33334242Sbostic 33434261Sbostic #define EFORMAT 0x01 33534261Sbostic #define FFORMAT 0x02 33634261Sbostic #define GFORMAT 0x04 33734314Sbostic #define DEFPREC 6 33834261Sbostic 33934323Sbostic static char * 34034327Sbostic _cvt(number, prec, flags, fmtch, padc, sign, startp, endp) 34134242Sbostic double number; 34234261Sbostic register int prec; 34334323Sbostic int flags; 34434323Sbostic u_char fmtch; 34534327Sbostic char padc, *sign, *startp, *endp; 34634242Sbostic { 34734248Sbostic register char *p; 34834261Sbostic register int expcnt, format; 34934248Sbostic double fract, integer, tmp, modf(); 35034261Sbostic int decpt; 35134323Sbostic char *savep; 35234242Sbostic 35334314Sbostic if (prec == -1) 35434242Sbostic prec = DEFPREC; 35534242Sbostic 35634314Sbostic if (number < 0) { 35734327Sbostic *sign = '-'; 35834248Sbostic number = -number; 35934248Sbostic } 36034242Sbostic 36134327Sbostic /* if blank padded, add sign in as part of the number */ 36234327Sbostic if (*sign && padc == ' ') 36334327Sbostic *startp++ = *sign; 36434327Sbostic 36534261Sbostic switch(fmtch) { 36634261Sbostic case 'e': 36734261Sbostic case 'E': 36834261Sbostic format = EFORMAT; 36934261Sbostic break; 37034261Sbostic case 'f': 37134261Sbostic format = FFORMAT; 37234261Sbostic break; 37334261Sbostic case 'g': 37434261Sbostic case 'G': 37534261Sbostic format = GFORMAT; 37634261Sbostic fmtch -= 2; 37734261Sbostic } 37834261Sbostic 37934248Sbostic /* 38034248Sbostic * if the alternate flag is set, or, at least one digit of precision 38134248Sbostic * was requested, add a decimal point, unless it's the g/G format 38234314Sbostic * in which case we require two digits of precision, as it counts 38334248Sbostic * precision differently. 38434248Sbostic */ 38534318Sbostic decpt = flags&ALT || prec > (format&GFORMAT ? 1 : 0); 38634248Sbostic 38734248Sbostic expcnt = 0; 38834323Sbostic p = endp - 1; 38934248Sbostic fract = modf(number, &integer); 39034248Sbostic if (integer) { 39134248Sbostic register char *p2; 39234248Sbostic 39334248Sbostic /* get integer part of number; count decimal places */ 39434248Sbostic for (; integer; ++expcnt) { 39534248Sbostic tmp = modf(integer / 10, &integer); 39634248Sbostic *p-- = (int)((tmp + .03) * 10) + '0'; 39734242Sbostic } 39834248Sbostic 39934248Sbostic /* copy, in reverse order, to start of buffer */ 40034248Sbostic p2 = startp; 40134248Sbostic *p2++ = *++p; 40234248Sbostic 40334248Sbostic /* 40434248Sbostic * if the format is g/G, and the resulting exponent will be 40534248Sbostic * greater than the precision, use e/E format. If e/E format, 40634248Sbostic * put in a decimal point as needed, and decrement precision 40734248Sbostic * count for each digit after the decimal point. 40834248Sbostic */ 40934248Sbostic if (format&GFORMAT && expcnt - 1 > prec || format&EFORMAT) { 41034248Sbostic if (format&GFORMAT) { 41134248Sbostic format |= EFORMAT; 41234248Sbostic 41334248Sbostic /* first digit is precision for g/G format */ 41434248Sbostic if (prec) 41534248Sbostic --prec; 41634248Sbostic } 41734248Sbostic if (decpt) 41834248Sbostic *p2++ = '.'; 41934248Sbostic for (; ++p < endp && prec; --prec, *p2++ = *p); 42034248Sbostic 42134318Sbostic /* precision ran out, round */ 42234248Sbostic if (p < endp) { 42334248Sbostic if (*p > '4') { 42434248Sbostic for (savep = p2--;; *p2-- = '0') { 42534248Sbostic if (*p2 == '.') 42634248Sbostic --p2; 42734248Sbostic if (++*p2 <= '9') 42834248Sbostic break; 42934248Sbostic } 43034248Sbostic p2 = savep; 43134248Sbostic } 43234248Sbostic fract = 0; 43334248Sbostic } 43434242Sbostic } 43534248Sbostic /* 43634314Sbostic * g/G in f format; if out of precision, replace digits with 43734314Sbostic * zeroes, note, have to round first. 43834248Sbostic */ 43934248Sbostic else if (format&GFORMAT) { 44034248Sbostic for (; ++p < endp && prec; --prec, *p2++ = *p); 44134248Sbostic /* precision ran out; round and then add zeroes */ 44234248Sbostic if (p < endp) { 44334248Sbostic if (*p > '4') { 44434248Sbostic for (savep = p2--; ++*p2 > '9'; 44534248Sbostic *p2-- = '0'); 44634248Sbostic p2 = savep; 44734248Sbostic } 44834248Sbostic do { 44934248Sbostic *p2++ = '0'; 45034248Sbostic } while (++p < endp); 45134248Sbostic fract = 0; 45234248Sbostic } 45334248Sbostic if (decpt) 45434248Sbostic *p2++ = '.'; 45534242Sbostic } 45634248Sbostic /* f format */ 45734248Sbostic else { 45834248Sbostic for (; ++p < endp; *p2++ = *p); 45934248Sbostic if (decpt) 46034248Sbostic *p2++ = '.'; 46134248Sbostic } 46234248Sbostic p = p2; 46334248Sbostic } 46434248Sbostic /* 46534318Sbostic * if no fraction, the number was zero, and if no precision, can't 46634318Sbostic * show anything after the decimal point. 46734248Sbostic */ 46834248Sbostic else if (!fract || !prec) { 46934248Sbostic *startp++ = '0'; 47034318Sbostic if (decpt && !(format&GFORMAT)) 47134248Sbostic *startp++ = '.'; 47234318Sbostic *startp = '\0'; 47334323Sbostic return(startp); 47434248Sbostic } 47534248Sbostic /* 47634248Sbostic * if the format is g/G, and the resulting exponent will be less than 47734248Sbostic * -4 use e/E format. If e/E format, compute exponent value. 47834248Sbostic */ 47934248Sbostic else if (format&GFORMAT && fract < .0001 || format&EFORMAT) { 48034248Sbostic format |= EFORMAT; 48134248Sbostic if (fract) 48234248Sbostic for (p = startp; fract;) { 48334248Sbostic fract = modf(fract * 10, &tmp); 48434248Sbostic if (!tmp) { 48534248Sbostic --expcnt; 48634248Sbostic continue; 48734248Sbostic } 48834248Sbostic *p++ = (int)tmp + '0'; 48934248Sbostic break; 49034248Sbostic } 49134242Sbostic else 49234248Sbostic *p++ = '0'; 49334248Sbostic 49434248Sbostic /* g/G format, decrement precision for first digit */ 49534248Sbostic if (format&GFORMAT && prec) 49634248Sbostic --prec; 49734248Sbostic 49834248Sbostic /* add decimal after first non-zero digit */ 49934248Sbostic if (decpt) 50034248Sbostic *p++ = '.'; 50134242Sbostic } 50234248Sbostic /* 50334248Sbostic * f format or g/G printed as f format; don't worry about decimal 50434248Sbostic * point, if g/G format doesn't need it, will get stripped later. 50534248Sbostic */ 50634242Sbostic else { 50734248Sbostic p = startp; 50834248Sbostic *p++ = '0'; 50934248Sbostic *p++ = '.'; 51034248Sbostic } 51134248Sbostic 51234319Sbostic /* finish out requested precision */ 51334319Sbostic while (fract && prec-- > 0) { 51434319Sbostic fract = modf(fract * 10, &tmp); 51534319Sbostic *p++ = (int)tmp + '0'; 51634319Sbostic } 51734319Sbostic while (prec-- > 0) 51834319Sbostic *p++ = '0'; 51934248Sbostic 52034248Sbostic /* 52134248Sbostic * if any fractional value left, "round" it back up to the beginning 52234248Sbostic * of the number, fixing the exponent as necessary, and avoiding the 52334248Sbostic * decimal point. 52434248Sbostic */ 52534248Sbostic if (fract) { 52634248Sbostic (void)modf(fract * 10, &tmp); 52734248Sbostic if (tmp > 4) { 52834248Sbostic for (savep = p--;; *p-- = '0') { 52934248Sbostic if (*p == '.') 53034248Sbostic --p; 53134248Sbostic if (p == startp) { 53234248Sbostic *p = '1'; 53334248Sbostic ++expcnt; 53434248Sbostic break; 53534248Sbostic } 53634248Sbostic if (++*p <= '9') 53734248Sbostic break; 53834242Sbostic } 53934248Sbostic p = savep; 54034242Sbostic } 54134248Sbostic } 54234248Sbostic 54334248Sbostic /* 54434248Sbostic * if a g/G format and not alternate flag, lose trailing zeroes, 54534248Sbostic * if e/E or g/G format, and last char is decimal point, lose it. 54634248Sbostic */ 54734318Sbostic if (!(flags&ALT)) { 54834248Sbostic if (format&GFORMAT) 54934248Sbostic for (; p[-1] == '0'; --p); 55034248Sbostic if (format&(GFORMAT|EFORMAT) && p[-1] == '.') 55134248Sbostic --p; 55234248Sbostic } 55334248Sbostic 55434248Sbostic /* if an e/E format, add exponent */ 55534248Sbostic if (format&EFORMAT) { 55634248Sbostic *p++ = fmtch; 55734248Sbostic if (--expcnt < 0) { 55834248Sbostic expcnt = -expcnt; 55934248Sbostic *p++ = '-'; 56034242Sbostic } 56134248Sbostic else 56234248Sbostic *p++ = '+'; 56334248Sbostic *p++ = expcnt / 10 + '0'; 56434248Sbostic *p++ = expcnt % 10 + '0'; 56534242Sbostic } 56634323Sbostic return(p); 56734242Sbostic } 568