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