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