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 39 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 40 #endif 41 static const char rcsid[] = 42 "$Id: vfprintf.c,v 1.19 1998/05/08 05:10:32 jb 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 #include "libc_private.h" 67 68 /* Define FLOATING_POINT to get floating point. */ 69 #define FLOATING_POINT 70 71 static int __sprint __P((FILE *, struct __suio *)); 72 static int __sbprintf __P((FILE *, const char *, va_list)); 73 static char * __ultoa __P((u_long, char *, int, int, char *)); 74 static char * __uqtoa __P((u_quad_t, char *, int, int, char *)); 75 static void __find_arguments __P((const char *, va_list, void ***)); 76 static void __grow_type_table __P((int, unsigned char **, int *)); 77 78 /* 79 * Flush out all the vectors defined by the given uio, 80 * then reset it so that it can be reused. 81 */ 82 static int 83 __sprint(fp, uio) 84 FILE *fp; 85 register struct __suio *uio; 86 { 87 register int err; 88 89 if (uio->uio_resid == 0) { 90 uio->uio_iovcnt = 0; 91 return (0); 92 } 93 err = __sfvwrite(fp, uio); 94 uio->uio_resid = 0; 95 uio->uio_iovcnt = 0; 96 return (err); 97 } 98 99 /* 100 * Helper function for `fprintf to unbuffered unix file': creates a 101 * temporary buffer. We only work on write-only files; this avoids 102 * worries about ungetc buffers and so forth. 103 */ 104 static int 105 __sbprintf(fp, fmt, ap) 106 register FILE *fp; 107 const char *fmt; 108 va_list ap; 109 { 110 int ret; 111 FILE fake; 112 unsigned char buf[BUFSIZ]; 113 114 /* copy the important variables */ 115 fake._flags = fp->_flags & ~__SNBF; 116 fake._file = fp->_file; 117 fake._cookie = fp->_cookie; 118 fake._write = fp->_write; 119 120 /* set up the buffer */ 121 fake._bf._base = fake._p = buf; 122 fake._bf._size = fake._w = sizeof(buf); 123 fake._lbfsize = 0; /* not actually used, but Just In Case */ 124 125 /* do the work, then copy any error status */ 126 ret = vfprintf(&fake, fmt, ap); 127 if (ret >= 0 && fflush(&fake)) 128 ret = EOF; 129 if (fake._flags & __SERR) 130 fp->_flags |= __SERR; 131 return (ret); 132 } 133 134 /* 135 * Macros for converting digits to letters and vice versa 136 */ 137 #define to_digit(c) ((c) - '0') 138 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 139 #define to_char(n) ((n) + '0') 140 141 /* 142 * Convert an unsigned long to ASCII for printf purposes, returning 143 * a pointer to the first character of the string representation. 144 * Octal numbers can be forced to have a leading zero; hex numbers 145 * use the given digits. 146 */ 147 static char * 148 __ultoa(val, endp, base, octzero, xdigs) 149 register u_long val; 150 char *endp; 151 int base, octzero; 152 char *xdigs; 153 { 154 register char *cp = endp; 155 register long sval; 156 157 /* 158 * Handle the three cases separately, in the hope of getting 159 * better/faster code. 160 */ 161 switch (base) { 162 case 10: 163 if (val < 10) { /* many numbers are 1 digit */ 164 *--cp = to_char(val); 165 return (cp); 166 } 167 /* 168 * On many machines, unsigned arithmetic is harder than 169 * signed arithmetic, so we do at most one unsigned mod and 170 * divide; this is sufficient to reduce the range of 171 * the incoming value to where signed arithmetic works. 172 */ 173 if (val > LONG_MAX) { 174 *--cp = to_char(val % 10); 175 sval = val / 10; 176 } else 177 sval = val; 178 do { 179 *--cp = to_char(sval % 10); 180 sval /= 10; 181 } while (sval != 0); 182 break; 183 184 case 8: 185 do { 186 *--cp = to_char(val & 7); 187 val >>= 3; 188 } while (val); 189 if (octzero && *cp != '0') 190 *--cp = '0'; 191 break; 192 193 case 16: 194 do { 195 *--cp = xdigs[val & 15]; 196 val >>= 4; 197 } while (val); 198 break; 199 200 default: /* oops */ 201 abort(); 202 } 203 return (cp); 204 } 205 206 /* Identical to __ultoa, but for quads. */ 207 static char * 208 __uqtoa(val, endp, base, octzero, xdigs) 209 register u_quad_t val; 210 char *endp; 211 int base, octzero; 212 char *xdigs; 213 { 214 register char *cp = endp; 215 register quad_t sval; 216 217 /* quick test for small values; __ultoa is typically much faster */ 218 /* (perhaps instead we should run until small, then call __ultoa?) */ 219 if (val <= ULONG_MAX) 220 return (__ultoa((u_long)val, endp, base, octzero, xdigs)); 221 switch (base) { 222 case 10: 223 if (val < 10) { 224 *--cp = to_char(val % 10); 225 return (cp); 226 } 227 if (val > QUAD_MAX) { 228 *--cp = to_char(val % 10); 229 sval = val / 10; 230 } else 231 sval = val; 232 do { 233 *--cp = to_char(sval % 10); 234 sval /= 10; 235 } while (sval != 0); 236 break; 237 238 case 8: 239 do { 240 *--cp = to_char(val & 7); 241 val >>= 3; 242 } while (val); 243 if (octzero && *cp != '0') 244 *--cp = '0'; 245 break; 246 247 case 16: 248 do { 249 *--cp = xdigs[val & 15]; 250 val >>= 4; 251 } while (val); 252 break; 253 254 default: 255 abort(); 256 } 257 return (cp); 258 } 259 260 #ifdef FLOATING_POINT 261 #include <math.h> 262 #include "floatio.h" 263 264 #define BUF (MAXEXP+MAXFRACT+1) /* + decimal point */ 265 #define DEFPREC 6 266 267 static char *cvt __P((double, int, int, char *, int *, int, int *)); 268 static int exponent __P((char *, int, int)); 269 270 #else /* no FLOATING_POINT */ 271 272 #define BUF 68 273 274 #endif /* FLOATING_POINT */ 275 276 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ 277 278 /* 279 * Flags used during conversion. 280 */ 281 #define ALT 0x001 /* alternate form */ 282 #define HEXPREFIX 0x002 /* add 0x or 0X prefix */ 283 #define LADJUST 0x004 /* left adjustment */ 284 #define LONGDBL 0x008 /* long double */ 285 #define LONGINT 0x010 /* long integer */ 286 #define QUADINT 0x020 /* quad integer */ 287 #define SHORTINT 0x040 /* short integer */ 288 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 289 #define FPT 0x100 /* Floating point number */ 290 int 291 vfprintf(fp, fmt0, ap) 292 FILE *fp; 293 const char *fmt0; 294 va_list ap; 295 { 296 register char *fmt; /* format string */ 297 register int ch; /* character from fmt */ 298 register int n, n2; /* handy integer (short term usage) */ 299 register char *cp; /* handy char pointer (short term usage) */ 300 register struct __siov *iovp;/* for PRINT macro */ 301 register int flags; /* flags as above */ 302 int ret; /* return value accumulator */ 303 int width; /* width from format (%8d), or 0 */ 304 int prec; /* precision from format (%.3d), or -1 */ 305 char sign; /* sign prefix (' ', '+', '-', or \0) */ 306 #ifdef FLOATING_POINT 307 char softsign; /* temporary negative sign for floats */ 308 double _double; /* double precision arguments %[eEfgG] */ 309 int expt; /* integer value of exponent */ 310 int expsize; /* character count for expstr */ 311 int ndig; /* actual number of digits returned by cvt */ 312 char expstr[7]; /* buffer for exponent string */ 313 #endif 314 u_long ulval; /* integer arguments %[diouxX] */ 315 u_quad_t uqval; /* %q integers */ 316 int base; /* base for [diouxX] conversion */ 317 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 318 int realsz; /* field size expanded by dprec, sign, etc */ 319 int size; /* size of converted field or string */ 320 int prsize; /* max size of printed field */ 321 char *xdigs; /* digits for [xX] conversion */ 322 #define NIOV 8 323 struct __suio uio; /* output information: summary */ 324 struct __siov iov[NIOV];/* ... and individual io vectors */ 325 char buf[BUF]; /* space for %c, %[diouxX], %[eEfgG] */ 326 char ox[2]; /* space for 0x hex-prefix */ 327 void **argtable; /* args, built due to positional arg */ 328 void *statargtable [STATIC_ARG_TBL_SIZE]; 329 int nextarg; /* 1-based argument index */ 330 va_list orgap; /* original argument pointer */ 331 332 /* 333 * Choose PADSIZE to trade efficiency vs. size. If larger printf 334 * fields occur frequently, increase PADSIZE and make the initialisers 335 * below longer. 336 */ 337 #define PADSIZE 16 /* pad chunk size */ 338 static char blanks[PADSIZE] = 339 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 340 static char zeroes[PADSIZE] = 341 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 342 343 /* 344 * BEWARE, these `goto error' on error, and PAD uses `n'. 345 */ 346 #define PRINT(ptr, len) { \ 347 iovp->iov_base = (ptr); \ 348 iovp->iov_len = (len); \ 349 uio.uio_resid += (len); \ 350 iovp++; \ 351 if (++uio.uio_iovcnt >= NIOV) { \ 352 if (__sprint(fp, &uio)) \ 353 goto error; \ 354 iovp = iov; \ 355 } \ 356 } 357 #define PAD(howmany, with) { \ 358 if ((n = (howmany)) > 0) { \ 359 while (n > PADSIZE) { \ 360 PRINT(with, PADSIZE); \ 361 n -= PADSIZE; \ 362 } \ 363 PRINT(with, n); \ 364 } \ 365 } 366 #define FLUSH() { \ 367 if (uio.uio_resid && __sprint(fp, &uio)) \ 368 goto error; \ 369 uio.uio_iovcnt = 0; \ 370 iovp = iov; \ 371 } 372 373 /* 374 * Get the argument indexed by nextarg. If the argument table is 375 * built, use it to get the argument. If its not, get the next 376 * argument (and arguments must be gotten sequentially). 377 */ 378 #define GETARG(type) \ 379 ((argtable != NULL) ? *((type*)(argtable[nextarg++])) : \ 380 (nextarg++, va_arg(ap, type))) 381 382 /* 383 * To extend shorts properly, we need both signed and unsigned 384 * argument extraction methods. 385 */ 386 #define SARG() \ 387 (flags&LONGINT ? GETARG(long) : \ 388 flags&SHORTINT ? (long)(short)GETARG(int) : \ 389 (long)GETARG(int)) 390 #define UARG() \ 391 (flags&LONGINT ? GETARG(u_long) : \ 392 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 393 (u_long)GETARG(u_int)) 394 395 /* 396 * Get * arguments, including the form *nn$. Preserve the nextarg 397 * that the argument can be gotten once the type is determined. 398 */ 399 #define GETASTER(val) \ 400 n2 = 0; \ 401 cp = fmt; \ 402 while (is_digit(*cp)) { \ 403 n2 = 10 * n2 + to_digit(*cp); \ 404 cp++; \ 405 } \ 406 if (*cp == '$') { \ 407 int hold = nextarg; \ 408 if (argtable == NULL) { \ 409 argtable = statargtable; \ 410 __find_arguments (fmt0, orgap, &argtable); \ 411 } \ 412 nextarg = n2; \ 413 val = GETARG (int); \ 414 nextarg = hold; \ 415 fmt = ++cp; \ 416 } else { \ 417 val = GETARG (int); \ 418 } 419 420 421 FLOCKFILE(fp); 422 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 423 if (cantwrite(fp)) { 424 FUNLOCKFILE(fp); 425 return (EOF); 426 } 427 428 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 429 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 430 fp->_file >= 0) { 431 FUNLOCKFILE(fp); 432 return (__sbprintf(fp, fmt0, ap)); 433 } 434 435 fmt = (char *)fmt0; 436 argtable = NULL; 437 nextarg = 1; 438 orgap = ap; 439 uio.uio_iov = iovp = iov; 440 uio.uio_resid = 0; 441 uio.uio_iovcnt = 0; 442 ret = 0; 443 444 /* 445 * Scan the format for conversions (`%' character). 446 */ 447 for (;;) { 448 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 449 /* void */; 450 if ((n = fmt - cp) != 0) { 451 if ((unsigned)ret + n > INT_MAX) { 452 ret = EOF; 453 goto error; 454 } 455 PRINT(cp, n); 456 ret += n; 457 } 458 if (ch == '\0') 459 goto done; 460 fmt++; /* skip over '%' */ 461 462 flags = 0; 463 dprec = 0; 464 width = 0; 465 prec = -1; 466 sign = '\0'; 467 468 rflag: ch = *fmt++; 469 reswitch: switch (ch) { 470 case ' ': 471 /* 472 * ``If the space and + flags both appear, the space 473 * flag will be ignored.'' 474 * -- ANSI X3J11 475 */ 476 if (!sign) 477 sign = ' '; 478 goto rflag; 479 case '#': 480 flags |= ALT; 481 goto rflag; 482 case '*': 483 /* 484 * ``A negative field width argument is taken as a 485 * - flag followed by a positive field width.'' 486 * -- ANSI X3J11 487 * They don't exclude field widths read from args. 488 */ 489 GETASTER (width); 490 if (width >= 0) 491 goto rflag; 492 width = -width; 493 /* FALLTHROUGH */ 494 case '-': 495 flags |= LADJUST; 496 goto rflag; 497 case '+': 498 sign = '+'; 499 goto rflag; 500 case '.': 501 if ((ch = *fmt++) == '*') { 502 GETASTER (n); 503 prec = n < 0 ? -1 : n; 504 goto rflag; 505 } 506 n = 0; 507 while (is_digit(ch)) { 508 n = 10 * n + to_digit(ch); 509 ch = *fmt++; 510 } 511 prec = n < 0 ? -1 : n; 512 goto reswitch; 513 case '0': 514 /* 515 * ``Note that 0 is taken as a flag, not as the 516 * beginning of a field width.'' 517 * -- ANSI X3J11 518 */ 519 flags |= ZEROPAD; 520 goto rflag; 521 case '1': case '2': case '3': case '4': 522 case '5': case '6': case '7': case '8': case '9': 523 n = 0; 524 do { 525 n = 10 * n + to_digit(ch); 526 ch = *fmt++; 527 } while (is_digit(ch)); 528 if (ch == '$') { 529 nextarg = n; 530 if (argtable == NULL) { 531 argtable = statargtable; 532 __find_arguments (fmt0, orgap, 533 &argtable); 534 } 535 goto rflag; 536 } 537 width = n; 538 goto reswitch; 539 #ifdef FLOATING_POINT 540 case 'L': 541 flags |= LONGDBL; 542 goto rflag; 543 #endif 544 case 'h': 545 flags |= SHORTINT; 546 goto rflag; 547 case 'l': 548 flags |= LONGINT; 549 goto rflag; 550 case 'q': 551 flags |= QUADINT; 552 goto rflag; 553 case 'c': 554 *(cp = buf) = GETARG(int); 555 size = 1; 556 sign = '\0'; 557 break; 558 case 'D': 559 flags |= LONGINT; 560 /*FALLTHROUGH*/ 561 case 'd': 562 case 'i': 563 if (flags & QUADINT) { 564 uqval = GETARG(quad_t); 565 if ((quad_t)uqval < 0) { 566 uqval = -uqval; 567 sign = '-'; 568 } 569 } else { 570 ulval = SARG(); 571 if ((long)ulval < 0) { 572 ulval = -ulval; 573 sign = '-'; 574 } 575 } 576 base = 10; 577 goto number; 578 #ifdef FLOATING_POINT 579 case 'e': 580 case 'E': 581 case 'f': 582 goto fp_begin; 583 case 'g': 584 case 'G': 585 if (prec == 0) 586 prec = 1; 587 fp_begin: if (prec == -1) 588 prec = DEFPREC; 589 if (flags & LONGDBL) 590 /* XXX this loses precision. */ 591 _double = (double)GETARG(long double); 592 else 593 _double = GETARG(double); 594 /* do this before tricky precision changes */ 595 if (isinf(_double)) { 596 if (_double < 0) 597 sign = '-'; 598 cp = "Inf"; 599 size = 3; 600 break; 601 } 602 if (isnan(_double)) { 603 cp = "NaN"; 604 size = 3; 605 break; 606 } 607 flags |= FPT; 608 cp = cvt(_double, prec, flags, &softsign, 609 &expt, ch, &ndig); 610 if (ch == 'g' || ch == 'G') { 611 if (expt <= -4 || expt > prec) 612 ch = (ch == 'g') ? 'e' : 'E'; 613 else 614 ch = 'g'; 615 } 616 if (ch <= 'e') { /* 'e' or 'E' fmt */ 617 --expt; 618 expsize = exponent(expstr, expt, ch); 619 size = expsize + ndig; 620 if (ndig > 1 || flags & ALT) 621 ++size; 622 } else if (ch == 'f') { /* f fmt */ 623 if (expt > 0) { 624 size = expt; 625 if (prec || flags & ALT) 626 size += prec + 1; 627 } else /* "0.X" */ 628 size = prec + 2; 629 } else if (expt >= ndig) { /* fixed g fmt */ 630 size = expt; 631 if (flags & ALT) 632 ++size; 633 } else 634 size = ndig + (expt > 0 ? 635 1 : 2 - expt); 636 637 if (softsign) 638 sign = '-'; 639 break; 640 #endif /* FLOATING_POINT */ 641 case 'n': 642 if (flags & QUADINT) 643 *GETARG(quad_t *) = ret; 644 else if (flags & LONGINT) 645 *GETARG(long *) = ret; 646 else if (flags & SHORTINT) 647 *GETARG(short *) = ret; 648 else 649 *GETARG(int *) = ret; 650 continue; /* no output */ 651 case 'O': 652 flags |= LONGINT; 653 /*FALLTHROUGH*/ 654 case 'o': 655 if (flags & QUADINT) 656 uqval = GETARG(u_quad_t); 657 else 658 ulval = UARG(); 659 base = 8; 660 goto nosign; 661 case 'p': 662 /* 663 * ``The argument shall be a pointer to void. The 664 * value of the pointer is converted to a sequence 665 * of printable characters, in an implementation- 666 * defined manner.'' 667 * -- ANSI X3J11 668 */ 669 ulval = (u_long)GETARG(void *); 670 base = 16; 671 xdigs = "0123456789abcdef"; 672 flags = (flags & ~QUADINT) | HEXPREFIX; 673 ch = 'x'; 674 goto nosign; 675 case 's': 676 if ((cp = GETARG(char *)) == NULL) 677 cp = "(null)"; 678 if (prec >= 0) { 679 /* 680 * can't use strlen; can only look for the 681 * NUL in the first `prec' characters, and 682 * strlen() will go further. 683 */ 684 char *p = memchr(cp, 0, (size_t)prec); 685 686 if (p != NULL) { 687 size = p - cp; 688 if (size > prec) 689 size = prec; 690 } else 691 size = prec; 692 } else 693 size = strlen(cp); 694 sign = '\0'; 695 break; 696 case 'U': 697 flags |= LONGINT; 698 /*FALLTHROUGH*/ 699 case 'u': 700 if (flags & QUADINT) 701 uqval = GETARG(u_quad_t); 702 else 703 ulval = UARG(); 704 base = 10; 705 goto nosign; 706 case 'X': 707 xdigs = "0123456789ABCDEF"; 708 goto hex; 709 case 'x': 710 xdigs = "0123456789abcdef"; 711 hex: if (flags & QUADINT) 712 uqval = GETARG(u_quad_t); 713 else 714 ulval = UARG(); 715 base = 16; 716 /* leading 0x/X only if non-zero */ 717 if (flags & ALT && 718 (flags & QUADINT ? uqval != 0 : ulval != 0)) 719 flags |= HEXPREFIX; 720 721 /* unsigned conversions */ 722 nosign: sign = '\0'; 723 /* 724 * ``... diouXx conversions ... if a precision is 725 * specified, the 0 flag will be ignored.'' 726 * -- ANSI X3J11 727 */ 728 number: if ((dprec = prec) >= 0) 729 flags &= ~ZEROPAD; 730 731 /* 732 * ``The result of converting a zero value with an 733 * explicit precision of zero is no characters.'' 734 * -- ANSI X3J11 735 */ 736 cp = buf + BUF; 737 if (flags & QUADINT) { 738 if (uqval != 0 || prec != 0) 739 cp = __uqtoa(uqval, cp, base, 740 flags & ALT, xdigs); 741 } else { 742 if (ulval != 0 || prec != 0) 743 cp = __ultoa(ulval, cp, base, 744 flags & ALT, xdigs); 745 } 746 size = buf + BUF - cp; 747 break; 748 default: /* "%?" prints ?, unless ? is NUL */ 749 if (ch == '\0') 750 goto done; 751 /* pretend it was %c with argument ch */ 752 cp = buf; 753 *cp = ch; 754 size = 1; 755 sign = '\0'; 756 break; 757 } 758 759 /* 760 * All reasonable formats wind up here. At this point, `cp' 761 * points to a string which (if not flags&LADJUST) should be 762 * padded out to `width' places. If flags&ZEROPAD, it should 763 * first be prefixed by any sign or other prefix; otherwise, 764 * it should be blank padded before the prefix is emitted. 765 * After any left-hand padding and prefixing, emit zeroes 766 * required by a decimal [diouxX] precision, then print the 767 * string proper, then emit zeroes required by any leftover 768 * floating precision; finally, if LADJUST, pad with blanks. 769 * 770 * Compute actual size, so we know how much to pad. 771 * size excludes decimal prec; realsz includes it. 772 */ 773 realsz = dprec > size ? dprec : size; 774 if (sign) 775 realsz++; 776 else if (flags & HEXPREFIX) 777 realsz += 2; 778 779 prsize = width > realsz ? width : realsz; 780 if ((unsigned)ret + prsize > INT_MAX) { 781 ret = EOF; 782 goto error; 783 } 784 785 /* right-adjusting blank padding */ 786 if ((flags & (LADJUST|ZEROPAD)) == 0) 787 PAD(width - realsz, blanks); 788 789 /* prefix */ 790 if (sign) { 791 PRINT(&sign, 1); 792 } else if (flags & HEXPREFIX) { 793 ox[0] = '0'; 794 ox[1] = ch; 795 PRINT(ox, 2); 796 } 797 798 /* right-adjusting zero padding */ 799 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 800 PAD(width - realsz, zeroes); 801 802 /* leading zeroes from decimal precision */ 803 PAD(dprec - size, zeroes); 804 805 /* the string or number proper */ 806 #ifdef FLOATING_POINT 807 if ((flags & FPT) == 0) { 808 PRINT(cp, size); 809 } else { /* glue together f_p fragments */ 810 if (ch >= 'f') { /* 'f' or 'g' */ 811 if (_double == 0) { 812 /* kludge for __dtoa irregularity */ 813 if (expt >= ndig && 814 (flags & ALT) == 0) { 815 PRINT("0", 1); 816 } else { 817 PRINT("0.", 2); 818 PAD(ndig - 1, zeroes); 819 } 820 } else if (expt <= 0) { 821 PRINT("0.", 2); 822 PAD(-expt, zeroes); 823 PRINT(cp, ndig); 824 } else if (expt >= ndig) { 825 PRINT(cp, ndig); 826 PAD(expt - ndig, zeroes); 827 if (flags & ALT) 828 PRINT(".", 1); 829 } else { 830 PRINT(cp, expt); 831 cp += expt; 832 PRINT(".", 1); 833 PRINT(cp, ndig-expt); 834 } 835 } else { /* 'e' or 'E' */ 836 if (ndig > 1 || flags & ALT) { 837 ox[0] = *cp++; 838 ox[1] = '.'; 839 PRINT(ox, 2); 840 if (_double) { 841 PRINT(cp, ndig-1); 842 } else /* 0.[0..] */ 843 /* __dtoa irregularity */ 844 PAD(ndig - 1, zeroes); 845 } else /* XeYYY */ 846 PRINT(cp, 1); 847 PRINT(expstr, expsize); 848 } 849 } 850 #else 851 PRINT(cp, size); 852 #endif 853 /* left-adjusting padding (always blank) */ 854 if (flags & LADJUST) 855 PAD(width - realsz, blanks); 856 857 /* finally, adjust ret */ 858 ret += prsize; 859 860 FLUSH(); /* copy out the I/O vectors */ 861 } 862 done: 863 FLUSH(); 864 error: 865 if (__sferror(fp)) 866 ret = EOF; 867 FUNLOCKFILE(fp); 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 */ 901 static 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 975 rflag: ch = *fmt++; 976 reswitch: 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 } 1097 done: 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 switch (typetable [n]) { 1109 case T_UNUSED: 1110 (*argtable) [n] = (void *) &va_arg (ap, int); 1111 break; 1112 case T_SHORT: 1113 (*argtable) [n] = (void *) &va_arg (ap, int); 1114 break; 1115 case T_U_SHORT: 1116 (*argtable) [n] = (void *) &va_arg (ap, int); 1117 break; 1118 case TP_SHORT: 1119 (*argtable) [n] = (void *) &va_arg (ap, short *); 1120 break; 1121 case T_INT: 1122 (*argtable) [n] = (void *) &va_arg (ap, int); 1123 break; 1124 case T_U_INT: 1125 (*argtable) [n] = (void *) &va_arg (ap, unsigned int); 1126 break; 1127 case TP_INT: 1128 (*argtable) [n] = (void *) &va_arg (ap, int *); 1129 break; 1130 case T_LONG: 1131 (*argtable) [n] = (void *) &va_arg (ap, long); 1132 break; 1133 case T_U_LONG: 1134 (*argtable) [n] = (void *) &va_arg (ap, unsigned long); 1135 break; 1136 case TP_LONG: 1137 (*argtable) [n] = (void *) &va_arg (ap, long *); 1138 break; 1139 case T_QUAD: 1140 (*argtable) [n] = (void *) &va_arg (ap, quad_t); 1141 break; 1142 case T_U_QUAD: 1143 (*argtable) [n] = (void *) &va_arg (ap, u_quad_t); 1144 break; 1145 case TP_QUAD: 1146 (*argtable) [n] = (void *) &va_arg (ap, quad_t *); 1147 break; 1148 case T_DOUBLE: 1149 (*argtable) [n] = (void *) &va_arg (ap, double); 1150 break; 1151 case T_LONG_DOUBLE: 1152 (*argtable) [n] = (void *) &va_arg (ap, long double); 1153 break; 1154 case TP_CHAR: 1155 (*argtable) [n] = (void *) &va_arg (ap, char *); 1156 break; 1157 case TP_VOID: 1158 (*argtable) [n] = (void *) &va_arg (ap, void *); 1159 break; 1160 } 1161 } 1162 1163 if ((typetable != NULL) && (typetable != stattypetable)) 1164 free (typetable); 1165 } 1166 1167 /* 1168 * Increase the size of the type table. 1169 */ 1170 static void 1171 __grow_type_table (nextarg, typetable, tablesize) 1172 int nextarg; 1173 unsigned char **typetable; 1174 int *tablesize; 1175 { 1176 unsigned char *oldtable = *typetable; 1177 int newsize = *tablesize * 2; 1178 1179 if (*tablesize == STATIC_ARG_TBL_SIZE) { 1180 *typetable = (unsigned char *) 1181 malloc (sizeof (unsigned char) * newsize); 1182 bcopy (oldtable, *typetable, *tablesize); 1183 } else { 1184 *typetable = (unsigned char *) 1185 reallocf (typetable, sizeof (unsigned char) * newsize); 1186 1187 } 1188 memset (&typetable [*tablesize], T_UNUSED, (newsize - *tablesize)); 1189 1190 *tablesize = newsize; 1191 } 1192 1193 1194 #ifdef FLOATING_POINT 1195 1196 extern char *__dtoa __P((double, int, int, int *, int *, char **)); 1197 1198 static char * 1199 cvt(value, ndigits, flags, sign, decpt, ch, length) 1200 double value; 1201 int ndigits, flags, *decpt, ch, *length; 1202 char *sign; 1203 { 1204 int mode, dsgn; 1205 char *digits, *bp, *rve; 1206 1207 if (ch == 'f') 1208 mode = 3; /* ndigits after the decimal point */ 1209 else { 1210 /* 1211 * To obtain ndigits after the decimal point for the 'e' 1212 * and 'E' formats, round to ndigits + 1 significant 1213 * figures. 1214 */ 1215 if (ch == 'e' || ch == 'E') 1216 ndigits++; 1217 mode = 2; /* ndigits significant digits */ 1218 } 1219 if (value < 0) { 1220 value = -value; 1221 *sign = '-'; 1222 } else 1223 *sign = '\000'; 1224 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve); 1225 if ((ch != 'g' && ch != 'G') || flags & ALT) { 1226 /* print trailing zeros */ 1227 bp = digits + ndigits; 1228 if (ch == 'f') { 1229 if (*digits == '0' && value) 1230 *decpt = -ndigits + 1; 1231 bp += *decpt; 1232 } 1233 if (value == 0) /* kludge for __dtoa irregularity */ 1234 rve = bp; 1235 while (rve < bp) 1236 *rve++ = '0'; 1237 } 1238 *length = rve - digits; 1239 return (digits); 1240 } 1241 1242 static int 1243 exponent(p0, exp, fmtch) 1244 char *p0; 1245 int exp, fmtch; 1246 { 1247 register char *p, *t; 1248 char expbuf[MAXEXP]; 1249 1250 p = p0; 1251 *p++ = fmtch; 1252 if (exp < 0) { 1253 exp = -exp; 1254 *p++ = '-'; 1255 } 1256 else 1257 *p++ = '+'; 1258 t = expbuf + MAXEXP; 1259 if (exp > 9) { 1260 do { 1261 *--t = to_char(exp % 10); 1262 } while ((exp /= 10) > 9); 1263 *--t = to_char(exp); 1264 for (; t < expbuf + MAXEXP; *p++ = *t++); 1265 } 1266 else { 1267 *p++ = '0'; 1268 *p++ = to_char(exp); 1269 } 1270 return (p - p0); 1271 } 1272 #endif /* FLOATING_POINT */ 1273