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