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