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