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