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