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