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