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