Home | History | Annotate | Line # | Download | only in stdio
vfprintf.c revision 1.13
      1 /*-
      2  * Copyright (c) 1990 The Regents of the University of California.
      3  * All rights reserved.
      4  *
      5  * This code is derived from software contributed to Berkeley by
      6  * Chris Torek.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 /*static char *sccsid = "from: @(#)vfprintf.c	5.50 (Berkeley) 12/16/92";*/
     39 static char *rcsid = "$Id: vfprintf.c,v 1.13 1994/10/20 03:56:56 jtc Exp $";
     40 #endif /* LIBC_SCCS and not lint */
     41 
     42 /*
     43  * Actual printf innards.
     44  *
     45  * This code is large and complicated...
     46  */
     47 
     48 #include <sys/types.h>
     49 
     50 #include <stdio.h>
     51 #include <stdlib.h>
     52 #include <string.h>
     53 
     54 #if __STDC__
     55 #include <stdarg.h>
     56 #else
     57 #include <varargs.h>
     58 #endif
     59 
     60 #include "local.h"
     61 #include "fvwrite.h"
     62 
     63 /*
     64  * Flush out all the vectors defined by the given uio,
     65  * then reset it so that it can be reused.
     66  */
     67 static int
     68 __sprint(fp, uio)
     69 	FILE *fp;
     70 	register struct __suio *uio;
     71 {
     72 	register int err;
     73 
     74 	if (uio->uio_resid == 0) {
     75 		uio->uio_iovcnt = 0;
     76 		return (0);
     77 	}
     78 	err = __sfvwrite(fp, uio);
     79 	uio->uio_resid = 0;
     80 	uio->uio_iovcnt = 0;
     81 	return (err);
     82 }
     83 
     84 /*
     85  * Helper function for `fprintf to unbuffered unix file': creates a
     86  * temporary buffer.  We only work on write-only files; this avoids
     87  * worries about ungetc buffers and so forth.
     88  */
     89 static int
     90 __sbprintf(fp, fmt, ap)
     91 	register FILE *fp;
     92 	const char *fmt;
     93 	va_list ap;
     94 {
     95 	int ret;
     96 	FILE fake;
     97 	unsigned char buf[BUFSIZ];
     98 
     99 	/* copy the important variables */
    100 	fake._flags = fp->_flags & ~__SNBF;
    101 	fake._file = fp->_file;
    102 	fake._cookie = fp->_cookie;
    103 	fake._write = fp->_write;
    104 
    105 	/* set up the buffer */
    106 	fake._bf._base = fake._p = buf;
    107 	fake._bf._size = fake._w = sizeof(buf);
    108 	fake._lbfsize = 0;	/* not actually used, but Just In Case */
    109 
    110 	/* do the work, then copy any error status */
    111 	ret = vfprintf(&fake, fmt, ap);
    112 	if (ret >= 0 && fflush(&fake))
    113 		ret = EOF;
    114 	if (fake._flags & __SERR)
    115 		fp->_flags |= __SERR;
    116 	return (ret);
    117 }
    118 
    119 
    120 #ifdef FLOATING_POINT
    121 #include <locale.h>
    122 #include <math.h>
    123 #include "floatio.h"
    124 
    125 #define	BUF		(MAXEXP+MAXFRACT+1)	/* + decimal point */
    126 #define	DEFPREC		6
    127 
    128 static char *cvt __P((double, int, int, char *, int *, int, int *));
    129 static int exponent __P((char *, int, int));
    130 
    131 #else /* no FLOATING_POINT */
    132 
    133 #define	BUF		40
    134 
    135 #endif /* FLOATING_POINT */
    136 
    137 
    138 /*
    139  * Macros for converting digits to letters and vice versa
    140  */
    141 #define	to_digit(c)	((c) - '0')
    142 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
    143 #define	to_char(n)	((n) + '0')
    144 
    145 /*
    146  * Flags used during conversion.
    147  */
    148 #define	ALT		0x001		/* alternate form */
    149 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
    150 #define	LADJUST		0x004		/* left adjustment */
    151 #define	LONGDBL		0x008		/* long double; unimplemented */
    152 #define	LONGINT		0x010		/* long integer */
    153 #define	QUADINT		0x020		/* quad integer */
    154 #define	SHORTINT	0x040		/* short integer */
    155 #define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
    156 #define FPT		0x100		/* Floating point number */
    157 int
    158 vfprintf(fp, fmt0, ap)
    159 	FILE *fp;
    160 	const char *fmt0;
    161 	_BSD_VA_LIST_ ap;
    162 {
    163 	register char *fmt;	/* format string */
    164 	register int ch;	/* character from fmt */
    165 	register int n;		/* handy integer (short term usage) */
    166 	register char *cp;	/* handy char pointer (short term usage) */
    167 	register struct __siov *iovp;/* for PRINT macro */
    168 	register int flags;	/* flags as above */
    169 	int ret;		/* return value accumulator */
    170 	int width;		/* width from format (%8d), or 0 */
    171 	int prec;		/* precision from format (%.3d), or -1 */
    172 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
    173 #ifdef FLOATING_POINT
    174 	char *decimal_point = localeconv()->decimal_point;
    175 	char softsign;		/* temporary negative sign for floats */
    176 	double _double;		/* double precision arguments %[eEfgG] */
    177 	int expt;		/* integer value of exponent */
    178 	int expsize;		/* character count for expstr */
    179 	int ndig;		/* actual number of digits returned by cvt */
    180 	char expstr[7];		/* buffer for exponent string */
    181 #endif
    182 
    183 #ifdef __GNUC__			/* gcc has builtin quad type (long long) SOS */
    184 #define	quad_t	  long long
    185 #define	u_quad_t  unsigned long long
    186 #endif
    187 
    188 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
    189 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
    190 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
    191 	int fieldsz;		/* field size expanded by sign, etc */
    192 	int realsz;		/* field size expanded by dprec */
    193 	int size;		/* size of converted field or string */
    194 	char *xdigs;		/* digits for [xX] conversion */
    195 #define NIOV 8
    196 	struct __suio uio;	/* output information: summary */
    197 	struct __siov iov[NIOV];/* ... and individual io vectors */
    198 	char buf[BUF];		/* space for %c, %[diouxX], %[eEfgG] */
    199 	char ox[2];		/* space for 0x hex-prefix */
    200 
    201 	/*
    202 	 * Choose PADSIZE to trade efficiency vs. size.  If larger printf
    203 	 * fields occur frequently, increase PADSIZE and make the initialisers
    204 	 * below longer.
    205 	 */
    206 #define	PADSIZE	16		/* pad chunk size */
    207 	static char blanks[PADSIZE] =
    208 	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
    209 	static char zeroes[PADSIZE] =
    210 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
    211 
    212 	/*
    213 	 * BEWARE, these `goto error' on error, and PAD uses `n'.
    214 	 */
    215 #define	PRINT(ptr, len) { \
    216 	iovp->iov_base = (ptr); \
    217 	iovp->iov_len = (len); \
    218 	uio.uio_resid += (len); \
    219 	iovp++; \
    220 	if (++uio.uio_iovcnt >= NIOV) { \
    221 		if (__sprint(fp, &uio)) \
    222 			goto error; \
    223 		iovp = iov; \
    224 	} \
    225 }
    226 #define	PAD(howmany, with) { \
    227 	if ((n = (howmany)) > 0) { \
    228 		while (n > PADSIZE) { \
    229 			PRINT(with, PADSIZE); \
    230 			n -= PADSIZE; \
    231 		} \
    232 		PRINT(with, n); \
    233 	} \
    234 }
    235 #define	FLUSH() { \
    236 	if (uio.uio_resid && __sprint(fp, &uio)) \
    237 		goto error; \
    238 	uio.uio_iovcnt = 0; \
    239 	iovp = iov; \
    240 }
    241 
    242 	/*
    243 	 * To extend shorts properly, we need both signed and unsigned
    244 	 * argument extraction methods.
    245 	 */
    246 #define	SARG() \
    247 	(flags&QUADINT ? va_arg(ap, quad_t) : \
    248 	    flags&LONGINT ? va_arg(ap, long) : \
    249 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
    250 	    (long)va_arg(ap, int))
    251 #define	UARG() \
    252 	(flags&QUADINT ? va_arg(ap, u_quad_t) : \
    253 	    flags&LONGINT ? va_arg(ap, u_long) : \
    254 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
    255 	    (u_long)va_arg(ap, u_int))
    256 
    257 	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
    258 	if (cantwrite(fp))
    259 		return (EOF);
    260 
    261 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
    262 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
    263 	    fp->_file >= 0)
    264 		return (__sbprintf(fp, fmt0, ap));
    265 
    266 	fmt = (char *)fmt0;
    267 	uio.uio_iov = iovp = iov;
    268 	uio.uio_resid = 0;
    269 	uio.uio_iovcnt = 0;
    270 	ret = 0;
    271 
    272 	/*
    273 	 * Scan the format for conversions (`%' character).
    274 	 */
    275 	for (;;) {
    276 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
    277 			/* void */;
    278 		if ((n = fmt - cp) != 0) {
    279 			PRINT(cp, n);
    280 			ret += n;
    281 		}
    282 		if (ch == '\0')
    283 			goto done;
    284 		fmt++;		/* skip over '%' */
    285 
    286 		flags = 0;
    287 		dprec = 0;
    288 		width = 0;
    289 		prec = -1;
    290 		sign = '\0';
    291 
    292 rflag:		ch = *fmt++;
    293 reswitch:	switch (ch) {
    294 		case ' ':
    295 			/*
    296 			 * ``If the space and + flags both appear, the space
    297 			 * flag will be ignored.''
    298 			 *	-- ANSI X3J11
    299 			 */
    300 			if (!sign)
    301 				sign = ' ';
    302 			goto rflag;
    303 		case '#':
    304 			flags |= ALT;
    305 			goto rflag;
    306 		case '*':
    307 			/*
    308 			 * ``A negative field width argument is taken as a
    309 			 * - flag followed by a positive field width.''
    310 			 *	-- ANSI X3J11
    311 			 * They don't exclude field widths read from args.
    312 			 */
    313 			if ((width = va_arg(ap, int)) >= 0)
    314 				goto rflag;
    315 			width = -width;
    316 			/* FALLTHROUGH */
    317 		case '-':
    318 			flags |= LADJUST;
    319 			goto rflag;
    320 		case '+':
    321 			sign = '+';
    322 			goto rflag;
    323 		case '.':
    324 			if ((ch = *fmt++) == '*') {
    325 				n = va_arg(ap, int);
    326 				prec = n < 0 ? -1 : n;
    327 				goto rflag;
    328 			}
    329 			n = 0;
    330 			while (is_digit(ch)) {
    331 				n = 10 * n + to_digit(ch);
    332 				ch = *fmt++;
    333 			}
    334 			prec = n < 0 ? -1 : n;
    335 			goto reswitch;
    336 		case '0':
    337 			/*
    338 			 * ``Note that 0 is taken as a flag, not as the
    339 			 * beginning of a field width.''
    340 			 *	-- ANSI X3J11
    341 			 */
    342 			flags |= ZEROPAD;
    343 			goto rflag;
    344 		case '1': case '2': case '3': case '4':
    345 		case '5': case '6': case '7': case '8': case '9':
    346 			n = 0;
    347 			do {
    348 				n = 10 * n + to_digit(ch);
    349 				ch = *fmt++;
    350 			} while (is_digit(ch));
    351 			width = n;
    352 			goto reswitch;
    353 #ifdef FLOATING_POINT
    354 		case 'L':
    355 			flags |= LONGDBL;
    356 			goto rflag;
    357 #endif
    358 		case 'h':
    359 			flags |= SHORTINT;
    360 			goto rflag;
    361 		case 'l':
    362 			flags |= LONGINT;
    363 			goto rflag;
    364 		case 'q':
    365 			flags |= QUADINT;
    366 			goto rflag;
    367 		case 'c':
    368 			*(cp = buf) = va_arg(ap, int);
    369 			size = 1;
    370 			sign = '\0';
    371 			break;
    372 		case 'D':
    373 			flags |= LONGINT;
    374 			/*FALLTHROUGH*/
    375 		case 'd':
    376 		case 'i':
    377 			_uquad = SARG();
    378 			if ((quad_t)_uquad < 0) {
    379 				_uquad = -_uquad;
    380 				sign = '-';
    381 			}
    382 			base = DEC;
    383 			goto number;
    384 #ifdef FLOATING_POINT
    385 		case 'e':
    386 		case 'E':
    387 		case 'f':
    388 		case 'g':
    389 		case 'G':
    390 			if (prec == -1) {
    391 				prec = DEFPREC;
    392 			} else if ((ch == 'g' || ch == 'G') && prec == 0) {
    393 				prec = 1;
    394 			}
    395 
    396 			if (flags & LONGDBL) {
    397 				_double = (double) va_arg(ap, long double);
    398 			} else {
    399 				_double = va_arg(ap, double);
    400 			}
    401 
    402 			/* do this before tricky precision changes */
    403 			if (isinf(_double)) {
    404 				if (_double < 0)
    405 					sign = '-';
    406 				cp = "Inf";
    407 				size = 3;
    408 				break;
    409 			}
    410 			if (isnan(_double)) {
    411 				cp = "NaN";
    412 				size = 3;
    413 				break;
    414 			}
    415 
    416 			flags |= FPT;
    417 			cp = cvt(_double, prec, flags, &softsign,
    418 				&expt, ch, &ndig);
    419 			if (ch == 'g' || ch == 'G') {
    420 				if (expt <= -4 || expt > prec)
    421 					ch = (ch == 'g') ? 'e' : 'E';
    422 				else
    423 					ch = 'g';
    424 			}
    425 			if (ch <= 'e') {	/* 'e' or 'E' fmt */
    426 				--expt;
    427 				expsize = exponent(expstr, expt, ch);
    428 				size = expsize + ndig;
    429 				if (ndig > 1 || flags & ALT)
    430 					++size;
    431 			} else if (ch == 'f') {		/* f fmt */
    432 				if (expt > 0) {
    433 					size = expt;
    434 					if (prec || flags & ALT)
    435 						size += prec + 1;
    436 				} else	/* "0.X" */
    437 					size = prec + 2;
    438 			} else if (expt >= ndig) {	/* fixed g fmt */
    439 				size = expt;
    440 				if (flags & ALT)
    441 					++size;
    442 			} else
    443 				size = ndig + (expt > 0 ?
    444 					1 : 2 - expt);
    445 
    446 			if (softsign)
    447 				sign = '-';
    448 			break;
    449 #endif /* FLOATING_POINT */
    450 		case 'n':
    451 			if (flags & QUADINT)
    452 				*va_arg(ap, quad_t *) = ret;
    453 			else if (flags & LONGINT)
    454 				*va_arg(ap, long *) = ret;
    455 			else if (flags & SHORTINT)
    456 				*va_arg(ap, short *) = ret;
    457 			else
    458 				*va_arg(ap, int *) = ret;
    459 			continue;	/* no output */
    460 		case 'O':
    461 			flags |= LONGINT;
    462 			/*FALLTHROUGH*/
    463 		case 'o':
    464 			_uquad = UARG();
    465 			base = OCT;
    466 			goto nosign;
    467 		case 'p':
    468 			/*
    469 			 * ``The argument shall be a pointer to void.  The
    470 			 * value of the pointer is converted to a sequence
    471 			 * of printable characters, in an implementation-
    472 			 * defined manner.''
    473 			 *	-- ANSI X3J11
    474 			 */
    475 			/* NOSTRICT */
    476 			_uquad = (u_quad_t)va_arg(ap, void *);
    477 			base = HEX;
    478 			xdigs = "0123456789abcdef";
    479 			flags |= HEXPREFIX;
    480 			ch = 'x';
    481 			goto nosign;
    482 		case 's':
    483 			if ((cp = va_arg(ap, char *)) == NULL)
    484 				cp = "(null)";
    485 			if (prec >= 0) {
    486 				/*
    487 				 * can't use strlen; can only look for the
    488 				 * NUL in the first `prec' characters, and
    489 				 * strlen() will go further.
    490 				 */
    491 				char *p = memchr(cp, 0, prec);
    492 
    493 				if (p != NULL) {
    494 					size = p - cp;
    495 					if (size > prec)
    496 						size = prec;
    497 				} else
    498 					size = prec;
    499 			} else
    500 				size = strlen(cp);
    501 			sign = '\0';
    502 			break;
    503 		case 'U':
    504 			flags |= LONGINT;
    505 			/*FALLTHROUGH*/
    506 		case 'u':
    507 			_uquad = UARG();
    508 			base = DEC;
    509 			goto nosign;
    510 		case 'X':
    511 			xdigs = "0123456789ABCDEF";
    512 			goto hex;
    513 		case 'x':
    514 			xdigs = "0123456789abcdef";
    515 hex:			_uquad = UARG();
    516 			base = HEX;
    517 			/* leading 0x/X only if non-zero */
    518 			if (flags & ALT && _uquad != 0)
    519 				flags |= HEXPREFIX;
    520 
    521 			/* unsigned conversions */
    522 nosign:			sign = '\0';
    523 			/*
    524 			 * ``... diouXx conversions ... if a precision is
    525 			 * specified, the 0 flag will be ignored.''
    526 			 *	-- ANSI X3J11
    527 			 */
    528 number:			if ((dprec = prec) >= 0)
    529 				flags &= ~ZEROPAD;
    530 
    531 			/*
    532 			 * ``The result of converting a zero value with an
    533 			 * explicit precision of zero is no characters.''
    534 			 *	-- ANSI X3J11
    535 			 */
    536 			cp = buf + BUF;
    537 			if (_uquad != 0 || prec != 0) {
    538 				/*
    539 				 * Unsigned mod is hard, and unsigned mod
    540 				 * by a constant is easier than that by
    541 				 * a variable; hence this switch.
    542 				 */
    543 				switch (base) {
    544 				case OCT:
    545 					do {
    546 						*--cp = to_char(_uquad & 7);
    547 						_uquad >>= 3;
    548 					} while (_uquad);
    549 					/* handle octal leading 0 */
    550 					if (flags & ALT && *cp != '0')
    551 						*--cp = '0';
    552 					break;
    553 
    554 				case DEC:
    555 					/* many numbers are 1 digit */
    556 					while (_uquad >= 10) {
    557 						*--cp = to_char(_uquad % 10);
    558 						_uquad /= 10;
    559 					}
    560 					*--cp = to_char(_uquad);
    561 					break;
    562 
    563 				case HEX:
    564 					do {
    565 						*--cp = xdigs[_uquad & 15];
    566 						_uquad >>= 4;
    567 					} while (_uquad);
    568 					break;
    569 
    570 				default:
    571 					cp = "bug in vfprintf: bad base";
    572 					size = strlen(cp);
    573 					goto skipsize;
    574 				}
    575 			}
    576 			size = buf + BUF - cp;
    577 		skipsize:
    578 			break;
    579 		default:	/* "%?" prints ?, unless ? is NUL */
    580 			if (ch == '\0')
    581 				goto done;
    582 			/* pretend it was %c with argument ch */
    583 			cp = buf;
    584 			*cp = ch;
    585 			size = 1;
    586 			sign = '\0';
    587 			break;
    588 		}
    589 
    590 		/*
    591 		 * All reasonable formats wind up here.  At this point, `cp'
    592 		 * points to a string which (if not flags&LADJUST) should be
    593 		 * padded out to `width' places.  If flags&ZEROPAD, it should
    594 		 * first be prefixed by any sign or other prefix; otherwise,
    595 		 * it should be blank padded before the prefix is emitted.
    596 		 * After any left-hand padding and prefixing, emit zeroes
    597 		 * required by a decimal [diouxX] precision, then print the
    598 		 * string proper, then emit zeroes required by any leftover
    599 		 * floating precision; finally, if LADJUST, pad with blanks.
    600 		 *
    601 		 * Compute actual size, so we know how much to pad.
    602 		 * fieldsz excludes decimal prec; realsz includes it.
    603 		 */
    604 		fieldsz = size;
    605 		if (sign)
    606 			fieldsz++;
    607 		else if (flags & HEXPREFIX)
    608 			fieldsz += 2;
    609 		realsz = dprec > fieldsz ? dprec : fieldsz;
    610 
    611 		/* right-adjusting blank padding */
    612 		if ((flags & (LADJUST|ZEROPAD)) == 0)
    613 			PAD(width - realsz, blanks);
    614 
    615 		/* prefix */
    616 		if (sign) {
    617 			PRINT(&sign, 1);
    618 		} else if (flags & HEXPREFIX) {
    619 			ox[0] = '0';
    620 			ox[1] = ch;
    621 			PRINT(ox, 2);
    622 		}
    623 
    624 		/* right-adjusting zero padding */
    625 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
    626 			PAD(width - realsz, zeroes);
    627 
    628 		/* leading zeroes from decimal precision */
    629 		PAD(dprec - size, zeroes);
    630 
    631 		/* the string or number proper */
    632 #ifdef FLOATING_POINT
    633 		if ((flags & FPT) == 0) {
    634 			PRINT(cp, size);
    635 		} else {	/* glue together f_p fragments */
    636 			if (ch >= 'f') {	/* 'f' or 'g' */
    637 				if (_double == 0) {
    638 					/* kludge for __dtoa irregularity */
    639 					PRINT("0", 1);
    640 					if (expt < ndig || (flags & ALT) != 0) {
    641 						PRINT(decimal_point, 1);
    642 						PAD(ndig - 1, zeroes);
    643 					}
    644 				} else if (expt <= 0) {
    645 					PRINT("0", 1);
    646 					PRINT(decimal_point, 1);
    647 					PAD(-expt, zeroes);
    648 					PRINT(cp, ndig);
    649 				} else if (expt >= ndig) {
    650 					PRINT(cp, ndig);
    651 					PAD(expt - ndig, zeroes);
    652 					if (flags & ALT)
    653 						PRINT(".", 1);
    654 				} else {
    655 					PRINT(cp, expt);
    656 					cp += expt;
    657 					PRINT(".", 1);
    658 					PRINT(cp, ndig-expt);
    659 				}
    660 			} else {	/* 'e' or 'E' */
    661 				if (ndig > 1 || flags & ALT) {
    662 					ox[0] = *cp++;
    663 					ox[1] = '.';
    664 					PRINT(ox, 2);
    665 					if (_double || flags & ALT == 0) {
    666 						PRINT(cp, ndig-1);
    667 					} else	/* 0.[0..] */
    668 						/* __dtoa irregularity */
    669 						PAD(ndig - 1, zeroes);
    670 				} else	/* XeYYY */
    671 					PRINT(cp, 1);
    672 				PRINT(expstr, expsize);
    673 			}
    674 		}
    675 #else
    676 		PRINT(cp, size);
    677 #endif
    678 		/* left-adjusting padding (always blank) */
    679 		if (flags & LADJUST)
    680 			PAD(width - realsz, blanks);
    681 
    682 		/* finally, adjust ret */
    683 		ret += width > realsz ? width : realsz;
    684 
    685 		FLUSH();	/* copy out the I/O vectors */
    686 	}
    687 done:
    688 	FLUSH();
    689 error:
    690 	return (__sferror(fp) ? EOF : ret);
    691 	/* NOTREACHED */
    692 }
    693 
    694 #ifdef FLOATING_POINT
    695 
    696 extern char *__dtoa __P((double, int, int, int *, int *, char **));
    697 
    698 static char *
    699 cvt(value, ndigits, flags, sign, decpt, ch, length)
    700 	double value;
    701 	int ndigits, flags, *decpt, ch, *length;
    702 	char *sign;
    703 {
    704 	int mode, dsgn;
    705 	char *digits, *bp, *rve;
    706 
    707 	if (ch == 'f') {
    708 		mode = 3;		/* ndigits after the decimal point */
    709 	} else {
    710 		/* To obtain ndigits after the decimal point for the 'e'
    711 		 * and 'E' formats, round to ndigits + 1 significant
    712 		 * figures.
    713 		 */
    714 		if (ch == 'e' || ch == 'E') {
    715 			ndigits++;
    716 		}
    717 		mode = 2;		/* ndigits significant digits */
    718 	}
    719 
    720 	if (value < 0) {
    721 		value = -value;
    722 		*sign = '-';
    723 	} else
    724 		*sign = '\000';
    725 	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
    726 	if ((ch != 'g' && ch != 'G') || flags & ALT) {	/* Print trailing zeros */
    727 		bp = digits + ndigits;
    728 		if (ch == 'f') {
    729 			if (*digits == '0' && value)
    730 				*decpt = -ndigits + 1;
    731 			bp += *decpt;
    732 		}
    733 		if (value == 0)	/* kludge for __dtoa irregularity */
    734 			rve = bp;
    735 		while (rve < bp)
    736 			*rve++ = '0';
    737 	}
    738 	*length = rve - digits;
    739 	return (digits);
    740 }
    741 
    742 static int
    743 exponent(p0, exp, fmtch)
    744 	char *p0;
    745 	int exp, fmtch;
    746 {
    747 	register char *p, *t;
    748 	char expbuf[MAXEXP];
    749 
    750 	p = p0;
    751 	*p++ = fmtch;
    752 	if (exp < 0) {
    753 		exp = -exp;
    754 		*p++ = '-';
    755 	}
    756 	else
    757 		*p++ = '+';
    758 	t = expbuf + MAXEXP;
    759 	if (exp > 9) {
    760 		do {
    761 			*--t = to_char(exp % 10);
    762 		} while ((exp /= 10) > 9);
    763 		*--t = to_char(exp);
    764 		for (; t < expbuf + MAXEXP; *p++ = *t++);
    765 	}
    766 	else {
    767 		*p++ = '0';
    768 		*p++ = to_char(exp);
    769 	}
    770 	return (p - p0);
    771 }
    772 #endif /* FLOATING_POINT */
    773