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