vfprintf.c revision 31871
1/*- 2 * Copyright (c) 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley by 6 * Chris Torek. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37#if defined(LIBC_SCCS) && !defined(lint) 38#if 0 39static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 40#endif 41static const char rcsid[] = 42 "$Id: vfprintf.c,v 1.12 1997/02/22 15:02:40 peter Exp $"; 43#endif /* LIBC_SCCS and not lint */ 44 45/* 46 * Actual printf innards. 47 * 48 * This code is large and complicated... 49 */ 50 51#include <sys/types.h> 52 53#include <limits.h> 54#include <stdio.h> 55#include <stdlib.h> 56#include <string.h> 57 58#if __STDC__ 59#include <stdarg.h> 60#else 61#include <varargs.h> 62#endif 63 64#include "local.h" 65#include "fvwrite.h" 66#ifdef _THREAD_SAFE 67#include <pthread.h> 68#include "pthread_private.h" 69#endif 70 71/* Define FLOATING_POINT to get floating point. */ 72#define FLOATING_POINT 73 74static int __sprint __P((FILE *, struct __suio *)); 75static int __sbprintf __P((FILE *, const char *, va_list)); 76static char * __ultoa __P((u_long, char *, int, int, char *)); 77static char * __uqtoa __P((u_quad_t, char *, int, int, char *)); 78static void __find_arguments __P((const char *, va_list, void ***)); 79static void __grow_type_table __P((int, unsigned char **, int *)); 80 81/* 82 * Flush out all the vectors defined by the given uio, 83 * then reset it so that it can be reused. 84 */ 85static int 86__sprint(fp, uio) 87 FILE *fp; 88 register struct __suio *uio; 89{ 90 register int err; 91 92 if (uio->uio_resid == 0) { 93 uio->uio_iovcnt = 0; 94 return (0); 95 } 96 err = __sfvwrite(fp, uio); 97 uio->uio_resid = 0; 98 uio->uio_iovcnt = 0; 99 return (err); 100} 101 102/* 103 * Helper function for `fprintf to unbuffered unix file': creates a 104 * temporary buffer. We only work on write-only files; this avoids 105 * worries about ungetc buffers and so forth. 106 */ 107static int 108__sbprintf(fp, fmt, ap) 109 register FILE *fp; 110 const char *fmt; 111 va_list ap; 112{ 113 int ret; 114 FILE fake; 115 unsigned char buf[BUFSIZ]; 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 = EOF; 132 if (fake._flags & __SERR) 133 fp->_flags |= __SERR; 134 return (ret); 135} 136 137/* 138 * Macros for converting digits to letters and vice versa 139 */ 140#define to_digit(c) ((c) - '0') 141#define is_digit(c) ((unsigned)to_digit(c) <= 9) 142#define to_char(n) ((n) + '0') 143 144/* 145 * Convert an unsigned long to ASCII for printf purposes, returning 146 * a pointer to the first character of the string representation. 147 * Octal numbers can be forced to have a leading zero; hex numbers 148 * use the given digits. 149 */ 150static char * 151__ultoa(val, endp, base, octzero, xdigs) 152 register u_long val; 153 char *endp; 154 int base, octzero; 155 char *xdigs; 156{ 157 register char *cp = endp; 158 register long sval; 159 160 /* 161 * Handle the three cases separately, in the hope of getting 162 * better/faster code. 163 */ 164 switch (base) { 165 case 10: 166 if (val < 10) { /* many numbers are 1 digit */ 167 *--cp = to_char(val); 168 return (cp); 169 } 170 /* 171 * On many machines, unsigned arithmetic is harder than 172 * signed arithmetic, so we do at most one unsigned mod and 173 * divide; this is sufficient to reduce the range of 174 * the incoming value to where signed arithmetic works. 175 */ 176 if (val > LONG_MAX) { 177 *--cp = to_char(val % 10); 178 sval = val / 10; 179 } else 180 sval = val; 181 do { 182 *--cp = to_char(sval % 10); 183 sval /= 10; 184 } while (sval != 0); 185 break; 186 187 case 8: 188 do { 189 *--cp = to_char(val & 7); 190 val >>= 3; 191 } while (val); 192 if (octzero && *cp != '0') 193 *--cp = '0'; 194 break; 195 196 case 16: 197 do { 198 *--cp = xdigs[val & 15]; 199 val >>= 4; 200 } while (val); 201 break; 202 203 default: /* oops */ 204 abort(); 205 } 206 return (cp); 207} 208 209/* Identical to __ultoa, but for quads. */ 210static char * 211__uqtoa(val, endp, base, octzero, xdigs) 212 register u_quad_t val; 213 char *endp; 214 int base, octzero; 215 char *xdigs; 216{ 217 register char *cp = endp; 218 register quad_t sval; 219 220 /* quick test for small values; __ultoa is typically much faster */ 221 /* (perhaps instead we should run until small, then call __ultoa?) */ 222 if (val <= ULONG_MAX) 223 return (__ultoa((u_long)val, endp, base, octzero, xdigs)); 224 switch (base) { 225 case 10: 226 if (val < 10) { 227 *--cp = to_char(val % 10); 228 return (cp); 229 } 230 if (val > QUAD_MAX) { 231 *--cp = to_char(val % 10); 232 sval = val / 10; 233 } else 234 sval = val; 235 do { 236 *--cp = to_char(sval % 10); 237 sval /= 10; 238 } while (sval != 0); 239 break; 240 241 case 8: 242 do { 243 *--cp = to_char(val & 7); 244 val >>= 3; 245 } while (val); 246 if (octzero && *cp != '0') 247 *--cp = '0'; 248 break; 249 250 case 16: 251 do { 252 *--cp = xdigs[val & 15]; 253 val >>= 4; 254 } while (val); 255 break; 256 257 default: 258 abort(); 259 } 260 return (cp); 261} 262 263#ifdef FLOATING_POINT 264#include <math.h> 265#include "floatio.h" 266 267#define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ 268#define DEFPREC 6 269 270static char *cvt __P((double, int, int, char *, int *, int, int *)); 271static int exponent __P((char *, int, int)); 272 273#else /* no FLOATING_POINT */ 274 275#define BUF 68 276 277#endif /* FLOATING_POINT */ 278 279#define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ 280 281/* 282 * Flags used during conversion. 283 */ 284#define ALT 0x001 /* alternate form */ 285#define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 286#define LADJUST 0x004 /* left adjustment */ 287#define LONGDBL 0x008 /* long double */ 288#define LONGINT 0x010 /* long integer */ 289#define QUADINT 0x020 /* quad integer */ 290#define SHORTINT 0x040 /* short integer */ 291#define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 292#define FPT 0x100 /* Floating point number */ 293int 294vfprintf(fp, fmt0, ap) 295 FILE *fp; 296 const char *fmt0; 297 va_list ap; 298{ 299 register char *fmt; /* format string */ 300 register int ch; /* character from fmt */ 301 register int n, n2; /* handy integer (short term usage) */ 302 register char *cp; /* handy char pointer (short term usage) */ 303 register struct __siov *iovp;/* for PRINT macro */ 304 register int flags; /* flags as above */ 305 int ret; /* return value accumulator */ 306 int width; /* width from format (%8d), or 0 */ 307 int prec; /* precision from format (%.3d), or -1 */ 308 char sign; /* sign prefix (' ', '+', '-', or \0) */ 309#ifdef FLOATING_POINT 310 char softsign; /* temporary negative sign for floats */ 311 double _double; /* double precision arguments %[eEfgG] */ 312 int expt; /* integer value of exponent */ 313 int expsize; /* character count for expstr */ 314 int ndig; /* actual number of digits returned by cvt */ 315 char expstr[7]; /* buffer for exponent string */ 316#endif 317 u_long ulval; /* integer arguments %[diouxX] */ 318 u_quad_t uqval; /* %q integers */ 319 int base; /* base for [diouxX] conversion */ 320 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 321 int realsz; /* field size expanded by dprec, sign, etc */ 322 int size; /* size of converted field or string */ 323 char *xdigs; /* digits for [xX] conversion */ 324#define NIOV 8 325 struct __suio uio; /* output information: summary */ 326 struct __siov iov[NIOV];/* ... and individual io vectors */ 327 char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ 328 char ox[2]; /* space for 0x hex-prefix */ 329 void **argtable; /* args, built due to positional arg */ 330 void *statargtable [STATIC_ARG_TBL_SIZE]; 331 int nextarg; /* 1-based argument index */ 332 va_list orgap; /* original argument pointer */ 333 334 /* 335 * Choose PADSIZE to trade efficiency vs. size. If larger printf 336 * fields occur frequently, increase PADSIZE and make the initialisers 337 * below longer. 338 */ 339#define PADSIZE 16 /* pad chunk size */ 340 static char blanks[PADSIZE] = 341 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 342 static char zeroes[PADSIZE] = 343 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 344 345 /* 346 * BEWARE, these `goto error' on error, and PAD uses `n'. 347 */ 348#define PRINT(ptr, len) { \ 349 iovp->iov_base = (ptr); \ 350 iovp->iov_len = (len); \ 351 uio.uio_resid += (len); \ 352 iovp++; \ 353 if (++uio.uio_iovcnt >= NIOV) { \ 354 if (__sprint(fp, &uio)) \ 355 goto error; \ 356 iovp = iov; \ 357 } \ 358} 359#define PAD(howmany, with) { \ 360 if ((n = (howmany)) > 0) { \ 361 while (n > PADSIZE) { \ 362 PRINT(with, PADSIZE); \ 363 n -= PADSIZE; \ 364 } \ 365 PRINT(with, n); \ 366 } \ 367} 368#define FLUSH() { \ 369 if (uio.uio_resid && __sprint(fp, &uio)) \ 370 goto error; \ 371 uio.uio_iovcnt = 0; \ 372 iovp = iov; \ 373} 374 375 /* 376 * Get the argument indexed by nextarg. If the argument table is 377 * built, use it to get the argument. If its not, get the next 378 * argument (and arguments must be gotten sequentially). 379 */ 380#define GETARG(type) \ 381 ((argtable != NULL) ? *((type*)(argtable[nextarg++])) : \ 382 (nextarg++, va_arg(ap, type))) 383 384 /* 385 * To extend shorts properly, we need both signed and unsigned 386 * argument extraction methods. 387 */ 388#define SARG() \ 389 (flags&LONGINT ? GETARG(long) : \ 390 flags&SHORTINT ? (long)(short)GETARG(int) : \ 391 (long)GETARG(int)) 392#define UARG() \ 393 (flags&LONGINT ? GETARG(u_long) : \ 394 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 395 (u_long)GETARG(u_int)) 396 397 /* 398 * Get * arguments, including the form *nn$. Preserve the nextarg 399 * that the argument can be gotten once the type is determined. 400 */ 401#define GETASTER(val) \ 402 n2 = 0; \ 403 cp = fmt; \ 404 while (is_digit(*cp)) { \ 405 n2 = 10 * n2 + to_digit(*cp); \ 406 cp++; \ 407 } \ 408 if (*cp == '$') { \ 409 int hold = nextarg; \ 410 if (argtable == NULL) { \ 411 argtable = statargtable; \ 412 __find_arguments (fmt0, orgap, &argtable); \ 413 } \ 414 nextarg = n2; \ 415 val = GETARG (int); \ 416 nextarg = hold; \ 417 fmt = ++cp; \ 418 } else { \ 419 val = GETARG (int); \ 420 } 421 422 423#ifdef _THREAD_SAFE 424 _thread_flockfile(fp,__FILE__,__LINE__); 425#endif 426 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 427 if (cantwrite(fp)) { 428#ifdef _THREAD_SAFE 429 _thread_funlockfile(fp); 430#endif 431 return (EOF); 432 } 433 434 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 435 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 436 fp->_file >= 0) { 437#ifdef _THREAD_SAFE 438 _thread_funlockfile(fp); 439#endif 440 return (__sbprintf(fp, fmt0, ap)); 441 } 442 443 fmt = (char *)fmt0; 444 argtable = NULL; 445 nextarg = 1; 446 orgap = ap; 447 uio.uio_iov = iovp = iov; 448 uio.uio_resid = 0; 449 uio.uio_iovcnt = 0; 450 ret = 0; 451 452 /* 453 * Scan the format for conversions (`%' character). 454 */ 455 for (;;) { 456 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 457 /* void */; 458 if ((n = fmt - cp) != 0) { 459 PRINT(cp, n); 460 ret += n; 461 } 462 if (ch == '\0') 463 goto done; 464 fmt++; /* skip over '%' */ 465 466 flags = 0; 467 dprec = 0; 468 width = 0; 469 prec = -1; 470 sign = '\0'; 471 472rflag: ch = *fmt++; 473reswitch: switch (ch) { 474 case ' ': 475 /* 476 * ``If the space and + flags both appear, the space 477 * flag will be ignored.'' 478 * -- ANSI X3J11 479 */ 480 if (!sign) 481 sign = ' '; 482 goto rflag; 483 case '#': 484 flags |= ALT; 485 goto rflag; 486 case '*': 487 /* 488 * ``A negative field width argument is taken as a 489 * - flag followed by a positive field width.'' 490 * -- ANSI X3J11 491 * They don't exclude field widths read from args. 492 */ 493 GETASTER (width); 494 if (width >= 0) 495 goto rflag; 496 width = -width; 497 /* FALLTHROUGH */ 498 case '-': 499 flags |= LADJUST; 500 goto rflag; 501 case '+': 502 sign = '+'; 503 goto rflag; 504 case '.': 505 if ((ch = *fmt++) == '*') { 506 GETASTER (n); 507 prec = n < 0 ? -1 : n; 508 goto rflag; 509 } 510 n = 0; 511 while (is_digit(ch)) { 512 n = 10 * n + to_digit(ch); 513 ch = *fmt++; 514 } 515 prec = n < 0 ? -1 : n; 516 goto reswitch; 517 case '0': 518 /* 519 * ``Note that 0 is taken as a flag, not as the 520 * beginning of a field width.'' 521 * -- ANSI X3J11 522 */ 523 flags |= ZEROPAD; 524 goto rflag; 525 case '1': case '2': case '3': case '4': 526 case '5': case '6': case '7': case '8': case '9': 527 n = 0; 528 do { 529 n = 10 * n + to_digit(ch); 530 ch = *fmt++; 531 } while (is_digit(ch)); 532 if (ch == '$') { 533 nextarg = n; 534 if (argtable == NULL) { 535 argtable = statargtable; 536 __find_arguments (fmt0, orgap, 537 &argtable); 538 } 539 goto rflag; 540 } 541 width = n; 542 goto reswitch; 543#ifdef FLOATING_POINT 544 case 'L': 545 flags |= LONGDBL; 546 goto rflag; 547#endif 548 case 'h': 549 flags |= SHORTINT; 550 goto rflag; 551 case 'l': 552 flags |= LONGINT; 553 goto rflag; 554 case 'q': 555 flags |= QUADINT; 556 goto rflag; 557 case 'c': 558 *(cp = buf) = GETARG(int); 559 size = 1; 560 sign = '\0'; 561 break; 562 case 'D': 563 flags |= LONGINT; 564 /*FALLTHROUGH*/ 565 case 'd': 566 case 'i': 567 if (flags & QUADINT) { 568 uqval = GETARG(quad_t); 569 if ((quad_t)uqval < 0) { 570 uqval = -uqval; 571 sign = '-'; 572 } 573 } else { 574 ulval = SARG(); 575 if ((long)ulval < 0) { 576 ulval = -ulval; 577 sign = '-'; 578 } 579 } 580 base = 10; 581 goto number; 582#ifdef FLOATING_POINT 583 case 'e': 584 case 'E': 585 case 'f': 586 goto fp_begin; 587 case 'g': 588 case 'G': 589 if (prec == 0) 590 prec = 1; 591fp_begin: if (prec == -1) 592 prec = DEFPREC; 593 if (flags & LONGDBL) 594 /* XXX this loses precision. */ 595 _double = (double)GETARG(long double); 596 else 597 _double = GETARG(double); 598 /* do this before tricky precision changes */ 599 if (isinf(_double)) { 600 if (_double < 0) 601 sign = '-'; 602 cp = "Inf"; 603 size = 3; 604 break; 605 } 606 if (isnan(_double)) { 607 cp = "NaN"; 608 size = 3; 609 break; 610 } 611 flags |= FPT; 612 cp = cvt(_double, prec, flags, &softsign, 613 &expt, ch, &ndig); 614 if (ch == 'g' || ch == 'G') { 615 if (expt <= -4 || expt > prec) 616 ch = (ch == 'g') ? 'e' : 'E'; 617 else 618 ch = 'g'; 619 } 620 if (ch <= 'e') { /* 'e' or 'E' fmt */ 621 --expt; 622 expsize = exponent(expstr, expt, ch); 623 size = expsize + ndig; 624 if (ndig > 1 || flags & ALT) 625 ++size; 626 } else if (ch == 'f') { /* f fmt */ 627 if (expt > 0) { 628 size = expt; 629 if (prec || flags & ALT) 630 size += prec + 1; 631 } else /* "0.X" */ 632 size = prec + 2; 633 } else if (expt >= ndig) { /* fixed g fmt */ 634 size = expt; 635 if (flags & ALT) 636 ++size; 637 } else 638 size = ndig + (expt > 0 ? 639 1 : 2 - expt); 640 641 if (softsign) 642 sign = '-'; 643 break; 644#endif /* FLOATING_POINT */ 645 case 'n': 646 if (flags & QUADINT) 647 *GETARG(quad_t *) = ret; 648 else if (flags & LONGINT) 649 *GETARG(long *) = ret; 650 else if (flags & SHORTINT) 651 *GETARG(short *) = ret; 652 else 653 *GETARG(int *) = ret; 654 continue; /* no output */ 655 case 'O': 656 flags |= LONGINT; 657 /*FALLTHROUGH*/ 658 case 'o': 659 if (flags & QUADINT) 660 uqval = GETARG(u_quad_t); 661 else 662 ulval = UARG(); 663 base = 8; 664 goto nosign; 665 case 'p': 666 /* 667 * ``The argument shall be a pointer to void. The 668 * value of the pointer is converted to a sequence 669 * of printable characters, in an implementation- 670 * defined manner.'' 671 * -- ANSI X3J11 672 */ 673 ulval = (u_long)GETARG(void *); 674 base = 16; 675 xdigs = "0123456789abcdef"; 676 flags = (flags & ~QUADINT) | HEXPREFIX; 677 ch = 'x'; 678 goto nosign; 679 case 's': 680 if ((cp = GETARG(char *)) == NULL) 681 cp = "(null)"; 682 if (prec >= 0) { 683 /* 684 * can't use strlen; can only look for the 685 * NUL in the first `prec' characters, and 686 * strlen() will go further. 687 */ 688 char *p = memchr(cp, 0, (size_t)prec); 689 690 if (p != NULL) { 691 size = p - cp; 692 if (size > prec) 693 size = prec; 694 } else 695 size = prec; 696 } else 697 size = strlen(cp); 698 sign = '\0'; 699 break; 700 case 'U': 701 flags |= LONGINT; 702 /*FALLTHROUGH*/ 703 case 'u': 704 if (flags & QUADINT) 705 uqval = GETARG(u_quad_t); 706 else 707 ulval = UARG(); 708 base = 10; 709 goto nosign; 710 case 'X': 711 xdigs = "0123456789ABCDEF"; 712 goto hex; 713 case 'x': 714 xdigs = "0123456789abcdef"; 715hex: if (flags & QUADINT) 716 uqval = GETARG(u_quad_t); 717 else 718 ulval = UARG(); 719 base = 16; 720 /* leading 0x/X only if non-zero */ 721 if (flags & ALT && 722 (flags & QUADINT ? uqval != 0 : ulval != 0)) 723 flags |= HEXPREFIX; 724 725 /* unsigned conversions */ 726nosign: sign = '\0'; 727 /* 728 * ``... diouXx conversions ... if a precision is 729 * specified, the 0 flag will be ignored.'' 730 * -- ANSI X3J11 731 */ 732number: if ((dprec = prec) >= 0) 733 flags &= ~ZEROPAD; 734 735 /* 736 * ``The result of converting a zero value with an 737 * explicit precision of zero is no characters.'' 738 * -- ANSI X3J11 739 */ 740 cp = buf + BUF; 741 if (flags & QUADINT) { 742 if (uqval != 0 || prec != 0) 743 cp = __uqtoa(uqval, cp, base, 744 flags & ALT, xdigs); 745 } else { 746 if (ulval != 0 || prec != 0) 747 cp = __ultoa(ulval, cp, base, 748 flags & ALT, xdigs); 749 } 750 size = buf + BUF - cp; 751 break; 752 default: /* "%?" prints ?, unless ? is NUL */ 753 if (ch == '\0') 754 goto done; 755 /* pretend it was %c with argument ch */ 756 cp = buf; 757 *cp = ch; 758 size = 1; 759 sign = '\0'; 760 break; 761 } 762 763 /* 764 * All reasonable formats wind up here. At this point, `cp' 765 * points to a string which (if not flags&LADJUST) should be 766 * padded out to `width' places. If flags&ZEROPAD, it should 767 * first be prefixed by any sign or other prefix; otherwise, 768 * it should be blank padded before the prefix is emitted. 769 * After any left-hand padding and prefixing, emit zeroes 770 * required by a decimal [diouxX] precision, then print the 771 * string proper, then emit zeroes required by any leftover 772 * floating precision; finally, if LADJUST, pad with blanks. 773 * 774 * Compute actual size, so we know how much to pad. 775 * size excludes decimal prec; realsz includes it. 776 */ 777 realsz = dprec > size ? dprec : size; 778 if (sign) 779 realsz++; 780 else if (flags & HEXPREFIX) 781 realsz += 2; 782 783 /* right-adjusting blank padding */ 784 if ((flags & (LADJUST|ZEROPAD)) == 0) 785 PAD(width - realsz, blanks); 786 787 /* prefix */ 788 if (sign) { 789 PRINT(&sign, 1); 790 } else if (flags & HEXPREFIX) { 791 ox[0] = '0'; 792 ox[1] = ch; 793 PRINT(ox, 2); 794 } 795 796 /* right-adjusting zero padding */ 797 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 798 PAD(width - realsz, zeroes); 799 800 /* leading zeroes from decimal precision */ 801 PAD(dprec - size, zeroes); 802 803 /* the string or number proper */ 804#ifdef FLOATING_POINT 805 if ((flags & FPT) == 0) { 806 PRINT(cp, size); 807 } else { /* glue together f_p fragments */ 808 if (ch >= 'f') { /* 'f' or 'g' */ 809 if (_double == 0) { 810 /* kludge for __dtoa irregularity */ 811 if (expt >= ndig && 812 (flags & ALT) == 0) { 813 PRINT("0", 1); 814 } else { 815 PRINT("0.", 2); 816 PAD(ndig - 1, zeroes); 817 } 818 } else if (expt <= 0) { 819 PRINT("0.", 2); 820 PAD(-expt, zeroes); 821 PRINT(cp, ndig); 822 } else if (expt >= ndig) { 823 PRINT(cp, ndig); 824 PAD(expt - ndig, zeroes); 825 if (flags & ALT) 826 PRINT(".", 1); 827 } else { 828 PRINT(cp, expt); 829 cp += expt; 830 PRINT(".", 1); 831 PRINT(cp, ndig-expt); 832 } 833 } else { /* 'e' or 'E' */ 834 if (ndig > 1 || flags & ALT) { 835 ox[0] = *cp++; 836 ox[1] = '.'; 837 PRINT(ox, 2); 838 if (_double) { 839 PRINT(cp, ndig-1); 840 } else /* 0.[0..] */ 841 /* __dtoa irregularity */ 842 PAD(ndig - 1, zeroes); 843 } else /* XeYYY */ 844 PRINT(cp, 1); 845 PRINT(expstr, expsize); 846 } 847 } 848#else 849 PRINT(cp, size); 850#endif 851 /* left-adjusting padding (always blank) */ 852 if (flags & LADJUST) 853 PAD(width - realsz, blanks); 854 855 /* finally, adjust ret */ 856 ret += width > realsz ? width : realsz; 857 858 FLUSH(); /* copy out the I/O vectors */ 859 } 860done: 861 FLUSH(); 862error: 863 if (__sferror(fp)) 864 ret = EOF; 865#ifdef _THREAD_SAFE 866 _thread_funlockfile(fp); 867#endif 868 if ((argtable != NULL) && (argtable != statargtable)) 869 free (argtable); 870 return (ret); 871 /* NOTREACHED */ 872} 873 874/* 875 * Type ids for argument type table. 876 */ 877#define T_UNUSED 0 878#define T_SHORT 1 879#define T_U_SHORT 2 880#define TP_SHORT 3 881#define T_INT 4 882#define T_U_INT 5 883#define TP_INT 6 884#define T_LONG 7 885#define T_U_LONG 8 886#define TP_LONG 9 887#define T_QUAD 10 888#define T_U_QUAD 11 889#define TP_QUAD 12 890#define T_DOUBLE 13 891#define T_LONG_DOUBLE 14 892#define TP_CHAR 15 893#define TP_VOID 16 894 895/* 896 * Find all arguments when a positional parameter is encountered. Returns a 897 * table, indexed by argument number, of pointers to each arguments. The 898 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. 899 * It will be replaces with a malloc-ed on if it overflows. 900 */ 901static void 902__find_arguments (fmt0, ap, argtable) 903 const char *fmt0; 904 va_list ap; 905 void ***argtable; 906{ 907 register char *fmt; /* format string */ 908 register int ch; /* character from fmt */ 909 register int n, n2; /* handy integer (short term usage) */ 910 register char *cp; /* handy char pointer (short term usage) */ 911 register int flags; /* flags as above */ 912 int width; /* width from format (%8d), or 0 */ 913 unsigned char *typetable; /* table of types */ 914 unsigned char stattypetable [STATIC_ARG_TBL_SIZE]; 915 int tablesize; /* current size of type table */ 916 int tablemax; /* largest used index in table */ 917 int nextarg; /* 1-based argument index */ 918 919 /* 920 * Add an argument type to the table, expanding if necessary. 921 */ 922#define ADDTYPE(type) \ 923 ((nextarg >= tablesize) ? \ 924 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \ 925 typetable[nextarg++] = type, \ 926 (nextarg > tablemax) ? tablemax = nextarg : 0) 927 928#define ADDSARG() \ 929 ((flags&LONGINT) ? ADDTYPE(T_LONG) : \ 930 ((flags&SHORTINT) ? ADDTYPE(T_SHORT) : ADDTYPE(T_INT))) 931 932#define ADDUARG() \ 933 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : \ 934 ((flags&SHORTINT) ? ADDTYPE(T_U_SHORT) : ADDTYPE(T_U_INT))) 935 936 /* 937 * Add * arguments to the type array. 938 */ 939#define ADDASTER() \ 940 n2 = 0; \ 941 cp = fmt; \ 942 while (is_digit(*cp)) { \ 943 n2 = 10 * n2 + to_digit(*cp); \ 944 cp++; \ 945 } \ 946 if (*cp == '$') { \ 947 int hold = nextarg; \ 948 nextarg = n2; \ 949 ADDTYPE (T_INT); \ 950 nextarg = hold; \ 951 fmt = ++cp; \ 952 } else { \ 953 ADDTYPE (T_INT); \ 954 } 955 fmt = (char *)fmt0; 956 typetable = stattypetable; 957 tablesize = STATIC_ARG_TBL_SIZE; 958 tablemax = 0; 959 nextarg = 1; 960 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE); 961 962 /* 963 * Scan the format for conversions (`%' character). 964 */ 965 for (;;) { 966 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 967 /* void */; 968 if (ch == '\0') 969 goto done; 970 fmt++; /* skip over '%' */ 971 972 flags = 0; 973 width = 0; 974 975rflag: ch = *fmt++; 976reswitch: switch (ch) { 977 case ' ': 978 case '#': 979 goto rflag; 980 case '*': 981 ADDASTER (); 982 goto rflag; 983 case '-': 984 case '+': 985 goto rflag; 986 case '.': 987 if ((ch = *fmt++) == '*') { 988 ADDASTER (); 989 goto rflag; 990 } 991 while (is_digit(ch)) { 992 ch = *fmt++; 993 } 994 goto reswitch; 995 case '0': 996 goto rflag; 997 case '1': case '2': case '3': case '4': 998 case '5': case '6': case '7': case '8': case '9': 999 n = 0; 1000 do { 1001 n = 10 * n + to_digit(ch); 1002 ch = *fmt++; 1003 } while (is_digit(ch)); 1004 if (ch == '$') { 1005 nextarg = n; 1006 goto rflag; 1007 } 1008 width = n; 1009 goto reswitch; 1010#ifdef FLOATING_POINT 1011 case 'L': 1012 flags |= LONGDBL; 1013 goto rflag; 1014#endif 1015 case 'h': 1016 flags |= SHORTINT; 1017 goto rflag; 1018 case 'l': 1019 flags |= LONGINT; 1020 goto rflag; 1021 case 'q': 1022 flags |= QUADINT; 1023 goto rflag; 1024 case 'c': 1025 ADDTYPE(T_INT); 1026 break; 1027 case 'D': 1028 flags |= LONGINT; 1029 /*FALLTHROUGH*/ 1030 case 'd': 1031 case 'i': 1032 if (flags & QUADINT) { 1033 ADDTYPE(T_QUAD); 1034 } else { 1035 ADDSARG(); 1036 } 1037 break; 1038#ifdef FLOATING_POINT 1039 case 'e': 1040 case 'E': 1041 case 'f': 1042 case 'g': 1043 case 'G': 1044 if (flags & LONGDBL) 1045 ADDTYPE(T_LONG_DOUBLE); 1046 else 1047 ADDTYPE(T_DOUBLE); 1048 break; 1049#endif /* FLOATING_POINT */ 1050 case 'n': 1051 if (flags & QUADINT) 1052 ADDTYPE(TP_QUAD); 1053 else if (flags & LONGINT) 1054 ADDTYPE(TP_LONG); 1055 else if (flags & SHORTINT) 1056 ADDTYPE(TP_SHORT); 1057 else 1058 ADDTYPE(TP_INT); 1059 continue; /* no output */ 1060 case 'O': 1061 flags |= LONGINT; 1062 /*FALLTHROUGH*/ 1063 case 'o': 1064 if (flags & QUADINT) 1065 ADDTYPE(T_U_QUAD); 1066 else 1067 ADDUARG(); 1068 break; 1069 case 'p': 1070 ADDTYPE(TP_VOID); 1071 break; 1072 case 's': 1073 ADDTYPE(TP_CHAR); 1074 break; 1075 case 'U': 1076 flags |= LONGINT; 1077 /*FALLTHROUGH*/ 1078 case 'u': 1079 if (flags & QUADINT) 1080 ADDTYPE(T_U_QUAD); 1081 else 1082 ADDUARG(); 1083 break; 1084 case 'X': 1085 case 'x': 1086 if (flags & QUADINT) 1087 ADDTYPE(T_U_QUAD); 1088 else 1089 ADDUARG(); 1090 break; 1091 default: /* "%?" prints ?, unless ? is NUL */ 1092 if (ch == '\0') 1093 goto done; 1094 break; 1095 } 1096 } 1097done: 1098 /* 1099 * Build the argument table. 1100 */ 1101 if (tablemax >= STATIC_ARG_TBL_SIZE) { 1102 *argtable = (void **) 1103 malloc (sizeof (void *) * (tablemax + 1)); 1104 } 1105 1106 (*argtable) [0] = NULL; 1107 for (n = 1; n <= tablemax; n++) { 1108 (*argtable) [n] = ap; 1109 switch (typetable [n]) { 1110 case T_UNUSED: 1111 (void) va_arg (ap, int); 1112 break; 1113 case T_SHORT: 1114 (void) va_arg (ap, int); 1115 break; 1116 case T_U_SHORT: 1117 (void) va_arg (ap, int); 1118 break; 1119 case TP_SHORT: 1120 (void) va_arg (ap, short *); 1121 break; 1122 case T_INT: 1123 (void) va_arg (ap, int); 1124 break; 1125 case T_U_INT: 1126 (void) va_arg (ap, unsigned int); 1127 break; 1128 case TP_INT: 1129 (void) va_arg (ap, int *); 1130 break; 1131 case T_LONG: 1132 (void) va_arg (ap, long); 1133 break; 1134 case T_U_LONG: 1135 (void) va_arg (ap, unsigned long); 1136 break; 1137 case TP_LONG: 1138 (void) va_arg (ap, long *); 1139 break; 1140 case T_QUAD: 1141 (void) va_arg (ap, quad_t); 1142 break; 1143 case T_U_QUAD: 1144 (void) va_arg (ap, u_quad_t); 1145 break; 1146 case TP_QUAD: 1147 (void) va_arg (ap, quad_t *); 1148 break; 1149 case T_DOUBLE: 1150 (void) va_arg (ap, double); 1151 break; 1152 case T_LONG_DOUBLE: 1153 (void) va_arg (ap, long double); 1154 break; 1155 case TP_CHAR: 1156 (void) va_arg (ap, char *); 1157 break; 1158 case TP_VOID: 1159 (void) va_arg (ap, void *); 1160 break; 1161 } 1162 } 1163 1164 if ((typetable != NULL) && (typetable != stattypetable)) 1165 free (typetable); 1166} 1167 1168/* 1169 * Increase the size of the type table. 1170 */ 1171static void 1172__grow_type_table (nextarg, typetable, tablesize) 1173 int nextarg; 1174 unsigned char **typetable; 1175 int *tablesize; 1176{ 1177 unsigned char *oldtable = *typetable; 1178 int newsize = *tablesize * 2; 1179 1180 if (*tablesize == STATIC_ARG_TBL_SIZE) { 1181 *typetable = (unsigned char *) 1182 malloc (sizeof (unsigned char) * newsize); 1183 bcopy (oldtable, *typetable, *tablesize); 1184 } else { 1185 *typetable = (unsigned char *) 1186 realloc (typetable, sizeof (unsigned char) * newsize); 1187 1188 } 1189 memset (&typetable [*tablesize], T_UNUSED, (newsize - *tablesize)); 1190 1191 *tablesize = newsize; 1192} 1193 1194 1195#ifdef FLOATING_POINT 1196 1197extern char *__dtoa __P((double, int, int, int *, int *, char **)); 1198 1199static char * 1200cvt(value, ndigits, flags, sign, decpt, ch, length) 1201 double value; 1202 int ndigits, flags, *decpt, ch, *length; 1203 char *sign; 1204{ 1205 int mode, dsgn; 1206 char *digits, *bp, *rve; 1207 1208 if (ch == 'f') 1209 mode = 3; /* ndigits after the decimal point */ 1210 else { 1211 /* 1212 * To obtain ndigits after the decimal point for the 'e' 1213 * and 'E' formats, round to ndigits + 1 significant 1214 * figures. 1215 */ 1216 if (ch == 'e' || ch == 'E') 1217 ndigits++; 1218 mode = 2; /* ndigits significant digits */ 1219 } 1220 if (value < 0) { 1221 value = -value; 1222 *sign = '-'; 1223 } else 1224 *sign = '\000'; 1225 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve); 1226 if ((ch != 'g' && ch != 'G') || flags & ALT) { 1227 /* print trailing zeros */ 1228 bp = digits + ndigits; 1229 if (ch == 'f') { 1230 if (*digits == '0' && value) 1231 *decpt = -ndigits + 1; 1232 bp += *decpt; 1233 } 1234 if (value == 0) /* kludge for __dtoa irregularity */ 1235 rve = bp; 1236 while (rve < bp) 1237 *rve++ = '0'; 1238 } 1239 *length = rve - digits; 1240 return (digits); 1241} 1242 1243static int 1244exponent(p0, exp, fmtch) 1245 char *p0; 1246 int exp, fmtch; 1247{ 1248 register char *p, *t; 1249 char expbuf[MAXEXP]; 1250 1251 p = p0; 1252 *p++ = fmtch; 1253 if (exp < 0) { 1254 exp = -exp; 1255 *p++ = '-'; 1256 } 1257 else 1258 *p++ = '+'; 1259 t = expbuf + MAXEXP; 1260 if (exp > 9) { 1261 do { 1262 *--t = to_char(exp % 10); 1263 } while ((exp /= 10) > 9); 1264 *--t = to_char(exp); 1265 for (; t < expbuf + MAXEXP; *p++ = *t++); 1266 } 1267 else { 1268 *p++ = '0'; 1269 *p++ = to_char(exp); 1270 } 1271 return (p - p0); 1272} 1273#endif /* FLOATING_POINT */ 1274