Home | History | Annotate | Line # | Download | only in stdio
vfprintf.c revision 1.7
      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.7 1993/08/26 00:47:33 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 /* Define FLOATING_POINT to get floating point. */
     64 #define	FLOATING_POINT
     65 
     66 /*
     67  * Flush out all the vectors defined by the given uio,
     68  * then reset it so that it can be reused.
     69  */
     70 static int
     71 __sprint(fp, uio)
     72 	FILE *fp;
     73 	register struct __suio *uio;
     74 {
     75 	register int err;
     76 
     77 	if (uio->uio_resid == 0) {
     78 		uio->uio_iovcnt = 0;
     79 		return (0);
     80 	}
     81 	err = __sfvwrite(fp, uio);
     82 	uio->uio_resid = 0;
     83 	uio->uio_iovcnt = 0;
     84 	return (err);
     85 }
     86 
     87 /*
     88  * Helper function for `fprintf to unbuffered unix file': creates a
     89  * temporary buffer.  We only work on write-only files; this avoids
     90  * worries about ungetc buffers and so forth.
     91  */
     92 static int
     93 __sbprintf(fp, fmt, ap)
     94 	register FILE *fp;
     95 	const char *fmt;
     96 	va_list ap;
     97 {
     98 	int ret;
     99 	FILE fake;
    100 	unsigned char buf[BUFSIZ];
    101 
    102 	/* copy the important variables */
    103 	fake._flags = fp->_flags & ~__SNBF;
    104 	fake._file = fp->_file;
    105 	fake._cookie = fp->_cookie;
    106 	fake._write = fp->_write;
    107 
    108 	/* set up the buffer */
    109 	fake._bf._base = fake._p = buf;
    110 	fake._bf._size = fake._w = sizeof(buf);
    111 	fake._lbfsize = 0;	/* not actually used, but Just In Case */
    112 
    113 	/* do the work, then copy any error status */
    114 	ret = vfprintf(&fake, fmt, ap);
    115 	if (ret >= 0 && fflush(&fake))
    116 		ret = EOF;
    117 	if (fake._flags & __SERR)
    118 		fp->_flags |= __SERR;
    119 	return (ret);
    120 }
    121 
    122 
    123 #ifdef FLOATING_POINT
    124 #include <math.h>
    125 #include "floatio.h"
    126 
    127 #define	BUF		(MAXEXP+MAXFRACT+1)	/* + decimal point */
    128 #define	DEFPREC		6
    129 
    130 static char *cvt __P((double, int, int, char *, int *, int, int *));
    131 static int exponent __P((char *, int, int));
    132 
    133 #else /* no FLOATING_POINT */
    134 
    135 #define	BUF		40
    136 
    137 #endif /* FLOATING_POINT */
    138 
    139 
    140 /*
    141  * Macros for converting digits to letters and vice versa
    142  */
    143 #define	to_digit(c)	((c) - '0')
    144 #define is_digit(c)	((unsigned)to_digit(c) <= 9)
    145 #define	to_char(n)	((n) + '0')
    146 
    147 /*
    148  * Flags used during conversion.
    149  */
    150 #define	ALT		0x001		/* alternate form */
    151 #define	HEXPREFIX	0x002		/* add 0x or 0X prefix */
    152 #define	LADJUST		0x004		/* left adjustment */
    153 #define	LONGDBL		0x008		/* long double; unimplemented */
    154 #define	LONGINT		0x010		/* long integer */
    155 #define	QUADINT		0x020		/* quad integer */
    156 #define	SHORTINT	0x040		/* short integer */
    157 #define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
    158 #define FPT		0x100		/* Floating point number */
    159 int
    160 vfprintf(fp, fmt0, ap)
    161 	FILE *fp;
    162 	const char *fmt0;
    163 	_VA_LIST_ ap;
    164 {
    165 	register char *fmt;	/* format string */
    166 	register int ch;	/* character from fmt */
    167 	register int n;		/* handy integer (short term usage) */
    168 	register char *cp;	/* handy char pointer (short term usage) */
    169 	register struct __siov *iovp;/* for PRINT macro */
    170 	register int flags;	/* flags as above */
    171 	int ret;		/* return value accumulator */
    172 	int width;		/* width from format (%8d), or 0 */
    173 	int prec;		/* precision from format (%.3d), or -1 */
    174 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
    175 #ifdef FLOATING_POINT
    176 	char softsign;		/* temporary negative sign for floats */
    177 	double _double;		/* double precision arguments %[eEfgG] */
    178 	int expt;		/* integer value of exponent */
    179 	int expsize;		/* character count for expstr */
    180 	int ndig;		/* actual number of digits returned by cvt */
    181 	char expstr[7];		/* buffer for exponent string */
    182 #endif
    183 
    184 #ifdef __GNUC__			/* gcc has builtin quad type (long long) SOS */
    185 #define	quad_t	  long long
    186 #define	u_quad_t  unsigned long long
    187 #endif
    188 
    189 	u_quad_t _uquad;	/* integer arguments %[diouxX] */
    190 	enum { OCT, DEC, HEX } base;/* base for [diouxX] conversion */
    191 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
    192 	int fieldsz;		/* field size expanded by sign, etc */
    193 	int realsz;		/* field size expanded by dprec */
    194 	int size;		/* size of converted field or string */
    195 	char *xdigs;		/* digits for [xX] conversion */
    196 #define NIOV 8
    197 	struct __suio uio;	/* output information: summary */
    198 	struct __siov iov[NIOV];/* ... and individual io vectors */
    199 	char buf[BUF];		/* space for %c, %[diouxX], %[eEfgG] */
    200 	char ox[2];		/* space for 0x hex-prefix */
    201 
    202 	/*
    203 	 * Choose PADSIZE to trade efficiency vs. size.  If larger printf
    204 	 * fields occur frequently, increase PADSIZE and make the initialisers
    205 	 * below longer.
    206 	 */
    207 #define	PADSIZE	16		/* pad chunk size */
    208 	static char blanks[PADSIZE] =
    209 	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
    210 	static char zeroes[PADSIZE] =
    211 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
    212 
    213 	/*
    214 	 * BEWARE, these `goto error' on error, and PAD uses `n'.
    215 	 */
    216 #define	PRINT(ptr, len) { \
    217 	iovp->iov_base = (ptr); \
    218 	iovp->iov_len = (len); \
    219 	uio.uio_resid += (len); \
    220 	iovp++; \
    221 	if (++uio.uio_iovcnt >= NIOV) { \
    222 		if (__sprint(fp, &uio)) \
    223 			goto error; \
    224 		iovp = iov; \
    225 	} \
    226 }
    227 #define	PAD(howmany, with) { \
    228 	if ((n = (howmany)) > 0) { \
    229 		while (n > PADSIZE) { \
    230 			PRINT(with, PADSIZE); \
    231 			n -= PADSIZE; \
    232 		} \
    233 		PRINT(with, n); \
    234 	} \
    235 }
    236 #define	FLUSH() { \
    237 	if (uio.uio_resid && __sprint(fp, &uio)) \
    238 		goto error; \
    239 	uio.uio_iovcnt = 0; \
    240 	iovp = iov; \
    241 }
    242 
    243 	/*
    244 	 * To extend shorts properly, we need both signed and unsigned
    245 	 * argument extraction methods.
    246 	 */
    247 #define	SARG() \
    248 	(flags&QUADINT ? va_arg(ap, quad_t) : \
    249 	    flags&LONGINT ? va_arg(ap, long) : \
    250 	    flags&SHORTINT ? (long)(short)va_arg(ap, int) : \
    251 	    (long)va_arg(ap, int))
    252 #define	UARG() \
    253 	(flags&QUADINT ? va_arg(ap, u_quad_t) : \
    254 	    flags&LONGINT ? va_arg(ap, u_long) : \
    255 	    flags&SHORTINT ? (u_long)(u_short)va_arg(ap, int) : \
    256 	    (u_long)va_arg(ap, u_int))
    257 
    258 	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
    259 	if (cantwrite(fp))
    260 		return (EOF);
    261 
    262 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
    263 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
    264 	    fp->_file >= 0)
    265 		return (__sbprintf(fp, fmt0, ap));
    266 
    267 	fmt = (char *)fmt0;
    268 	uio.uio_iov = iovp = iov;
    269 	uio.uio_resid = 0;
    270 	uio.uio_iovcnt = 0;
    271 	ret = 0;
    272 
    273 	/*
    274 	 * Scan the format for conversions (`%' character).
    275 	 */
    276 	for (;;) {
    277 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
    278 			/* void */;
    279 		if ((n = fmt - cp) != 0) {
    280 			PRINT(cp, n);
    281 			ret += n;
    282 		}
    283 		if (ch == '\0')
    284 			goto done;
    285 		fmt++;		/* skip over '%' */
    286 
    287 		flags = 0;
    288 		dprec = 0;
    289 		width = 0;
    290 		prec = -1;
    291 		sign = '\0';
    292 
    293 rflag:		ch = *fmt++;
    294 reswitch:	switch (ch) {
    295 		case ' ':
    296 			/*
    297 			 * ``If the space and + flags both appear, the space
    298 			 * flag will be ignored.''
    299 			 *	-- ANSI X3J11
    300 			 */
    301 			if (!sign)
    302 				sign = ' ';
    303 			goto rflag;
    304 		case '#':
    305 			flags |= ALT;
    306 			goto rflag;
    307 		case '*':
    308 			/*
    309 			 * ``A negative field width argument is taken as a
    310 			 * - flag followed by a positive field width.''
    311 			 *	-- ANSI X3J11
    312 			 * They don't exclude field widths read from args.
    313 			 */
    314 			if ((width = va_arg(ap, int)) >= 0)
    315 				goto rflag;
    316 			width = -width;
    317 			/* FALLTHROUGH */
    318 		case '-':
    319 			flags |= LADJUST;
    320 			goto rflag;
    321 		case '+':
    322 			sign = '+';
    323 			goto rflag;
    324 		case '.':
    325 			if ((ch = *fmt++) == '*') {
    326 				n = va_arg(ap, int);
    327 				prec = n < 0 ? -1 : n;
    328 				goto rflag;
    329 			}
    330 			n = 0;
    331 			while (is_digit(ch)) {
    332 				n = 10 * n + to_digit(ch);
    333 				ch = *fmt++;
    334 			}
    335 			prec = n < 0 ? -1 : n;
    336 			goto reswitch;
    337 		case '0':
    338 			/*
    339 			 * ``Note that 0 is taken as a flag, not as the
    340 			 * beginning of a field width.''
    341 			 *	-- ANSI X3J11
    342 			 */
    343 			flags |= ZEROPAD;
    344 			goto rflag;
    345 		case '1': case '2': case '3': case '4':
    346 		case '5': case '6': case '7': case '8': case '9':
    347 			n = 0;
    348 			do {
    349 				n = 10 * n + to_digit(ch);
    350 				ch = *fmt++;
    351 			} while (is_digit(ch));
    352 			width = n;
    353 			goto reswitch;
    354 #ifdef FLOATING_POINT
    355 		case 'L':
    356 			flags |= LONGDBL;
    357 			goto rflag;
    358 #endif
    359 		case 'h':
    360 			flags |= SHORTINT;
    361 			goto rflag;
    362 		case 'l':
    363 			flags |= LONGINT;
    364 			goto rflag;
    365 		case 'q':
    366 			flags |= QUADINT;
    367 			goto rflag;
    368 		case 'c':
    369 			*(cp = buf) = va_arg(ap, int);
    370 			size = 1;
    371 			sign = '\0';
    372 			break;
    373 		case 'D':
    374 			flags |= LONGINT;
    375 			/*FALLTHROUGH*/
    376 		case 'd':
    377 		case 'i':
    378 			_uquad = SARG();
    379 			if ((quad_t)_uquad < 0) {
    380 				_uquad = -_uquad;
    381 				sign = '-';
    382 			}
    383 			base = DEC;
    384 			goto number;
    385 #ifdef FLOATING_POINT
    386 		case 'e':		/* anomalous precision */
    387 		case 'E':
    388 			prec = (prec == -1) ?
    389 				DEFPREC + 1 : prec + 1;
    390 			/* FALLTHROUGH */
    391 			goto fp_begin;
    392 		case 'f':		/* always print trailing zeroes */
    393 			if (prec != 0)
    394 				flags |= ALT;
    395 		case 'g':
    396 		case 'G':
    397 			if (prec == -1)
    398 				prec = DEFPREC;
    399 fp_begin:		_double = va_arg(ap, double);
    400 			/* do this before tricky precision changes */
    401 			if (isinf(_double)) {
    402 				if (_double < 0)
    403 					sign = '-';
    404 				cp = "Inf";
    405 				size = 3;
    406 				break;
    407 			}
    408 			if (isnan(_double)) {
    409 				cp = "NaN";
    410 				size = 3;
    411 				break;
    412 			}
    413 			flags |= FPT;
    414 			cp = cvt(_double, prec, flags, &softsign,
    415 				&expt, ch, &ndig);
    416 			if (ch == 'g' || ch == 'G') {
    417 				if (expt <= -4 || expt > prec)
    418 					ch = (ch == 'g') ? 'e' : 'E';
    419 				else
    420 					ch = 'g';
    421 			}
    422 			if (ch <= 'e') {	/* 'e' or 'E' fmt */
    423 				--expt;
    424 				expsize = exponent(expstr, expt, ch);
    425 				size = expsize + ndig;
    426 				if (ndig > 1 || flags & ALT)
    427 					++size;
    428 			} else if (ch == 'f') {		/* f fmt */
    429 				if (expt > 0) {
    430 					size = expt;
    431 					if (prec || flags & ALT)
    432 						size += prec + 1;
    433 				} else	/* "0.X" */
    434 					size = prec + 2;
    435 			} else if (expt >= ndig) {	/* fixed g fmt */
    436 				size = expt;
    437 				if (flags & ALT)
    438 					++size;
    439 			} else
    440 				size = ndig + (expt > 0 ?
    441 					1 : 2 - expt);
    442 
    443 			if (softsign)
    444 				sign = '-';
    445 			break;
    446 #endif /* FLOATING_POINT */
    447 		case 'n':
    448 			if (flags & QUADINT)
    449 				*va_arg(ap, quad_t *) = ret;
    450 			else if (flags & LONGINT)
    451 				*va_arg(ap, long *) = ret;
    452 			else if (flags & SHORTINT)
    453 				*va_arg(ap, short *) = ret;
    454 			else
    455 				*va_arg(ap, int *) = ret;
    456 			continue;	/* no output */
    457 		case 'O':
    458 			flags |= LONGINT;
    459 			/*FALLTHROUGH*/
    460 		case 'o':
    461 			_uquad = UARG();
    462 			base = OCT;
    463 			goto nosign;
    464 		case 'p':
    465 			/*
    466 			 * ``The argument shall be a pointer to void.  The
    467 			 * value of the pointer is converted to a sequence
    468 			 * of printable characters, in an implementation-
    469 			 * defined manner.''
    470 			 *	-- ANSI X3J11
    471 			 */
    472 			/* NOSTRICT */
    473 			_uquad = (u_quad_t)va_arg(ap, void *);
    474 			base = HEX;
    475 			xdigs = "0123456789abcdef";
    476 			flags |= HEXPREFIX;
    477 			ch = 'x';
    478 			goto nosign;
    479 		case 's':
    480 			if ((cp = va_arg(ap, char *)) == NULL)
    481 				cp = "(null)";
    482 			if (prec >= 0) {
    483 				/*
    484 				 * can't use strlen; can only look for the
    485 				 * NUL in the first `prec' characters, and
    486 				 * strlen() will go further.
    487 				 */
    488 				char *p = memchr(cp, 0, prec);
    489 
    490 				if (p != NULL) {
    491 					size = p - cp;
    492 					if (size > prec)
    493 						size = prec;
    494 				} else
    495 					size = prec;
    496 			} else
    497 				size = strlen(cp);
    498 			sign = '\0';
    499 			break;
    500 		case 'U':
    501 			flags |= LONGINT;
    502 			/*FALLTHROUGH*/
    503 		case 'u':
    504 			_uquad = UARG();
    505 			base = DEC;
    506 			goto nosign;
    507 		case 'X':
    508 			xdigs = "0123456789ABCDEF";
    509 			goto hex;
    510 		case 'x':
    511 			xdigs = "0123456789abcdef";
    512 hex:			_uquad = UARG();
    513 			base = HEX;
    514 			/* leading 0x/X only if non-zero */
    515 			if (flags & ALT && _uquad != 0)
    516 				flags |= HEXPREFIX;
    517 
    518 			/* unsigned conversions */
    519 nosign:			sign = '\0';
    520 			/*
    521 			 * ``... diouXx conversions ... if a precision is
    522 			 * specified, the 0 flag will be ignored.''
    523 			 *	-- ANSI X3J11
    524 			 */
    525 number:			if ((dprec = prec) >= 0)
    526 				flags &= ~ZEROPAD;
    527 
    528 			/*
    529 			 * ``The result of converting a zero value with an
    530 			 * explicit precision of zero is no characters.''
    531 			 *	-- ANSI X3J11
    532 			 */
    533 			cp = buf + BUF;
    534 			if (_uquad != 0 || prec != 0) {
    535 				/*
    536 				 * Unsigned mod is hard, and unsigned mod
    537 				 * by a constant is easier than that by
    538 				 * a variable; hence this switch.
    539 				 */
    540 				switch (base) {
    541 				case OCT:
    542 					do {
    543 						*--cp = to_char(_uquad & 7);
    544 						_uquad >>= 3;
    545 					} while (_uquad);
    546 					/* handle octal leading 0 */
    547 					if (flags & ALT && *cp != '0')
    548 						*--cp = '0';
    549 					break;
    550 
    551 				case DEC:
    552 					/* many numbers are 1 digit */
    553 					while (_uquad >= 10) {
    554 						*--cp = to_char(_uquad % 10);
    555 						_uquad /= 10;
    556 					}
    557 					*--cp = to_char(_uquad);
    558 					break;
    559 
    560 				case HEX:
    561 					do {
    562 						*--cp = xdigs[_uquad & 15];
    563 						_uquad >>= 4;
    564 					} while (_uquad);
    565 					break;
    566 
    567 				default:
    568 					cp = "bug in vfprintf: bad base";
    569 					size = strlen(cp);
    570 					goto skipsize;
    571 				}
    572 			}
    573 			size = buf + BUF - cp;
    574 		skipsize:
    575 			break;
    576 		default:	/* "%?" prints ?, unless ? is NUL */
    577 			if (ch == '\0')
    578 				goto done;
    579 			/* pretend it was %c with argument ch */
    580 			cp = buf;
    581 			*cp = ch;
    582 			size = 1;
    583 			sign = '\0';
    584 			break;
    585 		}
    586 
    587 		/*
    588 		 * All reasonable formats wind up here.  At this point, `cp'
    589 		 * points to a string which (if not flags&LADJUST) should be
    590 		 * padded out to `width' places.  If flags&ZEROPAD, it should
    591 		 * first be prefixed by any sign or other prefix; otherwise,
    592 		 * it should be blank padded before the prefix is emitted.
    593 		 * After any left-hand padding and prefixing, emit zeroes
    594 		 * required by a decimal [diouxX] precision, then print the
    595 		 * string proper, then emit zeroes required by any leftover
    596 		 * floating precision; finally, if LADJUST, pad with blanks.
    597 		 *
    598 		 * Compute actual size, so we know how much to pad.
    599 		 * fieldsz excludes decimal prec; realsz includes it.
    600 		 */
    601 		fieldsz = size;
    602 		if (sign)
    603 			fieldsz++;
    604 		else if (flags & HEXPREFIX)
    605 			fieldsz += 2;
    606 		realsz = dprec > fieldsz ? dprec : fieldsz;
    607 
    608 		/* right-adjusting blank padding */
    609 		if ((flags & (LADJUST|ZEROPAD)) == 0)
    610 			PAD(width - realsz, blanks);
    611 
    612 		/* prefix */
    613 		if (sign) {
    614 			PRINT(&sign, 1);
    615 		} else if (flags & HEXPREFIX) {
    616 			ox[0] = '0';
    617 			ox[1] = ch;
    618 			PRINT(ox, 2);
    619 		}
    620 
    621 		/* right-adjusting zero padding */
    622 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
    623 			PAD(width - realsz, zeroes);
    624 
    625 		/* leading zeroes from decimal precision */
    626 		PAD(dprec - fieldsz, zeroes);
    627 
    628 		/* the string or number proper */
    629 #ifdef FLOATING_POINT
    630 		if ((flags & FPT) == 0) {
    631 			PRINT(cp, size);
    632 		} else {	/* glue together f_p fragments */
    633 			if (ch >= 'f') {	/* 'f' or 'g' */
    634 				if (_double == 0) {
    635 				/* kludge for __dtoa irregularity */
    636 					if (prec == 0 ||
    637 					    (flags & ALT) == 0) {
    638 						PRINT("0", 1);
    639 					} else {
    640 						PRINT("0.", 2);
    641 						PAD(ndig - 1, zeroes);
    642 					}
    643 				} else if (expt <= 0) {
    644 					PRINT("0.", 2);
    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;
    707 	else {
    708 		mode = 2;
    709 	}
    710 	if (value < 0) {
    711 		value = -value;
    712 		*sign = '-';
    713 	} else
    714 		*sign = '\000';
    715 	digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve);
    716 	if (flags & ALT) {	/* Print trailing zeros */
    717 		bp = digits + ndigits;
    718 		if (ch == 'f') {
    719 			if (*digits == '0' && value)
    720 				*decpt = -ndigits + 1;
    721 			bp += *decpt;
    722 		}
    723 		if (value == 0)	/* kludge for __dtoa irregularity */
    724 			rve = bp;
    725 		while (rve < bp)
    726 			*rve++ = '0';
    727 	}
    728 	*length = rve - digits;
    729 	return (digits);
    730 }
    731 
    732 static int
    733 exponent(p0, exp, fmtch)
    734 	char *p0;
    735 	int exp, fmtch;
    736 {
    737 	register char *p, *t;
    738 	char expbuf[MAXEXP];
    739 
    740 	p = p0;
    741 	*p++ = fmtch;
    742 	if (exp < 0) {
    743 		exp = -exp;
    744 		*p++ = '-';
    745 	}
    746 	else
    747 		*p++ = '+';
    748 	t = expbuf + MAXEXP;
    749 	if (exp > 9) {
    750 		do {
    751 			*--t = to_char(exp % 10);
    752 		} while ((exp /= 10) > 9);
    753 		*--t = to_char(exp);
    754 		for (; t < expbuf + MAXEXP; *p++ = *t++);
    755 	}
    756 	else {
    757 		*p++ = '0';
    758 		*p++ = to_char(exp);
    759 	}
    760 	return (p - p0);
    761 }
    762 #endif /* FLOATING_POINT */
    763