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