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