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