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