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*34428Sbostic static char sccsid[] = "@(#)vfprintf.c 5.29 (Berkeley) 05/23/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 2234328Sbostic /* 11-bit exponent (VAX G floating point) is 308 decimal digits */ 2334328Sbostic #define MAXEXP 308 2434328Sbostic /* 128 bit fraction takes up 39 decimal digits; max reasonable precision */ 2534328Sbostic #define MAXFRACT 39 2634226Sbostic 2734328Sbostic #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ 2834328Sbostic 2934427Sbostic #define PUTC(ch) (void) putc(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 3534331Sbostic #define todigit(c) ((c) - '0') 3634331Sbostic #define tochar(n) ((n) + '0') 3734331Sbostic 3834318Sbostic /* have to deal with the negative buffer count kludge */ 3934318Sbostic #define NEGATIVE_COUNT_KLUDGE 4034318Sbostic 4134318Sbostic #define LONGINT 0x01 /* long integer */ 4234318Sbostic #define LONGDBL 0x02 /* long double; unimplemented */ 4334318Sbostic #define SHORTINT 0x04 /* short integer */ 4434318Sbostic #define ALT 0x08 /* alternate form */ 4534318Sbostic #define LADJUST 0x10 /* left adjustment */ 4634427Sbostic #define ZEROPAD 0x20 /* zero (as opposed to blank) pad */ 4734427Sbostic #define HEXPREFIX 0x40 /* add 0x or 0X prefix */ 4834318Sbostic 4934323Sbostic _doprnt(fmt0, argp, fp) 5034323Sbostic u_char *fmt0; 5134233Sbostic va_list argp; 5234235Sbostic register FILE *fp; 5334226Sbostic { 5434427Sbostic register u_char *fmt; /* format string */ 5534427Sbostic register int ch; /* character from fmt */ 5634427Sbostic register int cnt; /* return value accumulator */ 5734427Sbostic register int n; /* random handy integer */ 5834427Sbostic register char *t; /* buffer pointer */ 5934427Sbostic double _double; /* double precision arguments %[eEfgG] */ 6034427Sbostic u_long _ulong; /* integer arguments %[diouxX] */ 6134427Sbostic int flags; /* flags as above */ 6234427Sbostic int dprec; /* decimal precision in [diouxX] */ 6334427Sbostic int fpprec; /* `extra' floating precision in [eEfgG] */ 6434427Sbostic int width; /* width from format (%8d), or 0 */ 6534427Sbostic int prec; /* precision from format (%.3d), or -1 */ 6634427Sbostic int size; /* size of converted field or string */ 6734427Sbostic int fieldsz; /* field size expanded by sign, etc */ 6834427Sbostic int realsz; /* field size expanded by decimal precision */ 6934427Sbostic char sign; /* sign prefix (+ - or \0) */ 7034427Sbostic int base; /* base for [diouxX] conversion */ 7134427Sbostic char *digs; /* digits for [diouxX] conversion */ 7234427Sbostic char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ 7334427Sbostic char *_cvt(); /* handles [eEfgG] formats */ 7434226Sbostic 75*34428Sbostic if (fp->_flag & _IORW) { 76*34428Sbostic fp->_flag |= _IOWRT; 77*34428Sbostic fp->_flag &= ~(_IOEOF|_IOREAD); 78*34428Sbostic } 79*34428Sbostic if ((fp->_flag & _IOWRT) == 0) 80*34428Sbostic return (EOF); 81*34428Sbostic 8234323Sbostic fmt = fmt0; 8334243Sbostic digs = "0123456789abcdef"; 8434322Sbostic for (cnt = 0;; ++fmt) { 8534318Sbostic n = fp->_cnt; 8634427Sbostic for (t = (char *)fp->_ptr; (ch = *fmt) && ch != '%'; 8734427Sbostic ++cnt, ++fmt) 8834318Sbostic if (--n < 0 8934318Sbostic #ifdef NEGATIVE_COUNT_KLUDGE 9034318Sbostic && (!(fp->_flag & _IOLBF) || -n >= fp->_bufsiz) 9134318Sbostic #endif 9234427Sbostic || ch == '\n' && fp->_flag & _IOLBF) { 9334318Sbostic fp->_cnt = n; 9434427Sbostic fp->_ptr = (u_char *)t; 9534427Sbostic (void) _flsbuf((u_char)ch, fp); 9634318Sbostic n = fp->_cnt; 9734427Sbostic t = (char *)fp->_ptr; 9834427Sbostic } else 9934314Sbostic *t++ = ch; 10034318Sbostic fp->_cnt = n; 10134427Sbostic fp->_ptr = (u_char *)t; 10234318Sbostic if (!ch) 10334427Sbostic return (cnt); 10434314Sbostic 10534427Sbostic flags = dprec = fpprec = width = 0; 10634233Sbostic prec = -1; 10734318Sbostic sign = '\0'; 10834226Sbostic 10934318Sbostic rflag: switch (*++fmt) { 11034318Sbostic case ' ': 11134318Sbostic sign = ' '; 11234318Sbostic goto rflag; 11334233Sbostic case '#': 11434318Sbostic flags |= ALT; 11534318Sbostic goto rflag; 11634233Sbostic case '*': 11734235Sbostic /* 11834235Sbostic * ``A negative field width argument is taken as a 11934235Sbostic * - flag followed by a positive field width.'' 12034235Sbostic * -- ANSI X3J11 12134235Sbostic * They don't exclude field widths read from args. 12234235Sbostic */ 12334235Sbostic if ((width = va_arg(argp, int)) >= 0) 12434318Sbostic goto rflag; 12534235Sbostic width = -width; 12634427Sbostic /* FALLTHROUGH */ 12734235Sbostic case '-': 12834318Sbostic flags |= LADJUST; 12934318Sbostic goto rflag; 13034233Sbostic case '+': 13134314Sbostic sign = '+'; 13234318Sbostic goto rflag; 13334233Sbostic case '.': 13434235Sbostic if (*++fmt == '*') 13534318Sbostic n = va_arg(argp, int); 13634427Sbostic else { 13734318Sbostic n = 0; 13834427Sbostic while (isascii(*fmt) && isdigit(*fmt)) 13934427Sbostic n = 10 * n + todigit(*fmt++); 14034233Sbostic --fmt; 14134226Sbostic } 14234318Sbostic prec = n < 0 ? -1 : n; 14334318Sbostic goto rflag; 14434233Sbostic case '0': 14534427Sbostic /* 14634427Sbostic * ``Note that 0 is taken as a flag, not as the 14734427Sbostic * beginning of a field width.'' 14834427Sbostic * -- ANSI X3J11 14934427Sbostic */ 15034427Sbostic flags |= ZEROPAD; 15134427Sbostic goto rflag; 15234233Sbostic case '1': case '2': case '3': case '4': 15334233Sbostic case '5': case '6': case '7': case '8': case '9': 15434318Sbostic n = 0; 15534233Sbostic do { 15634331Sbostic n = 10 * n + todigit(*fmt); 15734318Sbostic } while (isascii(*++fmt) && isdigit(*fmt)); 15834318Sbostic width = n; 15934233Sbostic --fmt; 16034319Sbostic goto rflag; 16134235Sbostic case 'L': 16234329Sbostic flags |= LONGDBL; 16334318Sbostic goto rflag; 16434235Sbostic case 'h': 16534318Sbostic flags |= SHORTINT; 16634318Sbostic goto rflag; 16734233Sbostic case 'l': 16834318Sbostic flags |= LONGINT; 16934318Sbostic goto rflag; 17034314Sbostic case 'c': 17134427Sbostic *(t = buf) = va_arg(argp, int); 17234314Sbostic size = 1; 17334427Sbostic sign = '\0'; 17434314Sbostic goto pforw; 17534314Sbostic case 'd': 17634318Sbostic case 'i': 17734318Sbostic ARG(); 17834318Sbostic if ((long)_ulong < 0) { 17934318Sbostic _ulong = -_ulong; 18034314Sbostic sign = '-'; 18134241Sbostic } 18234241Sbostic base = 10; 18334327Sbostic goto number; 18434261Sbostic case 'e': 18534236Sbostic case 'E': 18634235Sbostic case 'f': 18734261Sbostic case 'g': 18834243Sbostic case 'G': 18934243Sbostic _double = va_arg(argp, double); 19034328Sbostic /* 19134328Sbostic * don't bother to do unrealistic precision; just 19234328Sbostic * pad it with zeroes later. This keeps buffer size 19334328Sbostic * rational. 19434328Sbostic */ 19534328Sbostic if (prec > MAXFRACT) { 19634328Sbostic fpprec = prec - MAXFRACT; 19734328Sbostic prec = MAXFRACT; 19834328Sbostic } 19934319Sbostic t = buf; 20034427Sbostic size = _cvt(_double, prec, flags, *fmt, &sign, 20134427Sbostic t, t + sizeof(buf)) - t; 20234314Sbostic goto pforw; 20334235Sbostic case 'n': 20434427Sbostic if (flags & LONGINT) 20534318Sbostic *va_arg(argp, long *) = cnt; 20634427Sbostic else if (flags & SHORTINT) 20734318Sbostic *va_arg(argp, short *) = cnt; 20834318Sbostic else 20934318Sbostic *va_arg(argp, int *) = cnt; 21034235Sbostic break; 21134226Sbostic case 'o': 21234318Sbostic ARG(); 21334226Sbostic base = 8; 21434327Sbostic goto nosign; 21534235Sbostic case 'p': 21634320Sbostic /* 21734321Sbostic * ``The argument shall be a pointer to void. The 21834321Sbostic * value of the pointer is converted to a sequence 21934321Sbostic * of printable characters, in an implementation- 22034321Sbostic * defined manner.'' 22134321Sbostic * -- ANSI X3J11 22234320Sbostic */ 22334427Sbostic /* NOSTRICT */ 22434320Sbostic _ulong = (u_long)va_arg(argp, void *); 22534320Sbostic base = 16; 22634327Sbostic goto nosign; 22734226Sbostic case 's': 22834314Sbostic if (!(t = va_arg(argp, char *))) 22934314Sbostic t = "(null)"; 23034321Sbostic if (prec >= 0) { 23134321Sbostic /* 23234321Sbostic * can't use strlen; can only look for the 23334321Sbostic * NUL in the first `prec' characters, and 23434321Sbostic * strlen() will go further. 23534321Sbostic */ 23634321Sbostic char *p, *memchr(); 23734321Sbostic 23834321Sbostic if (p = memchr(t, 0, prec)) { 23934321Sbostic size = p - t; 24034321Sbostic if (size > prec) 24134321Sbostic size = prec; 24234427Sbostic } else 24334321Sbostic size = prec; 24434427Sbostic } else 24534321Sbostic size = strlen(t); 24634427Sbostic sign = '\0'; 24734328Sbostic goto pforw; 24834226Sbostic case 'u': 24934318Sbostic ARG(); 25034226Sbostic base = 10; 25134327Sbostic goto nosign; 25234226Sbostic case 'X': 25334226Sbostic digs = "0123456789ABCDEF"; 25434427Sbostic /* FALLTHROUGH */ 25534226Sbostic case 'x': 25634318Sbostic ARG(); 25734314Sbostic base = 16; 25834326Sbostic /* leading 0x/X only if non-zero */ 25934427Sbostic if (flags & ALT && _ulong != 0) 26034427Sbostic flags |= HEXPREFIX; 26134327Sbostic 26234327Sbostic /* unsigned conversions */ 26334427Sbostic nosign: sign = '\0'; 26434326Sbostic /* 26534330Sbostic * ``... diouXx conversions ... if a precision is 26634330Sbostic * specified, the 0 flag will be ignored.'' 26734330Sbostic * -- ANSI X3J11 26834330Sbostic */ 26934427Sbostic number: if ((dprec = prec) >= 0) 27034427Sbostic flags &= ~ZEROPAD; 27134427Sbostic 27234330Sbostic /* 27334326Sbostic * ``The result of converting a zero value with an 27434326Sbostic * explicit precision of zero is no characters.'' 27534326Sbostic * -- ANSI X3J11 27634326Sbostic */ 27734427Sbostic t = buf + BUF; 27834427Sbostic if (_ulong != 0 || prec != 0) { 27934427Sbostic do { 28034427Sbostic *--t = digs[_ulong % base]; 28134427Sbostic _ulong /= base; 28234427Sbostic } while (_ulong); 28334427Sbostic digs = "0123456789abcdef"; 28434427Sbostic if (flags & ALT && base == 8 && *t != '0') 28534427Sbostic *--t = '0'; /* octal leading 0 */ 28634330Sbostic } 28734427Sbostic size = buf + BUF - t; 28834327Sbostic 28934427Sbostic pforw: 29034427Sbostic /* 29134427Sbostic * All reasonable formats wind up here. At this 29234427Sbostic * point, `t' points to a string which (if not 29334427Sbostic * flags&LADJUST) should be padded out to `width' 29434427Sbostic * places. If flags&ZEROPAD, it should first be 29534427Sbostic * prefixed by any sign or other prefix; otherwise, 29634427Sbostic * it should be blank padded before the prefix is 29734427Sbostic * emitted. After any left-hand padding and 29834427Sbostic * prefixing, emit zeroes required by a decimal 29934427Sbostic * [diouxX] precision, then print the string proper, 30034427Sbostic * then emit zeroes required by any leftover floating 30134427Sbostic * precision; finally, if LADJUST, pad with blanks. 30234427Sbostic */ 30334327Sbostic 30434427Sbostic /* compute actual size, so we know how much to pad */ 30534427Sbostic /* this code is not terribly satisfactory */ 30634427Sbostic /* fieldsz excludes decimal prec; realsz includes it */ 30734427Sbostic fieldsz = size + fpprec; 30834427Sbostic if (sign) 30934427Sbostic fieldsz++; 31034427Sbostic if (flags & HEXPREFIX) 31134427Sbostic fieldsz += 2; 31234427Sbostic realsz = dprec > fieldsz ? dprec : fieldsz; 31334327Sbostic 31434427Sbostic /* right-adjusting blank padding */ 31534427Sbostic if ((flags & (LADJUST|ZEROPAD)) == 0 && width) 31634427Sbostic for (n = realsz; n < width; n++) 31734427Sbostic PUTC(' '); 31834427Sbostic /* prefix */ 31934427Sbostic if (sign) 32034427Sbostic PUTC(sign); 32134427Sbostic if (flags & HEXPREFIX) { 32234427Sbostic PUTC('0'); 32334427Sbostic PUTC((char)*fmt); 32434327Sbostic } 32534427Sbostic /* right-adjusting zero padding */ 32634427Sbostic if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 32734427Sbostic for (n = realsz; n < width; n++) 32834427Sbostic PUTC('0'); 32934427Sbostic /* leading zeroes from decimal precision */ 33034427Sbostic for (n = fieldsz; n < dprec; n++) 33134427Sbostic PUTC('0'); 33234327Sbostic 33334427Sbostic /* the string or number proper */ 33434427Sbostic if (fp->_cnt - (n = size) >= 0 && 33534427Sbostic (fp->_flag & _IOLBF) == 0) { 33634328Sbostic fp->_cnt -= n; 33734427Sbostic bcopy(t, (char *)fp->_ptr, n); 33834328Sbostic fp->_ptr += n; 33934427Sbostic } else 34034427Sbostic while (--n >= 0) 34134427Sbostic PUTC(*t++); 34234427Sbostic /* trailing f.p. zeroes */ 34334427Sbostic while (--fpprec >= 0) 34434328Sbostic PUTC('0'); 34534427Sbostic /* left-adjusting padding (always blank) */ 34634427Sbostic if (flags & LADJUST) 34734427Sbostic for (n = realsz; n < width; n++) 34834328Sbostic PUTC(' '); 34934427Sbostic 35034427Sbostic /* finally, adjust cnt */ 35134427Sbostic cnt += width > realsz ? width : realsz; 35234226Sbostic break; 35334427Sbostic case '\0': /* "%?" prints ?, unless ? is NULL */ 35434427Sbostic return (cnt); 35534226Sbostic default: 35634427Sbostic PUTC((char)*fmt); 35734427Sbostic cnt++; 35834226Sbostic } 35934226Sbostic } 36034427Sbostic /* NOTREACHED */ 36134226Sbostic } 36234242Sbostic 36334261Sbostic #define EFORMAT 0x01 36434261Sbostic #define FFORMAT 0x02 36534261Sbostic #define GFORMAT 0x04 36634314Sbostic #define DEFPREC 6 36734261Sbostic 36834323Sbostic static char * 36934427Sbostic _cvt(number, prec, flags, fmtch, sign, startp, endp) 37034242Sbostic double number; 37134261Sbostic register int prec; 37234323Sbostic int flags; 37334323Sbostic u_char fmtch; 37434427Sbostic char *sign, *startp, *endp; 37534242Sbostic { 37634331Sbostic register char *p, *t; 37734261Sbostic register int expcnt, format; 37834248Sbostic double fract, integer, tmp, modf(); 37934261Sbostic int decpt; 38034331Sbostic char *savep, exponent[MAXEXP]; 38134242Sbostic 38234314Sbostic if (prec == -1) 38334242Sbostic prec = DEFPREC; 38434242Sbostic 38534314Sbostic if (number < 0) { 38634327Sbostic *sign = '-'; 38734248Sbostic number = -number; 38834248Sbostic } 38934242Sbostic 39034261Sbostic switch(fmtch) { 39134261Sbostic case 'e': 39234261Sbostic case 'E': 39334261Sbostic format = EFORMAT; 39434261Sbostic break; 39534261Sbostic case 'f': 39634261Sbostic format = FFORMAT; 39734261Sbostic break; 39834261Sbostic case 'g': 39934261Sbostic case 'G': 40034261Sbostic format = GFORMAT; 40134261Sbostic fmtch -= 2; 40234261Sbostic } 40334261Sbostic 40434248Sbostic /* 40534248Sbostic * if the alternate flag is set, or, at least one digit of precision 40634248Sbostic * was requested, add a decimal point, unless it's the g/G format 40734314Sbostic * in which case we require two digits of precision, as it counts 40834248Sbostic * precision differently. 40934248Sbostic */ 41034318Sbostic decpt = flags&ALT || prec > (format&GFORMAT ? 1 : 0); 41134248Sbostic 41234248Sbostic expcnt = 0; 41334323Sbostic p = endp - 1; 41434248Sbostic fract = modf(number, &integer); 41534248Sbostic if (integer) { 41634248Sbostic /* get integer part of number; count decimal places */ 41734248Sbostic for (; integer; ++expcnt) { 41834248Sbostic tmp = modf(integer / 10, &integer); 41934331Sbostic *p-- = tochar((int)((tmp + .03) * 10)); 42034242Sbostic } 42134248Sbostic 42234248Sbostic /* copy, in reverse order, to start of buffer */ 42334331Sbostic t = startp; 42434331Sbostic *t++ = *++p; 42534248Sbostic 42634248Sbostic /* 42734248Sbostic * if the format is g/G, and the resulting exponent will be 42834248Sbostic * greater than the precision, use e/E format. If e/E format, 42934248Sbostic * put in a decimal point as needed, and decrement precision 43034248Sbostic * count for each digit after the decimal point. 43134248Sbostic */ 43234248Sbostic if (format&GFORMAT && expcnt - 1 > prec || format&EFORMAT) { 43334248Sbostic if (format&GFORMAT) { 43434248Sbostic format |= EFORMAT; 43534248Sbostic 43634248Sbostic /* first digit is precision for g/G format */ 43734248Sbostic if (prec) 43834248Sbostic --prec; 43934248Sbostic } 44034248Sbostic if (decpt) 44134331Sbostic *t++ = '.'; 44234331Sbostic for (; ++p < endp && prec; --prec, *t++ = *p); 44334248Sbostic 44434318Sbostic /* precision ran out, round */ 44534248Sbostic if (p < endp) { 44634248Sbostic if (*p > '4') { 44734331Sbostic for (savep = t--;; *t-- = '0') { 44834331Sbostic if (*t == '.') 44934331Sbostic --t; 45034331Sbostic if (++*t <= '9') 45134248Sbostic break; 45234248Sbostic } 45334331Sbostic t = savep; 45434248Sbostic } 45534248Sbostic fract = 0; 45634248Sbostic } 45734242Sbostic } 45834248Sbostic /* 45934314Sbostic * g/G in f format; if out of precision, replace digits with 46034314Sbostic * zeroes, note, have to round first. 46134248Sbostic */ 46234248Sbostic else if (format&GFORMAT) { 46334331Sbostic for (; ++p < endp && prec; --prec, *t++ = *p); 46434248Sbostic /* precision ran out; round and then add zeroes */ 46534248Sbostic if (p < endp) { 46634248Sbostic if (*p > '4') { 46734331Sbostic for (savep = t--; ++*t > '9'; 46834331Sbostic *t-- = '0'); 46934331Sbostic t = savep; 47034248Sbostic } 47134248Sbostic do { 47234331Sbostic *t++ = '0'; 47334248Sbostic } while (++p < endp); 47434248Sbostic fract = 0; 47534248Sbostic } 47634248Sbostic if (decpt) 47734331Sbostic *t++ = '.'; 47834242Sbostic } 47934248Sbostic /* f format */ 48034248Sbostic else { 48134331Sbostic for (; ++p < endp; *t++ = *p); 48234248Sbostic if (decpt) 48334331Sbostic *t++ = '.'; 48434248Sbostic } 48534331Sbostic p = t; 48634248Sbostic } 48734248Sbostic /* 48834318Sbostic * if no fraction, the number was zero, and if no precision, can't 48934318Sbostic * show anything after the decimal point. 49034248Sbostic */ 49134248Sbostic else if (!fract || !prec) { 49234248Sbostic *startp++ = '0'; 49334318Sbostic if (decpt && !(format&GFORMAT)) 49434248Sbostic *startp++ = '.'; 49534318Sbostic *startp = '\0'; 49634323Sbostic return(startp); 49734248Sbostic } 49834248Sbostic /* 49934248Sbostic * if the format is g/G, and the resulting exponent will be less than 50034248Sbostic * -4 use e/E format. If e/E format, compute exponent value. 50134248Sbostic */ 50234248Sbostic else if (format&GFORMAT && fract < .0001 || format&EFORMAT) { 50334248Sbostic format |= EFORMAT; 50434248Sbostic if (fract) 50534248Sbostic for (p = startp; fract;) { 50634248Sbostic fract = modf(fract * 10, &tmp); 50734248Sbostic if (!tmp) { 50834248Sbostic --expcnt; 50934248Sbostic continue; 51034248Sbostic } 51134331Sbostic *p++ = tochar((int)tmp); 51234248Sbostic break; 51334248Sbostic } 51434242Sbostic else 51534248Sbostic *p++ = '0'; 51634248Sbostic 51734248Sbostic /* g/G format, decrement precision for first digit */ 51834248Sbostic if (format&GFORMAT && prec) 51934248Sbostic --prec; 52034248Sbostic 52134248Sbostic /* add decimal after first non-zero digit */ 52234248Sbostic if (decpt) 52334248Sbostic *p++ = '.'; 52434242Sbostic } 52534248Sbostic /* 52634248Sbostic * f format or g/G printed as f format; don't worry about decimal 52734248Sbostic * point, if g/G format doesn't need it, will get stripped later. 52834248Sbostic */ 52934242Sbostic else { 53034248Sbostic p = startp; 53134248Sbostic *p++ = '0'; 53234248Sbostic *p++ = '.'; 53334248Sbostic } 53434248Sbostic 53534319Sbostic /* finish out requested precision */ 53634319Sbostic while (fract && prec-- > 0) { 53734319Sbostic fract = modf(fract * 10, &tmp); 53834331Sbostic *p++ = tochar((int)tmp); 53934319Sbostic } 54034319Sbostic while (prec-- > 0) 54134319Sbostic *p++ = '0'; 54234248Sbostic 54334248Sbostic /* 54434248Sbostic * if any fractional value left, "round" it back up to the beginning 54534248Sbostic * of the number, fixing the exponent as necessary, and avoiding the 54634248Sbostic * decimal point. 54734248Sbostic */ 54834248Sbostic if (fract) { 54934248Sbostic (void)modf(fract * 10, &tmp); 55034248Sbostic if (tmp > 4) { 55134248Sbostic for (savep = p--;; *p-- = '0') { 55234248Sbostic if (*p == '.') 55334248Sbostic --p; 55434248Sbostic if (p == startp) { 55534248Sbostic *p = '1'; 55634248Sbostic ++expcnt; 55734248Sbostic break; 55834248Sbostic } 55934248Sbostic if (++*p <= '9') 56034248Sbostic break; 56134242Sbostic } 56234248Sbostic p = savep; 56334242Sbostic } 56434248Sbostic } 56534248Sbostic 56634248Sbostic /* 56734248Sbostic * if a g/G format and not alternate flag, lose trailing zeroes, 56834248Sbostic * if e/E or g/G format, and last char is decimal point, lose it. 56934248Sbostic */ 57034318Sbostic if (!(flags&ALT)) { 57134248Sbostic if (format&GFORMAT) 57234248Sbostic for (; p[-1] == '0'; --p); 57334248Sbostic if (format&(GFORMAT|EFORMAT) && p[-1] == '.') 57434248Sbostic --p; 57534248Sbostic } 57634248Sbostic 57734248Sbostic /* if an e/E format, add exponent */ 57834248Sbostic if (format&EFORMAT) { 57934248Sbostic *p++ = fmtch; 58034248Sbostic if (--expcnt < 0) { 58134248Sbostic expcnt = -expcnt; 58234248Sbostic *p++ = '-'; 58334242Sbostic } 58434248Sbostic else 58534248Sbostic *p++ = '+'; 58634331Sbostic t = exponent + MAXEXP; 58734331Sbostic if (expcnt > 9) { 58834331Sbostic do { 58934331Sbostic *--t = tochar(expcnt % 10); 59034331Sbostic } while ((expcnt /= 10) > 9); 59134331Sbostic *--t = tochar(expcnt); 59234331Sbostic for (; t < exponent + MAXEXP; *p++ = *t++); 59334331Sbostic } 59434331Sbostic else { 59534331Sbostic *p++ = '0'; 59634331Sbostic *p++ = tochar(expcnt); 59734331Sbostic } 59834242Sbostic } 59934323Sbostic return(p); 60034242Sbostic } 601