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