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