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