1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Chris Torek. 9 * 10 * Copyright (c) 2011 The FreeBSD Foundation 11 * 12 * Portions of this software were developed by David Chisnall 13 * under sponsorship from the FreeBSD Foundation. 14 * 15 * Redistribution and use in source and binary forms, with or without 16 * modification, are permitted provided that the following conditions 17 * are met: 18 * 1. Redistributions of source code must retain the above copyright 19 * notice, this list of conditions and the following disclaimer. 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in the 22 * documentation and/or other materials provided with the distribution. 23 * 3. Neither the name of the University nor the names of its contributors 24 * may be used to endorse or promote products derived from this software 25 * without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 37 * SUCH DAMAGE. 38 */ 39 40 #if 0 41 #if defined(LIBC_SCCS) && !defined(lint) 42 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 43 #endif /* LIBC_SCCS and not lint */ 44 #endif 45 #include <sys/cdefs.h> 46 /* 47 * Actual wprintf innards. 48 * 49 * Avoid making gratuitous changes to this source file; it should be kept 50 * as close as possible to vfprintf.c for ease of maintenance. 51 */ 52 53 #include "namespace.h" 54 #include <sys/types.h> 55 56 #include <ctype.h> 57 #include <errno.h> 58 #include <limits.h> 59 #include <locale.h> 60 #include <stdarg.h> 61 #include <stddef.h> 62 #include <stdint.h> 63 #include <stdio.h> 64 #include <stdlib.h> 65 #include <string.h> 66 #include <wchar.h> 67 #include <wctype.h> 68 #include "un-namespace.h" 69 70 #include "libc_private.h" 71 #include "local.h" 72 #include "fvwrite.h" 73 #include "printflocal.h" 74 #include "xlocale_private.h" 75 76 static int __sprint(FILE *, struct __suio *, locale_t); 77 static int __sbprintf(FILE *, locale_t, const wchar_t *, va_list) __noinline; 78 static wint_t __xfputwc(wchar_t, FILE *, locale_t); 79 static wchar_t *__mbsconv(char *, int); 80 81 #define CHAR wchar_t 82 #include "printfcommon.h" 83 84 struct grouping_state { 85 wchar_t thousands_sep; /* locale-specific thousands separator */ 86 const char *grouping; /* locale-specific numeric grouping rules */ 87 int lead; /* sig figs before decimal or group sep */ 88 int nseps; /* number of group separators with ' */ 89 int nrepeats; /* number of repeats of the last group */ 90 }; 91 92 static const mbstate_t initial_mbs; 93 94 static inline wchar_t 95 get_decpt(locale_t locale) 96 { 97 mbstate_t mbs; 98 wchar_t decpt; 99 int nconv; 100 101 mbs = initial_mbs; 102 nconv = mbrtowc(&decpt, localeconv_l(locale)->decimal_point, MB_CUR_MAX, &mbs); 103 if (nconv == (size_t)-1 || nconv == (size_t)-2) 104 decpt = '.'; /* failsafe */ 105 return (decpt); 106 } 107 108 static inline wchar_t 109 get_thousep(locale_t locale) 110 { 111 mbstate_t mbs; 112 wchar_t thousep; 113 int nconv; 114 115 mbs = initial_mbs; 116 nconv = mbrtowc(&thousep, localeconv_l(locale)->thousands_sep, 117 MB_CUR_MAX, &mbs); 118 if (nconv == (size_t)-1 || nconv == (size_t)-2) 119 thousep = '\0'; /* failsafe */ 120 return (thousep); 121 } 122 123 /* 124 * Initialize the thousands' grouping state in preparation to print a 125 * number with ndigits digits. This routine returns the total number 126 * of wide characters that will be printed. 127 */ 128 static int 129 grouping_init(struct grouping_state *gs, int ndigits, locale_t locale) 130 { 131 132 gs->grouping = localeconv_l(locale)->grouping; 133 gs->thousands_sep = get_thousep(locale); 134 135 gs->nseps = gs->nrepeats = 0; 136 gs->lead = ndigits; 137 while (*gs->grouping != CHAR_MAX) { 138 if (gs->lead <= *gs->grouping) 139 break; 140 gs->lead -= *gs->grouping; 141 if (*(gs->grouping+1)) { 142 gs->nseps++; 143 gs->grouping++; 144 } else 145 gs->nrepeats++; 146 } 147 return (gs->nseps + gs->nrepeats); 148 } 149 150 /* 151 * Print a number with thousands' separators. 152 */ 153 static int 154 grouping_print(struct grouping_state *gs, struct io_state *iop, 155 const CHAR *cp, const CHAR *ep, locale_t locale) 156 { 157 const CHAR *cp0 = cp; 158 159 if (io_printandpad(iop, cp, ep, gs->lead, zeroes, locale)) 160 return (-1); 161 cp += gs->lead; 162 while (gs->nseps > 0 || gs->nrepeats > 0) { 163 if (gs->nrepeats > 0) 164 gs->nrepeats--; 165 else { 166 gs->grouping--; 167 gs->nseps--; 168 } 169 if (io_print(iop, &gs->thousands_sep, 1, locale)) 170 return (-1); 171 if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, locale)) 172 return (-1); 173 cp += *gs->grouping; 174 } 175 if (cp > ep) 176 cp = ep; 177 return (cp - cp0); 178 } 179 180 181 /* 182 * Flush out all the vectors defined by the given uio, 183 * then reset it so that it can be reused. 184 * 185 * XXX The fact that we do this a character at a time and convert to a 186 * multibyte character sequence even if the destination is a wide 187 * string eclipses the benefits of buffering. 188 */ 189 static int 190 __sprint(FILE *fp, struct __suio *uio, locale_t locale) 191 { 192 struct __siov *iov; 193 wchar_t *p; 194 int i, len; 195 196 iov = uio->uio_iov; 197 for (; uio->uio_resid != 0; uio->uio_resid -= len, iov++) { 198 p = (wchar_t *)iov->iov_base; 199 len = iov->iov_len; 200 for (i = 0; i < len; i++) { 201 if (__xfputwc(p[i], fp, locale) == WEOF) 202 return (-1); 203 } 204 } 205 uio->uio_iovcnt = 0; 206 return (0); 207 } 208 209 /* 210 * Helper function for `fprintf to unbuffered unix file': creates a 211 * temporary buffer. We only work on write-only files; this avoids 212 * worries about ungetc buffers and so forth. 213 */ 214 static int 215 __sbprintf(FILE *fp, locale_t locale, const wchar_t *fmt, va_list ap) 216 { 217 int ret; 218 FILE fake; 219 unsigned char buf[BUFSIZ]; 220 221 /* XXX This is probably not needed. */ 222 if (prepwrite(fp) != 0) 223 return (EOF); 224 225 /* copy the important variables */ 226 fake._flags = fp->_flags & ~__SNBF; 227 fake._file = fp->_file; 228 fake._cookie = fp->_cookie; 229 fake._write = fp->_write; 230 fake._orientation = fp->_orientation; 231 fake._mbstate = fp->_mbstate; 232 233 /* set up the buffer */ 234 fake._bf._base = fake._p = buf; 235 fake._bf._size = fake._w = sizeof(buf); 236 fake._lbfsize = 0; /* not actually used, but Just In Case */ 237 238 /* do the work, then copy any error status */ 239 ret = __vfwprintf(&fake, locale, fmt, ap); 240 if (ret >= 0 && __fflush(&fake)) 241 ret = WEOF; 242 if (fake._flags & __SERR) 243 fp->_flags |= __SERR; 244 return (ret); 245 } 246 247 /* 248 * Like __fputwc, but handles fake string (__SSTR) files properly. 249 * File must already be locked. 250 */ 251 static wint_t 252 __xfputwc(wchar_t wc, FILE *fp, locale_t locale) 253 { 254 mbstate_t mbs; 255 char buf[MB_LEN_MAX]; 256 struct __suio uio; 257 struct __siov iov; 258 size_t len; 259 260 if ((fp->_flags & __SSTR) == 0) 261 return (__fputwc(wc, fp, locale)); 262 263 mbs = initial_mbs; 264 if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) { 265 fp->_flags |= __SERR; 266 return (WEOF); 267 } 268 uio.uio_iov = &iov; 269 uio.uio_resid = len; 270 uio.uio_iovcnt = 1; 271 iov.iov_base = buf; 272 iov.iov_len = len; 273 return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF); 274 } 275 276 /* 277 * Convert a multibyte character string argument for the %s format to a wide 278 * string representation. ``prec'' specifies the maximum number of bytes 279 * to output. If ``prec'' is greater than or equal to zero, we can't assume 280 * that the multibyte char. string ends in a null character. 281 */ 282 static wchar_t * 283 __mbsconv(char *mbsarg, int prec) 284 { 285 mbstate_t mbs; 286 wchar_t *convbuf, *wcp; 287 const char *p; 288 size_t insize, nchars, nconv; 289 290 if (mbsarg == NULL) 291 return (NULL); 292 293 /* 294 * Supplied argument is a multibyte string; convert it to wide 295 * characters first. 296 */ 297 if (prec >= 0) { 298 /* 299 * String is not guaranteed to be NUL-terminated. Find the 300 * number of characters to print. 301 */ 302 p = mbsarg; 303 insize = nchars = nconv = 0; 304 mbs = initial_mbs; 305 while (nchars != (size_t)prec) { 306 nconv = mbrlen(p, MB_CUR_MAX, &mbs); 307 if (nconv == 0 || nconv == (size_t)-1 || 308 nconv == (size_t)-2) 309 break; 310 p += nconv; 311 nchars++; 312 insize += nconv; 313 } 314 if (nconv == (size_t)-1 || nconv == (size_t)-2) 315 return (NULL); 316 } else { 317 insize = strlen(mbsarg); 318 nconv = 0; 319 } 320 321 /* 322 * Allocate buffer for the result and perform the conversion, 323 * converting at most `size' bytes of the input multibyte string to 324 * wide characters for printing. 325 */ 326 convbuf = malloc((insize + 1) * sizeof(*convbuf)); 327 if (convbuf == NULL) 328 return (NULL); 329 wcp = convbuf; 330 p = mbsarg; 331 mbs = initial_mbs; 332 while (insize != 0) { 333 nconv = mbrtowc(wcp, p, insize, &mbs); 334 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2) 335 break; 336 wcp++; 337 p += nconv; 338 insize -= nconv; 339 } 340 if (nconv == (size_t)-1 || nconv == (size_t)-2) { 341 free(convbuf); 342 return (NULL); 343 } 344 *wcp = L'\0'; 345 346 return (convbuf); 347 } 348 349 /* 350 * MT-safe version 351 */ 352 int 353 vfwprintf_l(FILE * __restrict fp, locale_t locale, 354 const wchar_t * __restrict fmt0, va_list ap) 355 356 { 357 int ret; 358 FIX_LOCALE(locale); 359 FLOCKFILE_CANCELSAFE(fp); 360 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 361 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 362 fp->_file >= 0) 363 ret = __sbprintf(fp, locale, fmt0, ap); 364 else 365 ret = __vfwprintf(fp, locale, fmt0, ap); 366 FUNLOCKFILE_CANCELSAFE(); 367 return (ret); 368 } 369 int 370 vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap) 371 { 372 return vfwprintf_l(fp, __get_locale(), fmt0, ap); 373 } 374 375 /* 376 * The size of the buffer we use as scratch space for integer 377 * conversions, among other things. We need enough space to 378 * write a uintmax_t in octal (plus one byte). 379 */ 380 #if UINTMAX_MAX <= UINT64_MAX 381 #define BUF 32 382 #else 383 #error "BUF must be large enough to format a uintmax_t" 384 #endif 385 386 /* 387 * Non-MT-safe version 388 */ 389 int 390 __vfwprintf(FILE *fp, locale_t locale, const wchar_t *fmt0, va_list ap) 391 { 392 wchar_t *fmt; /* format string */ 393 wchar_t ch; /* character from fmt */ 394 int n, n2; /* handy integer (short term usage) */ 395 wchar_t *cp; /* handy char pointer (short term usage) */ 396 int flags; /* flags as above */ 397 int ret; /* return value accumulator */ 398 int width; /* width from format (%8d), or 0 */ 399 int prec; /* precision from format; <0 for N/A */ 400 wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */ 401 struct grouping_state gs; /* thousands' grouping info */ 402 #ifndef NO_FLOATING_POINT 403 /* 404 * We can decompose the printed representation of floating 405 * point numbers into several parts, some of which may be empty: 406 * 407 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ 408 * A B ---C--- D E F 409 * 410 * A: 'sign' holds this value if present; '\0' otherwise 411 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal 412 * C: cp points to the string MMMNNN. Leading and trailing 413 * zeros are not in the string and must be added. 414 * D: expchar holds this character; '\0' if no exponent, e.g. %f 415 * F: at least two digits for decimal, at least one digit for hex 416 */ 417 wchar_t decimal_point; /* locale specific decimal point */ 418 int signflag; /* true if float is negative */ 419 union { /* floating point arguments %[aAeEfFgG] */ 420 double dbl; 421 long double ldbl; 422 } fparg; 423 int expt; /* integer value of exponent */ 424 char expchar; /* exponent character: [eEpP\0] */ 425 char *dtoaend; /* pointer to end of converted digits */ 426 int expsize; /* character count for expstr */ 427 int ndig; /* actual number of digits returned by dtoa */ 428 wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ 429 char *dtoaresult; /* buffer allocated by dtoa */ 430 #endif 431 u_long ulval; /* integer arguments %[diouxX] */ 432 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 433 int base; /* base for [diouxX] conversion */ 434 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 435 int realsz; /* field size expanded by dprec, sign, etc */ 436 int size; /* size of converted field or string */ 437 int prsize; /* max size of printed field */ 438 const char *xdigs; /* digits for [xX] conversion */ 439 struct io_state io; /* I/O buffering state */ 440 wchar_t buf[BUF]; /* buffer with space for digits of uintmax_t */ 441 wchar_t ox[2]; /* space for 0x hex-prefix */ 442 union arg *argtable; /* args, built due to positional arg */ 443 union arg statargtable [STATIC_ARG_TBL_SIZE]; 444 int nextarg; /* 1-based argument index */ 445 va_list orgap; /* original argument pointer */ 446 wchar_t *convbuf; /* multibyte to wide conversion result */ 447 int savserr; 448 449 static const char xdigs_lower[16] = "0123456789abcdef"; 450 static const char xdigs_upper[16] = "0123456789ABCDEF"; 451 452 /* BEWARE, these `goto error' on error. */ 453 #define PRINT(ptr, len) do { \ 454 if (io_print(&io, (ptr), (len), locale)) \ 455 goto error; \ 456 } while (0) 457 #define PAD(howmany, with) { \ 458 if (io_pad(&io, (howmany), (with), locale)) \ 459 goto error; \ 460 } 461 #define PRINTANDPAD(p, ep, len, with) { \ 462 if (io_printandpad(&io, (p), (ep), (len), (with), locale)) \ 463 goto error; \ 464 } 465 #define FLUSH() { \ 466 if (io_flush(&io, locale)) \ 467 goto error; \ 468 } 469 470 /* 471 * Get the argument indexed by nextarg. If the argument table is 472 * built, use it to get the argument. If its not, get the next 473 * argument (and arguments must be gotten sequentially). 474 */ 475 #define GETARG(type) \ 476 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ 477 (nextarg++, va_arg(ap, type))) 478 479 /* 480 * To extend shorts properly, we need both signed and unsigned 481 * argument extraction methods. 482 */ 483 #define SARG() \ 484 (flags&LONGINT ? GETARG(long) : \ 485 flags&SHORTINT ? (long)(short)GETARG(int) : \ 486 flags&CHARINT ? (long)(signed char)GETARG(int) : \ 487 (long)GETARG(int)) 488 #define UARG() \ 489 (flags&LONGINT ? GETARG(u_long) : \ 490 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 491 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 492 (u_long)GETARG(u_int)) 493 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 494 #define SJARG() \ 495 (flags&INTMAXT ? GETARG(intmax_t) : \ 496 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \ 497 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 498 (intmax_t)GETARG(long long)) 499 #define UJARG() \ 500 (flags&INTMAXT ? GETARG(uintmax_t) : \ 501 flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 502 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \ 503 (uintmax_t)GETARG(unsigned long long)) 504 505 /* 506 * Get * arguments, including the form *nn$. Preserve the nextarg 507 * that the argument can be gotten once the type is determined. 508 */ 509 #define GETASTER(val) \ 510 n2 = 0; \ 511 cp = fmt; \ 512 while (is_digit(*cp)) { \ 513 n2 = 10 * n2 + to_digit(*cp); \ 514 cp++; \ 515 } \ 516 if (*cp == '$') { \ 517 int hold = nextarg; \ 518 if (argtable == NULL) { \ 519 argtable = statargtable; \ 520 if (__find_warguments (fmt0, orgap, &argtable)) { \ 521 ret = EOF; \ 522 goto error; \ 523 } \ 524 } \ 525 nextarg = n2; \ 526 val = GETARG (int); \ 527 nextarg = hold; \ 528 fmt = ++cp; \ 529 } else { \ 530 val = GETARG (int); \ 531 } 532 533 534 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */ 535 if (prepwrite(fp) != 0) { 536 errno = EBADF; 537 return (EOF); 538 } 539 540 savserr = fp->_flags & __SERR; 541 fp->_flags &= ~__SERR; 542 543 convbuf = NULL; 544 fmt = (wchar_t *)fmt0; 545 argtable = NULL; 546 nextarg = 1; 547 va_copy(orgap, ap); 548 io_init(&io, fp); 549 ret = 0; 550 #ifndef NO_FLOATING_POINT 551 decimal_point = get_decpt(locale); 552 #endif 553 554 /* 555 * Scan the format for conversions (`%' character). 556 */ 557 for (;;) { 558 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 559 /* void */; 560 if ((n = fmt - cp) != 0) { 561 if ((unsigned)ret + n > INT_MAX) { 562 ret = EOF; 563 errno = EOVERFLOW; 564 goto error; 565 } 566 PRINT(cp, n); 567 ret += n; 568 } 569 if (ch == '\0') 570 goto done; 571 fmt++; /* skip over '%' */ 572 573 flags = 0; 574 dprec = 0; 575 width = 0; 576 prec = -1; 577 gs.grouping = NULL; 578 sign = '\0'; 579 ox[1] = '\0'; 580 581 rflag: ch = *fmt++; 582 reswitch: switch (ch) { 583 case ' ': 584 /*- 585 * ``If the space and + flags both appear, the space 586 * flag will be ignored.'' 587 * -- ANSI X3J11 588 */ 589 if (!sign) 590 sign = ' '; 591 goto rflag; 592 case '#': 593 flags |= ALT; 594 goto rflag; 595 case '*': 596 /*- 597 * ``A negative field width argument is taken as a 598 * - flag followed by a positive field width.'' 599 * -- ANSI X3J11 600 * They don't exclude field widths read from args. 601 */ 602 GETASTER (width); 603 if (width >= 0) 604 goto rflag; 605 width = -width; 606 /* FALLTHROUGH */ 607 case '-': 608 flags |= LADJUST; 609 goto rflag; 610 case '+': 611 sign = '+'; 612 goto rflag; 613 case '\'': 614 flags |= GROUPING; 615 goto rflag; 616 case '.': 617 if ((ch = *fmt++) == '*') { 618 GETASTER (prec); 619 goto rflag; 620 } 621 prec = 0; 622 while (is_digit(ch)) { 623 prec = 10 * prec + to_digit(ch); 624 ch = *fmt++; 625 } 626 goto reswitch; 627 case '0': 628 /*- 629 * ``Note that 0 is taken as a flag, not as the 630 * beginning of a field width.'' 631 * -- ANSI X3J11 632 */ 633 flags |= ZEROPAD; 634 goto rflag; 635 case '1': case '2': case '3': case '4': 636 case '5': case '6': case '7': case '8': case '9': 637 n = 0; 638 do { 639 n = 10 * n + to_digit(ch); 640 ch = *fmt++; 641 } while (is_digit(ch)); 642 if (ch == '$') { 643 nextarg = n; 644 if (argtable == NULL) { 645 argtable = statargtable; 646 if (__find_warguments (fmt0, orgap, 647 &argtable)) { 648 ret = EOF; 649 goto error; 650 } 651 } 652 goto rflag; 653 } 654 width = n; 655 goto reswitch; 656 #ifndef NO_FLOATING_POINT 657 case 'L': 658 flags |= LONGDBL; 659 goto rflag; 660 #endif 661 case 'h': 662 if (flags & SHORTINT) { 663 flags &= ~SHORTINT; 664 flags |= CHARINT; 665 } else 666 flags |= SHORTINT; 667 goto rflag; 668 case 'j': 669 flags |= INTMAXT; 670 goto rflag; 671 case 'l': 672 if (flags & LONGINT) { 673 flags &= ~LONGINT; 674 flags |= LLONGINT; 675 } else 676 flags |= LONGINT; 677 goto rflag; 678 case 'q': 679 flags |= LLONGINT; /* not necessarily */ 680 goto rflag; 681 case 't': 682 flags |= PTRDIFFT; 683 goto rflag; 684 case 'w': 685 /* 686 * Fixed-width integer types. On all platforms we 687 * support, int8_t is equivalent to char, int16_t 688 * is equivalent to short, int32_t is equivalent 689 * to int, int64_t is equivalent to long long int. 690 * Furthermore, int_fast8_t, int_fast16_t and 691 * int_fast32_t are equivalent to int, and 692 * int_fast64_t is equivalent to long long int. 693 */ 694 flags &= ~(CHARINT|SHORTINT|LONGINT|LLONGINT|INTMAXT); 695 if (fmt[0] == 'f') { 696 flags |= FASTINT; 697 fmt++; 698 } else { 699 flags &= ~FASTINT; 700 } 701 if (fmt[0] == '8') { 702 if (!(flags & FASTINT)) 703 flags |= CHARINT; 704 else 705 /* no flag set = 32 */ ; 706 fmt += 1; 707 } else if (fmt[0] == '1' && fmt[1] == '6') { 708 if (!(flags & FASTINT)) 709 flags |= SHORTINT; 710 else 711 /* no flag set = 32 */ ; 712 fmt += 2; 713 } else if (fmt[0] == '3' && fmt[1] == '2') { 714 /* no flag set = 32 */ ; 715 fmt += 2; 716 } else if (fmt[0] == '6' && fmt[1] == '4') { 717 flags |= LLONGINT; 718 fmt += 2; 719 } else { 720 if (flags & FASTINT) { 721 flags &= ~FASTINT; 722 fmt--; 723 } 724 goto invalid; 725 } 726 goto rflag; 727 case 'z': 728 flags |= SIZET; 729 goto rflag; 730 case 'B': 731 case 'b': 732 if (flags & INTMAX_SIZE) 733 ujval = UJARG(); 734 else 735 ulval = UARG(); 736 base = 2; 737 /* leading 0b/B only if non-zero */ 738 if (flags & ALT && 739 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 740 ox[1] = ch; 741 goto nosign; 742 break; 743 case 'C': 744 flags |= LONGINT; 745 /*FALLTHROUGH*/ 746 case 'c': 747 if (flags & LONGINT) 748 *(cp = buf) = (wchar_t)GETARG(wint_t); 749 else 750 *(cp = buf) = (wchar_t)btowc(GETARG(int)); 751 size = 1; 752 sign = '\0'; 753 break; 754 case 'D': 755 flags |= LONGINT; 756 /*FALLTHROUGH*/ 757 case 'd': 758 case 'i': 759 if (flags & INTMAX_SIZE) { 760 ujval = SJARG(); 761 if ((intmax_t)ujval < 0) { 762 ujval = -ujval; 763 sign = '-'; 764 } 765 } else { 766 ulval = SARG(); 767 if ((long)ulval < 0) { 768 ulval = -ulval; 769 sign = '-'; 770 } 771 } 772 base = 10; 773 goto number; 774 #ifndef NO_FLOATING_POINT 775 case 'a': 776 case 'A': 777 if (ch == 'a') { 778 ox[1] = 'x'; 779 xdigs = xdigs_lower; 780 expchar = 'p'; 781 } else { 782 ox[1] = 'X'; 783 xdigs = xdigs_upper; 784 expchar = 'P'; 785 } 786 if (prec >= 0) 787 prec++; 788 if (flags & LONGDBL) { 789 fparg.ldbl = GETARG(long double); 790 dtoaresult = 791 __hldtoa(fparg.ldbl, xdigs, prec, 792 &expt, &signflag, &dtoaend); 793 } else { 794 fparg.dbl = GETARG(double); 795 dtoaresult = 796 __hdtoa(fparg.dbl, xdigs, prec, 797 &expt, &signflag, &dtoaend); 798 } 799 if (prec < 0) 800 prec = dtoaend - dtoaresult; 801 if (expt == INT_MAX) 802 ox[1] = '\0'; 803 if (convbuf != NULL) 804 free(convbuf); 805 ndig = dtoaend - dtoaresult; 806 cp = convbuf = __mbsconv(dtoaresult, -1); 807 freedtoa(dtoaresult); 808 goto fp_common; 809 case 'e': 810 case 'E': 811 expchar = ch; 812 if (prec < 0) /* account for digit before decpt */ 813 prec = DEFPREC + 1; 814 else 815 prec++; 816 goto fp_begin; 817 case 'f': 818 case 'F': 819 expchar = '\0'; 820 goto fp_begin; 821 case 'g': 822 case 'G': 823 expchar = ch - ('g' - 'e'); 824 if (prec == 0) 825 prec = 1; 826 fp_begin: 827 if (prec < 0) 828 prec = DEFPREC; 829 if (convbuf != NULL) 830 free(convbuf); 831 if (flags & LONGDBL) { 832 fparg.ldbl = GETARG(long double); 833 dtoaresult = 834 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, 835 &expt, &signflag, &dtoaend); 836 } else { 837 fparg.dbl = GETARG(double); 838 dtoaresult = 839 dtoa(fparg.dbl, expchar ? 2 : 3, prec, 840 &expt, &signflag, &dtoaend); 841 if (expt == 9999) 842 expt = INT_MAX; 843 } 844 ndig = dtoaend - dtoaresult; 845 cp = convbuf = __mbsconv(dtoaresult, -1); 846 freedtoa(dtoaresult); 847 fp_common: 848 if (signflag) 849 sign = '-'; 850 if (expt == INT_MAX) { /* inf or nan */ 851 if (*cp == 'N') { 852 cp = (ch >= 'a') ? L"nan" : L"NAN"; 853 sign = '\0'; 854 } else 855 cp = (ch >= 'a') ? L"inf" : L"INF"; 856 size = 3; 857 flags &= ~ZEROPAD; 858 break; 859 } 860 flags |= FPT; 861 if (ch == 'g' || ch == 'G') { 862 if (expt > -4 && expt <= prec) { 863 /* Make %[gG] smell like %[fF] */ 864 expchar = '\0'; 865 if (flags & ALT) 866 prec -= expt; 867 else 868 prec = ndig - expt; 869 if (prec < 0) 870 prec = 0; 871 } else { 872 /* 873 * Make %[gG] smell like %[eE], but 874 * trim trailing zeroes if no # flag. 875 */ 876 if (!(flags & ALT)) 877 prec = ndig; 878 } 879 } 880 if (expchar) { 881 expsize = exponent(expstr, expt - 1, expchar); 882 size = expsize + prec; 883 if (prec > 1 || flags & ALT) 884 ++size; 885 } else { 886 /* space for digits before decimal point */ 887 if (expt > 0) 888 size = expt; 889 else /* "0" */ 890 size = 1; 891 /* space for decimal pt and following digits */ 892 if (prec || flags & ALT) 893 size += prec + 1; 894 if ((flags & GROUPING) && expt > 0) 895 size += grouping_init(&gs, expt, locale); 896 } 897 break; 898 #endif /* !NO_FLOATING_POINT */ 899 case 'n': 900 /* 901 * Assignment-like behavior is specified if the 902 * value overflows or is otherwise unrepresentable. 903 * C99 says to use `signed char' for %hhn conversions. 904 */ 905 if (flags & LLONGINT) 906 *GETARG(long long *) = ret; 907 else if (flags & SIZET) 908 *GETARG(ssize_t *) = (ssize_t)ret; 909 else if (flags & PTRDIFFT) 910 *GETARG(ptrdiff_t *) = ret; 911 else if (flags & INTMAXT) 912 *GETARG(intmax_t *) = ret; 913 else if (flags & LONGINT) 914 *GETARG(long *) = ret; 915 else if (flags & SHORTINT) 916 *GETARG(short *) = ret; 917 else if (flags & CHARINT) 918 *GETARG(signed char *) = ret; 919 else 920 *GETARG(int *) = ret; 921 continue; /* no output */ 922 case 'O': 923 flags |= LONGINT; 924 /*FALLTHROUGH*/ 925 case 'o': 926 if (flags & INTMAX_SIZE) 927 ujval = UJARG(); 928 else 929 ulval = UARG(); 930 base = 8; 931 goto nosign; 932 case 'p': 933 /*- 934 * ``The argument shall be a pointer to void. The 935 * value of the pointer is converted to a sequence 936 * of printable characters, in an implementation- 937 * defined manner.'' 938 * -- ANSI X3J11 939 */ 940 ujval = (uintmax_t)(uintptr_t)GETARG(void *); 941 base = 16; 942 xdigs = xdigs_lower; 943 flags = flags | INTMAXT; 944 ox[1] = 'x'; 945 goto nosign; 946 case 'S': 947 flags |= LONGINT; 948 /*FALLTHROUGH*/ 949 case 's': 950 if (flags & LONGINT) { 951 if ((cp = GETARG(wchar_t *)) == NULL) 952 cp = L"(null)"; 953 } else { 954 char *mbp; 955 956 if (convbuf != NULL) 957 free(convbuf); 958 if ((mbp = GETARG(char *)) == NULL) 959 cp = L"(null)"; 960 else { 961 convbuf = __mbsconv(mbp, prec); 962 if (convbuf == NULL) { 963 fp->_flags |= __SERR; 964 goto error; 965 } 966 cp = convbuf; 967 } 968 } 969 size = (prec >= 0) ? wcsnlen(cp, prec) : wcslen(cp); 970 sign = '\0'; 971 break; 972 case 'U': 973 flags |= LONGINT; 974 /*FALLTHROUGH*/ 975 case 'u': 976 if (flags & INTMAX_SIZE) 977 ujval = UJARG(); 978 else 979 ulval = UARG(); 980 base = 10; 981 goto nosign; 982 case 'X': 983 xdigs = xdigs_upper; 984 goto hex; 985 case 'x': 986 xdigs = xdigs_lower; 987 hex: 988 if (flags & INTMAX_SIZE) 989 ujval = UJARG(); 990 else 991 ulval = UARG(); 992 base = 16; 993 /* leading 0x/X only if non-zero */ 994 if (flags & ALT && 995 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 996 ox[1] = ch; 997 998 flags &= ~GROUPING; 999 /* unsigned conversions */ 1000 nosign: sign = '\0'; 1001 /*- 1002 * ``... diouXx conversions ... if a precision is 1003 * specified, the 0 flag will be ignored.'' 1004 * -- ANSI X3J11 1005 */ 1006 number: if ((dprec = prec) >= 0) 1007 flags &= ~ZEROPAD; 1008 1009 /*- 1010 * ``The result of converting a zero value with an 1011 * explicit precision of zero is no characters.'' 1012 * -- ANSI X3J11 1013 * 1014 * ``The C Standard is clear enough as is. The call 1015 * printf("%#.0o", 0) should print 0.'' 1016 * -- Defect Report #151 1017 */ 1018 cp = buf + BUF; 1019 if (flags & INTMAX_SIZE) { 1020 if (ujval != 0 || prec != 0 || 1021 (flags & ALT && base == 8)) 1022 cp = __ujtoa(ujval, cp, base, 1023 flags & ALT, xdigs); 1024 } else { 1025 if (ulval != 0 || prec != 0 || 1026 (flags & ALT && base == 8)) 1027 cp = __ultoa(ulval, cp, base, 1028 flags & ALT, xdigs); 1029 } 1030 size = buf + BUF - cp; 1031 if (size > BUF) /* should never happen */ 1032 abort(); 1033 if ((flags & GROUPING) && size != 0) 1034 size += grouping_init(&gs, size, locale); 1035 break; 1036 default: /* "%?" prints ?, unless ? is NUL */ 1037 if (ch == '\0') 1038 goto done; 1039 invalid: 1040 /* pretend it was %c with argument ch */ 1041 cp = buf; 1042 *cp = ch; 1043 size = 1; 1044 sign = '\0'; 1045 break; 1046 } 1047 1048 /* 1049 * All reasonable formats wind up here. At this point, `cp' 1050 * points to a string which (if not flags&LADJUST) should be 1051 * padded out to `width' places. If flags&ZEROPAD, it should 1052 * first be prefixed by any sign or other prefix; otherwise, 1053 * it should be blank padded before the prefix is emitted. 1054 * After any left-hand padding and prefixing, emit zeroes 1055 * required by a decimal [diouxX] precision, then print the 1056 * string proper, then emit zeroes required by any leftover 1057 * floating precision; finally, if LADJUST, pad with blanks. 1058 * 1059 * Compute actual size, so we know how much to pad. 1060 * size excludes decimal prec; realsz includes it. 1061 */ 1062 realsz = dprec > size ? dprec : size; 1063 if (sign) 1064 realsz++; 1065 if (ox[1]) 1066 realsz += 2; 1067 1068 prsize = width > realsz ? width : realsz; 1069 if ((unsigned)ret + prsize > INT_MAX) { 1070 ret = EOF; 1071 errno = EOVERFLOW; 1072 goto error; 1073 } 1074 1075 /* right-adjusting blank padding */ 1076 if ((flags & (LADJUST|ZEROPAD)) == 0) 1077 PAD(width - realsz, blanks); 1078 1079 /* prefix */ 1080 if (sign) 1081 PRINT(&sign, 1); 1082 1083 if (ox[1]) { /* ox[1] is either x, X, or \0 */ 1084 ox[0] = '0'; 1085 PRINT(ox, 2); 1086 } 1087 1088 /* right-adjusting zero padding */ 1089 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 1090 PAD(width - realsz, zeroes); 1091 1092 /* the string or number proper */ 1093 #ifndef NO_FLOATING_POINT 1094 if ((flags & FPT) == 0) { 1095 #endif 1096 /* leading zeroes from decimal precision */ 1097 PAD(dprec - size, zeroes); 1098 if (gs.grouping) { 1099 if (grouping_print(&gs, &io, cp, buf+BUF, locale) < 0) 1100 goto error; 1101 } else { 1102 PRINT(cp, size); 1103 } 1104 #ifndef NO_FLOATING_POINT 1105 } else { /* glue together f_p fragments */ 1106 if (!expchar) { /* %[fF] or sufficiently short %[gG] */ 1107 if (expt <= 0) { 1108 PRINT(zeroes, 1); 1109 if (prec || flags & ALT) 1110 PRINT(&decimal_point, 1); 1111 PAD(-expt, zeroes); 1112 /* already handled initial 0's */ 1113 prec += expt; 1114 } else { 1115 if (gs.grouping) { 1116 n = grouping_print(&gs, &io, 1117 cp, convbuf + ndig, locale); 1118 if (n < 0) 1119 goto error; 1120 cp += n; 1121 } else { 1122 PRINTANDPAD(cp, convbuf + ndig, 1123 expt, zeroes); 1124 cp += expt; 1125 } 1126 if (prec || flags & ALT) 1127 PRINT(&decimal_point, 1); 1128 } 1129 PRINTANDPAD(cp, convbuf + ndig, prec, zeroes); 1130 } else { /* %[eE] or sufficiently long %[gG] */ 1131 if (prec > 1 || flags & ALT) { 1132 buf[0] = *cp++; 1133 buf[1] = decimal_point; 1134 PRINT(buf, 2); 1135 PRINT(cp, ndig-1); 1136 PAD(prec - ndig, zeroes); 1137 } else /* XeYYY */ 1138 PRINT(cp, 1); 1139 PRINT(expstr, expsize); 1140 } 1141 } 1142 #endif 1143 /* left-adjusting padding (always blank) */ 1144 if (flags & LADJUST) 1145 PAD(width - realsz, blanks); 1146 1147 /* finally, adjust ret */ 1148 ret += prsize; 1149 1150 FLUSH(); /* copy out the I/O vectors */ 1151 } 1152 done: 1153 FLUSH(); 1154 error: 1155 va_end(orgap); 1156 if (convbuf != NULL) 1157 free(convbuf); 1158 if (__sferror(fp)) 1159 ret = EOF; 1160 else 1161 fp->_flags |= savserr; 1162 if ((argtable != NULL) && (argtable != statargtable)) 1163 free (argtable); 1164 return (ret); 1165 /* NOTREACHED */ 1166 } 1167