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