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