xref: /csrg-svn/lib/libc/stdio/vfprintf.c (revision 46442)
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*46442Storek static char sccsid[] = "@(#)vfprintf.c	5.44 (Berkeley) 02/16/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>
2246076Sbostic #include <stdio.h>
2346076Sbostic #include <string.h>
2446076Sbostic #if __STDC__
2546076Sbostic #include <stdarg.h>
2646076Sbostic #else
2734233Sbostic #include <varargs.h>
2846076Sbostic #endif
2946076Sbostic #include "local.h"
3046076Sbostic #include "fvwrite.h"
3134226Sbostic 
3246076Sbostic /*
3346076Sbostic  * Define FLOATING_POINT to get floating point.
3446076Sbostic  * Define CSH to get a csh-specific version (grr).
3546076Sbostic  */
3646076Sbostic #ifndef CSH
3746076Sbostic #define	FLOATING_POINT
3846076Sbostic #endif
3934226Sbostic 
4046076Sbostic /* end of configuration stuff */
4134624Sbostic 
4246076Sbostic 
4346076Sbostic #ifdef CSH
4446076Sbostic /*
4546076Sbostic  * C shell hacks.  Ick, gag.
4646076Sbostic  */
4746076Sbostic #undef BUFSIZ
4846076Sbostic #include "sh.h"
4946076Sbostic 
5046180Storek #if __STDC__
5146180Storek int
5246180Storek printf(const char *fmt, ...) {
5346180Storek 	FILE f;
5446180Storek 
5546180Storek 	f._flags = __SWR;
5646180Storek 	return (vfprintf(&f, fmt, (va_list)(&fmt + 1)));
5746180Storek }
5846180Storek #else
5946180Storek int
6046076Sbostic printf(fmt, args)
6146076Sbostic 	char *fmt;
6246076Sbostic {
6346076Sbostic 	FILE f;
6446076Sbostic 
6546076Sbostic 	f._flags = __SWR;
66*46442Storek 	f._write = NULL;
6746076Sbostic 	return (vfprintf(&f, fmt, &args));
6846076Sbostic }
6946180Storek #endif
7046076Sbostic 
7146180Storek int
72*46442Storek __sprint(fp, uio)
73*46442Storek 	FILE *fp;
7446076Sbostic 	register struct __suio *uio;
7546076Sbostic {
7646076Sbostic 	register char *p;
7746076Sbostic 	register int n, ch, iovcnt;
78*46442Storek 	register struct __siov *iov;
7946076Sbostic 
80*46442Storek 	/* must allow sprintf to work, might as well allow others too */
81*46442Storek 	if (fp->_write || fp->_flags & __SSTR) {
82*46442Storek 		if (uio->uio_resid == 0) {
83*46442Storek 			uio->uio_iovcnt = 0;
84*46442Storek 			return (0);
85*46442Storek 		}
86*46442Storek 		n = __sfvwrite(fp, uio);
87*46442Storek 		uio->uio_resid = 0;
88*46442Storek 		uio->uio_iovcnt = 0;
89*46442Storek 		return (n);
90*46442Storek 	}
91*46442Storek 	iov = uio->uio_iov;
9246076Sbostic 	for (iovcnt = uio->uio_iovcnt; --iovcnt >= 0; iov++) {
9346076Sbostic 		for (p = iov->iov_base, n = iov->iov_len; --n >= 0;) {
9446076Sbostic #ifdef CSHPUTCHAR
9546076Sbostic 			ch = *p++;
9646076Sbostic 			CSHPUTCHAR;	/* this horrid macro uses `ch' */
9746076Sbostic #else
9846076Sbostic #undef putchar
9946076Sbostic 			putchar(*p++);
10046076Sbostic #endif
10146076Sbostic 		}
10246076Sbostic 	}
10346076Sbostic 	uio->uio_resid = 0;
10446076Sbostic 	uio->uio_iovcnt = 0;
10546076Sbostic 	return (0);
10646076Sbostic }
10746076Sbostic 
10846076Sbostic #else /* CSH */
10946076Sbostic 
11046076Sbostic /*
11146076Sbostic  * Flush out all the vectors defined by the given uio,
11246076Sbostic  * then reset it so that it can be reused.
11346076Sbostic  */
11446180Storek static int
11546076Sbostic __sprint(fp, uio)
11646076Sbostic 	FILE *fp;
11746076Sbostic 	register struct __suio *uio;
11846076Sbostic {
11946076Sbostic 	register int err;
12046076Sbostic 
12146076Sbostic 	if (uio->uio_resid == 0) {
12246076Sbostic 		uio->uio_iovcnt = 0;
12346076Sbostic 		return (0);
12446076Sbostic 	}
12546076Sbostic 	err = __sfvwrite(fp, uio);
12646076Sbostic 	uio->uio_resid = 0;
12746076Sbostic 	uio->uio_iovcnt = 0;
12846076Sbostic 	return (err);
12946076Sbostic }
13046076Sbostic 
13146076Sbostic /*
13246076Sbostic  * Helper function for `fprintf to unbuffered unix file': creates a
13346076Sbostic  * temporary buffer.  We only work on write-only files; this avoids
13446076Sbostic  * worries about ungetc buffers and so forth.
13546076Sbostic  */
13646180Storek static int
13746076Sbostic __sbprintf(fp, fmt, ap)
13846076Sbostic 	register FILE *fp;
13946180Storek 	const char *fmt;
14046076Sbostic 	va_list ap;
14146076Sbostic {
14246076Sbostic 	int ret;
14346076Sbostic 	FILE fake;
14446076Sbostic 	unsigned char buf[BUFSIZ];
14546076Sbostic 
14646076Sbostic 	/* copy the important variables */
14746076Sbostic 	fake._flags = fp->_flags & ~__SNBF;
14846076Sbostic 	fake._file = fp->_file;
14946076Sbostic 	fake._cookie = fp->_cookie;
15046076Sbostic 	fake._write = fp->_write;
15146076Sbostic 
15246076Sbostic 	/* set up the buffer */
15346076Sbostic 	fake._bf._base = fake._p = buf;
15446076Sbostic 	fake._bf._size = fake._w = sizeof(buf);
15546076Sbostic 	fake._lbfsize = 0;	/* not actually used, but Just In Case */
15646076Sbostic 
15746076Sbostic 	/* do the work, then copy any error status */
15846076Sbostic 	ret = vfprintf(&fake, fmt, ap);
15946076Sbostic 	if (ret >= 0 && fflush(&fake))
16046076Sbostic 		ret = EOF;
16146076Sbostic 	if (fake._flags & __SERR)
16246076Sbostic 		fp->_flags |= __SERR;
16346076Sbostic 	return (ret);
16446076Sbostic }
16546076Sbostic 
16646076Sbostic #endif /* CSH */
16746076Sbostic 
16846076Sbostic 
16946076Sbostic #ifdef FLOATING_POINT
17046180Storek #include "floatio.h"
17146076Sbostic 
17234328Sbostic #define	BUF		(MAXEXP+MAXFRACT+1)	/* + decimal point */
17346076Sbostic #define	DEFPREC		6
17434328Sbostic 
17546076Sbostic static int cvt();
17646126Storek #if defined(hp300) || defined(sparc)
17746126Storek static char *isspecial();
17846126Storek #endif
17934236Sbostic 
18046076Sbostic #else /* no FLOATING_POINT */
18134235Sbostic 
18246076Sbostic #define	BUF		40
18334331Sbostic 
18446076Sbostic #endif /* FLOATING_POINT */
18534318Sbostic 
18646076Sbostic 
18746076Sbostic /*
18846076Sbostic  * Macros for converting digits to letters and vice versa
18946076Sbostic  */
19046076Sbostic #define	to_digit(c)	((c) - '0')
19146076Sbostic #define is_digit(c)	((unsigned)to_digit(c) <= 9)
19246076Sbostic #define	to_char(n)	((n) + '0')
19346076Sbostic 
19446076Sbostic /*
19546076Sbostic  * Flags used during conversion.
19646076Sbostic  */
19734318Sbostic #define	LONGINT		0x01		/* long integer */
19834318Sbostic #define	LONGDBL		0x02		/* long double; unimplemented */
19934318Sbostic #define	SHORTINT	0x04		/* short integer */
20034318Sbostic #define	ALT		0x08		/* alternate form */
20134318Sbostic #define	LADJUST		0x10		/* left adjustment */
20234427Sbostic #define	ZEROPAD		0x20		/* zero (as opposed to blank) pad */
20334427Sbostic #define	HEXPREFIX	0x40		/* add 0x or 0X prefix */
20434318Sbostic 
20546180Storek int
20646076Sbostic vfprintf(fp, fmt0, ap)
20746076Sbostic 	FILE *fp;
20846180Storek 	const char *fmt0;
20946076Sbostic #if tahoe
21046076Sbostic  register /* technically illegal, since we do not know what type va_list is */
21146076Sbostic #endif
21246076Sbostic 	va_list ap;
21334226Sbostic {
21446076Sbostic 	register char *fmt;	/* format string */
21534427Sbostic 	register int ch;	/* character from fmt */
21646076Sbostic 	register int n;		/* handy integer (short term usage) */
21746076Sbostic 	register char *cp;	/* handy char pointer (short term usage) */
21846076Sbostic 	register struct __siov *iovp;/* for PRINT macro */
21946076Sbostic 	register int flags;	/* flags as above */
22046076Sbostic 	int ret;		/* return value accumulator */
22146076Sbostic 	int width;		/* width from format (%8d), or 0 */
22246076Sbostic 	int prec;		/* precision from format (%.3d), or -1 */
22346076Sbostic 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
22446076Sbostic #ifdef FLOATING_POINT
22546076Sbostic 	char softsign;		/* temporary negative sign for floats */
22634427Sbostic 	double _double;		/* double precision arguments %[eEfgG] */
22746076Sbostic 	int fpprec;		/* `extra' floating precision in [eEfgG] */
22846076Sbostic #endif
22934427Sbostic 	u_long _ulong;		/* integer arguments %[diouxX] */
23046076Sbostic 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
23146076Sbostic 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
23234624Sbostic 	int fieldsz;		/* field size expanded by sign, etc */
23346076Sbostic 	int realsz;		/* field size expanded by dprec */
23434427Sbostic 	int size;		/* size of converted field or string */
23546076Sbostic 	char *xdigs;		/* digits for [xX] conversion */
23646076Sbostic #define NIOV 8
23746076Sbostic 	struct __suio uio;	/* output information: summary */
23846076Sbostic 	struct __siov iov[NIOV];/* ... and individual io vectors */
23934427Sbostic 	char buf[BUF];		/* space for %c, %[diouxX], %[eEfgG] */
24046076Sbostic 	char ox[2];		/* space for 0x hex-prefix */
24134226Sbostic 
24246076Sbostic 	/*
24346076Sbostic 	 * Choose PADSIZE to trade efficiency vs size.  If larger
24446076Sbostic 	 * printf fields occur frequently, increase PADSIZE (and make
24546076Sbostic 	 * the initialisers below longer).
24646076Sbostic 	 */
24746076Sbostic #define	PADSIZE	16		/* pad chunk size */
24846076Sbostic 	static char blanks[PADSIZE] =
24946076Sbostic 	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
25046076Sbostic 	static char zeroes[PADSIZE] =
25146076Sbostic 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
25246076Sbostic 
25346076Sbostic 	/*
25446076Sbostic 	 * BEWARE, these `goto error' on error, and PAD uses `n'.
25546076Sbostic 	 */
25646076Sbostic #define	PRINT(ptr, len) { \
25746076Sbostic 	iovp->iov_base = (ptr); \
25846076Sbostic 	iovp->iov_len = (len); \
25946076Sbostic 	uio.uio_resid += (len); \
26046076Sbostic 	iovp++; \
26146076Sbostic 	if (++uio.uio_iovcnt >= NIOV) { \
26246076Sbostic 		if (__sprint(fp, &uio)) \
26346076Sbostic 			goto error; \
26446076Sbostic 		iovp = iov; \
26546076Sbostic 	} \
26646076Sbostic }
26746076Sbostic #define	PAD(howmany, with) { \
26846076Sbostic 	if ((n = (howmany)) > 0) { \
26946076Sbostic 		while (n > PADSIZE) { \
27046076Sbostic 			PRINT(with, PADSIZE); \
27146076Sbostic 			n -= PADSIZE; \
27246076Sbostic 		} \
27346076Sbostic 		PRINT(with, n); \
27446076Sbostic 	} \
27546076Sbostic }
27646076Sbostic #define	FLUSH() { \
27746076Sbostic 	if (uio.uio_resid && __sprint(fp, &uio)) \
27846076Sbostic 		goto error; \
27946076Sbostic 	uio.uio_iovcnt = 0; \
28046076Sbostic 	iovp = iov; \
28146076Sbostic }
28246076Sbostic 
28346076Sbostic 	/*
28446076Sbostic 	 * To extend shorts properly, we need both signed and unsigned
28546076Sbostic 	 * argument extraction methods.
28646076Sbostic 	 */
28746076Sbostic #define	SARG() \
28846076Sbostic 	(flags&LONGINT ? va_arg(ap, long) : \
28946076Sbostic 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
29046076Sbostic 	    (long)va_arg(ap, int))
29146076Sbostic #define	UARG() \
29246076Sbostic 	(flags&LONGINT ? va_arg(ap, u_long) : \
29346076Sbostic 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
29446076Sbostic 	    (u_long)va_arg(ap, u_int))
29546076Sbostic 
29646076Sbostic #ifndef CSH
29746076Sbostic 	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
29846076Sbostic 	if (cantwrite(fp))
29934428Sbostic 		return (EOF);
30034428Sbostic 
30146076Sbostic 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
30246076Sbostic 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
30346076Sbostic 	    fp->_file >= 0)
30446076Sbostic 		return (__sbprintf(fp, fmt0, ap));
30546076Sbostic #endif /* CSH */
30646076Sbostic 
30746076Sbostic 	fmt = (char *)fmt0;
30846076Sbostic 	uio.uio_iov = iovp = iov;
30946076Sbostic 	uio.uio_resid = 0;
31046076Sbostic 	uio.uio_iovcnt = 0;
31146076Sbostic 	ret = 0;
31246076Sbostic 
31346076Sbostic 	/*
31446076Sbostic 	 * Scan the format for conversions (`%' character).
31546076Sbostic 	 */
31646076Sbostic 	for (;;) {
31746076Sbostic 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
31846076Sbostic 			/* void */;
31946076Sbostic 		if ((n = fmt - cp) != 0) {
32046076Sbostic 			PRINT(cp, n);
32146076Sbostic 			ret += n;
32246076Sbostic 		}
32346076Sbostic 		if (ch == '\0')
32446076Sbostic 			goto done;
32546076Sbostic 		fmt++;		/* skip over '%' */
32646076Sbostic 
32746076Sbostic 		flags = 0;
32846076Sbostic 		dprec = 0;
32946076Sbostic #ifdef FLOATING_POINT
33046076Sbostic 		fpprec = 0;
33134318Sbostic #endif
33246076Sbostic 		width = 0;
33334233Sbostic 		prec = -1;
33434318Sbostic 		sign = '\0';
33534226Sbostic 
33646076Sbostic rflag:		ch = *fmt++;
33746076Sbostic reswitch:	switch (ch) {
33834318Sbostic 		case ' ':
33934669Sbostic 			/*
34034669Sbostic 			 * ``If the space and + flags both appear, the space
34134669Sbostic 			 * flag will be ignored.''
34234669Sbostic 			 *	-- ANSI X3J11
34334669Sbostic 			 */
34434669Sbostic 			if (!sign)
34534669Sbostic 				sign = ' ';
34634318Sbostic 			goto rflag;
34734233Sbostic 		case '#':
34834318Sbostic 			flags |= ALT;
34934318Sbostic 			goto rflag;
35034233Sbostic 		case '*':
35134235Sbostic 			/*
35234235Sbostic 			 * ``A negative field width argument is taken as a
35346076Sbostic 			 * - flag followed by a positive field width.''
35434235Sbostic 			 *	-- ANSI X3J11
35534235Sbostic 			 * They don't exclude field widths read from args.
35634235Sbostic 			 */
35746076Sbostic 			if ((width = va_arg(ap, int)) >= 0)
35834318Sbostic 				goto rflag;
35934235Sbostic 			width = -width;
36034427Sbostic 			/* FALLTHROUGH */
36134235Sbostic 		case '-':
36234318Sbostic 			flags |= LADJUST;
36334318Sbostic 			goto rflag;
36434233Sbostic 		case '+':
36534314Sbostic 			sign = '+';
36634318Sbostic 			goto rflag;
36734233Sbostic 		case '.':
36846076Sbostic 			if ((ch = *fmt++) == '*') {
36946076Sbostic 				n = va_arg(ap, int);
37046076Sbostic 				prec = n < 0 ? -1 : n;
37146076Sbostic 				goto rflag;
37234226Sbostic 			}
37346076Sbostic 			n = 0;
37446076Sbostic 			while (is_digit(ch)) {
37546076Sbostic 				n = 10 * n + to_digit(ch);
37646076Sbostic 				ch = *fmt++;
37746076Sbostic 			}
37834318Sbostic 			prec = n < 0 ? -1 : n;
37946076Sbostic 			goto reswitch;
38034233Sbostic 		case '0':
38134427Sbostic 			/*
38234427Sbostic 			 * ``Note that 0 is taken as a flag, not as the
38334427Sbostic 			 * beginning of a field width.''
38434427Sbostic 			 *	-- ANSI X3J11
38534427Sbostic 			 */
38634427Sbostic 			flags |= ZEROPAD;
38734427Sbostic 			goto rflag;
38834233Sbostic 		case '1': case '2': case '3': case '4':
38934233Sbostic 		case '5': case '6': case '7': case '8': case '9':
39034318Sbostic 			n = 0;
39134233Sbostic 			do {
39246076Sbostic 				n = 10 * n + to_digit(ch);
39346076Sbostic 				ch = *fmt++;
39446076Sbostic 			} while (is_digit(ch));
39534318Sbostic 			width = n;
39646076Sbostic 			goto reswitch;
39746076Sbostic #ifdef FLOATING_POINT
39834235Sbostic 		case 'L':
39934329Sbostic 			flags |= LONGDBL;
40034318Sbostic 			goto rflag;
40146076Sbostic #endif
40234235Sbostic 		case 'h':
40334318Sbostic 			flags |= SHORTINT;
40434318Sbostic 			goto rflag;
40534233Sbostic 		case 'l':
40634318Sbostic 			flags |= LONGINT;
40734318Sbostic 			goto rflag;
40834314Sbostic 		case 'c':
40946076Sbostic 			*(cp = buf) = va_arg(ap, int);
41034314Sbostic 			size = 1;
41134427Sbostic 			sign = '\0';
41246076Sbostic 			break;
41334624Sbostic 		case 'D':
41434624Sbostic 			flags |= LONGINT;
41534624Sbostic 			/*FALLTHROUGH*/
41634314Sbostic 		case 'd':
41734318Sbostic 		case 'i':
41846076Sbostic 			_ulong = SARG();
41934318Sbostic 			if ((long)_ulong < 0) {
42034318Sbostic 				_ulong = -_ulong;
42134314Sbostic 				sign = '-';
42234241Sbostic 			}
42346076Sbostic 			base = DEC;
42434327Sbostic 			goto number;
42546076Sbostic #ifdef FLOATING_POINT
42634261Sbostic 		case 'e':
42734236Sbostic 		case 'E':
42834235Sbostic 		case 'f':
42934261Sbostic 		case 'g':
43034243Sbostic 		case 'G':
43146076Sbostic 			_double = va_arg(ap, double);
43246126Storek #if defined(hp300) || defined(sparc)
43346126Storek 			/* do this before tricky precision changes */
43446126Storek 			if ((cp = isspecial(_double, &sign)) != NULL) {
43546126Storek 				size = strlen(cp);
43646126Storek 				break;
43746126Storek 			}
43846126Storek #endif
43934328Sbostic 			/*
44034669Sbostic 			 * don't do unrealistic precision; just pad it with
44134669Sbostic 			 * zeroes later, so buffer size stays rational.
44234328Sbostic 			 */
44334328Sbostic 			if (prec > MAXFRACT) {
44446076Sbostic 				if (ch != 'g' && ch != 'G' || (flags&ALT))
44534475Sbostic 					fpprec = prec - MAXFRACT;
44634328Sbostic 				prec = MAXFRACT;
44746076Sbostic 			} else if (prec == -1)
44834624Sbostic 				prec = DEFPREC;
44934669Sbostic 			/*
45046076Sbostic 			 * cvt may have to round up before the "start" of
45146076Sbostic 			 * its buffer, i.e. ``intf("%.2f", (double)9.999);'';
45246076Sbostic 			 * if the first character is still NUL, it did.
45346076Sbostic 			 * softsign avoids negative 0 if _double < 0 but
45446076Sbostic 			 * no significant digits will be shown.
45534669Sbostic 			 */
45646076Sbostic 			cp = buf;
45746076Sbostic 			*cp = '\0';
45846076Sbostic 			size = cvt(_double, prec, flags, &softsign, ch,
45946076Sbostic 			    cp, buf + sizeof(buf));
46034669Sbostic 			if (softsign)
46134669Sbostic 				sign = '-';
46246076Sbostic 			if (*cp == '\0')
46346076Sbostic 				cp++;
46446076Sbostic 			break;
46546076Sbostic #endif /* FLOATING_POINT */
46634235Sbostic 		case 'n':
46734427Sbostic 			if (flags & LONGINT)
46846076Sbostic 				*va_arg(ap, long *) = ret;
46934427Sbostic 			else if (flags & SHORTINT)
47046076Sbostic 				*va_arg(ap, short *) = ret;
47134318Sbostic 			else
47246076Sbostic 				*va_arg(ap, int *) = ret;
47346076Sbostic 			continue;	/* no output */
47434624Sbostic 		case 'O':
47534624Sbostic 			flags |= LONGINT;
47634624Sbostic 			/*FALLTHROUGH*/
47734226Sbostic 		case 'o':
47846076Sbostic 			_ulong = UARG();
47946076Sbostic 			base = OCT;
48034327Sbostic 			goto nosign;
48134235Sbostic 		case 'p':
48234320Sbostic 			/*
48334321Sbostic 			 * ``The argument shall be a pointer to void.  The
48434321Sbostic 			 * value of the pointer is converted to a sequence
48534321Sbostic 			 * of printable characters, in an implementation-
48634321Sbostic 			 * defined manner.''
48734321Sbostic 			 *	-- ANSI X3J11
48834320Sbostic 			 */
48934427Sbostic 			/* NOSTRICT */
49046076Sbostic 			_ulong = (u_long)va_arg(ap, void *);
49146076Sbostic 			base = HEX;
49246076Sbostic 			xdigs = "0123456789abcdef";
49346076Sbostic 			flags |= HEXPREFIX;
49446076Sbostic 			ch = 'x';
49534327Sbostic 			goto nosign;
49634226Sbostic 		case 's':
49746076Sbostic 			if ((cp = va_arg(ap, char *)) == NULL)
49846076Sbostic 				cp = "(null)";
49934321Sbostic 			if (prec >= 0) {
50034321Sbostic 				/*
50134321Sbostic 				 * can't use strlen; can only look for the
50234321Sbostic 				 * NUL in the first `prec' characters, and
50334321Sbostic 				 * strlen() will go further.
50434321Sbostic 				 */
50546076Sbostic 				char *p = memchr(cp, 0, prec);
50634321Sbostic 
50746076Sbostic 				if (p != NULL) {
50846076Sbostic 					size = p - cp;
50934321Sbostic 					if (size > prec)
51034321Sbostic 						size = prec;
51134427Sbostic 				} else
51234321Sbostic 					size = prec;
51334427Sbostic 			} else
51446076Sbostic 				size = strlen(cp);
51534427Sbostic 			sign = '\0';
51646076Sbostic 			break;
51734624Sbostic 		case 'U':
51834624Sbostic 			flags |= LONGINT;
51934624Sbostic 			/*FALLTHROUGH*/
52034226Sbostic 		case 'u':
52146076Sbostic 			_ulong = UARG();
52246076Sbostic 			base = DEC;
52334327Sbostic 			goto nosign;
52434226Sbostic 		case 'X':
52546076Sbostic 			xdigs = "0123456789ABCDEF";
52646076Sbostic 			goto hex;
52734226Sbostic 		case 'x':
52846076Sbostic 			xdigs = "0123456789abcdef";
52946076Sbostic hex:			_ulong = UARG();
53046076Sbostic 			base = HEX;
53134326Sbostic 			/* leading 0x/X only if non-zero */
53234427Sbostic 			if (flags & ALT && _ulong != 0)
53334427Sbostic 				flags |= HEXPREFIX;
53434327Sbostic 
53534327Sbostic 			/* unsigned conversions */
53634427Sbostic nosign:			sign = '\0';
53734326Sbostic 			/*
53834330Sbostic 			 * ``... diouXx conversions ... if a precision is
53934330Sbostic 			 * specified, the 0 flag will be ignored.''
54034330Sbostic 			 *	-- ANSI X3J11
54134330Sbostic 			 */
54234427Sbostic number:			if ((dprec = prec) >= 0)
54334427Sbostic 				flags &= ~ZEROPAD;
54434427Sbostic 
54534330Sbostic 			/*
54634326Sbostic 			 * ``The result of converting a zero value with an
54734326Sbostic 			 * explicit precision of zero is no characters.''
54834326Sbostic 			 *	-- ANSI X3J11
54934326Sbostic 			 */
55046076Sbostic 			cp = buf + BUF;
55134427Sbostic 			if (_ulong != 0 || prec != 0) {
55246076Sbostic 				/*
55346076Sbostic 				 * unsigned mod is hard, and unsigned mod
55446076Sbostic 				 * by a constant is easier than that by
55546076Sbostic 				 * a variable; hence this switch.
55646076Sbostic 				 */
55746076Sbostic 				switch (base) {
55846076Sbostic 				case OCT:
55946076Sbostic 					do {
56046076Sbostic 						*--cp = to_char(_ulong & 7);
56146076Sbostic 						_ulong >>= 3;
56246076Sbostic 					} while (_ulong);
56346076Sbostic 					/* handle octal leading 0 */
56446076Sbostic 					if (flags & ALT && *cp != '0')
56546076Sbostic 						*--cp = '0';
56646076Sbostic 					break;
56734327Sbostic 
56846076Sbostic 				case DEC:
56946076Sbostic 					/* many numbers are 1 digit */
57046076Sbostic 					while (_ulong >= 10) {
57146076Sbostic 						*--cp = to_char(_ulong % 10);
57246076Sbostic 						_ulong /= 10;
57346076Sbostic 					}
57446076Sbostic 					*--cp = to_char(_ulong);
57546076Sbostic 					break;
57634327Sbostic 
57746076Sbostic 				case HEX:
57846076Sbostic 					do {
57946076Sbostic 						*--cp = xdigs[_ulong & 15];
58046076Sbostic 						_ulong >>= 4;
58146076Sbostic 					} while (_ulong);
58246076Sbostic 					break;
58334327Sbostic 
58446076Sbostic 				default:
58546076Sbostic 					cp = "bug in vfprintf: bad base";
58646180Storek 					size = strlen(cp);
58746076Sbostic 					goto skipsize;
58846076Sbostic 				}
58934327Sbostic 			}
59046076Sbostic 			size = buf + BUF - cp;
59146076Sbostic 		skipsize:
59234226Sbostic 			break;
59346076Sbostic 		default:	/* "%?" prints ?, unless ? is NUL */
59446076Sbostic 			if (ch == '\0')
59546076Sbostic 				goto done;
59646076Sbostic 			/* pretend it was %c with argument ch */
59746076Sbostic 			cp = buf;
59846076Sbostic 			*cp = ch;
59946076Sbostic 			size = 1;
60046076Sbostic 			sign = '\0';
60146076Sbostic 			break;
60234226Sbostic 		}
60346076Sbostic 
60446076Sbostic 		/*
60546076Sbostic 		 * All reasonable formats wind up here.  At this point,
60646076Sbostic 		 * `cp' points to a string which (if not flags&LADJUST)
60746076Sbostic 		 * should be padded out to `width' places.  If
60846076Sbostic 		 * flags&ZEROPAD, it should first be prefixed by any
60946076Sbostic 		 * sign or other prefix; otherwise, it should be blank
61046076Sbostic 		 * padded before the prefix is emitted.  After any
61146076Sbostic 		 * left-hand padding and prefixing, emit zeroes
61246076Sbostic 		 * required by a decimal [diouxX] precision, then print
61346076Sbostic 		 * the string proper, then emit zeroes required by any
61446076Sbostic 		 * leftover floating precision; finally, if LADJUST,
61546076Sbostic 		 * pad with blanks.
61646076Sbostic 		 */
61746076Sbostic 
61846076Sbostic 		/*
61946076Sbostic 		 * compute actual size, so we know how much to pad.
62046076Sbostic 		 * fieldsz excludes decimal prec; realsz includes it
62146076Sbostic 		 */
62246076Sbostic #ifdef FLOATING_POINT
62346076Sbostic 		fieldsz = size + fpprec;
62446076Sbostic #else
62546076Sbostic 		fieldsz = size;
62646076Sbostic #endif
62746076Sbostic 		if (sign)
62846076Sbostic 			fieldsz++;
62946076Sbostic 		else if (flags & HEXPREFIX)
63046076Sbostic 			fieldsz += 2;
63146076Sbostic 		realsz = dprec > fieldsz ? dprec : fieldsz;
63246076Sbostic 
63346076Sbostic 		/* right-adjusting blank padding */
63446076Sbostic 		if ((flags & (LADJUST|ZEROPAD)) == 0)
63546076Sbostic 			PAD(width - realsz, blanks);
63646076Sbostic 
63746076Sbostic 		/* prefix */
63846076Sbostic 		if (sign) {
63946076Sbostic 			PRINT(&sign, 1);
64046076Sbostic 		} else if (flags & HEXPREFIX) {
64146076Sbostic 			ox[0] = '0';
64246076Sbostic 			ox[1] = ch;
64346076Sbostic 			PRINT(ox, 2);
64446076Sbostic 		}
64546076Sbostic 
64646076Sbostic 		/* right-adjusting zero padding */
64746076Sbostic 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
64846076Sbostic 			PAD(width - realsz, zeroes);
64946076Sbostic 
65046076Sbostic 		/* leading zeroes from decimal precision */
65146076Sbostic 		PAD(dprec - fieldsz, zeroes);
65246076Sbostic 
65346076Sbostic 		/* the string or number proper */
65446076Sbostic 		PRINT(cp, size);
65546076Sbostic 
65646076Sbostic #ifdef FLOATING_POINT
65746076Sbostic 		/* trailing f.p. zeroes */
65846076Sbostic 		PAD(fpprec, zeroes);
65946076Sbostic #endif
66046076Sbostic 
66146076Sbostic 		/* left-adjusting padding (always blank) */
66246076Sbostic 		if (flags & LADJUST)
66346076Sbostic 			PAD(width - realsz, blanks);
66446076Sbostic 
66546076Sbostic 		/* finally, adjust ret */
66646076Sbostic 		ret += width > realsz ? width : realsz;
66746076Sbostic 
66846076Sbostic 		FLUSH();	/* copy out the I/O vectors */
66934226Sbostic 	}
67046076Sbostic done:
67146076Sbostic 	FLUSH();
67246076Sbostic error:
67346076Sbostic 	return (__sferror(fp) ? EOF : ret);
67434427Sbostic 	/* NOTREACHED */
67534226Sbostic }
67634242Sbostic 
67746076Sbostic #ifdef FLOATING_POINT
67846180Storek #include <math.h>
67946180Storek 
68046076Sbostic static char *exponent();
68146076Sbostic static char *round();
68246076Sbostic 
68346126Storek #if defined(hp300) || defined(sparc)
68446126Storek /*
68546126Storek  * Check for special IEEE format values (NaN, Inf).
68646126Storek  */
68746126Storek static char *
68846126Storek isspecial(d, signp)
68946126Storek 	double d;
69046161Storek 	char *signp;
69146126Storek {
69246126Storek 	register struct IEEEdp {
69346126Storek 		unsigned sign:1;
69446126Storek 		unsigned exp:11;
69546126Storek 		unsigned manh:20;
69646126Storek 		unsigned manl:32;
69746126Storek 	} *ip = (struct IEEEdp *)&d;
69846126Storek 
69946126Storek 	if (ip->exp != 0x7ff)
70046126Storek 		return (NULL);
70146126Storek 	if (ip->sign)
70246126Storek 		*signp = '-';
70346161Storek 	return (ip->manh || ip->manl ? "NaN" : "Inf");
70446126Storek }
70546126Storek #endif /* hp300 or sparc */
70646126Storek 
70746180Storek static int
70834669Sbostic cvt(number, prec, flags, signp, fmtch, startp, endp)
70934242Sbostic 	double number;
71034261Sbostic 	register int prec;
71134323Sbostic 	int flags;
71246076Sbostic 	char *signp;
71346076Sbostic 	int fmtch;
71446076Sbostic 	char *startp, *endp;
71534242Sbostic {
71634331Sbostic 	register char *p, *t;
71734672Sbostic 	register double fract;
71834624Sbostic 	int dotrim, expcnt, gformat;
71946180Storek 	double integer, tmp;
72034242Sbostic 
72146076Sbostic 	dotrim = expcnt = gformat = 0;
72246076Sbostic 	if (number < 0) {
72346076Sbostic 		number = -number;
72446076Sbostic 		*signp = '-';
72546076Sbostic 	} else
72646076Sbostic 		*signp = 0;
72744426Sbostic 
72834624Sbostic 	fract = modf(number, &integer);
72934242Sbostic 
73034624Sbostic 	/* get an extra slot for rounding. */
73134624Sbostic 	t = ++startp;
73234624Sbostic 
73334624Sbostic 	/*
73434624Sbostic 	 * get integer portion of number; put into the end of the buffer; the
73534624Sbostic 	 * .01 is added for modf(356.0 / 10, &integer) returning .59999999...
73634624Sbostic 	 */
73734624Sbostic 	for (p = endp - 1; integer; ++expcnt) {
73834624Sbostic 		tmp = modf(integer / 10, &integer);
73946076Sbostic 		*p-- = to_char((int)((tmp + .01) * 10));
74034248Sbostic 	}
74146076Sbostic 	switch (fmtch) {
74234261Sbostic 	case 'f':
74334624Sbostic 		/* reverse integer into beginning of buffer */
74434624Sbostic 		if (expcnt)
74534624Sbostic 			for (; ++p < endp; *t++ = *p);
74634624Sbostic 		else
74734624Sbostic 			*t++ = '0';
74834248Sbostic 		/*
74934624Sbostic 		 * if precision required or alternate flag set, add in a
75034624Sbostic 		 * decimal point.
75134248Sbostic 		 */
75234624Sbostic 		if (prec || flags&ALT)
75334624Sbostic 			*t++ = '.';
75434624Sbostic 		/* if requires more precision and some fraction left */
75534624Sbostic 		if (fract) {
75634624Sbostic 			if (prec)
75734624Sbostic 				do {
75834624Sbostic 					fract = modf(fract * 10, &tmp);
75946076Sbostic 					*t++ = to_char((int)tmp);
76034624Sbostic 				} while (--prec && fract);
76134624Sbostic 			if (fract)
76234669Sbostic 				startp = round(fract, (int *)NULL, startp,
76334669Sbostic 				    t - 1, (char)0, signp);
76434624Sbostic 		}
76534624Sbostic 		for (; prec--; *t++ = '0');
76634624Sbostic 		break;
76734624Sbostic 	case 'e':
76834624Sbostic 	case 'E':
76934624Sbostic eformat:	if (expcnt) {
77034624Sbostic 			*t++ = *++p;
77134624Sbostic 			if (prec || flags&ALT)
77234331Sbostic 				*t++ = '.';
77334624Sbostic 			/* if requires more precision and some integer left */
77434624Sbostic 			for (; prec && ++p < endp; --prec)
77534624Sbostic 				*t++ = *p;
77634624Sbostic 			/*
77734624Sbostic 			 * if done precision and more of the integer component,
77834624Sbostic 			 * round using it; adjust fract so we don't re-round
77934624Sbostic 			 * later.
78034624Sbostic 			 */
78134624Sbostic 			if (!prec && ++p < endp) {
78234248Sbostic 				fract = 0;
78334669Sbostic 				startp = round((double)0, &expcnt, startp,
78434669Sbostic 				    t - 1, *p, signp);
78534248Sbostic 			}
78634624Sbostic 			/* adjust expcnt for digit in front of decimal */
78734624Sbostic 			--expcnt;
78834242Sbostic 		}
78934624Sbostic 		/* until first fractional digit, decrement exponent */
79034624Sbostic 		else if (fract) {
79134624Sbostic 			/* adjust expcnt for digit in front of decimal */
79234624Sbostic 			for (expcnt = -1;; --expcnt) {
79334624Sbostic 				fract = modf(fract * 10, &tmp);
79434624Sbostic 				if (tmp)
79534624Sbostic 					break;
79634248Sbostic 			}
79746076Sbostic 			*t++ = to_char((int)tmp);
79834624Sbostic 			if (prec || flags&ALT)
79934331Sbostic 				*t++ = '.';
80034242Sbostic 		}
80134248Sbostic 		else {
80234624Sbostic 			*t++ = '0';
80334624Sbostic 			if (prec || flags&ALT)
80434331Sbostic 				*t++ = '.';
80534248Sbostic 		}
80634624Sbostic 		/* if requires more precision and some fraction left */
80734624Sbostic 		if (fract) {
80834624Sbostic 			if (prec)
80934624Sbostic 				do {
81034624Sbostic 					fract = modf(fract * 10, &tmp);
81146076Sbostic 					*t++ = to_char((int)tmp);
81234624Sbostic 				} while (--prec && fract);
81334624Sbostic 			if (fract)
81434669Sbostic 				startp = round(fract, &expcnt, startp,
81534669Sbostic 				    t - 1, (char)0, signp);
81634584Sbostic 		}
81734624Sbostic 		/* if requires more precision */
81834624Sbostic 		for (; prec--; *t++ = '0');
81934624Sbostic 
82034624Sbostic 		/* unless alternate flag, trim any g/G format trailing 0's */
82134624Sbostic 		if (gformat && !(flags&ALT)) {
82234624Sbostic 			while (t > startp && *--t == '0');
82334624Sbostic 			if (*t == '.')
82434624Sbostic 				--t;
82534624Sbostic 			++t;
82634624Sbostic 		}
82734624Sbostic 		t = exponent(t, expcnt, fmtch);
82834624Sbostic 		break;
82934624Sbostic 	case 'g':
83034624Sbostic 	case 'G':
83134624Sbostic 		/* a precision of 0 is treated as a precision of 1. */
83234624Sbostic 		if (!prec)
83334624Sbostic 			++prec;
83434624Sbostic 		/*
83534624Sbostic 		 * ``The style used depends on the value converted; style e
83634624Sbostic 		 * will be used only if the exponent resulting from the
83734624Sbostic 		 * conversion is less than -4 or greater than the precision.''
83834624Sbostic 		 *	-- ANSI X3J11
83934624Sbostic 		 */
84034624Sbostic 		if (expcnt > prec || !expcnt && fract && fract < .0001) {
84134624Sbostic 			/*
84234624Sbostic 			 * g/G format counts "significant digits, not digits of
84334624Sbostic 			 * precision; for the e/E format, this just causes an
84434624Sbostic 			 * off-by-one problem, i.e. g/G considers the digit
84534624Sbostic 			 * before the decimal point significant and e/E doesn't
84634624Sbostic 			 * count it as precision.
84734624Sbostic 			 */
84834624Sbostic 			--prec;
84934624Sbostic 			fmtch -= 2;		/* G->E, g->e */
85034624Sbostic 			gformat = 1;
85134624Sbostic 			goto eformat;
85234624Sbostic 		}
85334624Sbostic 		/*
85434624Sbostic 		 * reverse integer into beginning of buffer,
85534624Sbostic 		 * note, decrement precision
85634624Sbostic 		 */
85734624Sbostic 		if (expcnt)
85834624Sbostic 			for (; ++p < endp; *t++ = *p, --prec);
85934624Sbostic 		else
86034624Sbostic 			*t++ = '0';
86134624Sbostic 		/*
86234624Sbostic 		 * if precision required or alternate flag set, add in a
86334624Sbostic 		 * decimal point.  If no digits yet, add in leading 0.
86434624Sbostic 		 */
86534624Sbostic 		if (prec || flags&ALT) {
86634624Sbostic 			dotrim = 1;
86734624Sbostic 			*t++ = '.';
86834624Sbostic 		}
86934624Sbostic 		else
87034624Sbostic 			dotrim = 0;
87134624Sbostic 		/* if requires more precision and some fraction left */
87234624Sbostic 		if (fract) {
87334624Sbostic 			if (prec) {
87434624Sbostic 				do {
87534624Sbostic 					fract = modf(fract * 10, &tmp);
87646076Sbostic 					*t++ = to_char((int)tmp);
87734624Sbostic 				} while(!tmp);
87834624Sbostic 				while (--prec && fract) {
87934624Sbostic 					fract = modf(fract * 10, &tmp);
88046076Sbostic 					*t++ = to_char((int)tmp);
88134248Sbostic 				}
88234248Sbostic 			}
88334624Sbostic 			if (fract)
88434669Sbostic 				startp = round(fract, (int *)NULL, startp,
88534669Sbostic 				    t - 1, (char)0, signp);
88634624Sbostic 		}
88734624Sbostic 		/* alternate format, adds 0's for precision, else trim 0's */
88834624Sbostic 		if (flags&ALT)
88934624Sbostic 			for (; prec--; *t++ = '0');
89034624Sbostic 		else if (dotrim) {
89134624Sbostic 			while (t > startp && *--t == '0');
89234624Sbostic 			if (*t != '.')
89334624Sbostic 				++t;
89434624Sbostic 		}
89534624Sbostic 	}
89646076Sbostic 	return (t - startp);
89734624Sbostic }
89834248Sbostic 
89934624Sbostic static char *
90034669Sbostic round(fract, exp, start, end, ch, signp)
90134624Sbostic 	double fract;
90234669Sbostic 	int *exp;
90334624Sbostic 	register char *start, *end;
90434669Sbostic 	char ch, *signp;
90534624Sbostic {
90634624Sbostic 	double tmp;
90734248Sbostic 
90834624Sbostic 	if (fract)
90946180Storek 		(void)modf(fract * 10, &tmp);
91034624Sbostic 	else
91146076Sbostic 		tmp = to_digit(ch);
91234624Sbostic 	if (tmp > 4)
91334624Sbostic 		for (;; --end) {
91434624Sbostic 			if (*end == '.')
91534624Sbostic 				--end;
91634624Sbostic 			if (++*end <= '9')
91734624Sbostic 				break;
91834624Sbostic 			*end = '0';
91934624Sbostic 			if (end == start) {
92034669Sbostic 				if (exp) {	/* e/E; increment exponent */
92134669Sbostic 					*end = '1';
92234669Sbostic 					++*exp;
92334669Sbostic 				}
92434669Sbostic 				else {		/* f; add extra digit */
92546076Sbostic 				*--end = '1';
92646076Sbostic 				--start;
92734669Sbostic 				}
92834624Sbostic 				break;
92934242Sbostic 			}
93034242Sbostic 		}
93134669Sbostic 	/* ``"%.3f", (double)-0.0004'' gives you a negative 0. */
93234669Sbostic 	else if (*signp == '-')
93334669Sbostic 		for (;; --end) {
93434669Sbostic 			if (*end == '.')
93534669Sbostic 				--end;
93634669Sbostic 			if (*end != '0')
93734669Sbostic 				break;
93834669Sbostic 			if (end == start)
93934669Sbostic 				*signp = 0;
94034669Sbostic 		}
94146076Sbostic 	return (start);
94234624Sbostic }
94334248Sbostic 
94434624Sbostic static char *
94534624Sbostic exponent(p, exp, fmtch)
94634624Sbostic 	register char *p;
94734624Sbostic 	register int exp;
94846076Sbostic 	int fmtch;
94934624Sbostic {
95034624Sbostic 	register char *t;
95134624Sbostic 	char expbuf[MAXEXP];
95234248Sbostic 
95334624Sbostic 	*p++ = fmtch;
95434624Sbostic 	if (exp < 0) {
95534624Sbostic 		exp = -exp;
95634624Sbostic 		*p++ = '-';
95734242Sbostic 	}
95834624Sbostic 	else
95934624Sbostic 		*p++ = '+';
96034624Sbostic 	t = expbuf + MAXEXP;
96134624Sbostic 	if (exp > 9) {
96234624Sbostic 		do {
96346076Sbostic 			*--t = to_char(exp % 10);
96434624Sbostic 		} while ((exp /= 10) > 9);
96546076Sbostic 		*--t = to_char(exp);
96634624Sbostic 		for (; t < expbuf + MAXEXP; *p++ = *t++);
96734624Sbostic 	}
96834624Sbostic 	else {
96934624Sbostic 		*p++ = '0';
97046076Sbostic 		*p++ = to_char(exp);
97134624Sbostic 	}
97246076Sbostic 	return (p);
97334242Sbostic }
97446076Sbostic #endif /* FLOATING_POINT */
975