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