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