xref: /csrg-svn/lib/libc/stdio/vfprintf.c (revision 34329)
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*34329Sbostic static char sccsid[] = "@(#)vfprintf.c	5.25 (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 
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 
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;
5434328Sbostic 	int base, flags, fpprec, prec, size, width;
5534328Sbostic 	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 
8034328Sbostic 		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':
139*34329Sbostic 			flags |= LONGDBL;
14034318Sbostic 			goto rflag;
14134235Sbostic 		case 'h':
14234318Sbostic 			flags |= SHORTINT;
14334318Sbostic 			goto rflag;
14434233Sbostic 		case 'l':
14534318Sbostic 			flags |= LONGINT;
14634318Sbostic 			goto rflag;
14734314Sbostic 		case 'c':
14834319Sbostic 			buf[0] = va_arg(argp, int);
14934314Sbostic 			size = 1;
15034319Sbostic 			t = buf;
15134314Sbostic 			goto pforw;
15234314Sbostic 		case 'd':
15334318Sbostic 		case 'i':
15434318Sbostic 			ARG();
15534318Sbostic 			if ((long)_ulong < 0) {
15634318Sbostic 				_ulong = -_ulong;
15734314Sbostic 				sign = '-';
15834241Sbostic 			}
15934241Sbostic 			base = 10;
16034327Sbostic 			goto number;
16134261Sbostic 		case 'e':
16234236Sbostic 		case 'E':
16334235Sbostic 		case 'f':
16434261Sbostic 		case 'g':
16534243Sbostic 		case 'G':
16634243Sbostic 			_double = va_arg(argp, double);
16734328Sbostic 			/*
16834328Sbostic 			 * don't bother to do unrealistic precision; just
16934328Sbostic 			 * pad it with zeroes later.  This keeps buffer size
17034328Sbostic 			 * rational.
17134328Sbostic 			 */
17234328Sbostic 			if (prec > MAXFRACT) {
17334328Sbostic 				fpprec = prec - MAXFRACT;
17434328Sbostic 				prec = MAXFRACT;
17534328Sbostic 			}
17634327Sbostic 			size = _cvt(_double, prec, flags, *fmt, padc, &sign,
17734323Sbostic 			    buf, buf + sizeof(buf)) - buf;
17834319Sbostic 			t = buf;
17934327Sbostic 			/*
18034327Sbostic 			 * zero-padded sign put out here; blank padded sign
18134327Sbostic 			 * placed in number in _cvt().
18234327Sbostic 			 */
18334327Sbostic 			if (sign && padc == '0') {
18434327Sbostic 				PUTC(sign);
18534327Sbostic 				--width;
18634327Sbostic 			}
18734314Sbostic 			goto pforw;
18834235Sbostic 		case 'n':
189*34329Sbostic 			if (flags&LONGINT)
19034318Sbostic 				*va_arg(argp, long *) = cnt;
19134318Sbostic 			else if (flags&SHORTINT)
19234318Sbostic 				*va_arg(argp, short *) = cnt;
19334318Sbostic 			else
19434318Sbostic 				*va_arg(argp, int *) = cnt;
19534235Sbostic 			break;
19634226Sbostic 		case 'o':
19734318Sbostic 			ARG();
19834226Sbostic 			base = 8;
19934327Sbostic 			goto nosign;
20034235Sbostic 		case 'p':
20134320Sbostic 			/*
20234321Sbostic 			 * ``The argument shall be a pointer to void.  The
20334321Sbostic 			 * value of the pointer is converted to a sequence
20434321Sbostic 			 * of printable characters, in an implementation-
20534321Sbostic 			 * defined manner.''
20634321Sbostic 			 *	-- ANSI X3J11
20734320Sbostic 			 */
20834327Sbostic 			/*NOSTRICT*/
20934320Sbostic 			_ulong = (u_long)va_arg(argp, void *);
21034320Sbostic 			base = 16;
21134327Sbostic 			goto nosign;
21234226Sbostic 		case 's':
21334314Sbostic 			if (!(t = va_arg(argp, char *)))
21434314Sbostic 				t = "(null)";
21534321Sbostic 			if (prec >= 0) {
21634321Sbostic 				/*
21734321Sbostic 				 * can't use strlen; can only look for the
21834321Sbostic 				 * NUL in the first `prec' characters, and
21934321Sbostic 				 * strlen() will go further.
22034321Sbostic 				 */
22134321Sbostic 				char *p, *memchr();
22234321Sbostic 
22334321Sbostic 				if (p = memchr(t, 0, prec)) {
22434321Sbostic 					size = p - t;
22534321Sbostic 					if (size > prec)
22634321Sbostic 						size = prec;
22734321Sbostic 				}
22834321Sbostic 				else
22934321Sbostic 					size = prec;
23034321Sbostic 			}
23134321Sbostic 			else
23234321Sbostic 				size = strlen(t);
23334328Sbostic 			goto pforw;
23434226Sbostic 		case 'u':
23534318Sbostic 			ARG();
23634226Sbostic 			base = 10;
23734327Sbostic 			goto nosign;
23834226Sbostic 		case 'X':
23934226Sbostic 			digs = "0123456789ABCDEF";
24034233Sbostic 			/*FALLTHROUGH*/
24134226Sbostic 		case 'x':
24234318Sbostic 			ARG();
24334314Sbostic 			base = 16;
24434326Sbostic 			/* leading 0x/X only if non-zero */
24534326Sbostic 			if (!_ulong)
24634324Sbostic 				flags &= ~ALT;
24734327Sbostic 
24834327Sbostic 			/* unsigned conversions */
24934327Sbostic nosign:			sign = NULL;
25034326Sbostic 			/*
25134326Sbostic 			 * ``The result of converting a zero value with an
25234326Sbostic 			 * explicit precision of zero is no characters.''
25334326Sbostic 			 *	-- ANSI X3J11
25434326Sbostic 			 */
25534327Sbostic number:			if (!_ulong && !prec)
25634326Sbostic 				break;
25734327Sbostic 
25834328Sbostic 			t = buf + BUF - 1;
25934233Sbostic 			do {
26034314Sbostic 				*t-- = digs[_ulong % base];
26134314Sbostic 				_ulong /= base;
26234314Sbostic 			} while(_ulong);
26334328Sbostic 			for (size = buf + BUF - 1 - t; size < prec; ++size)
26434324Sbostic 				*t-- = '0';
26534328Sbostic 			digs = "0123456789abcdef";
26634327Sbostic 
26734327Sbostic 			/* alternate mode for hex and octal numbers */
26834324Sbostic 			if (flags&ALT)
26934324Sbostic 				switch (base) {
27034324Sbostic 				case 16:
27134324Sbostic 					/* avoid "00000x35" */
27234327Sbostic 					if (padc == ' ') {
27334327Sbostic 						*t-- = *fmt;
27434327Sbostic 						*t-- = '0';
27534328Sbostic 						size += 2;
27634327Sbostic 					}
27734327Sbostic 					else {
27834324Sbostic 						PUTC('0');
27934324Sbostic 						PUTC(*fmt);
28034328Sbostic 						width -= 2;
28134324Sbostic 					}
28234324Sbostic 					break;
28334324Sbostic 				case 8:
28434324Sbostic 					if (t[1] != '0') {
28534324Sbostic 						*t-- = '0';
28634328Sbostic 						++size;
28734324Sbostic 					}
28834324Sbostic 					break;
28934314Sbostic 				}
29034327Sbostic 
29134327Sbostic 			if (sign) {
29234327Sbostic 				/* avoid "0000-3" */
29334328Sbostic 				if (padc == ' ') {
29434327Sbostic 					*t-- = sign;
29534328Sbostic 					++size;
29634328Sbostic 				}
29734328Sbostic 				else {
29834327Sbostic 					PUTC(sign);
29934328Sbostic 					--width;
30034328Sbostic 				}
30134327Sbostic 			}
30234328Sbostic 			++t;
30334327Sbostic 
30434328Sbostic pforw:			if (!(flags&LADJUST) && width)
30534328Sbostic 				for (n = size + fpprec; n++ < width;)
30634263Sbostic 					PUTC(padc);
30734328Sbostic 			if (fp->_cnt - (n = size) >= 0) {
30834328Sbostic 				cnt += n;
30934328Sbostic 				fp->_cnt -= n;
31034328Sbostic 				bcopy(t, fp->_ptr, n);
31134328Sbostic 				fp->_ptr += n;
31234328Sbostic 			}
31334328Sbostic 			else for (; n--; ++t)
31434314Sbostic 				PUTC(*t);
31534328Sbostic 			while (fpprec--)
31634328Sbostic 				PUTC('0');
31734328Sbostic 			if (flags&LADJUST)
31834328Sbostic 				for (n = size + fpprec; ++n < width;)
31934328Sbostic 					PUTC(' ');
32034226Sbostic 			break;
32134233Sbostic 		case '\0':		/* "%?" prints ?, unless ? is NULL */
32234314Sbostic 			return(cnt);
32334226Sbostic 		default:
32434263Sbostic 			PUTC(*fmt);
32534226Sbostic 		}
32634226Sbostic 	}
32734314Sbostic 	/*NOTREACHED*/
32834226Sbostic }
32934242Sbostic 
33034261Sbostic #define	EFORMAT	0x01
33134261Sbostic #define	FFORMAT	0x02
33234261Sbostic #define	GFORMAT	0x04
33334314Sbostic #define	DEFPREC	6
33434261Sbostic 
33534323Sbostic static char *
33634327Sbostic _cvt(number, prec, flags, fmtch, padc, sign, startp, endp)
33734242Sbostic 	double number;
33834261Sbostic 	register int prec;
33934323Sbostic 	int flags;
34034323Sbostic 	u_char fmtch;
34134327Sbostic 	char padc, *sign, *startp, *endp;
34234242Sbostic {
34334248Sbostic 	register char *p;
34434261Sbostic 	register int expcnt, format;
34534248Sbostic 	double fract, integer, tmp, modf();
34634261Sbostic 	int decpt;
34734323Sbostic 	char *savep;
34834242Sbostic 
34934314Sbostic 	if (prec == -1)
35034242Sbostic 		prec = DEFPREC;
35134242Sbostic 
35234314Sbostic 	if (number < 0) {
35334327Sbostic 		*sign = '-';
35434248Sbostic 		number = -number;
35534248Sbostic 	}
35634242Sbostic 
35734327Sbostic 	/* if blank padded, add sign in as part of the number */
35834327Sbostic 	if (*sign && padc == ' ')
35934327Sbostic 		*startp++ = *sign;
36034327Sbostic 
36134261Sbostic 	switch(fmtch) {
36234261Sbostic 	case 'e':
36334261Sbostic 	case 'E':
36434261Sbostic 		format = EFORMAT;
36534261Sbostic 		break;
36634261Sbostic 	case 'f':
36734261Sbostic 		format = FFORMAT;
36834261Sbostic 		break;
36934261Sbostic 	case 'g':
37034261Sbostic 	case 'G':
37134261Sbostic 		format = GFORMAT;
37234261Sbostic 		fmtch -= 2;
37334261Sbostic 	}
37434261Sbostic 
37534248Sbostic 	/*
37634248Sbostic 	 * if the alternate flag is set, or, at least one digit of precision
37734248Sbostic 	 * was requested, add a decimal point, unless it's the g/G format
37834314Sbostic 	 * in which case we require two digits of precision, as it counts
37934248Sbostic 	 * precision differently.
38034248Sbostic 	 */
38134318Sbostic 	decpt = flags&ALT || prec > (format&GFORMAT ? 1 : 0);
38234248Sbostic 
38334248Sbostic 	expcnt = 0;
38434323Sbostic 	p = endp - 1;
38534248Sbostic 	fract = modf(number, &integer);
38634248Sbostic 	if (integer) {
38734248Sbostic 		register char *p2;
38834248Sbostic 
38934248Sbostic 		/* get integer part of number; count decimal places */
39034248Sbostic 		for (; integer; ++expcnt) {
39134248Sbostic 			tmp = modf(integer / 10, &integer);
39234248Sbostic 			*p-- = (int)((tmp + .03) * 10) + '0';
39334242Sbostic 		}
39434248Sbostic 
39534248Sbostic 		/* copy, in reverse order, to start of buffer */
39634248Sbostic 		p2 = startp;
39734248Sbostic 		*p2++ = *++p;
39834248Sbostic 
39934248Sbostic 		/*
40034248Sbostic 		 * if the format is g/G, and the resulting exponent will be
40134248Sbostic 		 * greater than the precision, use e/E format.  If e/E format,
40234248Sbostic 		 * put in a decimal point as needed, and decrement precision
40334248Sbostic 		 * count for each digit after the decimal point.
40434248Sbostic 		 */
40534248Sbostic 		if (format&GFORMAT && expcnt - 1 > prec || format&EFORMAT) {
40634248Sbostic 			if (format&GFORMAT) {
40734248Sbostic 				format |= EFORMAT;
40834248Sbostic 
40934248Sbostic 				/* first digit is precision for g/G format */
41034248Sbostic 				if (prec)
41134248Sbostic 					--prec;
41234248Sbostic 			}
41334248Sbostic 			if (decpt)
41434248Sbostic 				*p2++ = '.';
41534248Sbostic 			for (; ++p < endp && prec; --prec, *p2++ = *p);
41634248Sbostic 
41734318Sbostic 			/* precision ran out, round */
41834248Sbostic 			if (p < endp) {
41934248Sbostic 				if (*p > '4') {
42034248Sbostic 					for (savep = p2--;; *p2-- = '0') {
42134248Sbostic 						if (*p2 == '.')
42234248Sbostic 							--p2;
42334248Sbostic 						if (++*p2 <= '9')
42434248Sbostic 							break;
42534248Sbostic 					}
42634248Sbostic 					p2 = savep;
42734248Sbostic 				}
42834248Sbostic 				fract = 0;
42934248Sbostic 			}
43034242Sbostic 		}
43134248Sbostic 		/*
43234314Sbostic 		 * g/G in f format; if out of precision, replace digits with
43334314Sbostic 		 * zeroes, note, have to round first.
43434248Sbostic 		 */
43534248Sbostic 		else if (format&GFORMAT) {
43634248Sbostic 			for (; ++p < endp && prec; --prec, *p2++ = *p);
43734248Sbostic 			/* precision ran out; round and then add zeroes */
43834248Sbostic 			if (p < endp) {
43934248Sbostic 				if (*p > '4') {
44034248Sbostic 					for (savep = p2--; ++*p2 > '9';
44134248Sbostic 					    *p2-- = '0');
44234248Sbostic 					p2 = savep;
44334248Sbostic 				}
44434248Sbostic 				do {
44534248Sbostic 					*p2++ = '0';
44634248Sbostic 				} while (++p < endp);
44734248Sbostic 				fract = 0;
44834248Sbostic 			}
44934248Sbostic 			if (decpt)
45034248Sbostic 				*p2++ = '.';
45134242Sbostic 		}
45234248Sbostic 		/* f format */
45334248Sbostic 		else {
45434248Sbostic 			for (; ++p < endp; *p2++ = *p);
45534248Sbostic 			if (decpt)
45634248Sbostic 				*p2++ = '.';
45734248Sbostic 		}
45834248Sbostic 		p = p2;
45934248Sbostic 	}
46034248Sbostic 	/*
46134318Sbostic 	 * if no fraction, the number was zero, and if no precision, can't
46234318Sbostic 	 * show anything after the decimal point.
46334248Sbostic 	 */
46434248Sbostic 	else if (!fract || !prec) {
46534248Sbostic 		*startp++ = '0';
46634318Sbostic 		if (decpt && !(format&GFORMAT))
46734248Sbostic 			*startp++ = '.';
46834318Sbostic 		*startp = '\0';
46934323Sbostic 		return(startp);
47034248Sbostic 	}
47134248Sbostic 	/*
47234248Sbostic 	 * if the format is g/G, and the resulting exponent will be less than
47334248Sbostic 	 * -4 use e/E format.  If e/E format, compute exponent value.
47434248Sbostic 	 */
47534248Sbostic 	else if (format&GFORMAT && fract < .0001 || format&EFORMAT) {
47634248Sbostic 		format |= EFORMAT;
47734248Sbostic 		if (fract)
47834248Sbostic 			for (p = startp; fract;) {
47934248Sbostic 				fract = modf(fract * 10, &tmp);
48034248Sbostic 				if (!tmp) {
48134248Sbostic 					--expcnt;
48234248Sbostic 					continue;
48334248Sbostic 				}
48434248Sbostic 				*p++ = (int)tmp + '0';
48534248Sbostic 				break;
48634248Sbostic 			}
48734242Sbostic 		else
48834248Sbostic 			*p++ = '0';
48934248Sbostic 
49034248Sbostic 		/* g/G format, decrement precision for first digit */
49134248Sbostic 		if (format&GFORMAT && prec)
49234248Sbostic 			--prec;
49334248Sbostic 
49434248Sbostic 		/* add decimal after first non-zero digit */
49534248Sbostic 		if (decpt)
49634248Sbostic 			*p++ = '.';
49734242Sbostic 	}
49834248Sbostic 	/*
49934248Sbostic 	 * f format or g/G printed as f format; don't worry about decimal
50034248Sbostic 	 * point, if g/G format doesn't need it, will get stripped later.
50134248Sbostic 	 */
50234242Sbostic 	else {
50334248Sbostic 		p = startp;
50434248Sbostic 		*p++ = '0';
50534248Sbostic 		*p++ = '.';
50634248Sbostic 	}
50734248Sbostic 
50834319Sbostic 	/* finish out requested precision */
50934319Sbostic 	while (fract && prec-- > 0) {
51034319Sbostic 		fract = modf(fract * 10, &tmp);
51134319Sbostic 		*p++ = (int)tmp + '0';
51234319Sbostic 	}
51334319Sbostic 	while (prec-- > 0)
51434319Sbostic 		*p++ = '0';
51534248Sbostic 
51634248Sbostic 	/*
51734248Sbostic 	 * if any fractional value left, "round" it back up to the beginning
51834248Sbostic 	 * of the number, fixing the exponent as necessary, and avoiding the
51934248Sbostic 	 * decimal point.
52034248Sbostic 	 */
52134248Sbostic 	if (fract) {
52234248Sbostic 		(void)modf(fract * 10, &tmp);
52334248Sbostic 		if (tmp > 4) {
52434248Sbostic 			for (savep = p--;; *p-- = '0') {
52534248Sbostic 				if (*p == '.')
52634248Sbostic 					--p;
52734248Sbostic 				if (p == startp) {
52834248Sbostic 					*p = '1';
52934248Sbostic 					++expcnt;
53034248Sbostic 					break;
53134248Sbostic 				}
53234248Sbostic 				if (++*p <= '9')
53334248Sbostic 					break;
53434242Sbostic 			}
53534248Sbostic 			p = savep;
53634242Sbostic 		}
53734248Sbostic 	}
53834248Sbostic 
53934248Sbostic 	/*
54034248Sbostic 	 * if a g/G format and not alternate flag, lose trailing zeroes,
54134248Sbostic 	 * if e/E or g/G format, and last char is decimal point, lose it.
54234248Sbostic 	 */
54334318Sbostic 	if (!(flags&ALT)) {
54434248Sbostic 		if (format&GFORMAT)
54534248Sbostic 			for (; p[-1] == '0'; --p);
54634248Sbostic 		if (format&(GFORMAT|EFORMAT) && p[-1] == '.')
54734248Sbostic 			--p;
54834248Sbostic 	}
54934248Sbostic 
55034248Sbostic 	/* if an e/E format, add exponent */
55134248Sbostic 	if (format&EFORMAT) {
55234248Sbostic 		*p++ = fmtch;
55334248Sbostic 		if (--expcnt < 0) {
55434248Sbostic 			expcnt = -expcnt;
55534248Sbostic 			*p++ = '-';
55634242Sbostic 		}
55734248Sbostic 		else
55834248Sbostic 			*p++ = '+';
55934248Sbostic 		*p++ = expcnt / 10 + '0';
56034248Sbostic 		*p++ = expcnt % 10 + '0';
56134242Sbostic 	}
56234323Sbostic 	return(p);
56334242Sbostic }
564