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