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 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 39 #endif /* LIBC_SCCS and not lint */ 40 #include <sys/cdefs.h> 41 __FBSDID("$FreeBSD$"); 42 43 /* 44 * Actual printf innards. 45 * 46 * This code is large and complicated... 47 */ 48 49 #include "namespace.h" 50 #include <sys/types.h> 51 52 #include <ctype.h> 53 #include <limits.h> 54 #include <locale.h> 55 #include <stddef.h> 56 #include <stdint.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <wchar.h> 61 #include <printf.h> 62 63 #include <stdarg.h> 64 #include "un-namespace.h" 65 66 #include "libc_private.h" 67 #include "local.h" 68 #include "fvwrite.h" 69 70 union arg { 71 int intarg; 72 u_int uintarg; 73 long longarg; 74 u_long ulongarg; 75 long long longlongarg; 76 unsigned long long ulonglongarg; 77 ptrdiff_t ptrdiffarg; 78 size_t sizearg; 79 intmax_t intmaxarg; 80 uintmax_t uintmaxarg; 81 void *pvoidarg; 82 char *pchararg; 83 signed char *pschararg; 84 short *pshortarg; 85 int *pintarg; 86 long *plongarg; 87 long long *plonglongarg; 88 ptrdiff_t *pptrdiffarg; 89 size_t *psizearg; 90 intmax_t *pintmaxarg; 91 #ifndef NO_FLOATING_POINT 92 double doublearg; 93 long double longdoublearg; 94 #endif 95 wint_t wintarg; 96 wchar_t *pwchararg; 97 }; 98 99 /* 100 * Type ids for argument type table. 101 */ 102 enum typeid { 103 T_UNUSED, TP_SHORT, T_INT, T_U_INT, TP_INT, 104 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG, 105 T_PTRDIFFT, TP_PTRDIFFT, T_SIZET, TP_SIZET, 106 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR, 107 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR 108 }; 109 110 static int __sprint(FILE *, struct __suio *); 111 static int __sbprintf(FILE *, const char *, va_list) __printflike(2, 0); 112 static char *__ujtoa(uintmax_t, char *, int, int, const char *, int, char, 113 const char *); 114 static char *__ultoa(u_long, char *, int, int, const char *, int, char, 115 const char *); 116 static char *__wcsconv(wchar_t *, int); 117 static void __find_arguments(const char *, va_list, union arg **); 118 static void __grow_type_table(int, enum typeid **, int *); 119 120 /* 121 * Flush out all the vectors defined by the given uio, 122 * then reset it so that it can be reused. 123 */ 124 static int 125 __sprint(FILE *fp, struct __suio *uio) 126 { 127 int err; 128 129 if (uio->uio_resid == 0) { 130 uio->uio_iovcnt = 0; 131 return (0); 132 } 133 err = __sfvwrite(fp, uio); 134 uio->uio_resid = 0; 135 uio->uio_iovcnt = 0; 136 return (err); 137 } 138 139 /* 140 * Helper function for `fprintf to unbuffered unix file': creates a 141 * temporary buffer. We only work on write-only files; this avoids 142 * worries about ungetc buffers and so forth. 143 */ 144 static int 145 __sbprintf(FILE *fp, const char *fmt, va_list ap) 146 { 147 int ret; 148 FILE fake; 149 unsigned char buf[BUFSIZ]; 150 151 /* copy the important variables */ 152 fake._flags = fp->_flags & ~__SNBF; 153 fake._file = fp->_file; 154 fake._cookie = fp->_cookie; 155 fake._write = fp->_write; 156 fake._extra = fp->_extra; 157 158 /* set up the buffer */ 159 fake._bf._base = fake._p = buf; 160 fake._bf._size = fake._w = sizeof(buf); 161 fake._lbfsize = 0; /* not actually used, but Just In Case */ 162 163 /* do the work, then copy any error status */ 164 ret = __vfprintf(&fake, fmt, ap); 165 if (ret >= 0 && __fflush(&fake)) 166 ret = EOF; 167 if (fake._flags & __SERR) 168 fp->_flags |= __SERR; 169 return (ret); 170 } 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) ((n) + '0') 178 179 /* 180 * Convert an unsigned long to ASCII for printf purposes, returning 181 * a pointer to the first character of the string representation. 182 * Octal numbers can be forced to have a leading zero; hex numbers 183 * use the given digits. 184 */ 185 static char * 186 __ultoa(u_long val, char *endp, int base, int octzero, const char *xdigs, 187 int needgrp, char thousep, const char *grp) 188 { 189 char *cp = endp; 190 long sval; 191 int ndig; 192 193 /* 194 * Handle the three cases separately, in the hope of getting 195 * better/faster code. 196 */ 197 switch (base) { 198 case 10: 199 if (val < 10) { /* many numbers are 1 digit */ 200 *--cp = to_char(val); 201 return (cp); 202 } 203 ndig = 0; 204 /* 205 * On many machines, unsigned arithmetic is harder than 206 * signed arithmetic, so we do at most one unsigned mod and 207 * divide; this is sufficient to reduce the range of 208 * the incoming value to where signed arithmetic works. 209 */ 210 if (val > LONG_MAX) { 211 *--cp = to_char(val % 10); 212 ndig++; 213 sval = val / 10; 214 } else 215 sval = val; 216 do { 217 *--cp = to_char(sval % 10); 218 ndig++; 219 /* 220 * If (*grp == CHAR_MAX) then no more grouping 221 * should be performed. 222 */ 223 if (needgrp && ndig == *grp && *grp != CHAR_MAX 224 && sval > 9) { 225 *--cp = thousep; 226 ndig = 0; 227 /* 228 * If (*(grp+1) == '\0') then we have to 229 * use *grp character (last grouping rule) 230 * for all next cases 231 */ 232 if (*(grp+1) != '\0') 233 grp++; 234 } 235 sval /= 10; 236 } while (sval != 0); 237 break; 238 239 case 8: 240 do { 241 *--cp = to_char(val & 7); 242 val >>= 3; 243 } while (val); 244 if (octzero && *cp != '0') 245 *--cp = '0'; 246 break; 247 248 case 16: 249 do { 250 *--cp = xdigs[val & 15]; 251 val >>= 4; 252 } while (val); 253 break; 254 255 default: /* oops */ 256 abort(); 257 } 258 return (cp); 259 } 260 261 /* Identical to __ultoa, but for intmax_t. */ 262 static char * 263 __ujtoa(uintmax_t val, char *endp, int base, int octzero, const char *xdigs, 264 int needgrp, char thousep, const char *grp) 265 { 266 char *cp = endp; 267 intmax_t sval; 268 int ndig; 269 270 /* quick test for small values; __ultoa is typically much faster */ 271 /* (perhaps instead we should run until small, then call __ultoa?) */ 272 if (val <= ULONG_MAX) 273 return (__ultoa((u_long)val, endp, base, octzero, xdigs, 274 needgrp, thousep, grp)); 275 switch (base) { 276 case 10: 277 if (val < 10) { 278 *--cp = to_char(val % 10); 279 return (cp); 280 } 281 ndig = 0; 282 if (val > INTMAX_MAX) { 283 *--cp = to_char(val % 10); 284 ndig++; 285 sval = val / 10; 286 } else 287 sval = val; 288 do { 289 *--cp = to_char(sval % 10); 290 ndig++; 291 /* 292 * If (*grp == CHAR_MAX) then no more grouping 293 * should be performed. 294 */ 295 if (needgrp && *grp != CHAR_MAX && ndig == *grp 296 && sval > 9) { 297 *--cp = thousep; 298 ndig = 0; 299 /* 300 * If (*(grp+1) == '\0') then we have to 301 * use *grp character (last grouping rule) 302 * for all next cases 303 */ 304 if (*(grp+1) != '\0') 305 grp++; 306 } 307 sval /= 10; 308 } while (sval != 0); 309 break; 310 311 case 8: 312 do { 313 *--cp = to_char(val & 7); 314 val >>= 3; 315 } while (val); 316 if (octzero && *cp != '0') 317 *--cp = '0'; 318 break; 319 320 case 16: 321 do { 322 *--cp = xdigs[val & 15]; 323 val >>= 4; 324 } while (val); 325 break; 326 327 default: 328 abort(); 329 } 330 return (cp); 331 } 332 333 /* 334 * Convert a wide character string argument for the %ls format to a multibyte 335 * string representation. If not -1, prec specifies the maximum number of 336 * bytes to output, and also means that we can't assume that the wide char. 337 * string ends is null-terminated. 338 */ 339 static char * 340 __wcsconv(wchar_t *wcsarg, int prec) 341 { 342 static const mbstate_t initial; 343 mbstate_t mbs; 344 char buf[MB_LEN_MAX]; 345 wchar_t *p; 346 char *convbuf; 347 size_t clen, nbytes; 348 349 /* Allocate space for the maximum number of bytes we could output. */ 350 if (prec < 0) { 351 p = wcsarg; 352 mbs = initial; 353 nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs); 354 if (nbytes == (size_t)-1) 355 return (NULL); 356 } else { 357 /* 358 * Optimisation: if the output precision is small enough, 359 * just allocate enough memory for the maximum instead of 360 * scanning the string. 361 */ 362 if (prec < 128) 363 nbytes = prec; 364 else { 365 nbytes = 0; 366 p = wcsarg; 367 mbs = initial; 368 for (;;) { 369 clen = wcrtomb(buf, *p++, &mbs); 370 if (clen == 0 || clen == (size_t)-1 || 371 nbytes + clen > prec) 372 break; 373 nbytes += clen; 374 } 375 } 376 } 377 if ((convbuf = malloc(nbytes + 1)) == NULL) 378 return (NULL); 379 380 /* Fill the output buffer. */ 381 p = wcsarg; 382 mbs = initial; 383 if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p, 384 nbytes, &mbs)) == (size_t)-1) { 385 free(convbuf); 386 return (NULL); 387 } 388 convbuf[nbytes] = '\0'; 389 return (convbuf); 390 } 391 392 /* 393 * MT-safe version 394 */ 395 int 396 vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap) 397 398 { 399 int ret; 400 401 FLOCKFILE(fp); 402 ret = __vfprintf(fp, fmt0, ap); 403 FUNLOCKFILE(fp); 404 return (ret); 405 } 406 407 #ifndef NO_FLOATING_POINT 408 409 #define dtoa __dtoa 410 #define freedtoa __freedtoa 411 412 #include <float.h> 413 #include <math.h> 414 #include "floatio.h" 415 #include "gdtoa.h" 416 417 #define DEFPREC 6 418 419 static int exponent(char *, int, int); 420 421 #endif /* !NO_FLOATING_POINT */ 422 423 /* 424 * The size of the buffer we use as scratch space for integer 425 * conversions, among other things. Technically, we would need the 426 * most space for base 10 conversions with thousands' grouping 427 * characters between each pair of digits. 100 bytes is a 428 * conservative overestimate even for a 128-bit uintmax_t. 429 */ 430 #define BUF 100 431 432 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ 433 434 /* 435 * Flags used during conversion. 436 */ 437 #define ALT 0x001 /* alternate form */ 438 #define LADJUST 0x004 /* left adjustment */ 439 #define LONGDBL 0x008 /* long double */ 440 #define LONGINT 0x010 /* long integer */ 441 #define LLONGINT 0x020 /* long long integer */ 442 #define SHORTINT 0x040 /* short integer */ 443 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 444 #define FPT 0x100 /* Floating point number */ 445 #define GROUPING 0x200 /* use grouping ("'" flag) */ 446 /* C99 additional size modifiers: */ 447 #define SIZET 0x400 /* size_t */ 448 #define PTRDIFFT 0x800 /* ptrdiff_t */ 449 #define INTMAXT 0x1000 /* intmax_t */ 450 #define CHARINT 0x2000 /* print char using int format */ 451 452 /* 453 * Non-MT-safe version 454 */ 455 int 456 __vfprintf(FILE *fp, const char *fmt0, va_list ap) 457 { 458 char *fmt; /* format string */ 459 int ch; /* character from fmt */ 460 int n, n2; /* handy integer (short term usage) */ 461 char *cp; /* handy char pointer (short term usage) */ 462 struct __siov *iovp; /* for PRINT macro */ 463 int flags; /* flags as above */ 464 int ret; /* return value accumulator */ 465 int width; /* width from format (%8d), or 0 */ 466 int prec; /* precision from format; <0 for N/A */ 467 char sign; /* sign prefix (' ', '+', '-', or \0) */ 468 char thousands_sep; /* locale specific thousands separator */ 469 const char *grouping; /* locale specific numeric grouping rules */ 470 471 if (__use_xprintf == 0 && getenv("USE_XPRINTF")) 472 __use_xprintf = 1; 473 if (__use_xprintf > 0) 474 return (__xvprintf(fp, fmt0, ap)); 475 476 #ifndef NO_FLOATING_POINT 477 /* 478 * We can decompose the printed representation of floating 479 * point numbers into several parts, some of which may be empty: 480 * 481 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ 482 * A B ---C--- D E F 483 * 484 * A: 'sign' holds this value if present; '\0' otherwise 485 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal 486 * C: cp points to the string MMMNNN. Leading and trailing 487 * zeros are not in the string and must be added. 488 * D: expchar holds this character; '\0' if no exponent, e.g. %f 489 * F: at least two digits for decimal, at least one digit for hex 490 */ 491 char *decimal_point; /* locale specific decimal point */ 492 int signflag; /* true if float is negative */ 493 union { /* floating point arguments %[aAeEfFgG] */ 494 double dbl; 495 long double ldbl; 496 } fparg; 497 int expt; /* integer value of exponent */ 498 char expchar; /* exponent character: [eEpP\0] */ 499 char *dtoaend; /* pointer to end of converted digits */ 500 int expsize; /* character count for expstr */ 501 int lead; /* sig figs before decimal or group sep */ 502 int ndig; /* actual number of digits returned by dtoa */ 503 char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ 504 char *dtoaresult; /* buffer allocated by dtoa */ 505 int nseps; /* number of group separators with ' */ 506 int nrepeats; /* number of repeats of the last group */ 507 #endif 508 u_long ulval; /* integer arguments %[diouxX] */ 509 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 510 int base; /* base for [diouxX] conversion */ 511 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 512 int realsz; /* field size expanded by dprec, sign, etc */ 513 int size; /* size of converted field or string */ 514 int prsize; /* max size of printed field */ 515 const char *xdigs; /* digits for %[xX] conversion */ 516 #define NIOV 8 517 struct __suio uio; /* output information: summary */ 518 struct __siov iov[NIOV];/* ... and individual io vectors */ 519 char buf[BUF]; /* buffer with space for digits of uintmax_t */ 520 char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ 521 union arg *argtable; /* args, built due to positional arg */ 522 union arg statargtable [STATIC_ARG_TBL_SIZE]; 523 int nextarg; /* 1-based argument index */ 524 va_list orgap; /* original argument pointer */ 525 char *convbuf; /* wide to multibyte conversion result */ 526 527 /* 528 * Choose PADSIZE to trade efficiency vs. size. If larger printf 529 * fields occur frequently, increase PADSIZE and make the initialisers 530 * below longer. 531 */ 532 #define PADSIZE 16 /* pad chunk size */ 533 static char blanks[PADSIZE] = 534 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 535 static char zeroes[PADSIZE] = 536 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 537 538 static const char xdigs_lower[17] = "0123456789abcdef?"; 539 static const char xdigs_upper[17] = "0123456789ABCDEF?"; 540 541 /* 542 * BEWARE, these `goto error' on error, and PAD uses `n'. 543 */ 544 #define PRINT(ptr, len) { \ 545 iovp->iov_base = (ptr); \ 546 iovp->iov_len = (len); \ 547 uio.uio_resid += (len); \ 548 iovp++; \ 549 if (++uio.uio_iovcnt >= NIOV) { \ 550 if (__sprint(fp, &uio)) \ 551 goto error; \ 552 iovp = iov; \ 553 } \ 554 } 555 #define PAD(howmany, with) { \ 556 if ((n = (howmany)) > 0) { \ 557 while (n > PADSIZE) { \ 558 PRINT(with, PADSIZE); \ 559 n -= PADSIZE; \ 560 } \ 561 PRINT(with, n); \ 562 } \ 563 } 564 #define PRINTANDPAD(p, ep, len, with) do { \ 565 n2 = (ep) - (p); \ 566 if (n2 > (len)) \ 567 n2 = (len); \ 568 if (n2 > 0) \ 569 PRINT((p), n2); \ 570 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \ 571 } while(0) 572 #define FLUSH() { \ 573 if (uio.uio_resid && __sprint(fp, &uio)) \ 574 goto error; \ 575 uio.uio_iovcnt = 0; \ 576 iovp = iov; \ 577 } 578 579 /* 580 * Get the argument indexed by nextarg. If the argument table is 581 * built, use it to get the argument. If its not, get the next 582 * argument (and arguments must be gotten sequentially). 583 */ 584 #define GETARG(type) \ 585 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ 586 (nextarg++, va_arg(ap, type))) 587 588 /* 589 * To extend shorts properly, we need both signed and unsigned 590 * argument extraction methods. 591 */ 592 #define SARG() \ 593 (flags&LONGINT ? GETARG(long) : \ 594 flags&SHORTINT ? (long)(short)GETARG(int) : \ 595 flags&CHARINT ? (long)(signed char)GETARG(int) : \ 596 (long)GETARG(int)) 597 #define UARG() \ 598 (flags&LONGINT ? GETARG(u_long) : \ 599 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 600 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 601 (u_long)GETARG(u_int)) 602 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 603 #define SJARG() \ 604 (flags&INTMAXT ? GETARG(intmax_t) : \ 605 flags&SIZET ? (intmax_t)GETARG(size_t) : \ 606 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 607 (intmax_t)GETARG(long long)) 608 #define UJARG() \ 609 (flags&INTMAXT ? GETARG(uintmax_t) : \ 610 flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 611 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \ 612 (uintmax_t)GETARG(unsigned long long)) 613 614 /* 615 * Get * arguments, including the form *nn$. Preserve the nextarg 616 * that the argument can be gotten once the type is determined. 617 */ 618 #define GETASTER(val) \ 619 n2 = 0; \ 620 cp = fmt; \ 621 while (is_digit(*cp)) { \ 622 n2 = 10 * n2 + to_digit(*cp); \ 623 cp++; \ 624 } \ 625 if (*cp == '$') { \ 626 int hold = nextarg; \ 627 if (argtable == NULL) { \ 628 argtable = statargtable; \ 629 __find_arguments (fmt0, orgap, &argtable); \ 630 } \ 631 nextarg = n2; \ 632 val = GETARG (int); \ 633 nextarg = hold; \ 634 fmt = ++cp; \ 635 } else { \ 636 val = GETARG (int); \ 637 } 638 639 640 thousands_sep = '\0'; 641 grouping = NULL; 642 convbuf = NULL; 643 #ifndef NO_FLOATING_POINT 644 dtoaresult = NULL; 645 decimal_point = localeconv()->decimal_point; 646 #endif 647 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 648 if (prepwrite(fp) != 0) 649 return (EOF); 650 651 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 652 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 653 fp->_file >= 0) 654 return (__sbprintf(fp, fmt0, ap)); 655 656 fmt = (char *)fmt0; 657 argtable = NULL; 658 nextarg = 1; 659 va_copy(orgap, ap); 660 uio.uio_iov = iovp = iov; 661 uio.uio_resid = 0; 662 uio.uio_iovcnt = 0; 663 ret = 0; 664 665 /* 666 * Scan the format for conversions (`%' character). 667 */ 668 for (;;) { 669 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 670 /* void */; 671 if ((n = fmt - cp) != 0) { 672 if ((unsigned)ret + n > INT_MAX) { 673 ret = EOF; 674 goto error; 675 } 676 PRINT(cp, n); 677 ret += n; 678 } 679 if (ch == '\0') 680 goto done; 681 fmt++; /* skip over '%' */ 682 683 flags = 0; 684 dprec = 0; 685 width = 0; 686 prec = -1; 687 sign = '\0'; 688 ox[1] = '\0'; 689 690 rflag: ch = *fmt++; 691 reswitch: switch (ch) { 692 case ' ': 693 /*- 694 * ``If the space and + flags both appear, the space 695 * flag will be ignored.'' 696 * -- ANSI X3J11 697 */ 698 if (!sign) 699 sign = ' '; 700 goto rflag; 701 case '#': 702 flags |= ALT; 703 goto rflag; 704 case '*': 705 /*- 706 * ``A negative field width argument is taken as a 707 * - flag followed by a positive field width.'' 708 * -- ANSI X3J11 709 * They don't exclude field widths read from args. 710 */ 711 GETASTER (width); 712 if (width >= 0) 713 goto rflag; 714 width = -width; 715 /* FALLTHROUGH */ 716 case '-': 717 flags |= LADJUST; 718 goto rflag; 719 case '+': 720 sign = '+'; 721 goto rflag; 722 case '\'': 723 flags |= GROUPING; 724 thousands_sep = *(localeconv()->thousands_sep); 725 grouping = localeconv()->grouping; 726 goto rflag; 727 case '.': 728 if ((ch = *fmt++) == '*') { 729 GETASTER (prec); 730 goto rflag; 731 } 732 prec = 0; 733 while (is_digit(ch)) { 734 prec = 10 * prec + to_digit(ch); 735 ch = *fmt++; 736 } 737 goto reswitch; 738 case '0': 739 /*- 740 * ``Note that 0 is taken as a flag, not as the 741 * beginning of a field width.'' 742 * -- ANSI X3J11 743 */ 744 flags |= ZEROPAD; 745 goto rflag; 746 case '1': case '2': case '3': case '4': 747 case '5': case '6': case '7': case '8': case '9': 748 n = 0; 749 do { 750 n = 10 * n + to_digit(ch); 751 ch = *fmt++; 752 } while (is_digit(ch)); 753 if (ch == '$') { 754 nextarg = n; 755 if (argtable == NULL) { 756 argtable = statargtable; 757 __find_arguments (fmt0, orgap, 758 &argtable); 759 } 760 goto rflag; 761 } 762 width = n; 763 goto reswitch; 764 #ifndef NO_FLOATING_POINT 765 case 'L': 766 flags |= LONGDBL; 767 goto rflag; 768 #endif 769 case 'h': 770 if (flags & SHORTINT) { 771 flags &= ~SHORTINT; 772 flags |= CHARINT; 773 } else 774 flags |= SHORTINT; 775 goto rflag; 776 case 'j': 777 flags |= INTMAXT; 778 goto rflag; 779 case 'l': 780 if (flags & LONGINT) { 781 flags &= ~LONGINT; 782 flags |= LLONGINT; 783 } else 784 flags |= LONGINT; 785 goto rflag; 786 case 'q': 787 flags |= LLONGINT; /* not necessarily */ 788 goto rflag; 789 case 't': 790 flags |= PTRDIFFT; 791 goto rflag; 792 case 'z': 793 flags |= SIZET; 794 goto rflag; 795 case 'C': 796 flags |= LONGINT; 797 /*FALLTHROUGH*/ 798 case 'c': 799 if (flags & LONGINT) { 800 static const mbstate_t initial; 801 mbstate_t mbs; 802 size_t mbseqlen; 803 804 mbs = initial; 805 mbseqlen = wcrtomb(cp = buf, 806 (wchar_t)GETARG(wint_t), &mbs); 807 if (mbseqlen == (size_t)-1) { 808 fp->_flags |= __SERR; 809 goto error; 810 } 811 size = (int)mbseqlen; 812 } else { 813 *(cp = buf) = GETARG(int); 814 size = 1; 815 } 816 sign = '\0'; 817 break; 818 case 'D': 819 flags |= LONGINT; 820 /*FALLTHROUGH*/ 821 case 'd': 822 case 'i': 823 if (flags & INTMAX_SIZE) { 824 ujval = SJARG(); 825 if ((intmax_t)ujval < 0) { 826 ujval = -ujval; 827 sign = '-'; 828 } 829 } else { 830 ulval = SARG(); 831 if ((long)ulval < 0) { 832 ulval = -ulval; 833 sign = '-'; 834 } 835 } 836 base = 10; 837 goto number; 838 #ifndef NO_FLOATING_POINT 839 case 'a': 840 case 'A': 841 if (ch == 'a') { 842 ox[1] = 'x'; 843 xdigs = xdigs_lower; 844 expchar = 'p'; 845 } else { 846 ox[1] = 'X'; 847 xdigs = xdigs_upper; 848 expchar = 'P'; 849 } 850 if (prec >= 0) 851 prec++; 852 if (dtoaresult != NULL) 853 freedtoa(dtoaresult); 854 if (flags & LONGDBL) { 855 fparg.ldbl = GETARG(long double); 856 dtoaresult = cp = 857 __hldtoa(fparg.ldbl, xdigs, prec, 858 &expt, &signflag, &dtoaend); 859 } else { 860 fparg.dbl = GETARG(double); 861 dtoaresult = cp = 862 __hdtoa(fparg.dbl, xdigs, prec, 863 &expt, &signflag, &dtoaend); 864 } 865 if (prec < 0) 866 prec = dtoaend - cp; 867 if (expt == INT_MAX) 868 ox[1] = '\0'; 869 goto fp_common; 870 case 'e': 871 case 'E': 872 expchar = ch; 873 if (prec < 0) /* account for digit before decpt */ 874 prec = DEFPREC + 1; 875 else 876 prec++; 877 goto fp_begin; 878 case 'f': 879 case 'F': 880 expchar = '\0'; 881 goto fp_begin; 882 case 'g': 883 case 'G': 884 expchar = ch - ('g' - 'e'); 885 if (prec == 0) 886 prec = 1; 887 fp_begin: 888 if (prec < 0) 889 prec = DEFPREC; 890 if (dtoaresult != NULL) 891 freedtoa(dtoaresult); 892 if (flags & LONGDBL) { 893 fparg.ldbl = GETARG(long double); 894 dtoaresult = cp = 895 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, 896 &expt, &signflag, &dtoaend); 897 } else { 898 fparg.dbl = GETARG(double); 899 dtoaresult = cp = 900 dtoa(fparg.dbl, expchar ? 2 : 3, prec, 901 &expt, &signflag, &dtoaend); 902 if (expt == 9999) 903 expt = INT_MAX; 904 } 905 fp_common: 906 if (signflag) 907 sign = '-'; 908 if (expt == INT_MAX) { /* inf or nan */ 909 if (*cp == 'N') { 910 cp = (ch >= 'a') ? "nan" : "NAN"; 911 sign = '\0'; 912 } else 913 cp = (ch >= 'a') ? "inf" : "INF"; 914 size = 3; 915 break; 916 } 917 flags |= FPT; 918 ndig = dtoaend - cp; 919 if (ch == 'g' || ch == 'G') { 920 if (expt > -4 && expt <= prec) { 921 /* Make %[gG] smell like %[fF] */ 922 expchar = '\0'; 923 if (flags & ALT) 924 prec -= expt; 925 else 926 prec = ndig - expt; 927 if (prec < 0) 928 prec = 0; 929 } else { 930 /* 931 * Make %[gG] smell like %[eE], but 932 * trim trailing zeroes if no # flag. 933 */ 934 if (!(flags & ALT)) 935 prec = ndig; 936 } 937 } 938 if (expchar) { 939 expsize = exponent(expstr, expt - 1, expchar); 940 size = expsize + prec; 941 if (prec > 1 || flags & ALT) 942 ++size; 943 } else { 944 /* space for digits before decimal point */ 945 if (expt > 0) 946 size = expt; 947 else /* "0" */ 948 size = 1; 949 /* space for decimal pt and following digits */ 950 if (prec || flags & ALT) 951 size += prec + 1; 952 if (grouping && expt > 0) { 953 /* space for thousands' grouping */ 954 nseps = nrepeats = 0; 955 lead = expt; 956 while (*grouping != CHAR_MAX) { 957 if (lead <= *grouping) 958 break; 959 lead -= *grouping; 960 if (*(grouping+1)) { 961 nseps++; 962 grouping++; 963 } else 964 nrepeats++; 965 } 966 size += nseps + nrepeats; 967 } else 968 lead = expt; 969 } 970 break; 971 #endif /* !NO_FLOATING_POINT */ 972 case 'n': 973 /* 974 * Assignment-like behavior is specified if the 975 * value overflows or is otherwise unrepresentable. 976 * C99 says to use `signed char' for %hhn conversions. 977 */ 978 if (flags & LLONGINT) 979 *GETARG(long long *) = ret; 980 else if (flags & SIZET) 981 *GETARG(ssize_t *) = (ssize_t)ret; 982 else if (flags & PTRDIFFT) 983 *GETARG(ptrdiff_t *) = ret; 984 else if (flags & INTMAXT) 985 *GETARG(intmax_t *) = ret; 986 else if (flags & LONGINT) 987 *GETARG(long *) = ret; 988 else if (flags & SHORTINT) 989 *GETARG(short *) = ret; 990 else if (flags & CHARINT) 991 *GETARG(signed char *) = ret; 992 else 993 *GETARG(int *) = ret; 994 continue; /* no output */ 995 case 'O': 996 flags |= LONGINT; 997 /*FALLTHROUGH*/ 998 case 'o': 999 if (flags & INTMAX_SIZE) 1000 ujval = UJARG(); 1001 else 1002 ulval = UARG(); 1003 base = 8; 1004 goto nosign; 1005 case 'p': 1006 /*- 1007 * ``The argument shall be a pointer to void. The 1008 * value of the pointer is converted to a sequence 1009 * of printable characters, in an implementation- 1010 * defined manner.'' 1011 * -- ANSI X3J11 1012 */ 1013 ujval = (uintmax_t)(uintptr_t)GETARG(void *); 1014 base = 16; 1015 xdigs = xdigs_lower; 1016 flags = flags | INTMAXT; 1017 ox[1] = 'x'; 1018 goto nosign; 1019 case 'S': 1020 flags |= LONGINT; 1021 /*FALLTHROUGH*/ 1022 case 's': 1023 if (flags & LONGINT) { 1024 wchar_t *wcp; 1025 1026 if (convbuf != NULL) 1027 free(convbuf); 1028 if ((wcp = GETARG(wchar_t *)) == NULL) 1029 cp = "(null)"; 1030 else { 1031 convbuf = __wcsconv(wcp, prec); 1032 if (convbuf == NULL) { 1033 fp->_flags |= __SERR; 1034 goto error; 1035 } 1036 cp = convbuf; 1037 } 1038 } else if ((cp = GETARG(char *)) == NULL) 1039 cp = "(null)"; 1040 if (prec >= 0) { 1041 /* 1042 * can't use strlen; can only look for the 1043 * NUL in the first `prec' characters, and 1044 * strlen() will go further. 1045 */ 1046 char *p = memchr(cp, 0, (size_t)prec); 1047 1048 if (p != NULL) { 1049 size = p - cp; 1050 if (size > prec) 1051 size = prec; 1052 } else 1053 size = prec; 1054 } else 1055 size = strlen(cp); 1056 sign = '\0'; 1057 break; 1058 case 'U': 1059 flags |= LONGINT; 1060 /*FALLTHROUGH*/ 1061 case 'u': 1062 if (flags & INTMAX_SIZE) 1063 ujval = UJARG(); 1064 else 1065 ulval = UARG(); 1066 base = 10; 1067 goto nosign; 1068 case 'X': 1069 xdigs = xdigs_upper; 1070 goto hex; 1071 case 'x': 1072 xdigs = xdigs_lower; 1073 hex: 1074 if (flags & INTMAX_SIZE) 1075 ujval = UJARG(); 1076 else 1077 ulval = UARG(); 1078 base = 16; 1079 /* leading 0x/X only if non-zero */ 1080 if (flags & ALT && 1081 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 1082 ox[1] = ch; 1083 1084 flags &= ~GROUPING; 1085 /* unsigned conversions */ 1086 nosign: sign = '\0'; 1087 /*- 1088 * ``... diouXx conversions ... if a precision is 1089 * specified, the 0 flag will be ignored.'' 1090 * -- ANSI X3J11 1091 */ 1092 number: if ((dprec = prec) >= 0) 1093 flags &= ~ZEROPAD; 1094 1095 /*- 1096 * ``The result of converting a zero value with an 1097 * explicit precision of zero is no characters.'' 1098 * -- ANSI X3J11 1099 * 1100 * ``The C Standard is clear enough as is. The call 1101 * printf("%#.0o", 0) should print 0.'' 1102 * -- Defect Report #151 1103 */ 1104 cp = buf + BUF; 1105 if (flags & INTMAX_SIZE) { 1106 if (ujval != 0 || prec != 0 || 1107 (flags & ALT && base == 8)) 1108 cp = __ujtoa(ujval, cp, base, 1109 flags & ALT, xdigs, 1110 flags & GROUPING, thousands_sep, 1111 grouping); 1112 } else { 1113 if (ulval != 0 || prec != 0 || 1114 (flags & ALT && base == 8)) 1115 cp = __ultoa(ulval, cp, base, 1116 flags & ALT, xdigs, 1117 flags & GROUPING, thousands_sep, 1118 grouping); 1119 } 1120 size = buf + BUF - cp; 1121 if (size > BUF) /* should never happen */ 1122 abort(); 1123 break; 1124 default: /* "%?" prints ?, unless ? is NUL */ 1125 if (ch == '\0') 1126 goto done; 1127 /* pretend it was %c with argument ch */ 1128 cp = buf; 1129 *cp = ch; 1130 size = 1; 1131 sign = '\0'; 1132 break; 1133 } 1134 1135 /* 1136 * All reasonable formats wind up here. At this point, `cp' 1137 * points to a string which (if not flags&LADJUST) should be 1138 * padded out to `width' places. If flags&ZEROPAD, it should 1139 * first be prefixed by any sign or other prefix; otherwise, 1140 * it should be blank padded before the prefix is emitted. 1141 * After any left-hand padding and prefixing, emit zeroes 1142 * required by a decimal [diouxX] precision, then print the 1143 * string proper, then emit zeroes required by any leftover 1144 * floating precision; finally, if LADJUST, pad with blanks. 1145 * 1146 * Compute actual size, so we know how much to pad. 1147 * size excludes decimal prec; realsz includes it. 1148 */ 1149 realsz = dprec > size ? dprec : size; 1150 if (sign) 1151 realsz++; 1152 if (ox[1]) 1153 realsz += 2; 1154 1155 prsize = width > realsz ? width : realsz; 1156 if ((unsigned)ret + prsize > INT_MAX) { 1157 ret = EOF; 1158 goto error; 1159 } 1160 1161 /* right-adjusting blank padding */ 1162 if ((flags & (LADJUST|ZEROPAD)) == 0) 1163 PAD(width - realsz, blanks); 1164 1165 /* prefix */ 1166 if (sign) 1167 PRINT(&sign, 1); 1168 1169 if (ox[1]) { /* ox[1] is either x, X, or \0 */ 1170 ox[0] = '0'; 1171 PRINT(ox, 2); 1172 } 1173 1174 /* right-adjusting zero padding */ 1175 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 1176 PAD(width - realsz, zeroes); 1177 1178 /* leading zeroes from decimal precision */ 1179 PAD(dprec - size, zeroes); 1180 1181 /* the string or number proper */ 1182 #ifndef NO_FLOATING_POINT 1183 if ((flags & FPT) == 0) { 1184 PRINT(cp, size); 1185 } else { /* glue together f_p fragments */ 1186 if (!expchar) { /* %[fF] or sufficiently short %[gG] */ 1187 if (expt <= 0) { 1188 PRINT(zeroes, 1); 1189 if (prec || flags & ALT) 1190 PRINT(decimal_point, 1); 1191 PAD(-expt, zeroes); 1192 /* already handled initial 0's */ 1193 prec += expt; 1194 } else { 1195 PRINTANDPAD(cp, dtoaend, lead, zeroes); 1196 cp += lead; 1197 if (grouping) { 1198 while (nseps>0 || nrepeats>0) { 1199 if (nrepeats > 0) 1200 nrepeats--; 1201 else { 1202 grouping--; 1203 nseps--; 1204 } 1205 PRINT(&thousands_sep, 1206 1); 1207 PRINTANDPAD(cp,dtoaend, 1208 *grouping, zeroes); 1209 cp += *grouping; 1210 } 1211 if (cp > dtoaend) 1212 cp = dtoaend; 1213 } 1214 if (prec || flags & ALT) 1215 PRINT(decimal_point,1); 1216 } 1217 PRINTANDPAD(cp, dtoaend, prec, zeroes); 1218 } else { /* %[eE] or sufficiently long %[gG] */ 1219 if (prec > 1 || flags & ALT) { 1220 buf[0] = *cp++; 1221 buf[1] = *decimal_point; 1222 PRINT(buf, 2); 1223 PRINT(cp, ndig-1); 1224 PAD(prec - ndig, zeroes); 1225 } else /* XeYYY */ 1226 PRINT(cp, 1); 1227 PRINT(expstr, expsize); 1228 } 1229 } 1230 #else 1231 PRINT(cp, size); 1232 #endif 1233 /* left-adjusting padding (always blank) */ 1234 if (flags & LADJUST) 1235 PAD(width - realsz, blanks); 1236 1237 /* finally, adjust ret */ 1238 ret += prsize; 1239 1240 FLUSH(); /* copy out the I/O vectors */ 1241 } 1242 done: 1243 FLUSH(); 1244 error: 1245 va_end(orgap); 1246 #ifndef NO_FLOATING_POINT 1247 if (dtoaresult != NULL) 1248 freedtoa(dtoaresult); 1249 #endif 1250 if (convbuf != NULL) 1251 free(convbuf); 1252 if (__sferror(fp)) 1253 ret = EOF; 1254 if ((argtable != NULL) && (argtable != statargtable)) 1255 free (argtable); 1256 return (ret); 1257 /* NOTREACHED */ 1258 } 1259 1260 /* 1261 * Find all arguments when a positional parameter is encountered. Returns a 1262 * table, indexed by argument number, of pointers to each arguments. The 1263 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. 1264 * It will be replaces with a malloc-ed one if it overflows. 1265 */ 1266 static void 1267 __find_arguments (const char *fmt0, va_list ap, union arg **argtable) 1268 { 1269 char *fmt; /* format string */ 1270 int ch; /* character from fmt */ 1271 int n, n2; /* handy integer (short term usage) */ 1272 char *cp; /* handy char pointer (short term usage) */ 1273 int flags; /* flags as above */ 1274 int width; /* width from format (%8d), or 0 */ 1275 enum typeid *typetable; /* table of types */ 1276 enum typeid stattypetable [STATIC_ARG_TBL_SIZE]; 1277 int tablesize; /* current size of type table */ 1278 int tablemax; /* largest used index in table */ 1279 int nextarg; /* 1-based argument index */ 1280 1281 /* 1282 * Add an argument type to the table, expanding if necessary. 1283 */ 1284 #define ADDTYPE(type) \ 1285 ((nextarg >= tablesize) ? \ 1286 __grow_type_table(nextarg, &typetable, &tablesize) : (void)0, \ 1287 (nextarg > tablemax) ? tablemax = nextarg : 0, \ 1288 typetable[nextarg++] = type) 1289 1290 #define ADDSARG() \ 1291 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \ 1292 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \ 1293 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \ 1294 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \ 1295 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT)))))) 1296 1297 #define ADDUARG() \ 1298 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \ 1299 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \ 1300 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \ 1301 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \ 1302 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT)))))) 1303 1304 /* 1305 * Add * arguments to the type array. 1306 */ 1307 #define ADDASTER() \ 1308 n2 = 0; \ 1309 cp = fmt; \ 1310 while (is_digit(*cp)) { \ 1311 n2 = 10 * n2 + to_digit(*cp); \ 1312 cp++; \ 1313 } \ 1314 if (*cp == '$') { \ 1315 int hold = nextarg; \ 1316 nextarg = n2; \ 1317 ADDTYPE (T_INT); \ 1318 nextarg = hold; \ 1319 fmt = ++cp; \ 1320 } else { \ 1321 ADDTYPE (T_INT); \ 1322 } 1323 fmt = (char *)fmt0; 1324 typetable = stattypetable; 1325 tablesize = STATIC_ARG_TBL_SIZE; 1326 tablemax = 0; 1327 nextarg = 1; 1328 for (n = 0; n < STATIC_ARG_TBL_SIZE; n++) 1329 typetable[n] = T_UNUSED; 1330 1331 /* 1332 * Scan the format for conversions (`%' character). 1333 */ 1334 for (;;) { 1335 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 1336 /* void */; 1337 if (ch == '\0') 1338 goto done; 1339 fmt++; /* skip over '%' */ 1340 1341 flags = 0; 1342 width = 0; 1343 1344 rflag: ch = *fmt++; 1345 reswitch: switch (ch) { 1346 case ' ': 1347 case '#': 1348 goto rflag; 1349 case '*': 1350 ADDASTER (); 1351 goto rflag; 1352 case '-': 1353 case '+': 1354 case '\'': 1355 goto rflag; 1356 case '.': 1357 if ((ch = *fmt++) == '*') { 1358 ADDASTER (); 1359 goto rflag; 1360 } 1361 while (is_digit(ch)) { 1362 ch = *fmt++; 1363 } 1364 goto reswitch; 1365 case '0': 1366 goto rflag; 1367 case '1': case '2': case '3': case '4': 1368 case '5': case '6': case '7': case '8': case '9': 1369 n = 0; 1370 do { 1371 n = 10 * n + to_digit(ch); 1372 ch = *fmt++; 1373 } while (is_digit(ch)); 1374 if (ch == '$') { 1375 nextarg = n; 1376 goto rflag; 1377 } 1378 width = n; 1379 goto reswitch; 1380 #ifndef NO_FLOATING_POINT 1381 case 'L': 1382 flags |= LONGDBL; 1383 goto rflag; 1384 #endif 1385 case 'h': 1386 if (flags & SHORTINT) { 1387 flags &= ~SHORTINT; 1388 flags |= CHARINT; 1389 } else 1390 flags |= SHORTINT; 1391 goto rflag; 1392 case 'j': 1393 flags |= INTMAXT; 1394 goto rflag; 1395 case 'l': 1396 if (flags & LONGINT) { 1397 flags &= ~LONGINT; 1398 flags |= LLONGINT; 1399 } else 1400 flags |= LONGINT; 1401 goto rflag; 1402 case 'q': 1403 flags |= LLONGINT; /* not necessarily */ 1404 goto rflag; 1405 case 't': 1406 flags |= PTRDIFFT; 1407 goto rflag; 1408 case 'z': 1409 flags |= SIZET; 1410 goto rflag; 1411 case 'C': 1412 flags |= LONGINT; 1413 /*FALLTHROUGH*/ 1414 case 'c': 1415 if (flags & LONGINT) 1416 ADDTYPE(T_WINT); 1417 else 1418 ADDTYPE(T_INT); 1419 break; 1420 case 'D': 1421 flags |= LONGINT; 1422 /*FALLTHROUGH*/ 1423 case 'd': 1424 case 'i': 1425 ADDSARG(); 1426 break; 1427 #ifndef NO_FLOATING_POINT 1428 case 'a': 1429 case 'A': 1430 case 'e': 1431 case 'E': 1432 case 'f': 1433 case 'g': 1434 case 'G': 1435 if (flags & LONGDBL) 1436 ADDTYPE(T_LONG_DOUBLE); 1437 else 1438 ADDTYPE(T_DOUBLE); 1439 break; 1440 #endif /* !NO_FLOATING_POINT */ 1441 case 'n': 1442 if (flags & INTMAXT) 1443 ADDTYPE(TP_INTMAXT); 1444 else if (flags & PTRDIFFT) 1445 ADDTYPE(TP_PTRDIFFT); 1446 else if (flags & SIZET) 1447 ADDTYPE(TP_SIZET); 1448 else if (flags & LLONGINT) 1449 ADDTYPE(TP_LLONG); 1450 else if (flags & LONGINT) 1451 ADDTYPE(TP_LONG); 1452 else if (flags & SHORTINT) 1453 ADDTYPE(TP_SHORT); 1454 else if (flags & CHARINT) 1455 ADDTYPE(TP_SCHAR); 1456 else 1457 ADDTYPE(TP_INT); 1458 continue; /* no output */ 1459 case 'O': 1460 flags |= LONGINT; 1461 /*FALLTHROUGH*/ 1462 case 'o': 1463 ADDUARG(); 1464 break; 1465 case 'p': 1466 ADDTYPE(TP_VOID); 1467 break; 1468 case 'S': 1469 flags |= LONGINT; 1470 /*FALLTHROUGH*/ 1471 case 's': 1472 if (flags & LONGINT) 1473 ADDTYPE(TP_WCHAR); 1474 else 1475 ADDTYPE(TP_CHAR); 1476 break; 1477 case 'U': 1478 flags |= LONGINT; 1479 /*FALLTHROUGH*/ 1480 case 'u': 1481 case 'X': 1482 case 'x': 1483 ADDUARG(); 1484 break; 1485 default: /* "%?" prints ?, unless ? is NUL */ 1486 if (ch == '\0') 1487 goto done; 1488 break; 1489 } 1490 } 1491 done: 1492 /* 1493 * Build the argument table. 1494 */ 1495 if (tablemax >= STATIC_ARG_TBL_SIZE) { 1496 *argtable = (union arg *) 1497 malloc (sizeof (union arg) * (tablemax + 1)); 1498 } 1499 1500 (*argtable) [0].intarg = 0; 1501 for (n = 1; n <= tablemax; n++) { 1502 switch (typetable [n]) { 1503 case T_UNUSED: /* whoops! */ 1504 (*argtable) [n].intarg = va_arg (ap, int); 1505 break; 1506 case TP_SCHAR: 1507 (*argtable) [n].pschararg = va_arg (ap, signed char *); 1508 break; 1509 case TP_SHORT: 1510 (*argtable) [n].pshortarg = va_arg (ap, short *); 1511 break; 1512 case T_INT: 1513 (*argtable) [n].intarg = va_arg (ap, int); 1514 break; 1515 case T_U_INT: 1516 (*argtable) [n].uintarg = va_arg (ap, unsigned int); 1517 break; 1518 case TP_INT: 1519 (*argtable) [n].pintarg = va_arg (ap, int *); 1520 break; 1521 case T_LONG: 1522 (*argtable) [n].longarg = va_arg (ap, long); 1523 break; 1524 case T_U_LONG: 1525 (*argtable) [n].ulongarg = va_arg (ap, unsigned long); 1526 break; 1527 case TP_LONG: 1528 (*argtable) [n].plongarg = va_arg (ap, long *); 1529 break; 1530 case T_LLONG: 1531 (*argtable) [n].longlongarg = va_arg (ap, long long); 1532 break; 1533 case T_U_LLONG: 1534 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long); 1535 break; 1536 case TP_LLONG: 1537 (*argtable) [n].plonglongarg = va_arg (ap, long long *); 1538 break; 1539 case T_PTRDIFFT: 1540 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t); 1541 break; 1542 case TP_PTRDIFFT: 1543 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *); 1544 break; 1545 case T_SIZET: 1546 (*argtable) [n].sizearg = va_arg (ap, size_t); 1547 break; 1548 case TP_SIZET: 1549 (*argtable) [n].psizearg = va_arg (ap, ssize_t *); 1550 break; 1551 case T_INTMAXT: 1552 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t); 1553 break; 1554 case T_UINTMAXT: 1555 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t); 1556 break; 1557 case TP_INTMAXT: 1558 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *); 1559 break; 1560 case T_DOUBLE: 1561 #ifndef NO_FLOATING_POINT 1562 (*argtable) [n].doublearg = va_arg (ap, double); 1563 #endif 1564 break; 1565 case T_LONG_DOUBLE: 1566 #ifndef NO_FLOATING_POINT 1567 (*argtable) [n].longdoublearg = va_arg (ap, long double); 1568 #endif 1569 break; 1570 case TP_CHAR: 1571 (*argtable) [n].pchararg = va_arg (ap, char *); 1572 break; 1573 case TP_VOID: 1574 (*argtable) [n].pvoidarg = va_arg (ap, void *); 1575 break; 1576 case T_WINT: 1577 (*argtable) [n].wintarg = va_arg (ap, wint_t); 1578 break; 1579 case TP_WCHAR: 1580 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *); 1581 break; 1582 } 1583 } 1584 1585 if ((typetable != NULL) && (typetable != stattypetable)) 1586 free (typetable); 1587 } 1588 1589 /* 1590 * Increase the size of the type table. 1591 */ 1592 static void 1593 __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize) 1594 { 1595 enum typeid *const oldtable = *typetable; 1596 const int oldsize = *tablesize; 1597 enum typeid *newtable; 1598 int n, newsize = oldsize * 2; 1599 1600 if (newsize < nextarg + 1) 1601 newsize = nextarg + 1; 1602 if (oldsize == STATIC_ARG_TBL_SIZE) { 1603 if ((newtable = malloc(newsize * sizeof(enum typeid))) == NULL) 1604 abort(); /* XXX handle better */ 1605 bcopy(oldtable, newtable, oldsize * sizeof(enum typeid)); 1606 } else { 1607 newtable = reallocf(oldtable, newsize * sizeof(enum typeid)); 1608 if (newtable == NULL) 1609 abort(); /* XXX handle better */ 1610 } 1611 for (n = oldsize; n < newsize; n++) 1612 newtable[n] = T_UNUSED; 1613 1614 *typetable = newtable; 1615 *tablesize = newsize; 1616 } 1617 1618 1619 #ifndef NO_FLOATING_POINT 1620 1621 static int 1622 exponent(char *p0, int exp, int fmtch) 1623 { 1624 char *p, *t; 1625 char expbuf[MAXEXPDIG]; 1626 1627 p = p0; 1628 *p++ = fmtch; 1629 if (exp < 0) { 1630 exp = -exp; 1631 *p++ = '-'; 1632 } 1633 else 1634 *p++ = '+'; 1635 t = expbuf + MAXEXPDIG; 1636 if (exp > 9) { 1637 do { 1638 *--t = to_char(exp % 10); 1639 } while ((exp /= 10) > 9); 1640 *--t = to_char(exp); 1641 for (; t < expbuf + MAXEXPDIG; *p++ = *t++); 1642 } 1643 else { 1644 /* 1645 * Exponents for decimal floating point conversions 1646 * (%[eEgG]) must be at least two characters long, 1647 * whereas exponents for hexadecimal conversions can 1648 * be only one character long. 1649 */ 1650 if (fmtch == 'e' || fmtch == 'E') 1651 *p++ = '0'; 1652 *p++ = to_char(exp); 1653 } 1654 return (p - p0); 1655 } 1656 #endif /* !NO_FLOATING_POINT */ 1657