Home | History | Annotate | Line # | Download | only in stdio
vfprintf.c revision 1.15
      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.15 1995/02/25 07:06:38 cgd 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 realsz;		/* field size expanded by dprec */
    192 	int size;		/* size of converted field or string */
    193 	char *xdigs;		/* digits for [xX] conversion */
    194 #define NIOV 8
    195 	struct __suio uio;	/* output information: summary */
    196 	struct __siov iov[NIOV];/* ... and individual io vectors */
    197 	char buf[BUF];		/* space for %c, %[diouxX], %[eEfgG] */
    198 	char ox[2];		/* space for 0x hex-prefix */
    199 
    200 	/*
    201 	 * Choose PADSIZE to trade efficiency vs. size.  If larger printf
    202 	 * fields occur frequently, increase PADSIZE and make the initialisers
    203 	 * below longer.
    204 	 */
    205 #define	PADSIZE	16		/* pad chunk size */
    206 	static char blanks[PADSIZE] =
    207 	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
    208 	static char zeroes[PADSIZE] =
    209 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
    210 
    211 	/*
    212 	 * BEWARE, these `goto error' on error, and PAD uses `n'.
    213 	 */
    214 #define	PRINT(ptr, len) { \
    215 	iovp->iov_base = (ptr); \
    216 	iovp->iov_len = (len); \
    217 	uio.uio_resid += (len); \
    218 	iovp++; \
    219 	if (++uio.uio_iovcnt >= NIOV) { \
    220 		if (__sprint(fp, &uio)) \
    221 			goto error; \
    222 		iovp = iov; \
    223 	} \
    224 }
    225 #define	PAD(howmany, with) { \
    226 	if ((n = (howmany)) > 0) { \
    227 		while (n > PADSIZE) { \
    228 			PRINT(with, PADSIZE); \
    229 			n -= PADSIZE; \
    230 		} \
    231 		PRINT(with, n); \
    232 	} \
    233 }
    234 #define	FLUSH() { \
    235 	if (uio.uio_resid && __sprint(fp, &uio)) \
    236 		goto error; \
    237 	uio.uio_iovcnt = 0; \
    238 	iovp = iov; \
    239 }
    240 
    241 	/*
    242 	 * To extend shorts properly, we need both signed and unsigned
    243 	 * argument extraction methods.
    244 	 */
    245 #define	SARG() \
    246 	(flags&QUADINT ? va_arg(ap, quad_t) : \
    247 	    flags&LONGINT ? va_arg(ap, long) : \
    248 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
    249 	    (long)va_arg(ap, int))
    250 #define	UARG() \
    251 	(flags&QUADINT ? va_arg(ap, u_quad_t) : \
    252 	    flags&LONGINT ? va_arg(ap, u_long) : \
    253 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
    254 	    (u_long)va_arg(ap, u_int))
    255 
    256 	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
    257 	if (cantwrite(fp))
    258 		return (EOF);
    259 
    260 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
    261 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
    262 	    fp->_file >= 0)
    263 		return (__sbprintf(fp, fmt0, ap));
    264 
    265 	fmt = (char *)fmt0;
    266 	uio.uio_iov = iovp = iov;
    267 	uio.uio_resid = 0;
    268 	uio.uio_iovcnt = 0;
    269 	ret = 0;
    270 
    271 	/*
    272 	 * Scan the format for conversions (`%' character).
    273 	 */
    274 	for (;;) {
    275 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
    276 			/* void */;
    277 		if ((n = fmt - cp) != 0) {
    278 			PRINT(cp, n);
    279 			ret += n;
    280 		}
    281 		if (ch == '\0')
    282 			goto done;
    283 		fmt++;		/* skip over '%' */
    284 
    285 		flags = 0;
    286 		dprec = 0;
    287 		width = 0;
    288 		prec = -1;
    289 		sign = '\0';
    290 
    291 rflag:		ch = *fmt++;
    292 reswitch:	switch (ch) {
    293 		case ' ':
    294 			/*
    295 			 * ``If the space and + flags both appear, the space
    296 			 * flag will be ignored.''
    297 			 *	-- ANSI X3J11
    298 			 */
    299 			if (!sign)
    300 				sign = ' ';
    301 			goto rflag;
    302 		case '#':
    303 			flags |= ALT;
    304 			goto rflag;
    305 		case '*':
    306 			/*
    307 			 * ``A negative field width argument is taken as a
    308 			 * - flag followed by a positive field width.''
    309 			 *	-- ANSI X3J11
    310 			 * They don't exclude field widths read from args.
    311 			 */
    312 			if ((width = va_arg(ap, int)) >= 0)
    313 				goto rflag;
    314 			width = -width;
    315 			/* FALLTHROUGH */
    316 		case '-':
    317 			flags |= LADJUST;
    318 			goto rflag;
    319 		case '+':
    320 			sign = '+';
    321 			goto rflag;
    322 		case '.':
    323 			if ((ch = *fmt++) == '*') {
    324 				n = va_arg(ap, int);
    325 				prec = n < 0 ? -1 : n;
    326 				goto rflag;
    327 			}
    328 			n = 0;
    329 			while (is_digit(ch)) {
    330 				n = 10 * n + to_digit(ch);
    331 				ch = *fmt++;
    332 			}
    333 			prec = n < 0 ? -1 : n;
    334 			goto reswitch;
    335 		case '0':
    336 			/*
    337 			 * ``Note that 0 is taken as a flag, not as the
    338 			 * beginning of a field width.''
    339 			 *	-- ANSI X3J11
    340 			 */
    341 			flags |= ZEROPAD;
    342 			goto rflag;
    343 		case '1': case '2': case '3': case '4':
    344 		case '5': case '6': case '7': case '8': case '9':
    345 			n = 0;
    346 			do {
    347 				n = 10 * n + to_digit(ch);
    348 				ch = *fmt++;
    349 			} while (is_digit(ch));
    350 			width = n;
    351 			goto reswitch;
    352 #ifdef FLOATING_POINT
    353 		case 'L':
    354 			flags |= LONGDBL;
    355 			goto rflag;
    356 #endif
    357 		case 'h':
    358 			flags |= SHORTINT;
    359 			goto rflag;
    360 		case 'l':
    361 			flags |= LONGINT;
    362 			goto rflag;
    363 		case 'q':
    364 			flags |= QUADINT;
    365 			goto rflag;
    366 		case 'c':
    367 			*(cp = buf) = va_arg(ap, int);
    368 			size = 1;
    369 			sign = '\0';
    370 			break;
    371 		case 'D':
    372 			flags |= LONGINT;
    373 			/*FALLTHROUGH*/
    374 		case 'd':
    375 		case 'i':
    376 			_uquad = SARG();
    377 			if ((quad_t)_uquad < 0) {
    378 				_uquad = -_uquad;
    379 				sign = '-';
    380 			}
    381 			base = DEC;
    382 			goto number;
    383 #ifdef FLOATING_POINT
    384 		case 'e':
    385 		case 'E':
    386 		case 'f':
    387 		case 'g':
    388 		case 'G':
    389 			if (prec == -1) {
    390 				prec = DEFPREC;
    391 			} else if ((ch == 'g' || ch == 'G') && prec == 0) {
    392 				prec = 1;
    393 			}
    394 
    395 			if (flags & LONGDBL) {
    396 				_double = (double) va_arg(ap, long double);
    397 			} else {
    398 				_double = va_arg(ap, double);
    399 			}
    400 
    401 			/* do this before tricky precision changes */
    402 			if (isinf(_double)) {
    403 				if (_double < 0)
    404 					sign = '-';
    405 				cp = "Inf";
    406 				size = 3;
    407 				break;
    408 			}
    409 			if (isnan(_double)) {
    410 				cp = "NaN";
    411 				size = 3;
    412 				break;
    413 			}
    414 
    415 			flags |= FPT;
    416 			cp = cvt(_double, prec, flags, &softsign,
    417 				&expt, ch, &ndig);
    418 			if (ch == 'g' || ch == 'G') {
    419 				if (expt <= -4 || expt > prec)
    420 					ch = (ch == 'g') ? 'e' : 'E';
    421 				else
    422 					ch = 'g';
    423 			}
    424 			if (ch <= 'e') {	/* 'e' or 'E' fmt */
    425 				--expt;
    426 				expsize = exponent(expstr, expt, ch);
    427 				size = expsize + ndig;
    428 				if (ndig > 1 || flags & ALT)
    429 					++size;
    430 			} else if (ch == 'f') {		/* f fmt */
    431 				if (expt > 0) {
    432 					size = expt;
    433 					if (prec || flags & ALT)
    434 						size += prec + 1;
    435 				} else	/* "0.X" */
    436 					size = prec + 2;
    437 			} else if (expt >= ndig) {	/* fixed g fmt */
    438 				size = expt;
    439 				if (flags & ALT)
    440 					++size;
    441 			} else
    442 				size = ndig + (expt > 0 ?
    443 					1 : 2 - expt);
    444 
    445 			if (softsign)
    446 				sign = '-';
    447 			break;
    448 #endif /* FLOATING_POINT */
    449 		case 'n':
    450 			if (flags & QUADINT)
    451 				*va_arg(ap, quad_t *) = ret;
    452 			else if (flags & LONGINT)
    453 				*va_arg(ap, long *) = ret;
    454 			else if (flags & SHORTINT)
    455 				*va_arg(ap, short *) = ret;
    456 			else
    457 				*va_arg(ap, int *) = ret;
    458 			continue;	/* no output */
    459 		case 'O':
    460 			flags |= LONGINT;
    461 			/*FALLTHROUGH*/
    462 		case 'o':
    463 			_uquad = UARG();
    464 			base = OCT;
    465 			goto nosign;
    466 		case 'p':
    467 			/*
    468 			 * ``The argument shall be a pointer to void.  The
    469 			 * value of the pointer is converted to a sequence
    470 			 * of printable characters, in an implementation-
    471 			 * defined manner.''
    472 			 *	-- ANSI X3J11
    473 			 */
    474 			/* NOSTRICT */
    475 			_uquad = (u_long)va_arg(ap, void *);
    476 			base = HEX;
    477 			xdigs = "0123456789abcdef";
    478 			flags |= HEXPREFIX;
    479 			ch = 'x';
    480 			goto nosign;
    481 		case 's':
    482 			if ((cp = va_arg(ap, char *)) == NULL)
    483 				cp = "(null)";
    484 			if (prec >= 0) {
    485 				/*
    486 				 * can't use strlen; can only look for the
    487 				 * NUL in the first `prec' characters, and
    488 				 * strlen() will go further.
    489 				 */
    490 				char *p = memchr(cp, 0, prec);
    491 
    492 				if (p != NULL) {
    493 					size = p - cp;
    494 					if (size > prec)
    495 						size = prec;
    496 				} else
    497 					size = prec;
    498 			} else
    499 				size = strlen(cp);
    500 			sign = '\0';
    501 			break;
    502 		case 'U':
    503 			flags |= LONGINT;
    504 			/*FALLTHROUGH*/
    505 		case 'u':
    506 			_uquad = UARG();
    507 			base = DEC;
    508 			goto nosign;
    509 		case 'X':
    510 			xdigs = "0123456789ABCDEF";
    511 			goto hex;
    512 		case 'x':
    513 			xdigs = "0123456789abcdef";
    514 hex:			_uquad = UARG();
    515 			base = HEX;
    516 			/* leading 0x/X only if non-zero */
    517 			if (flags & ALT && _uquad != 0)
    518 				flags |= HEXPREFIX;
    519 
    520 			/* unsigned conversions */
    521 nosign:			sign = '\0';
    522 			/*
    523 			 * ``... diouXx conversions ... if a precision is
    524 			 * specified, the 0 flag will be ignored.''
    525 			 *	-- ANSI X3J11
    526 			 */
    527 number:			if ((dprec = prec) >= 0)
    528 				flags &= ~ZEROPAD;
    529 
    530 			/*
    531 			 * ``The result of converting a zero value with an
    532 			 * explicit precision of zero is no characters.''
    533 			 *	-- ANSI X3J11
    534 			 */
    535 			cp = buf + BUF;
    536 			if (_uquad != 0 || prec != 0) {
    537 				/*
    538 				 * Unsigned mod is hard, and unsigned mod
    539 				 * by a constant is easier than that by
    540 				 * a variable; hence this switch.
    541 				 */
    542 				switch (base) {
    543 				case OCT:
    544 					do {
    545 						*--cp = to_char(_uquad & 7);
    546 						_uquad >>= 3;
    547 					} while (_uquad);
    548 					/* handle octal leading 0 */
    549 					if (flags & ALT && *cp != '0')
    550 						*--cp = '0';
    551 					break;
    552 
    553 				case DEC:
    554 					/* many numbers are 1 digit */
    555 					while (_uquad >= 10) {
    556 						*--cp = to_char(_uquad % 10);
    557 						_uquad /= 10;
    558 					}
    559 					*--cp = to_char(_uquad);
    560 					break;
    561 
    562 				case HEX:
    563 					do {
    564 						*--cp = xdigs[_uquad & 15];
    565 						_uquad >>= 4;
    566 					} while (_uquad);
    567 					break;
    568 
    569 				default:
    570 					cp = "bug in vfprintf: bad base";
    571 					size = strlen(cp);
    572 					goto skipsize;
    573 				}
    574 			}
    575 			size = buf + BUF - cp;
    576 		skipsize:
    577 			break;
    578 		default:	/* "%?" prints ?, unless ? is NUL */
    579 			if (ch == '\0')
    580 				goto done;
    581 			/* pretend it was %c with argument ch */
    582 			cp = buf;
    583 			*cp = ch;
    584 			size = 1;
    585 			sign = '\0';
    586 			break;
    587 		}
    588 
    589 		/*
    590 		 * All reasonable formats wind up here.  At this point, `cp'
    591 		 * points to a string which (if not flags&LADJUST) should be
    592 		 * padded out to `width' places.  If flags&ZEROPAD, it should
    593 		 * first be prefixed by any sign or other prefix; otherwise,
    594 		 * it should be blank padded before the prefix is emitted.
    595 		 * After any left-hand padding and prefixing, emit zeroes
    596 		 * required by a decimal [diouxX] precision, then print the
    597 		 * string proper, then emit zeroes required by any leftover
    598 		 * floating precision; finally, if LADJUST, pad with blanks.
    599 		 *
    600 		 * Compute actual size, so we know how much to pad.
    601 		 * size excludes decimal prec; realsz includes it.
    602 		 */
    603 		realsz = dprec > size ? dprec : size;
    604 		if (sign)
    605 			realsz++;
    606 		else if (flags & HEXPREFIX)
    607 			realsz+= 2;
    608 
    609 		/* right-adjusting blank padding */
    610 		if ((flags & (LADJUST|ZEROPAD)) == 0)
    611 			PAD(width - realsz, blanks);
    612 
    613 		/* prefix */
    614 		if (sign) {
    615 			PRINT(&sign, 1);
    616 		} else if (flags & HEXPREFIX) {
    617 			ox[0] = '0';
    618 			ox[1] = ch;
    619 			PRINT(ox, 2);
    620 		}
    621 
    622 		/* right-adjusting zero padding */
    623 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
    624 			PAD(width - realsz, zeroes);
    625 
    626 		/* leading zeroes from decimal precision */
    627 		PAD(dprec - size, zeroes);
    628 
    629 		/* the string or number proper */
    630 #ifdef FLOATING_POINT
    631 		if ((flags & FPT) == 0) {
    632 			PRINT(cp, size);
    633 		} else {	/* glue together f_p fragments */
    634 			if (ch >= 'f') {	/* 'f' or 'g' */
    635 				if (_double == 0) {
    636 					/* kludge for __dtoa irregularity */
    637 					PRINT("0", 1);
    638 					if (expt < ndig || (flags & ALT) != 0) {
    639 						PRINT(decimal_point, 1);
    640 						PAD(ndig - 1, zeroes);
    641 					}
    642 				} else if (expt <= 0) {
    643 					PRINT("0", 1);
    644 					PRINT(decimal_point, 1);
    645 					PAD(-expt, zeroes);
    646 					PRINT(cp, ndig);
    647 				} else if (expt >= ndig) {
    648 					PRINT(cp, ndig);
    649 					PAD(expt - ndig, zeroes);
    650 					if (flags & ALT)
    651 						PRINT(".", 1);
    652 				} else {
    653 					PRINT(cp, expt);
    654 					cp += expt;
    655 					PRINT(".", 1);
    656 					PRINT(cp, ndig-expt);
    657 				}
    658 			} else {	/* 'e' or 'E' */
    659 				if (ndig > 1 || flags & ALT) {
    660 					ox[0] = *cp++;
    661 					ox[1] = '.';
    662 					PRINT(ox, 2);
    663 					if (_double || flags & ALT == 0) {
    664 						PRINT(cp, ndig-1);
    665 					} else	/* 0.[0..] */
    666 						/* __dtoa irregularity */
    667 						PAD(ndig - 1, zeroes);
    668 				} else	/* XeYYY */
    669 					PRINT(cp, 1);
    670 				PRINT(expstr, expsize);
    671 			}
    672 		}
    673 #else
    674 		PRINT(cp, size);
    675 #endif
    676 		/* left-adjusting padding (always blank) */
    677 		if (flags & LADJUST)
    678 			PAD(width - realsz, blanks);
    679 
    680 		/* finally, adjust ret */
    681 		ret += width > realsz ? width : realsz;
    682 
    683 		FLUSH();	/* copy out the I/O vectors */
    684 	}
    685 done:
    686 	FLUSH();
    687 error:
    688 	return (__sferror(fp) ? EOF : ret);
    689 	/* NOTREACHED */
    690 }
    691 
    692 #ifdef FLOATING_POINT
    693 
    694 extern char *__dtoa __P((double, int, int, int *, int *, char **));
    695 
    696 static char *
    697 cvt(value, ndigits, flags, sign, decpt, ch, length)
    698 	double value;
    699 	int ndigits, flags, *decpt, ch, *length;
    700 	char *sign;
    701 {
    702 	int mode, dsgn;
    703 	char *digits, *bp, *rve;
    704 
    705 	if (ch == 'f') {
    706 		mode = 3;		/* ndigits after the decimal point */
    707 	} else {
    708 		/* To obtain ndigits after the decimal point for the 'e'
    709 		 * and 'E' formats, round to ndigits + 1 significant
    710 		 * figures.
    711 		 */
    712 		if (ch == 'e' || ch == 'E') {
    713 			ndigits++;
    714 		}
    715 		mode = 2;		/* ndigits significant digits */
    716 	}
    717 
    718 	if (value < 0) {
    719 		value = -value;
    720 		*sign = '-';
    721 	} else
    722 		*sign = '\000';
    723 	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
    724 	if ((ch != 'g' && ch != 'G') || flags & ALT) {	/* Print trailing zeros */
    725 		bp = digits + ndigits;
    726 		if (ch == 'f') {
    727 			if (*digits == '0' && value)
    728 				*decpt = -ndigits + 1;
    729 			bp += *decpt;
    730 		}
    731 		if (value == 0)	/* kludge for __dtoa irregularity */
    732 			rve = bp;
    733 		while (rve < bp)
    734 			*rve++ = '0';
    735 	}
    736 	*length = rve - digits;
    737 	return (digits);
    738 }
    739 
    740 static int
    741 exponent(p0, exp, fmtch)
    742 	char *p0;
    743 	int exp, fmtch;
    744 {
    745 	register char *p, *t;
    746 	char expbuf[MAXEXP];
    747 
    748 	p = p0;
    749 	*p++ = fmtch;
    750 	if (exp < 0) {
    751 		exp = -exp;
    752 		*p++ = '-';
    753 	}
    754 	else
    755 		*p++ = '+';
    756 	t = expbuf + MAXEXP;
    757 	if (exp > 9) {
    758 		do {
    759 			*--t = to_char(exp % 10);
    760 		} while ((exp /= 10) > 9);
    761 		*--t = to_char(exp);
    762 		for (; t < expbuf + MAXEXP; *p++ = *t++);
    763 	}
    764 	else {
    765 		*p++ = '0';
    766 		*p++ = to_char(exp);
    767 	}
    768 	return (p - p0);
    769 }
    770 #endif /* FLOATING_POINT */
    771