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, char *, int, char, 115 const char *); 116 static char *__ultoa(u_long, char *, int, int, 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, 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, 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 mbstate_t mbs; 349 350 /* 351 * Determine the number of bytes to output and allocate space for 352 * the output. 353 */ 354 memset(&mbs, 0, sizeof(mbs)); 355 if (prec >= 0) { 356 nbytes = 0; 357 p = wcsarg; 358 for (;;) { 359 clen = wcrtomb(buf, *p++, &mbs); 360 if (clen == 0 || clen == (size_t)-1 || 361 nbytes + clen > prec) 362 break; 363 nbytes += clen; 364 } 365 } else { 366 p = wcsarg; 367 nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs); 368 if (nbytes == (size_t)-1) 369 return (NULL); 370 } 371 if ((convbuf = malloc(nbytes + 1)) == NULL) 372 return (NULL); 373 374 /* 375 * Fill the output buffer with the multibyte representations of as 376 * many wide characters as will fit. 377 */ 378 mbp = convbuf; 379 p = wcsarg; 380 memset(&mbs, 0, sizeof(mbs)); 381 while (mbp - convbuf < nbytes) { 382 clen = wcrtomb(mbp, *p++, &mbs); 383 if (clen == 0 || clen == (size_t)-1) 384 break; 385 mbp += clen; 386 } 387 *mbp = '\0'; 388 if (clen == (size_t)-1) 389 return (NULL); 390 391 return (convbuf); 392 } 393 394 /* 395 * MT-safe version 396 */ 397 int 398 vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap) 399 400 { 401 int ret; 402 403 FLOCKFILE(fp); 404 ret = __vfprintf(fp, fmt0, ap); 405 FUNLOCKFILE(fp); 406 return (ret); 407 } 408 409 #ifdef FLOATING_POINT 410 #include <math.h> 411 #include "floatio.h" 412 413 #define BUF ((MAXEXP*2)+MAXFRACT+1) /* + decimal point */ 414 #define DEFPREC 6 415 416 extern char *__dtoa(double, int, int, int *, int *, char **); 417 extern void __freedtoa(char *s); 418 419 static char *cvt(double, int, int, char *, int *, int, int *); 420 static int exponent(char *, int, int); 421 422 #else /* no FLOATING_POINT */ 423 424 #define BUF 136 425 426 #endif /* FLOATING_POINT */ 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 HEXPREFIX 0x002 /* add 0x or 0X prefix */ 435 #define LADJUST 0x004 /* left adjustment */ 436 #define LONGDBL 0x008 /* long double */ 437 #define LONGINT 0x010 /* long integer */ 438 #define LLONGINT 0x020 /* long long integer */ 439 #define SHORTINT 0x040 /* short integer */ 440 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 441 #define FPT 0x100 /* Floating point number */ 442 #define GROUPING 0x200 /* use grouping ("'" flag) */ 443 /* C99 additional size modifiers: */ 444 #define SIZET 0x400 /* size_t */ 445 #define PTRDIFFT 0x800 /* ptrdiff_t */ 446 #define INTMAXT 0x1000 /* intmax_t */ 447 #define CHARINT 0x2000 /* print char using int format */ 448 449 /* 450 * Non-MT-safe version 451 */ 452 int 453 __vfprintf(FILE *fp, const char *fmt0, va_list ap) 454 { 455 char *fmt; /* format string */ 456 int ch; /* character from fmt */ 457 int n, n2; /* handy integer (short term usage) */ 458 char *cp; /* handy char pointer (short term usage) */ 459 struct __siov *iovp; /* for PRINT macro */ 460 int flags; /* flags as above */ 461 int ret; /* return value accumulator */ 462 int width; /* width from format (%8d), or 0 */ 463 int prec; /* precision from format (%.3d), or -1 */ 464 char sign; /* sign prefix (' ', '+', '-', or \0) */ 465 char thousands_sep; /* locale specific thousands separator */ 466 const char *grouping; /* locale specific numeric grouping rules */ 467 #ifdef FLOATING_POINT 468 char *decimal_point; /* locale specific decimal point */ 469 char softsign; /* temporary negative sign for floats */ 470 double _double; /* double precision arguments %[eEfgG] */ 471 int expt; /* integer value of exponent */ 472 int expsize; /* character count for expstr */ 473 int ndig; /* actual number of digits returned by cvt */ 474 char expstr[7]; /* buffer for exponent string */ 475 char *dtoaresult; /* buffer allocated by dtoa */ 476 #endif 477 u_long ulval; /* integer arguments %[diouxX] */ 478 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 479 int base; /* base for [diouxX] conversion */ 480 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 481 int realsz; /* field size expanded by dprec, sign, etc */ 482 int size; /* size of converted field or string */ 483 int prsize; /* max size of printed field */ 484 char *xdigs; /* digits for [xX] conversion */ 485 #define NIOV 8 486 struct __suio uio; /* output information: summary */ 487 struct __siov iov[NIOV];/* ... and individual io vectors */ 488 char buf[BUF]; /* space for %c, %[diouxX], %[eEfFgG] */ 489 char ox[2]; /* space for 0x hex-prefix */ 490 union arg *argtable; /* args, built due to positional arg */ 491 union arg statargtable [STATIC_ARG_TBL_SIZE]; 492 int nextarg; /* 1-based argument index */ 493 va_list orgap; /* original argument pointer */ 494 char *convbuf; /* wide to multibyte conversion result */ 495 496 /* 497 * Choose PADSIZE to trade efficiency vs. size. If larger printf 498 * fields occur frequently, increase PADSIZE and make the initialisers 499 * below longer. 500 */ 501 #define PADSIZE 16 /* pad chunk size */ 502 static char blanks[PADSIZE] = 503 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 504 static char zeroes[PADSIZE] = 505 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 506 507 /* 508 * BEWARE, these `goto error' on error, and PAD uses `n'. 509 */ 510 #define PRINT(ptr, len) { \ 511 iovp->iov_base = (ptr); \ 512 iovp->iov_len = (len); \ 513 uio.uio_resid += (len); \ 514 iovp++; \ 515 if (++uio.uio_iovcnt >= NIOV) { \ 516 if (__sprint(fp, &uio)) \ 517 goto error; \ 518 iovp = iov; \ 519 } \ 520 } 521 #define PAD(howmany, with) { \ 522 if ((n = (howmany)) > 0) { \ 523 while (n > PADSIZE) { \ 524 PRINT(with, PADSIZE); \ 525 n -= PADSIZE; \ 526 } \ 527 PRINT(with, n); \ 528 } \ 529 } 530 #define FLUSH() { \ 531 if (uio.uio_resid && __sprint(fp, &uio)) \ 532 goto error; \ 533 uio.uio_iovcnt = 0; \ 534 iovp = iov; \ 535 } 536 537 /* 538 * Get the argument indexed by nextarg. If the argument table is 539 * built, use it to get the argument. If its not, get the next 540 * argument (and arguments must be gotten sequentially). 541 */ 542 #define GETARG(type) \ 543 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ 544 (nextarg++, va_arg(ap, type))) 545 546 /* 547 * To extend shorts properly, we need both signed and unsigned 548 * argument extraction methods. 549 */ 550 #define SARG() \ 551 (flags&LONGINT ? GETARG(long) : \ 552 flags&SHORTINT ? (long)(short)GETARG(int) : \ 553 flags&CHARINT ? (long)(signed char)GETARG(int) : \ 554 (long)GETARG(int)) 555 #define UARG() \ 556 (flags&LONGINT ? GETARG(u_long) : \ 557 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 558 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 559 (u_long)GETARG(u_int)) 560 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 561 #define SJARG() \ 562 (flags&INTMAXT ? GETARG(intmax_t) : \ 563 flags&SIZET ? (intmax_t)GETARG(size_t) : \ 564 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 565 (intmax_t)GETARG(long long)) 566 #define UJARG() \ 567 (flags&INTMAXT ? GETARG(uintmax_t) : \ 568 flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 569 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \ 570 (uintmax_t)GETARG(unsigned long long)) 571 572 /* 573 * Get * arguments, including the form *nn$. Preserve the nextarg 574 * that the argument can be gotten once the type is determined. 575 */ 576 #define GETASTER(val) \ 577 n2 = 0; \ 578 cp = fmt; \ 579 while (is_digit(*cp)) { \ 580 n2 = 10 * n2 + to_digit(*cp); \ 581 cp++; \ 582 } \ 583 if (*cp == '$') { \ 584 int hold = nextarg; \ 585 if (argtable == NULL) { \ 586 argtable = statargtable; \ 587 __find_arguments (fmt0, orgap, &argtable); \ 588 } \ 589 nextarg = n2; \ 590 val = GETARG (int); \ 591 nextarg = hold; \ 592 fmt = ++cp; \ 593 } else { \ 594 val = GETARG (int); \ 595 } 596 597 598 thousands_sep = '\0'; 599 grouping = NULL; 600 convbuf = NULL; 601 #ifdef FLOATING_POINT 602 dtoaresult = NULL; 603 decimal_point = localeconv()->decimal_point; 604 #endif 605 /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 606 if (cantwrite(fp)) 607 return (EOF); 608 609 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 610 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 611 fp->_file >= 0) 612 return (__sbprintf(fp, fmt0, ap)); 613 614 fmt = (char *)fmt0; 615 argtable = NULL; 616 nextarg = 1; 617 va_copy(orgap, ap); 618 uio.uio_iov = iovp = iov; 619 uio.uio_resid = 0; 620 uio.uio_iovcnt = 0; 621 ret = 0; 622 623 /* 624 * Scan the format for conversions (`%' character). 625 */ 626 for (;;) { 627 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 628 /* void */; 629 if ((n = fmt - cp) != 0) { 630 if ((unsigned)ret + n > INT_MAX) { 631 ret = EOF; 632 goto error; 633 } 634 PRINT(cp, n); 635 ret += n; 636 } 637 if (ch == '\0') 638 goto done; 639 fmt++; /* skip over '%' */ 640 641 flags = 0; 642 dprec = 0; 643 width = 0; 644 prec = -1; 645 sign = '\0'; 646 647 rflag: ch = *fmt++; 648 reswitch: switch (ch) { 649 case ' ': 650 /*- 651 * ``If the space and + flags both appear, the space 652 * flag will be ignored.'' 653 * -- ANSI X3J11 654 */ 655 if (!sign) 656 sign = ' '; 657 goto rflag; 658 case '#': 659 flags |= ALT; 660 goto rflag; 661 case '*': 662 /*- 663 * ``A negative field width argument is taken as a 664 * - flag followed by a positive field width.'' 665 * -- ANSI X3J11 666 * They don't exclude field widths read from args. 667 */ 668 GETASTER (width); 669 if (width >= 0) 670 goto rflag; 671 width = -width; 672 /* FALLTHROUGH */ 673 case '-': 674 flags |= LADJUST; 675 goto rflag; 676 case '+': 677 sign = '+'; 678 goto rflag; 679 case '\'': 680 flags |= GROUPING; 681 thousands_sep = *(localeconv()->thousands_sep); 682 grouping = localeconv()->grouping; 683 goto rflag; 684 case '.': 685 if ((ch = *fmt++) == '*') { 686 GETASTER (n); 687 prec = n < 0 ? -1 : n; 688 goto rflag; 689 } 690 n = 0; 691 while (is_digit(ch)) { 692 n = 10 * n + to_digit(ch); 693 ch = *fmt++; 694 } 695 prec = n < 0 ? -1 : n; 696 goto reswitch; 697 case '0': 698 /*- 699 * ``Note that 0 is taken as a flag, not as the 700 * beginning of a field width.'' 701 * -- ANSI X3J11 702 */ 703 flags |= ZEROPAD; 704 goto rflag; 705 case '1': case '2': case '3': case '4': 706 case '5': case '6': case '7': case '8': case '9': 707 n = 0; 708 do { 709 n = 10 * n + to_digit(ch); 710 ch = *fmt++; 711 } while (is_digit(ch)); 712 if (ch == '$') { 713 nextarg = n; 714 if (argtable == NULL) { 715 argtable = statargtable; 716 __find_arguments (fmt0, orgap, 717 &argtable); 718 } 719 goto rflag; 720 } 721 width = n; 722 goto reswitch; 723 #ifdef FLOATING_POINT 724 case 'L': 725 flags |= LONGDBL; 726 goto rflag; 727 #endif 728 case 'h': 729 if (flags & SHORTINT) { 730 flags &= ~SHORTINT; 731 flags |= CHARINT; 732 } else 733 flags |= SHORTINT; 734 goto rflag; 735 case 'j': 736 flags |= INTMAXT; 737 goto rflag; 738 case 'l': 739 if (flags & LONGINT) { 740 flags &= ~LONGINT; 741 flags |= LLONGINT; 742 } else 743 flags |= LONGINT; 744 goto rflag; 745 case 'q': 746 flags |= LLONGINT; /* not necessarily */ 747 goto rflag; 748 case 't': 749 flags |= PTRDIFFT; 750 goto rflag; 751 case 'z': 752 flags |= SIZET; 753 goto rflag; 754 case 'C': 755 flags |= LONGINT; 756 /*FALLTHROUGH*/ 757 case 'c': 758 if (flags & LONGINT) { 759 mbstate_t mbs; 760 size_t mbseqlen; 761 762 memset(&mbs, 0, sizeof(mbs)); 763 mbseqlen = wcrtomb(cp = buf, 764 (wchar_t)GETARG(wint_t), &mbs); 765 if (mbseqlen == (size_t)-1) { 766 fp->_flags |= __SERR; 767 goto error; 768 } 769 size = (int)mbseqlen; 770 } else { 771 *(cp = buf) = GETARG(int); 772 size = 1; 773 } 774 sign = '\0'; 775 break; 776 case 'D': 777 flags |= LONGINT; 778 /*FALLTHROUGH*/ 779 case 'd': 780 case 'i': 781 if (flags & INTMAX_SIZE) { 782 ujval = SJARG(); 783 if ((intmax_t)ujval < 0) { 784 ujval = -ujval; 785 sign = '-'; 786 } 787 } else { 788 ulval = SARG(); 789 if ((long)ulval < 0) { 790 ulval = -ulval; 791 sign = '-'; 792 } 793 } 794 base = 10; 795 goto number; 796 #ifdef FLOATING_POINT 797 #ifdef HEXFLOAT 798 case 'a': 799 case 'A': 800 #endif 801 case 'e': 802 case 'E': 803 /*- 804 * Grouping apply to %i, %d, %u, %f, %F, %g, %G 805 * conversion specifiers only. For other conversions 806 * behavior is undefined. 807 * -- POSIX 808 */ 809 flags &= ~GROUPING; 810 /*FALLTHROUGH*/ 811 case 'f': 812 case 'F': 813 goto fp_begin; 814 case 'g': 815 case 'G': 816 if (prec == 0) 817 prec = 1; 818 fp_begin: if (prec == -1) 819 prec = DEFPREC; 820 if (flags & LONGDBL) 821 /* XXX this loses precision. */ 822 _double = (double)GETARG(long double); 823 else 824 _double = GETARG(double); 825 /* do this before tricky precision changes */ 826 if (isinf(_double)) { 827 if (_double < 0) 828 sign = '-'; 829 if (isupper(ch)) 830 cp = "INF"; 831 else 832 cp = "inf"; 833 size = 3; 834 break; 835 } 836 if (isnan(_double)) { 837 if (isupper(ch)) 838 cp = "NAN"; 839 else 840 cp = "nan"; 841 size = 3; 842 break; 843 } 844 flags |= FPT; 845 if (dtoaresult != NULL) { 846 __freedtoa(dtoaresult); 847 dtoaresult = NULL; 848 } 849 dtoaresult = cp = cvt(_double, prec, flags, &softsign, 850 &expt, ch, &ndig); 851 if (ch == 'g' || ch == 'G') { 852 if (expt <= -4 || expt > prec) 853 ch = (ch == 'g') ? 'e' : 'E'; 854 else 855 ch = 'g'; 856 } 857 if (ch == 'e' || ch == 'E') { 858 --expt; 859 expsize = exponent(expstr, expt, ch); 860 size = expsize + ndig; 861 if (ndig > 1 || flags & ALT) 862 ++size; 863 } else if (ch == 'f' || ch == 'F') { 864 if (expt > 0) { 865 size = expt; 866 if (prec || flags & ALT) 867 size += prec + 1; 868 } else /* "0.X" */ 869 size = prec + 2; 870 } else if (expt >= ndig) { /* fixed g fmt */ 871 size = expt; 872 if (flags & ALT) 873 ++size; 874 } else 875 size = ndig + (expt > 0 ? 876 1 : 2 - expt); 877 878 if (softsign) 879 sign = '-'; 880 break; 881 #endif /* FLOATING_POINT */ 882 case 'n': 883 /* 884 * Assignment-like behavior is specified if the 885 * value overflows or is otherwise unrepresentable. 886 * C99 says to use `signed char' for %hhn conversions. 887 */ 888 if (flags & LLONGINT) 889 *GETARG(long long *) = ret; 890 else if (flags & SIZET) 891 *GETARG(ssize_t *) = (ssize_t)ret; 892 else if (flags & PTRDIFFT) 893 *GETARG(ptrdiff_t *) = ret; 894 else if (flags & INTMAXT) 895 *GETARG(intmax_t *) = ret; 896 else if (flags & LONGINT) 897 *GETARG(long *) = ret; 898 else if (flags & SHORTINT) 899 *GETARG(short *) = ret; 900 else if (flags & CHARINT) 901 *GETARG(signed char *) = ret; 902 else 903 *GETARG(int *) = ret; 904 continue; /* no output */ 905 case 'O': 906 flags |= LONGINT; 907 /*FALLTHROUGH*/ 908 case 'o': 909 if (flags & INTMAX_SIZE) 910 ujval = UJARG(); 911 else 912 ulval = UARG(); 913 base = 8; 914 goto nosign; 915 case 'p': 916 /*- 917 * ``The argument shall be a pointer to void. The 918 * value of the pointer is converted to a sequence 919 * of printable characters, in an implementation- 920 * defined manner.'' 921 * -- ANSI X3J11 922 */ 923 ujval = (uintmax_t)(uintptr_t)GETARG(void *); 924 base = 16; 925 xdigs = "0123456789abcdef"; 926 flags = flags | INTMAXT | HEXPREFIX; 927 ch = 'x'; 928 goto nosign; 929 case 'S': 930 flags |= LONGINT; 931 /*FALLTHROUGH*/ 932 case 's': 933 if (flags & LONGINT) { 934 wchar_t *wcp; 935 936 if (convbuf != NULL) 937 free(convbuf); 938 if ((wcp = GETARG(wchar_t *)) == NULL) 939 cp = "(null)"; 940 else { 941 convbuf = __wcsconv(wcp, prec); 942 if (convbuf == NULL) { 943 fp->_flags |= __SERR; 944 goto error; 945 } 946 cp = convbuf; 947 } 948 } else if ((cp = GETARG(char *)) == NULL) 949 cp = "(null)"; 950 if (prec >= 0) { 951 /* 952 * can't use strlen; can only look for the 953 * NUL in the first `prec' characters, and 954 * strlen() will go further. 955 */ 956 char *p = memchr(cp, 0, (size_t)prec); 957 958 if (p != NULL) { 959 size = p - cp; 960 if (size > prec) 961 size = prec; 962 } else 963 size = prec; 964 } else 965 size = strlen(cp); 966 sign = '\0'; 967 break; 968 case 'U': 969 flags |= LONGINT; 970 /*FALLTHROUGH*/ 971 case 'u': 972 if (flags & INTMAX_SIZE) 973 ujval = UJARG(); 974 else 975 ulval = UARG(); 976 base = 10; 977 goto nosign; 978 case 'X': 979 xdigs = "0123456789ABCDEF"; 980 goto hex; 981 case 'x': 982 xdigs = "0123456789abcdef"; 983 hex: 984 if (flags & INTMAX_SIZE) 985 ujval = UJARG(); 986 else 987 ulval = UARG(); 988 base = 16; 989 /* leading 0x/X only if non-zero */ 990 if (flags & ALT && 991 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 992 flags |= HEXPREFIX; 993 994 flags &= ~GROUPING; 995 /* unsigned conversions */ 996 nosign: sign = '\0'; 997 /*- 998 * ``... diouXx conversions ... if a precision is 999 * specified, the 0 flag will be ignored.'' 1000 * -- ANSI X3J11 1001 */ 1002 number: if ((dprec = prec) >= 0) 1003 flags &= ~ZEROPAD; 1004 1005 /*- 1006 * ``The result of converting a zero value with an 1007 * explicit precision of zero is no characters.'' 1008 * -- ANSI X3J11 1009 */ 1010 cp = buf + BUF; 1011 if (flags & INTMAX_SIZE) { 1012 if (ujval != 0 || prec != 0) 1013 cp = __ujtoa(ujval, cp, base, 1014 flags & ALT, xdigs, 1015 flags & GROUPING, thousands_sep, 1016 grouping); 1017 } else { 1018 if (ulval != 0 || prec != 0) 1019 cp = __ultoa(ulval, cp, base, 1020 flags & ALT, xdigs, 1021 flags & GROUPING, thousands_sep, 1022 grouping); 1023 } 1024 size = buf + BUF - cp; 1025 break; 1026 default: /* "%?" prints ?, unless ? is NUL */ 1027 if (ch == '\0') 1028 goto done; 1029 /* pretend it was %c with argument ch */ 1030 cp = buf; 1031 *cp = ch; 1032 size = 1; 1033 sign = '\0'; 1034 break; 1035 } 1036 1037 /* 1038 * All reasonable formats wind up here. At this point, `cp' 1039 * points to a string which (if not flags&LADJUST) should be 1040 * padded out to `width' places. If flags&ZEROPAD, it should 1041 * first be prefixed by any sign or other prefix; otherwise, 1042 * it should be blank padded before the prefix is emitted. 1043 * After any left-hand padding and prefixing, emit zeroes 1044 * required by a decimal [diouxX] precision, then print the 1045 * string proper, then emit zeroes required by any leftover 1046 * floating precision; finally, if LADJUST, pad with blanks. 1047 * 1048 * Compute actual size, so we know how much to pad. 1049 * size excludes decimal prec; realsz includes it. 1050 */ 1051 realsz = dprec > size ? dprec : size; 1052 if (sign) 1053 realsz++; 1054 else if (flags & HEXPREFIX) 1055 realsz += 2; 1056 1057 prsize = width > realsz ? width : realsz; 1058 if ((unsigned)ret + prsize > INT_MAX) { 1059 ret = EOF; 1060 goto error; 1061 } 1062 1063 /* right-adjusting blank padding */ 1064 if ((flags & (LADJUST|ZEROPAD)) == 0) 1065 PAD(width - realsz, blanks); 1066 1067 /* prefix */ 1068 if (sign) { 1069 PRINT(&sign, 1); 1070 } else if (flags & HEXPREFIX) { 1071 ox[0] = '0'; 1072 ox[1] = ch; 1073 PRINT(ox, 2); 1074 } 1075 1076 /* right-adjusting zero padding */ 1077 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 1078 PAD(width - realsz, zeroes); 1079 1080 /* leading zeroes from decimal precision */ 1081 PAD(dprec - size, zeroes); 1082 1083 /* the string or number proper */ 1084 #ifdef FLOATING_POINT 1085 if ((flags & FPT) == 0) { 1086 PRINT(cp, size); 1087 } else { /* glue together f_p fragments */ 1088 if (ch >= 'f') { /* 'f' or 'g' */ 1089 if (_double == 0) { 1090 /* kludge for __dtoa irregularity */ 1091 PRINT("0", 1); 1092 if (expt < ndig || (flags & ALT) != 0) { 1093 PRINT(decimal_point, 1); 1094 PAD(ndig - 1, zeroes); 1095 } 1096 } else if (expt <= 0) { 1097 PRINT("0", 1); 1098 PRINT(decimal_point, 1); 1099 PAD(-expt, zeroes); 1100 PRINT(cp, ndig); 1101 } else if (expt >= ndig) { 1102 PRINT(cp, ndig); 1103 PAD(expt - ndig, zeroes); 1104 if (flags & ALT) 1105 PRINT(decimal_point, 1); 1106 } else { 1107 PRINT(cp, expt); 1108 cp += expt; 1109 PRINT(decimal_point, 1); 1110 PRINT(cp, ndig-expt); 1111 } 1112 } else { /* 'e' or 'E' */ 1113 if (ndig > 1 || flags & ALT) { 1114 ox[0] = *cp++; 1115 ox[1] = *decimal_point; 1116 PRINT(ox, 2); 1117 if (_double) { 1118 PRINT(cp, ndig-1); 1119 } else /* 0.[0..] */ 1120 /* __dtoa irregularity */ 1121 PAD(ndig - 1, zeroes); 1122 } else /* XeYYY */ 1123 PRINT(cp, 1); 1124 PRINT(expstr, expsize); 1125 } 1126 } 1127 #else 1128 PRINT(cp, size); 1129 #endif 1130 /* left-adjusting padding (always blank) */ 1131 if (flags & LADJUST) 1132 PAD(width - realsz, blanks); 1133 1134 /* finally, adjust ret */ 1135 ret += prsize; 1136 1137 FLUSH(); /* copy out the I/O vectors */ 1138 } 1139 done: 1140 FLUSH(); 1141 error: 1142 #ifdef FLOATING_POINT 1143 if (dtoaresult != NULL) 1144 __freedtoa(dtoaresult); 1145 #endif 1146 if (convbuf != NULL) 1147 free(convbuf); 1148 if (__sferror(fp)) 1149 ret = EOF; 1150 if ((argtable != NULL) && (argtable != statargtable)) 1151 free (argtable); 1152 return (ret); 1153 /* NOTREACHED */ 1154 } 1155 1156 /* 1157 * Find all arguments when a positional parameter is encountered. Returns a 1158 * table, indexed by argument number, of pointers to each arguments. The 1159 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. 1160 * It will be replaces with a malloc-ed one if it overflows. 1161 */ 1162 static void 1163 __find_arguments (const char *fmt0, va_list ap, union arg **argtable) 1164 { 1165 char *fmt; /* format string */ 1166 int ch; /* character from fmt */ 1167 int n, n2; /* handy integer (short term usage) */ 1168 char *cp; /* handy char pointer (short term usage) */ 1169 int flags; /* flags as above */ 1170 int width; /* width from format (%8d), or 0 */ 1171 enum typeid *typetable; /* table of types */ 1172 enum typeid stattypetable [STATIC_ARG_TBL_SIZE]; 1173 int tablesize; /* current size of type table */ 1174 int tablemax; /* largest used index in table */ 1175 int nextarg; /* 1-based argument index */ 1176 1177 /* 1178 * Add an argument type to the table, expanding if necessary. 1179 */ 1180 #define ADDTYPE(type) \ 1181 ((nextarg >= tablesize) ? \ 1182 __grow_type_table(nextarg, &typetable, &tablesize) : 0, \ 1183 (nextarg > tablemax) ? tablemax = nextarg : 0, \ 1184 typetable[nextarg++] = type) 1185 1186 #define ADDSARG() \ 1187 ((flags&INTMAXT) ? ADDTYPE(T_INTMAXT) : \ 1188 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \ 1189 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \ 1190 ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \ 1191 ((flags&LONGINT) ? ADDTYPE(T_LONG) : ADDTYPE(T_INT)))))) 1192 1193 #define ADDUARG() \ 1194 ((flags&INTMAXT) ? ADDTYPE(T_UINTMAXT) : \ 1195 ((flags&SIZET) ? ADDTYPE(T_SIZET) : \ 1196 ((flags&PTRDIFFT) ? ADDTYPE(T_PTRDIFFT) : \ 1197 ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \ 1198 ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : ADDTYPE(T_U_INT)))))) 1199 1200 /* 1201 * Add * arguments to the type array. 1202 */ 1203 #define ADDASTER() \ 1204 n2 = 0; \ 1205 cp = fmt; \ 1206 while (is_digit(*cp)) { \ 1207 n2 = 10 * n2 + to_digit(*cp); \ 1208 cp++; \ 1209 } \ 1210 if (*cp == '$') { \ 1211 int hold = nextarg; \ 1212 nextarg = n2; \ 1213 ADDTYPE (T_INT); \ 1214 nextarg = hold; \ 1215 fmt = ++cp; \ 1216 } else { \ 1217 ADDTYPE (T_INT); \ 1218 } 1219 fmt = (char *)fmt0; 1220 typetable = stattypetable; 1221 tablesize = STATIC_ARG_TBL_SIZE; 1222 tablemax = 0; 1223 nextarg = 1; 1224 memset (typetable, T_UNUSED, STATIC_ARG_TBL_SIZE); 1225 1226 /* 1227 * Scan the format for conversions (`%' character). 1228 */ 1229 for (;;) { 1230 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 1231 /* void */; 1232 if (ch == '\0') 1233 goto done; 1234 fmt++; /* skip over '%' */ 1235 1236 flags = 0; 1237 width = 0; 1238 1239 rflag: ch = *fmt++; 1240 reswitch: switch (ch) { 1241 case ' ': 1242 case '#': 1243 goto rflag; 1244 case '*': 1245 ADDASTER (); 1246 goto rflag; 1247 case '-': 1248 case '+': 1249 case '\'': 1250 goto rflag; 1251 case '.': 1252 if ((ch = *fmt++) == '*') { 1253 ADDASTER (); 1254 goto rflag; 1255 } 1256 while (is_digit(ch)) { 1257 ch = *fmt++; 1258 } 1259 goto reswitch; 1260 case '0': 1261 goto rflag; 1262 case '1': case '2': case '3': case '4': 1263 case '5': case '6': case '7': case '8': case '9': 1264 n = 0; 1265 do { 1266 n = 10 * n + to_digit(ch); 1267 ch = *fmt++; 1268 } while (is_digit(ch)); 1269 if (ch == '$') { 1270 nextarg = n; 1271 goto rflag; 1272 } 1273 width = n; 1274 goto reswitch; 1275 #ifdef FLOATING_POINT 1276 case 'L': 1277 flags |= LONGDBL; 1278 goto rflag; 1279 #endif 1280 case 'h': 1281 if (flags & SHORTINT) { 1282 flags &= ~SHORTINT; 1283 flags |= CHARINT; 1284 } else 1285 flags |= SHORTINT; 1286 goto rflag; 1287 case 'j': 1288 flags |= INTMAXT; 1289 goto rflag; 1290 case 'l': 1291 if (flags & LONGINT) { 1292 flags &= ~LONGINT; 1293 flags |= LLONGINT; 1294 } else 1295 flags |= LONGINT; 1296 goto rflag; 1297 case 'q': 1298 flags |= LLONGINT; /* not necessarily */ 1299 goto rflag; 1300 case 't': 1301 flags |= PTRDIFFT; 1302 goto rflag; 1303 case 'z': 1304 flags |= SIZET; 1305 goto rflag; 1306 case 'C': 1307 flags |= LONGINT; 1308 /*FALLTHROUGH*/ 1309 case 'c': 1310 if (flags & LONGINT) 1311 ADDTYPE(T_WINT); 1312 else 1313 ADDTYPE(T_INT); 1314 break; 1315 case 'D': 1316 flags |= LONGINT; 1317 /*FALLTHROUGH*/ 1318 case 'd': 1319 case 'i': 1320 ADDSARG(); 1321 break; 1322 #ifdef FLOATING_POINT 1323 #ifdef HEXFLOAT 1324 case 'a': 1325 case 'A': 1326 #endif 1327 case 'e': 1328 case 'E': 1329 case 'f': 1330 case 'g': 1331 case 'G': 1332 if (flags & LONGDBL) 1333 ADDTYPE(T_LONG_DOUBLE); 1334 else 1335 ADDTYPE(T_DOUBLE); 1336 break; 1337 #endif /* FLOATING_POINT */ 1338 case 'n': 1339 if (flags & INTMAXT) 1340 ADDTYPE(TP_INTMAXT); 1341 else if (flags & PTRDIFFT) 1342 ADDTYPE(TP_PTRDIFFT); 1343 else if (flags & SIZET) 1344 ADDTYPE(TP_SIZET); 1345 else if (flags & LLONGINT) 1346 ADDTYPE(TP_LLONG); 1347 else if (flags & LONGINT) 1348 ADDTYPE(TP_LONG); 1349 else if (flags & SHORTINT) 1350 ADDTYPE(TP_SHORT); 1351 else if (flags & CHARINT) 1352 ADDTYPE(TP_SCHAR); 1353 else 1354 ADDTYPE(TP_INT); 1355 continue; /* no output */ 1356 case 'O': 1357 flags |= LONGINT; 1358 /*FALLTHROUGH*/ 1359 case 'o': 1360 ADDUARG(); 1361 break; 1362 case 'p': 1363 ADDTYPE(TP_VOID); 1364 break; 1365 case 'S': 1366 flags |= LONGINT; 1367 /*FALLTHROUGH*/ 1368 case 's': 1369 if (flags & LONGINT) 1370 ADDTYPE(TP_WCHAR); 1371 else 1372 ADDTYPE(TP_CHAR); 1373 break; 1374 case 'U': 1375 flags |= LONGINT; 1376 /*FALLTHROUGH*/ 1377 case 'u': 1378 case 'X': 1379 case 'x': 1380 ADDUARG(); 1381 break; 1382 default: /* "%?" prints ?, unless ? is NUL */ 1383 if (ch == '\0') 1384 goto done; 1385 break; 1386 } 1387 } 1388 done: 1389 /* 1390 * Build the argument table. 1391 */ 1392 if (tablemax >= STATIC_ARG_TBL_SIZE) { 1393 *argtable = (union arg *) 1394 malloc (sizeof (union arg) * (tablemax + 1)); 1395 } 1396 1397 (*argtable) [0].intarg = 0; 1398 for (n = 1; n <= tablemax; n++) { 1399 switch (typetable [n]) { 1400 case T_UNUSED: /* whoops! */ 1401 (*argtable) [n].intarg = va_arg (ap, int); 1402 break; 1403 case TP_SCHAR: 1404 (*argtable) [n].pschararg = va_arg (ap, signed char *); 1405 break; 1406 case TP_SHORT: 1407 (*argtable) [n].pshortarg = va_arg (ap, short *); 1408 break; 1409 case T_INT: 1410 (*argtable) [n].intarg = va_arg (ap, int); 1411 break; 1412 case T_U_INT: 1413 (*argtable) [n].uintarg = va_arg (ap, unsigned int); 1414 break; 1415 case TP_INT: 1416 (*argtable) [n].pintarg = va_arg (ap, int *); 1417 break; 1418 case T_LONG: 1419 (*argtable) [n].longarg = va_arg (ap, long); 1420 break; 1421 case T_U_LONG: 1422 (*argtable) [n].ulongarg = va_arg (ap, unsigned long); 1423 break; 1424 case TP_LONG: 1425 (*argtable) [n].plongarg = va_arg (ap, long *); 1426 break; 1427 case T_LLONG: 1428 (*argtable) [n].longlongarg = va_arg (ap, long long); 1429 break; 1430 case T_U_LLONG: 1431 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long); 1432 break; 1433 case TP_LLONG: 1434 (*argtable) [n].plonglongarg = va_arg (ap, long long *); 1435 break; 1436 case T_PTRDIFFT: 1437 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t); 1438 break; 1439 case TP_PTRDIFFT: 1440 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *); 1441 break; 1442 case T_SIZET: 1443 (*argtable) [n].sizearg = va_arg (ap, size_t); 1444 break; 1445 case TP_SIZET: 1446 (*argtable) [n].psizearg = va_arg (ap, ssize_t *); 1447 break; 1448 case T_INTMAXT: 1449 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t); 1450 break; 1451 case T_UINTMAXT: 1452 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t); 1453 break; 1454 case TP_INTMAXT: 1455 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *); 1456 break; 1457 #ifdef FLOATING_POINT 1458 case T_DOUBLE: 1459 (*argtable) [n].doublearg = va_arg (ap, double); 1460 break; 1461 case T_LONG_DOUBLE: 1462 (*argtable) [n].longdoublearg = va_arg (ap, long double); 1463 break; 1464 #endif 1465 case TP_CHAR: 1466 (*argtable) [n].pchararg = va_arg (ap, char *); 1467 break; 1468 case TP_VOID: 1469 (*argtable) [n].pvoidarg = va_arg (ap, void *); 1470 break; 1471 case T_WINT: 1472 (*argtable) [n].wintarg = va_arg (ap, wint_t); 1473 break; 1474 case TP_WCHAR: 1475 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *); 1476 break; 1477 } 1478 } 1479 1480 if ((typetable != NULL) && (typetable != stattypetable)) 1481 free (typetable); 1482 } 1483 1484 /* 1485 * Increase the size of the type table. 1486 */ 1487 static void 1488 __grow_type_table (int nextarg, enum typeid **typetable, int *tablesize) 1489 { 1490 enum typeid *const oldtable = *typetable; 1491 const int oldsize = *tablesize; 1492 enum typeid *newtable; 1493 int newsize = oldsize * 2; 1494 1495 if (newsize < nextarg + 1) 1496 newsize = nextarg + 1; 1497 if (oldsize == STATIC_ARG_TBL_SIZE) { 1498 if ((newtable = malloc(newsize)) == NULL) 1499 abort(); /* XXX handle better */ 1500 bcopy(oldtable, newtable, oldsize); 1501 } else { 1502 if ((newtable = reallocf(oldtable, newsize)) == NULL) 1503 abort(); /* XXX handle better */ 1504 } 1505 memset(&newtable[oldsize], T_UNUSED, newsize - oldsize); 1506 1507 *typetable = newtable; 1508 *tablesize = newsize; 1509 } 1510 1511 1512 #ifdef FLOATING_POINT 1513 1514 static char * 1515 cvt(double value, int ndigits, int flags, char *sign, int *decpt, 1516 int ch, int *length) 1517 { 1518 int mode, dsgn; 1519 char *digits, *bp, *rve; 1520 1521 if (ch == 'f') 1522 mode = 3; /* ndigits after the decimal point */ 1523 else { 1524 /* 1525 * To obtain ndigits after the decimal point for the 'e' 1526 * and 'E' formats, round to ndigits + 1 significant 1527 * figures. 1528 */ 1529 if (ch == 'e' || ch == 'E') 1530 ndigits++; 1531 mode = 2; /* ndigits significant digits */ 1532 } 1533 digits = __dtoa(value, mode, ndigits, decpt, &dsgn, &rve); 1534 *sign = dsgn != 0; 1535 if ((ch != 'g' && ch != 'G') || flags & ALT) { 1536 /* print trailing zeros */ 1537 bp = digits + ndigits; 1538 if (ch == 'f') { 1539 if ((*digits == '0' || *digits == '\0') && value) 1540 *decpt = -ndigits + 1; 1541 bp += *decpt; 1542 } 1543 if (value == 0) /* kludge for __dtoa irregularity */ 1544 rve = bp; 1545 while (rve < bp) 1546 *rve++ = '0'; 1547 } 1548 *length = rve - digits; 1549 return (digits); 1550 } 1551 1552 static int 1553 exponent(char *p0, int exp, int fmtch) 1554 { 1555 char *p, *t; 1556 char expbuf[MAXEXP]; 1557 1558 p = p0; 1559 *p++ = fmtch; 1560 if (exp < 0) { 1561 exp = -exp; 1562 *p++ = '-'; 1563 } 1564 else 1565 *p++ = '+'; 1566 t = expbuf + MAXEXP; 1567 if (exp > 9) { 1568 do { 1569 *--t = to_char(exp % 10); 1570 } while ((exp /= 10) > 9); 1571 *--t = to_char(exp); 1572 for (; t < expbuf + MAXEXP; *p++ = *t++); 1573 } 1574 else { 1575 *p++ = '0'; 1576 *p++ = to_char(exp); 1577 } 1578 return (p - p0); 1579 } 1580 #endif /* FLOATING_POINT */ 1581