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*34319Sbostic static char sccsid[] = "@(#)vfprintf.c 5.15 (Berkeley) 05/17/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*34319Sbostic /* 23*34319Sbostic * To handle arbitrary floating point precision, the buffer has to hold the 24*34319Sbostic * number, a decimal point, and N precision digits. We can't just truncate 25*34319Sbostic * at some point is that the lower-level math routines may very well be 26*34319Sbostic * repeatedly returning some small fraction. A 128 bit fraction can be 27*34319Sbostic * represented in 39 decimal digits. Guess a max of 40 digits of precision, 28*34319Sbostic * and add one for the decimal point. 29*34319Sbostic */ 30*34319Sbostic #define MAXFRAC 39 31*34319Sbostic #define MAXPREC 40 32*34319Sbostic #define MAXDIGIT (MAXFRAC + MAXPREC + 1) 3334226Sbostic 3434263Sbostic #define PUTC(ch) {++cnt; putc(ch, fp);} 3534236Sbostic 3634318Sbostic #define ARG() \ 3734318Sbostic _ulong = flags&LONGINT ? va_arg(argp, long) : \ 3834318Sbostic flags&SHORTINT ? va_arg(argp, short) : va_arg(argp, int); 3934235Sbostic 4034318Sbostic /* have to deal with the negative buffer count kludge */ 4134318Sbostic #define NEGATIVE_COUNT_KLUDGE 4234318Sbostic 4334318Sbostic #define LONGINT 0x01 /* long integer */ 4434318Sbostic #define LONGDBL 0x02 /* long double; unimplemented */ 4534318Sbostic #define SHORTINT 0x04 /* short integer */ 4634318Sbostic #define ALT 0x08 /* alternate form */ 4734318Sbostic #define LADJUST 0x10 /* left adjustment */ 4834318Sbostic static int flags; 4934318Sbostic 50*34319Sbostic static char sign, *buf; 5134248Sbostic 5234235Sbostic x_doprnt(fmt, argp, fp) 5334233Sbostic register char *fmt; 5434233Sbostic va_list argp; 5534235Sbostic register FILE *fp; 5634226Sbostic { 5734314Sbostic register int cnt, n; 5834314Sbostic register char ch, *t; 5934235Sbostic double _double; 6034314Sbostic u_long _ulong; 6134318Sbostic int base, width, prec, size; 62*34319Sbostic char padc, *digs, sbuf[MAXDIGIT]; 6334226Sbostic 6434243Sbostic digs = "0123456789abcdef"; 65*34319Sbostic for (buf = sbuf, cnt = 0;; ++fmt) { 6634318Sbostic n = fp->_cnt; 6734318Sbostic for (t = fp->_ptr; (ch = *fmt) && ch != '%'; ++cnt, ++fmt) 6834318Sbostic if (--n < 0 6934318Sbostic #ifdef NEGATIVE_COUNT_KLUDGE 7034318Sbostic && (!(fp->_flag & _IOLBF) || -n >= fp->_bufsiz) 7134318Sbostic #endif 7234318Sbostic || ch == '\n' && fp->_flag&_IOLBF) { 7334318Sbostic fp->_cnt = n; 7434318Sbostic fp->_ptr = t; 7534318Sbostic (void)_flsbuf(ch, fp); 7634318Sbostic n = fp->_cnt; 7734318Sbostic t = fp->_ptr; 7834318Sbostic } 7934318Sbostic else 8034314Sbostic *t++ = ch; 8134318Sbostic fp->_cnt = n; 8234318Sbostic fp->_ptr = t; 8334318Sbostic if (!ch) 8434314Sbostic return(cnt); 8534314Sbostic 8634318Sbostic flags = width = 0; 8734233Sbostic prec = -1; 8834233Sbostic padc = ' '; 8934318Sbostic sign = '\0'; 9034226Sbostic 9134318Sbostic rflag: switch (*++fmt) { 9234318Sbostic case ' ': 9334318Sbostic sign = ' '; 9434318Sbostic goto rflag; 9534233Sbostic case '#': 9634318Sbostic flags |= ALT; 9734318Sbostic goto rflag; 9834233Sbostic case '*': 9934235Sbostic /* 10034235Sbostic * ``A negative field width argument is taken as a 10134235Sbostic * - flag followed by a positive field width.'' 10234235Sbostic * -- ANSI X3J11 10334235Sbostic * They don't exclude field widths read from args. 10434235Sbostic */ 10534235Sbostic if ((width = va_arg(argp, int)) >= 0) 10634318Sbostic goto rflag; 10734235Sbostic width = -width; 10834235Sbostic /*FALLTHROUGH*/ 10934235Sbostic case '-': 11034318Sbostic flags |= LADJUST; 11134318Sbostic goto rflag; 11234233Sbostic case '+': 11334314Sbostic sign = '+'; 11434318Sbostic goto rflag; 11534233Sbostic case '.': 11634235Sbostic if (*++fmt == '*') 11734318Sbostic n = va_arg(argp, int); 11834318Sbostic else if (isascii(*fmt) && isdigit(*fmt)) { 11934318Sbostic n = 0; 12034233Sbostic do { 12134318Sbostic n = 10 * n + *fmt - '0'; 12234318Sbostic } while (isascii(*++fmt) && isdigit(*fmt)); 12334233Sbostic --fmt; 12434226Sbostic } 12534235Sbostic else { 12634318Sbostic --fmt; 12734235Sbostic prec = 0; 12834318Sbostic goto rflag; 12934235Sbostic } 13034318Sbostic prec = n < 0 ? -1 : n; 13134318Sbostic goto rflag; 13234233Sbostic case '0': 13334233Sbostic padc = '0'; 13434235Sbostic /*FALLTHROUGH*/ 13534233Sbostic case '1': case '2': case '3': case '4': 13634233Sbostic case '5': case '6': case '7': case '8': case '9': 13734318Sbostic n = 0; 13834233Sbostic do { 13934318Sbostic n = 10 * n + *fmt - '0'; 14034318Sbostic } while (isascii(*++fmt) && isdigit(*fmt)); 14134318Sbostic width = n; 14234233Sbostic --fmt; 143*34319Sbostic goto rflag; 14434235Sbostic case 'L': 14534318Sbostic /* 14634318Sbostic * C doesn't have a long double; use long for now. 14734318Sbostic * flags |= LONGDBL; 14834318Sbostic */ 14934318Sbostic flags |= LONGINT; 15034318Sbostic goto rflag; 15134235Sbostic case 'h': 15234318Sbostic flags |= SHORTINT; 15334318Sbostic goto rflag; 15434233Sbostic case 'l': 15534318Sbostic flags |= LONGINT; 15634318Sbostic goto rflag; 15734314Sbostic case 'c': 158*34319Sbostic buf[0] = va_arg(argp, int); 15934314Sbostic size = 1; 160*34319Sbostic t = buf; 16134314Sbostic goto pforw; 16234314Sbostic case 'd': 16334318Sbostic case 'i': 16434318Sbostic ARG(); 16534318Sbostic if ((long)_ulong < 0) { 16634318Sbostic _ulong = -_ulong; 16734314Sbostic sign = '-'; 16834241Sbostic } 16934314Sbostic if (sign) 17034314Sbostic PUTC(sign); 17134241Sbostic base = 10; 17234314Sbostic goto num; 17334261Sbostic case 'e': 17434236Sbostic case 'E': 17534235Sbostic case 'f': 17634261Sbostic case 'g': 17734243Sbostic case 'G': 17834243Sbostic _double = va_arg(argp, double); 179*34319Sbostic size = _cvt(_double, prec, *fmt); 180*34319Sbostic t = buf; 18134314Sbostic goto pforw; 18234235Sbostic case 'n': 18334318Sbostic if (flags&LONGDBL || flags&LONGINT) 18434318Sbostic *va_arg(argp, long *) = cnt; 18534318Sbostic else if (flags&SHORTINT) 18634318Sbostic *va_arg(argp, short *) = cnt; 18734318Sbostic else 18834318Sbostic *va_arg(argp, int *) = cnt; 18934235Sbostic break; 19034226Sbostic case 'o': 19134318Sbostic ARG(); 19234226Sbostic base = 8; 19334314Sbostic goto num; 19434235Sbostic case 'p': 19534226Sbostic case 's': 19634314Sbostic if (!(t = va_arg(argp, char *))) 19734314Sbostic t = "(null)"; 19834316Sbostic if ((size = strlen(t)) > prec && prec >= 0) 19934314Sbostic size = prec; 20034318Sbostic pforw: if (!(flags&LADJUST) && width) 20134314Sbostic for (n = size; n++ < width;) 20234314Sbostic PUTC(padc); 20334314Sbostic if (fp->_cnt - (n = size) >= 0) { 20434314Sbostic cnt += n; 20534314Sbostic fp->_cnt -= n; 20634314Sbostic bcopy(t, fp->_ptr, n); 20734314Sbostic fp->_ptr += n; 20834233Sbostic } 20934314Sbostic else for (; n--; ++t) 21034314Sbostic PUTC(*t); 21134318Sbostic if (flags&LADJUST) 21234314Sbostic while (width-- > size) 21334314Sbostic PUTC(padc); 21434226Sbostic break; 21534226Sbostic case 'u': 21634318Sbostic ARG(); 21734226Sbostic base = 10; 21834314Sbostic goto num; 21934226Sbostic case 'X': 22034226Sbostic digs = "0123456789ABCDEF"; 22134233Sbostic /*FALLTHROUGH*/ 22234226Sbostic case 'x': 22334318Sbostic ARG(); 22434314Sbostic base = 16; 22534314Sbostic /* alternate form for hex; leading 0x/X */ 22634318Sbostic if (flags&ALT && _ulong) { 22734263Sbostic PUTC('0'); 22834263Sbostic PUTC(*fmt); 22934233Sbostic } 230*34319Sbostic num: t = buf + MAXDIGIT - 1; 23134233Sbostic do { 23234314Sbostic *t-- = digs[_ulong % base]; 23334314Sbostic _ulong /= base; 23434314Sbostic } while(_ulong); 23534314Sbostic digs = "0123456789abcdef"; 236*34319Sbostic size = buf + MAXDIGIT - 1 - t; 23734314Sbostic if (size >= prec) { 23834314Sbostic /* alternate form for octal; leading 0 */ 23934318Sbostic if (t[1] != '0' && flags&ALT && *fmt == 'o') { 24034314Sbostic *t-- = '0'; 24134314Sbostic ++size; 24234314Sbostic } 24334314Sbostic } 24434314Sbostic else 24534314Sbostic for (; size < prec; ++size) 24634314Sbostic *t-- = '0'; 24734318Sbostic if (!(flags&LADJUST)) 24834314Sbostic while (size++ < width) 24934263Sbostic PUTC(padc); 250*34319Sbostic while (++t < buf + MAXDIGIT) 25134314Sbostic PUTC(*t); 25234235Sbostic for (; width > size; --width) 25334263Sbostic PUTC(padc); 25434226Sbostic break; 25534233Sbostic case '\0': /* "%?" prints ?, unless ? is NULL */ 25634314Sbostic return(cnt); 25734226Sbostic default: 25834263Sbostic PUTC(*fmt); 25934226Sbostic } 26034226Sbostic } 26134314Sbostic /*NOTREACHED*/ 26234226Sbostic } 26334242Sbostic 26434261Sbostic #define EFORMAT 0x01 26534261Sbostic #define FFORMAT 0x02 26634261Sbostic #define GFORMAT 0x04 26734314Sbostic #define DEFPREC 6 26834261Sbostic 269*34319Sbostic static 270*34319Sbostic _cvt(number, prec, fmtch) 27134242Sbostic double number; 27234261Sbostic register int prec; 273*34319Sbostic char fmtch; 27434242Sbostic { 27534248Sbostic register char *p; 27634261Sbostic register int expcnt, format; 277*34319Sbostic static int maxprec = MAXPREC; 27834248Sbostic double fract, integer, tmp, modf(); 27934261Sbostic int decpt; 280*34319Sbostic char *endp, *savep, *startp, *malloc(); 28134242Sbostic 28234314Sbostic if (prec == -1) 28334242Sbostic prec = DEFPREC; 28434242Sbostic 285*34319Sbostic /* allocate space for large precision */ 286*34319Sbostic if (prec > maxprec) 287*34319Sbostic buf = malloc((u_int)((maxprec = prec) + MAXFRAC + 1)); 288*34319Sbostic 289*34319Sbostic startp = buf; 29034314Sbostic if (number < 0) { 29134248Sbostic *startp++ = '-'; 29234248Sbostic number = -number; 29334248Sbostic } 29434314Sbostic else if (sign) 29534318Sbostic *startp++ = sign; 29634242Sbostic 29734261Sbostic switch(fmtch) { 29834261Sbostic case 'e': 29934261Sbostic case 'E': 30034261Sbostic format = EFORMAT; 30134261Sbostic break; 30234261Sbostic case 'f': 30334261Sbostic format = FFORMAT; 30434261Sbostic break; 30534261Sbostic case 'g': 30634261Sbostic case 'G': 30734261Sbostic format = GFORMAT; 30834261Sbostic fmtch -= 2; 30934261Sbostic } 31034261Sbostic 31134248Sbostic /* 31234248Sbostic * if the alternate flag is set, or, at least one digit of precision 31334248Sbostic * was requested, add a decimal point, unless it's the g/G format 31434314Sbostic * in which case we require two digits of precision, as it counts 31534248Sbostic * precision differently. 31634248Sbostic */ 31734318Sbostic decpt = flags&ALT || prec > (format&GFORMAT ? 1 : 0); 31834248Sbostic 31934248Sbostic expcnt = 0; 320*34319Sbostic p = buf + maxprec + MAXFRAC; 321*34319Sbostic endp = p + 1; 32234248Sbostic fract = modf(number, &integer); 32334248Sbostic if (integer) { 32434248Sbostic register char *p2; 32534248Sbostic 32634248Sbostic /* get integer part of number; count decimal places */ 32734248Sbostic for (; integer; ++expcnt) { 32834248Sbostic tmp = modf(integer / 10, &integer); 32934248Sbostic *p-- = (int)((tmp + .03) * 10) + '0'; 33034242Sbostic } 33134248Sbostic 33234248Sbostic /* copy, in reverse order, to start of buffer */ 33334248Sbostic p2 = startp; 33434248Sbostic *p2++ = *++p; 33534248Sbostic 33634248Sbostic /* 33734248Sbostic * if the format is g/G, and the resulting exponent will be 33834248Sbostic * greater than the precision, use e/E format. If e/E format, 33934248Sbostic * put in a decimal point as needed, and decrement precision 34034248Sbostic * count for each digit after the decimal point. 34134248Sbostic */ 34234248Sbostic if (format&GFORMAT && expcnt - 1 > prec || format&EFORMAT) { 34334248Sbostic if (format&GFORMAT) { 34434248Sbostic format |= EFORMAT; 34534248Sbostic 34634248Sbostic /* first digit is precision for g/G format */ 34734248Sbostic if (prec) 34834248Sbostic --prec; 34934248Sbostic } 35034248Sbostic if (decpt) 35134248Sbostic *p2++ = '.'; 35234248Sbostic for (; ++p < endp && prec; --prec, *p2++ = *p); 35334248Sbostic 35434318Sbostic /* precision ran out, round */ 35534248Sbostic if (p < endp) { 35634248Sbostic if (*p > '4') { 35734248Sbostic for (savep = p2--;; *p2-- = '0') { 35834248Sbostic if (*p2 == '.') 35934248Sbostic --p2; 36034248Sbostic if (++*p2 <= '9') 36134248Sbostic break; 36234248Sbostic } 36334248Sbostic p2 = savep; 36434248Sbostic } 36534248Sbostic fract = 0; 36634248Sbostic } 36734242Sbostic } 36834248Sbostic /* 36934314Sbostic * g/G in f format; if out of precision, replace digits with 37034314Sbostic * zeroes, note, have to round first. 37134248Sbostic */ 37234248Sbostic else if (format&GFORMAT) { 37334248Sbostic for (; ++p < endp && prec; --prec, *p2++ = *p); 37434248Sbostic /* precision ran out; round and then add zeroes */ 37534248Sbostic if (p < endp) { 37634248Sbostic if (*p > '4') { 37734248Sbostic for (savep = p2--; ++*p2 > '9'; 37834248Sbostic *p2-- = '0'); 37934248Sbostic p2 = savep; 38034248Sbostic } 38134248Sbostic do { 38234248Sbostic *p2++ = '0'; 38334248Sbostic } while (++p < endp); 38434248Sbostic fract = 0; 38534248Sbostic } 38634248Sbostic if (decpt) 38734248Sbostic *p2++ = '.'; 38834242Sbostic } 38934248Sbostic /* f format */ 39034248Sbostic else { 39134248Sbostic for (; ++p < endp; *p2++ = *p); 39234248Sbostic if (decpt) 39334248Sbostic *p2++ = '.'; 39434248Sbostic } 39534248Sbostic p = p2; 39634248Sbostic } 39734248Sbostic /* 39834318Sbostic * if no fraction, the number was zero, and if no precision, can't 39934318Sbostic * show anything after the decimal point. 40034248Sbostic */ 40134248Sbostic else if (!fract || !prec) { 40234248Sbostic *startp++ = '0'; 40334318Sbostic if (decpt && !(format&GFORMAT)) 40434248Sbostic *startp++ = '.'; 40534318Sbostic *startp = '\0'; 406*34319Sbostic return(startp - buf); 40734248Sbostic } 40834248Sbostic /* 40934248Sbostic * if the format is g/G, and the resulting exponent will be less than 41034248Sbostic * -4 use e/E format. If e/E format, compute exponent value. 41134248Sbostic */ 41234248Sbostic else if (format&GFORMAT && fract < .0001 || format&EFORMAT) { 41334248Sbostic format |= EFORMAT; 41434248Sbostic if (fract) 41534248Sbostic for (p = startp; fract;) { 41634248Sbostic fract = modf(fract * 10, &tmp); 41734248Sbostic if (!tmp) { 41834248Sbostic --expcnt; 41934248Sbostic continue; 42034248Sbostic } 42134248Sbostic *p++ = (int)tmp + '0'; 42234248Sbostic break; 42334248Sbostic } 42434242Sbostic else 42534248Sbostic *p++ = '0'; 42634248Sbostic 42734248Sbostic /* g/G format, decrement precision for first digit */ 42834248Sbostic if (format&GFORMAT && prec) 42934248Sbostic --prec; 43034248Sbostic 43134248Sbostic /* add decimal after first non-zero digit */ 43234248Sbostic if (decpt) 43334248Sbostic *p++ = '.'; 43434242Sbostic } 43534248Sbostic /* 43634248Sbostic * f format or g/G printed as f format; don't worry about decimal 43734248Sbostic * point, if g/G format doesn't need it, will get stripped later. 43834248Sbostic */ 43934242Sbostic else { 44034248Sbostic p = startp; 44134248Sbostic *p++ = '0'; 44234248Sbostic *p++ = '.'; 44334248Sbostic } 44434248Sbostic 445*34319Sbostic /* finish out requested precision */ 446*34319Sbostic while (fract && prec-- > 0) { 447*34319Sbostic fract = modf(fract * 10, &tmp); 448*34319Sbostic *p++ = (int)tmp + '0'; 449*34319Sbostic } 450*34319Sbostic while (prec-- > 0) 451*34319Sbostic *p++ = '0'; 45234248Sbostic 45334248Sbostic /* 45434248Sbostic * if any fractional value left, "round" it back up to the beginning 45534248Sbostic * of the number, fixing the exponent as necessary, and avoiding the 45634248Sbostic * decimal point. 45734248Sbostic */ 45834248Sbostic if (fract) { 45934248Sbostic (void)modf(fract * 10, &tmp); 46034248Sbostic if (tmp > 4) { 46134248Sbostic for (savep = p--;; *p-- = '0') { 46234248Sbostic if (*p == '.') 46334248Sbostic --p; 46434248Sbostic if (p == startp) { 46534248Sbostic *p = '1'; 46634248Sbostic ++expcnt; 46734248Sbostic break; 46834248Sbostic } 46934248Sbostic if (++*p <= '9') 47034248Sbostic break; 47134242Sbostic } 47234248Sbostic p = savep; 47334242Sbostic } 47434248Sbostic } 47534248Sbostic 47634248Sbostic /* 47734248Sbostic * if a g/G format and not alternate flag, lose trailing zeroes, 47834248Sbostic * if e/E or g/G format, and last char is decimal point, lose it. 47934248Sbostic */ 48034318Sbostic if (!(flags&ALT)) { 48134248Sbostic if (format&GFORMAT) 48234248Sbostic for (; p[-1] == '0'; --p); 48334248Sbostic if (format&(GFORMAT|EFORMAT) && p[-1] == '.') 48434248Sbostic --p; 48534248Sbostic } 48634248Sbostic 48734248Sbostic /* if an e/E format, add exponent */ 48834248Sbostic if (format&EFORMAT) { 48934248Sbostic *p++ = fmtch; 49034248Sbostic if (--expcnt < 0) { 49134248Sbostic expcnt = -expcnt; 49234248Sbostic *p++ = '-'; 49334242Sbostic } 49434248Sbostic else 49534248Sbostic *p++ = '+'; 49634248Sbostic *p++ = expcnt / 10 + '0'; 49734248Sbostic *p++ = expcnt % 10 + '0'; 49834242Sbostic } 499*34319Sbostic return(p - buf); 50034242Sbostic } 501