1 /* $NetBSD: vfwprintf.c,v 1.40 2023/04/04 19:26:06 christos Exp $ */ 2 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 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 39 __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.27 2007/01/09 00:28:08 imp Exp $"); 40 #else 41 __RCSID("$NetBSD: vfwprintf.c,v 1.40 2023/04/04 19:26:06 christos Exp $"); 42 #endif 43 #endif /* LIBC_SCCS and not lint */ 44 45 /* 46 * Actual {w,}printf innards. 47 */ 48 49 #include "namespace.h" 50 #include <sys/types.h> 51 52 #include <assert.h> 53 #include <ctype.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 <errno.h> 63 #include <wchar.h> 64 #include <wctype.h> 65 66 #include "reentrant.h" 67 #include "setlocale_local.h" 68 #include "local.h" 69 #include "extern.h" 70 #include "fvwrite.h" 71 72 #ifndef NARROW 73 #define MCHAR_T char 74 #define CHAR_T wchar_t 75 #define STRLEN(a) wcslen(a) 76 #define MEMCHR(a, b, c) wmemchr(a, b, c) 77 #define SCONV(a, b, loc) __mbsconv(a, b, loc) 78 #define STRCONST(a) L ## a 79 #define WDECL(a, b) a ## w ## b 80 #define END_OF_FILE WEOF 81 #define MULTI 0 82 #else 83 #define MCHAR_T wchar_t 84 #define CHAR_T char 85 #define STRLEN(a) strlen(a) 86 #define MEMCHR(a, b, c) memchr(a, b, c) 87 #define SCONV(a, b, loc) __wcsconv(a, b, loc) 88 #define STRCONST(a) a 89 #define WDECL(a, b) a ## b 90 #define END_OF_FILE EOF 91 #define MULTI LONGINT 92 #endif 93 94 union arg { 95 int intarg; 96 u_int uintarg; 97 long longarg; 98 u_long ulongarg; 99 long long longlongarg; 100 unsigned long long ulonglongarg; 101 ptrdiff_t ptrdiffarg; 102 ssize_t ssizearg; 103 size_t sizearg; 104 intmax_t intmaxarg; 105 uintmax_t uintmaxarg; 106 void *pvoidarg; 107 char *pchararg; 108 signed char *pschararg; 109 short *pshortarg; 110 int *pintarg; 111 long *plongarg; 112 long long *plonglongarg; 113 ptrdiff_t *pptrdiffarg; 114 size_t *psizearg; 115 intmax_t *pintmaxarg; 116 #ifndef NO_FLOATING_POINT 117 double doublearg; 118 long double longdoublearg; 119 #endif 120 wint_t wintarg; 121 wchar_t *pwchararg; 122 }; 123 124 /* 125 * Type ids for argument type table. 126 */ 127 enum typeid { 128 T_UNUSED = 0, TP_SHORT, T_INT, T_U_INT, TP_INT, 129 T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG, 130 T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SIZET, 131 T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR, 132 T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR 133 }; 134 135 #ifdef NARROW 136 __printflike(3, 0) 137 #endif 138 static int __sbprintf(FILE *, locale_t, const CHAR_T *, va_list); 139 140 static CHAR_T *__ujtoa(uintmax_t, CHAR_T *, int, int, const char *, int, 141 char, const char *); 142 static CHAR_T *__ultoa(u_long, CHAR_T *, int, int, const char *, int, 143 char, const char *); 144 #ifndef NARROW 145 static CHAR_T *__mbsconv(char *, int, locale_t); 146 static wint_t __xfputwc(CHAR_T, FILE *, locale_t); 147 #else 148 static char *__wcsconv(wchar_t *, int, locale_t); 149 static int __sprint(FILE *, struct __suio *); 150 #endif 151 static int __find_arguments(const CHAR_T *, va_list, union arg **); 152 static int __grow_type_table(size_t, enum typeid **, size_t *); 153 154 /* 155 * Helper function for `fprintf to unbuffered unix file': creates a 156 * temporary buffer. We only work on write-only files; this avoids 157 * worries about ungetc buffers and so forth. 158 */ 159 static int 160 __sbprintf(FILE *fp, locale_t loc, const CHAR_T *fmt, va_list ap) 161 { 162 int ret; 163 FILE fake; 164 struct __sfileext fakeext; 165 unsigned char buf[BUFSIZ]; 166 167 _DIAGASSERT(fp != NULL); 168 _DIAGASSERT(fmt != NULL); 169 170 _FILEEXT_SETUP(&fake, &fakeext); 171 memset(WCIO_GET(&fake), 0, sizeof(struct wchar_io_data)); 172 173 /* copy the important variables */ 174 fake._flags = fp->_flags & ~__SNBF; 175 fake._file = fp->_file; 176 fake._cookie = fp->_cookie; 177 fake._write = fp->_write; 178 fake._flush = fp->_flush; 179 180 /* set up the buffer */ 181 fake._bf._base = fake._p = buf; 182 fake._bf._size = fake._w = sizeof(buf); 183 fake._lbfsize = 0; /* not actually used, but Just In Case */ 184 185 /* do the work, then copy any error status */ 186 ret = WDECL(__vf,printf_unlocked_l)(&fake, loc, fmt, ap); 187 if (ret >= 0 && fflush(&fake)) 188 ret = END_OF_FILE; 189 if (fake._flags & __SERR) 190 fp->_flags |= __SERR; 191 return ret; 192 } 193 194 #ifndef NARROW 195 /* 196 * Like __fputwc, but handles fake string (__SSTR) files properly. 197 * File must already be locked. 198 */ 199 static wint_t 200 __xfputwc(wchar_t wc, FILE *fp, locale_t loc) 201 { 202 static const mbstate_t initial; 203 mbstate_t mbs; 204 char buf[MB_LEN_MAX]; 205 struct __suio uio; 206 struct __siov iov; 207 size_t len; 208 209 if ((fp->_flags & __SSTR) == 0) 210 return __fputwc_unlock(wc, fp); 211 212 mbs = initial; 213 if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) { 214 fp->_flags |= __SERR; 215 return END_OF_FILE; 216 } 217 uio.uio_iov = &iov; 218 uio.uio_resid = len; 219 uio.uio_iovcnt = 1; 220 iov.iov_base = buf; 221 iov.iov_len = len; 222 return __sfvwrite(fp, &uio) != EOF ? (wint_t)wc : END_OF_FILE; 223 } 224 #else 225 /* 226 * Flush out all the vectors defined by the given uio, 227 * then reset it so that it can be reused. 228 */ 229 static int 230 __sprint(FILE *fp, struct __suio *uio) 231 { 232 int err; 233 234 _DIAGASSERT(fp != NULL); 235 _DIAGASSERT(uio != NULL); 236 237 if (uio->uio_resid == 0) { 238 uio->uio_iovcnt = 0; 239 return 0; 240 } 241 err = __sfvwrite(fp, uio); 242 uio->uio_resid = 0; 243 uio->uio_iovcnt = 0; 244 return err; 245 } 246 #endif 247 248 /* 249 * Macros for converting digits to letters and vice versa 250 */ 251 #define to_digit(c) ((c) - '0') 252 #define is_digit(c) ((unsigned)to_digit(c) <= 9) 253 #define to_char(n) (CHAR_T)((n) + '0') 254 255 /* 256 * Convert an unsigned long to ASCII for printf purposes, returning 257 * a pointer to the first character of the string representation. 258 * Octal numbers can be forced to have a leading zero; hex numbers 259 * use the given digits. 260 */ 261 static CHAR_T * 262 __ultoa(u_long val, CHAR_T *endp, int base, int octzero, const char *xdigs, 263 int needgrp, char thousep, const char *grp) 264 { 265 CHAR_T *cp = endp; 266 long sval; 267 int ndig; 268 269 /* 270 * Handle the three cases separately, in the hope of getting 271 * better/faster code. 272 */ 273 switch (base) { 274 case 10: 275 if (val < 10) { /* many numbers are 1 digit */ 276 *--cp = to_char(val); 277 return cp; 278 } 279 ndig = 0; 280 /* 281 * On many machines, unsigned arithmetic is harder than 282 * signed arithmetic, so we do at most one unsigned mod and 283 * divide; this is sufficient to reduce the range of 284 * the incoming value to where signed arithmetic works. 285 */ 286 if (val > LONG_MAX) { 287 *--cp = to_char(val % 10); 288 ndig++; 289 sval = val / 10; 290 } else 291 sval = val; 292 do { 293 *--cp = to_char(sval % 10); 294 ndig++; 295 /* 296 * If (*grp == CHAR_MAX) then no more grouping 297 * should be performed. 298 */ 299 if (needgrp && ndig == *grp 300 && (unsigned char)*grp != (unsigned char)CHAR_MAX 301 && sval > 9) { 302 *--cp = thousep; 303 ndig = 0; 304 /* 305 * If (*(grp+1) == '\0') then we have to 306 * use *grp character (last grouping rule) 307 * for all next cases 308 */ 309 if (*(grp+1) != '\0') 310 grp++; 311 } 312 sval /= 10; 313 } while (sval != 0); 314 break; 315 316 case 8: 317 do { 318 *--cp = to_char(val & 7); 319 val >>= 3; 320 } while (val); 321 if (octzero && *cp != '0') 322 *--cp = '0'; 323 break; 324 325 case 16: 326 do { 327 *--cp = xdigs[(size_t)val & 15]; 328 val >>= 4; 329 } while (val); 330 break; 331 332 default: /* oops */ 333 abort(); 334 } 335 return cp; 336 } 337 338 /* Identical to __ultoa, but for intmax_t. */ 339 static CHAR_T * 340 __ujtoa(uintmax_t val, CHAR_T *endp, int base, int octzero, 341 const char *xdigs, int needgrp, char thousep, const char *grp) 342 { 343 CHAR_T *cp = endp; 344 intmax_t sval; 345 int ndig; 346 347 /* quick test for small values; __ultoa is typically much faster */ 348 /* (perhaps instead we should run until small, then call __ultoa?) */ 349 if (val <= ULONG_MAX) 350 return __ultoa((u_long)val, endp, base, octzero, xdigs, 351 needgrp, thousep, grp); 352 switch (base) { 353 case 10: 354 if (val < 10) { 355 *--cp = to_char(val % 10); 356 return cp; 357 } 358 ndig = 0; 359 if (val > INTMAX_MAX) { 360 *--cp = to_char(val % 10); 361 ndig++; 362 sval = val / 10; 363 } else 364 sval = val; 365 do { 366 *--cp = to_char(sval % 10); 367 ndig++; 368 /* 369 * If (*grp == CHAR_MAX) then no more grouping 370 * should be performed. 371 */ 372 if (needgrp 373 && (unsigned char)*grp != (unsigned char)CHAR_MAX 374 && ndig == *grp 375 && sval > 9) { 376 *--cp = thousep; 377 ndig = 0; 378 /* 379 * If (*(grp+1) == '\0') then we have to 380 * use *grp character (last grouping rule) 381 * for all next cases 382 */ 383 if (*(grp+1) != '\0') 384 grp++; 385 } 386 sval /= 10; 387 } while (sval != 0); 388 break; 389 390 case 8: 391 do { 392 *--cp = to_char(val & 7); 393 val >>= 3; 394 } while (val); 395 if (octzero && *cp != '0') 396 *--cp = '0'; 397 break; 398 399 case 16: 400 do { 401 *--cp = xdigs[(size_t)val & 15]; 402 val >>= 4; 403 } while (val); 404 break; 405 406 default: 407 abort(); 408 } 409 return cp; 410 } 411 412 #ifndef NARROW 413 /* 414 * Convert a multibyte character string argument for the %s format to a wide 415 * string representation. ``prec'' specifies the maximum number of bytes 416 * to output. If ``prec'' is greater than or equal to zero, we can't assume 417 * that the multibyte char. string ends in a null character. 418 */ 419 static wchar_t * 420 __mbsconv(char *mbsarg, int prec, locale_t loc) 421 { 422 static const mbstate_t initial; 423 mbstate_t mbs; 424 wchar_t *convbuf, *wcp; 425 const char *p; 426 size_t insize, nchars, nconv; 427 428 if (mbsarg == NULL) 429 return NULL; 430 431 /* 432 * Supplied argument is a multibyte string; convert it to wide 433 * characters first. 434 */ 435 if (prec >= 0) { 436 /* 437 * String is not guaranteed to be NUL-terminated. Find the 438 * number of characters to print. 439 */ 440 p = mbsarg; 441 insize = nchars = nconv = 0; 442 mbs = initial; 443 while (nchars != (size_t)prec) { 444 nconv = mbrlen_l(p, MB_CUR_MAX_L(loc), &mbs, loc); 445 if (nconv == 0 || nconv == (size_t)-1 || 446 nconv == (size_t)-2) 447 break; 448 p += nconv; 449 nchars++; 450 insize += nconv; 451 } 452 if (nconv == (size_t)-1 || nconv == (size_t)-2) 453 return NULL; 454 } else 455 insize = strlen(mbsarg); 456 457 /* 458 * Allocate buffer for the result and perform the conversion, 459 * converting at most `size' bytes of the input multibyte string to 460 * wide characters for printing. 461 */ 462 convbuf = NULL; 463 errno = reallocarr(&convbuf, insize + 1, sizeof(*convbuf)); 464 if (errno) 465 return NULL; 466 wcp = convbuf; 467 p = mbsarg; 468 mbs = initial; 469 nconv = 0; 470 while (insize != 0) { 471 nconv = mbrtowc_l(wcp, p, insize, &mbs, loc); 472 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2) 473 break; 474 wcp++; 475 p += nconv; 476 insize -= nconv; 477 } 478 if (nconv == (size_t)-1 || nconv == (size_t)-2) { 479 int serrno = errno; 480 free(convbuf); 481 errno = serrno; 482 return NULL; 483 } 484 *wcp = L'\0'; 485 486 return convbuf; 487 } 488 #else 489 /* 490 * Convert a wide-character string argument for the %ls format to a multibyte 491 * string representation. If not -1, prec specifies the maximum number of 492 * bytes to output, and also means that we can't assume that the wide-char. 493 * string ends is null-terminated. 494 */ 495 static char * 496 __wcsconv(wchar_t *wcsarg, int prec, locale_t loc) 497 { 498 static const mbstate_t initial; 499 mbstate_t mbs; 500 char buf[MB_LEN_MAX]; 501 wchar_t *p; 502 char *convbuf; 503 size_t clen, nbytes; 504 505 /* Allocate space for the maximum number of bytes we could output. */ 506 if (prec < 0) { 507 p = wcsarg; 508 mbs = initial; 509 nbytes = wcsrtombs_l(NULL, (void *)&p, 0, &mbs, loc); 510 if (nbytes == (size_t)-1) 511 return NULL; 512 } else { 513 /* 514 * Optimisation: if the output precision is small enough, 515 * just allocate enough memory for the maximum instead of 516 * scanning the string. 517 */ 518 if (prec < 128) 519 nbytes = prec; 520 else { 521 nbytes = 0; 522 p = wcsarg; 523 mbs = initial; 524 for (;;) { 525 clen = wcrtomb_l(buf, *p++, &mbs, loc); 526 if (clen == 0 || clen == (size_t)-1 || 527 nbytes + clen > (size_t)prec) 528 break; 529 nbytes += clen; 530 } 531 } 532 } 533 if ((convbuf = malloc(nbytes + 1)) == NULL) 534 return NULL; 535 536 /* Fill the output buffer. */ 537 p = wcsarg; 538 mbs = initial; 539 if ((nbytes = wcsrtombs_l(convbuf, (void *)&p, 540 nbytes, &mbs, loc)) == (size_t)-1) { 541 free(convbuf); 542 return NULL; 543 } 544 convbuf[nbytes] = '\0'; 545 return convbuf; 546 } 547 #endif 548 549 /* 550 * MT-safe version 551 */ 552 int 553 WDECL(vf,printf)(FILE * __restrict fp, const CHAR_T * __restrict fmt0, va_list ap) 554 { 555 int ret; 556 557 FLOCKFILE(fp); 558 ret = WDECL(__vf,printf_unlocked_l)(fp, _current_locale(), fmt0, ap); 559 FUNLOCKFILE(fp); 560 return ret; 561 } 562 563 int 564 WDECL(vf,printf_l)(FILE * __restrict fp, locale_t loc, const CHAR_T * __restrict fmt0, 565 va_list ap) 566 { 567 int ret; 568 569 FLOCKFILE(fp); 570 ret = WDECL(__vf,printf_unlocked_l)(fp, loc, fmt0, ap); 571 FUNLOCKFILE(fp); 572 return ret; 573 } 574 575 #ifndef NO_FLOATING_POINT 576 577 #include <float.h> 578 #include <math.h> 579 #include "floatio.h" 580 581 #define DEFPREC 6 582 583 static int exponent(CHAR_T *, int, int); 584 585 #endif /* !NO_FLOATING_POINT */ 586 587 /* 588 * The size of the buffer we use as scratch space for integer 589 * conversions, among other things. Technically, we would need the 590 * most space for base 10 conversions with thousands' grouping 591 * characters between each pair of digits. 100 bytes is a 592 * conservative overestimate even for a 128-bit uintmax_t. 593 */ 594 #define BUF 100 595 596 #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */ 597 598 /* 599 * Flags used during conversion. 600 */ 601 #define ALT 0x001 /* alternate form */ 602 #define LADJUST 0x004 /* left adjustment */ 603 #define LONGDBL 0x008 /* long double */ 604 #define LONGINT 0x010 /* long integer */ 605 #define LLONGINT 0x020 /* long long integer */ 606 #define SHORTINT 0x040 /* short integer */ 607 #define ZEROPAD 0x080 /* zero (as opposed to blank) pad */ 608 #define FPT 0x100 /* Floating point number */ 609 #define GROUPING 0x200 /* use grouping ("'" flag) */ 610 /* C99 additional size modifiers: */ 611 #define SIZET 0x400 /* size_t */ 612 #define PTRDIFFT 0x800 /* ptrdiff_t */ 613 #define INTMAXT 0x1000 /* intmax_t */ 614 #define CHARINT 0x2000 /* print char using int format */ 615 616 /* 617 * Non-MT-safe version 618 */ 619 int 620 WDECL(__vf,printf_unlocked_l)(FILE *fp, locale_t loc, const CHAR_T *fmt0, va_list ap) 621 { 622 CHAR_T *fmt; /* format string */ 623 int ch; /* character from fmt */ 624 int n, n2; /* handy integer (short term usage) */ 625 CHAR_T *cp; /* handy char pointer (short term usage) */ 626 int flags; /* flags as above */ 627 int ret; /* return value accumulator */ 628 int width; /* width from format (%8d), or 0 */ 629 int prec; /* precision from format; <0 for N/A */ 630 CHAR_T sign; /* sign prefix (' ', '+', '-', or \0) */ 631 char thousands_sep; /* locale specific thousands separator */ 632 const char *grouping; /* locale specific numeric grouping rules */ 633 #ifndef NO_FLOATING_POINT 634 /* 635 * We can decompose the printed representation of floating 636 * point numbers into several parts, some of which may be empty: 637 * 638 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ 639 * A B ---C--- D E F 640 * 641 * A: 'sign' holds this value if present; '\0' otherwise 642 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal 643 * C: cp points to the string MMMNNN. Leading and trailing 644 * zeros are not in the string and must be added. 645 * D: expchar holds this character; '\0' if no exponent, e.g. %f 646 * F: at least two digits for decimal, at least one digit for hex 647 */ 648 char *decimal_point; /* locale specific decimal point */ 649 int signflag; /* true if float is negative */ 650 union { /* floating point arguments %[aAeEfFgG] */ 651 double dbl; 652 #ifdef WIDE_DOUBLE 653 long double ldbl; 654 #endif 655 } fparg; 656 char *dtoaend; /* pointer to end of converted digits */ 657 char *dtoaresult; /* buffer allocated by dtoa */ 658 int expt; /* integer value of exponent */ 659 char expchar; /* exponent character: [eEpP\0] */ 660 int expsize; /* character count for expstr */ 661 int lead; /* sig figs before decimal or group sep */ 662 int ndig; /* actual number of digits returned by dtoa */ 663 CHAR_T expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ 664 int nseps; /* number of group separators with ' */ 665 int nrepeats; /* number of repeats of the last group */ 666 #endif 667 u_long ulval; /* integer arguments %[diouxX] */ 668 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 669 int base; /* base for [diouxX] conversion */ 670 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 671 int realsz; /* field size expanded by dprec, sign, etc */ 672 int size; /* size of converted field or string */ 673 int prsize; /* max size of printed field */ 674 const char *xdigs; /* digits for %[xX] conversion */ 675 #ifdef NARROW 676 #define NIOV 8 677 struct __siov *iovp; /* for PRINT macro */ 678 struct __suio uio; /* output information: summary */ 679 struct __siov iov[NIOV];/* ... and individual io vectors */ 680 #else 681 int n3; 682 #endif 683 CHAR_T buf[BUF]; /* buffer with space for digits of uintmax_t */ 684 CHAR_T ox[2]; /* space for 0x hex-prefix */ 685 union arg *argtable; /* args, built due to positional arg */ 686 union arg statargtable [STATIC_ARG_TBL_SIZE]; 687 int nextarg; /* 1-based argument index */ 688 va_list orgap; /* original argument pointer */ 689 CHAR_T *convbuf; /* multibyte to wide conversion result */ 690 691 /* 692 * Choose PADSIZE to trade efficiency vs. size. If larger printf 693 * fields occur frequently, increase PADSIZE and make the initialisers 694 * below longer. 695 */ 696 #define PADSIZE 16 /* pad chunk size */ 697 static CHAR_T blanks[PADSIZE] = 698 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '}; 699 static CHAR_T zeroes[PADSIZE] = 700 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'}; 701 702 static const char xdigs_lower[16] = "0123456789abcdef"; 703 static const char xdigs_upper[16] = "0123456789ABCDEF"; 704 705 /* 706 * BEWARE, these `goto error' on error, PRINT uses `n2' and 707 * PAD uses `n'. 708 */ 709 #ifndef NARROW 710 #define PRINT(ptr, len) do { \ 711 for (n3 = 0; n3 < (len); n3++) \ 712 if (__xfputwc((ptr)[n3], fp, loc) == END_OF_FILE) { \ 713 fp->_flags |= __SERR; \ 714 goto error; \ 715 } \ 716 } while (0) 717 #define FLUSH() 718 #else 719 #define PRINT(ptr, len) do { \ 720 iovp->iov_base = __UNCONST(ptr); \ 721 iovp->iov_len = (len); \ 722 uio.uio_resid += (len); \ 723 iovp++; \ 724 if (++uio.uio_iovcnt >= NIOV) { \ 725 if (__sprint(fp, &uio)) \ 726 goto error; \ 727 iovp = iov; \ 728 } \ 729 } while (0) 730 #define FLUSH() do { \ 731 if (uio.uio_resid && __sprint(fp, &uio)) \ 732 goto error; \ 733 uio.uio_iovcnt = 0; \ 734 iovp = iov; \ 735 } while (0) 736 #endif /* NARROW */ 737 738 #define PAD(howmany, with) do { \ 739 if ((n = (howmany)) > 0) { \ 740 while (n > PADSIZE) { \ 741 PRINT(with, PADSIZE); \ 742 n -= PADSIZE; \ 743 } \ 744 PRINT(with, n); \ 745 } \ 746 } while (0) 747 #define PRINTANDPAD(p, ep, len, with) do { \ 748 ptrdiff_t td = (ep) - (p); \ 749 _DIAGASSERT(__type_fit(int, td)); \ 750 n2 = (int)td; \ 751 if (n2 > (len)) \ 752 n2 = (len); \ 753 if (n2 > 0) \ 754 PRINT((p), n2); \ 755 PAD((len) - (n2 > 0 ? n2 : 0), (with)); \ 756 } while (0) 757 758 /* 759 * Get the argument indexed by nextarg. If the argument table is 760 * built, use it to get the argument. If its not, get the next 761 * argument (and arguments must be gotten sequentially). 762 */ 763 #define GETARG(type) \ 764 ((/*CONSTCOND*/argtable != NULL) ? *((type*)(void*)(&argtable[nextarg++])) : \ 765 (nextarg++, va_arg(ap, type))) 766 767 /* 768 * To extend shorts properly, we need both signed and unsigned 769 * argument extraction methods. 770 */ 771 #define SARG() \ 772 (flags&LONGINT ? GETARG(long) : \ 773 flags&SHORTINT ? (long)(short)GETARG(int) : \ 774 flags&CHARINT ? (long)(signed char)GETARG(int) : \ 775 (long)GETARG(int)) 776 #define UARG() \ 777 (flags&LONGINT ? GETARG(u_long) : \ 778 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 779 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 780 (u_long)GETARG(u_int)) 781 #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 782 #define SJARG() \ 783 (flags&INTMAXT ? GETARG(intmax_t) : \ 784 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \ 785 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 786 (intmax_t)GETARG(long long)) 787 #define UJARG() \ 788 (flags&INTMAXT ? GETARG(uintmax_t) : \ 789 flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 790 flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \ 791 (uintmax_t)GETARG(unsigned long long)) 792 793 /* 794 * Get * arguments, including the form *nn$. Preserve the nextarg 795 * that the argument can be gotten once the type is determined. 796 */ 797 #define GETASTER(val) \ 798 n2 = 0; \ 799 cp = fmt; \ 800 while (is_digit(*cp)) { \ 801 n2 = 10 * n2 + to_digit(*cp); \ 802 cp++; \ 803 } \ 804 if (*cp == '$') { \ 805 int hold = nextarg; \ 806 if (argtable == NULL) { \ 807 argtable = statargtable; \ 808 if (__find_arguments(fmt0, orgap, &argtable) == -1) \ 809 goto oomem; \ 810 } \ 811 nextarg = n2; \ 812 val = GETARG (int); \ 813 nextarg = hold; \ 814 fmt = ++cp; \ 815 } else { \ 816 val = GETARG (int); \ 817 } 818 819 _DIAGASSERT(fp != NULL); 820 _DIAGASSERT(fmt0 != NULL); 821 822 _SET_ORIENTATION(fp, -1); 823 824 thousands_sep = '\0'; 825 grouping = NULL; 826 #ifndef NO_FLOATING_POINT 827 decimal_point = localeconv_l(loc)->decimal_point; 828 expsize = 0; /* XXXGCC -Wuninitialized [sh3,m68000] */ 829 ndig = -1; /* XXX gcc */ 830 #endif 831 convbuf = NULL; 832 /* sorry, f{w,}printf(read_only_file, L"") returns {W,}EOF, not 0 */ 833 if (cantwrite(fp)) { 834 errno = EBADF; 835 return END_OF_FILE; 836 } 837 838 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 839 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 840 __sfileno(fp) != -1) 841 return __sbprintf(fp, loc, fmt0, ap); 842 843 fmt = (CHAR_T *)__UNCONST(fmt0); 844 argtable = NULL; 845 nextarg = 1; 846 va_copy(orgap, ap); 847 #ifdef NARROW 848 uio.uio_iov = iovp = iov; 849 uio.uio_resid = 0; 850 uio.uio_iovcnt = 0; 851 #endif 852 ret = 0; 853 854 /* 855 * Scan the format for conversions (`%' character). 856 */ 857 for (;;) { 858 const CHAR_T *result; 859 860 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 861 continue; 862 _DIAGASSERT(__type_fit(int, fmt - cp)); 863 if ((n = (int)(fmt - cp)) != 0) { 864 if ((unsigned)ret + n > INT_MAX) { 865 ret = END_OF_FILE; 866 goto error; 867 } 868 PRINT(cp, n); 869 ret += n; 870 } 871 if (ch == '\0') 872 goto done; 873 fmt++; /* skip over '%' */ 874 875 flags = 0; 876 dprec = 0; 877 width = 0; 878 prec = -1; 879 sign = '\0'; 880 ox[1] = '\0'; 881 #ifndef NO_FLOATING_POINT 882 expchar = '\0'; 883 lead = 0; 884 nseps = nrepeats = 0; 885 #endif 886 ulval = 0; 887 ujval = 0; 888 xdigs = NULL; 889 890 rflag: ch = *fmt++; 891 reswitch: switch (ch) { 892 case ' ': 893 /*- 894 * ``If the space and + flags both appear, the space 895 * flag will be ignored.'' 896 * -- ANSI X3J11 897 */ 898 if (!sign) 899 sign = ' '; 900 goto rflag; 901 case '#': 902 flags |= ALT; 903 goto rflag; 904 case '*': 905 /*- 906 * ``A negative field width argument is taken as a 907 * - flag followed by a positive field width.'' 908 * -- ANSI X3J11 909 * They don't exclude field widths read from args. 910 */ 911 GETASTER (width); 912 if (width >= 0) 913 goto rflag; 914 width = -width; 915 /* FALLTHROUGH */ 916 case '-': 917 flags |= LADJUST; 918 goto rflag; 919 case '+': 920 sign = '+'; 921 goto rflag; 922 case '\'': 923 thousands_sep = *(localeconv_l(loc)->thousands_sep); 924 grouping = localeconv_l(loc)->grouping; 925 /* Use grouping if defined by locale */ 926 if (thousands_sep && grouping && *grouping) 927 flags |= GROUPING; 928 else { 929 thousands_sep = '\0'; 930 grouping = NULL; 931 } 932 goto rflag; 933 case '.': 934 if ((ch = *fmt++) == '*') { 935 GETASTER (prec); 936 goto rflag; 937 } 938 prec = 0; 939 while (is_digit(ch)) { 940 prec = 10 * prec + to_digit(ch); 941 ch = *fmt++; 942 } 943 goto reswitch; 944 case '0': 945 /*- 946 * ``Note that 0 is taken as a flag, not as the 947 * beginning of a field width.'' 948 * -- ANSI X3J11 949 */ 950 flags |= ZEROPAD; 951 goto rflag; 952 case '1': case '2': case '3': case '4': 953 case '5': case '6': case '7': case '8': case '9': 954 n = 0; 955 do { 956 n = 10 * n + to_digit(ch); 957 ch = *fmt++; 958 } while (is_digit(ch)); 959 if (ch == '$') { 960 nextarg = n; 961 if (argtable == NULL) { 962 argtable = statargtable; 963 if (__find_arguments(fmt0, orgap, 964 &argtable) == -1) 965 goto oomem; 966 } 967 goto rflag; 968 } 969 width = n; 970 goto reswitch; 971 #ifndef NO_FLOATING_POINT 972 case 'L': 973 flags |= LONGDBL; 974 goto rflag; 975 #endif 976 case 'h': 977 if (flags & SHORTINT) { 978 flags &= ~SHORTINT; 979 flags |= CHARINT; 980 } else 981 flags |= SHORTINT; 982 goto rflag; 983 case 'j': 984 flags |= INTMAXT; 985 goto rflag; 986 case 'l': 987 if (flags & LONGINT) { 988 flags &= ~LONGINT; 989 flags |= LLONGINT; 990 } else 991 flags |= LONGINT; 992 goto rflag; 993 case 'q': 994 flags |= LLONGINT; /* not necessarily */ 995 goto rflag; 996 case 't': 997 flags |= PTRDIFFT; 998 goto rflag; 999 case 'z': 1000 flags |= SIZET; 1001 goto rflag; 1002 case 'C': 1003 flags |= LONGINT; 1004 /*FALLTHROUGH*/ 1005 case 'c': 1006 #ifdef NARROW 1007 if (flags & LONGINT) { 1008 static const mbstate_t initial; 1009 mbstate_t mbs; 1010 size_t mbseqlen; 1011 1012 mbs = initial; 1013 mbseqlen = wcrtomb_l(buf, 1014 (wchar_t)GETARG(wint_t), &mbs, loc); 1015 if (mbseqlen == (size_t)-1) { 1016 fp->_flags |= __SERR; 1017 goto error; 1018 } 1019 size = (int)mbseqlen; 1020 } else { 1021 *buf = GETARG(int); 1022 size = 1; 1023 } 1024 #else 1025 if (flags & LONGINT) 1026 *buf = (wchar_t)GETARG(wint_t); 1027 else 1028 *buf = (wchar_t)btowc_l(GETARG(int), loc); 1029 size = 1; 1030 #endif 1031 result = buf; 1032 sign = '\0'; 1033 break; 1034 case 'D': 1035 flags |= LONGINT; 1036 /*FALLTHROUGH*/ 1037 case 'd': 1038 case 'i': 1039 if (flags & INTMAX_SIZE) { 1040 ujval = SJARG(); 1041 if ((intmax_t)ujval < 0) { 1042 ujval = -ujval; 1043 sign = '-'; 1044 } 1045 } else { 1046 ulval = SARG(); 1047 if ((long)ulval < 0) { 1048 ulval = -ulval; 1049 sign = '-'; 1050 } 1051 } 1052 base = 10; 1053 goto number; 1054 #ifndef NO_FLOATING_POINT 1055 case 'a': 1056 case 'A': 1057 if (ch == 'a') { 1058 ox[1] = 'x'; 1059 xdigs = xdigs_lower; 1060 expchar = 'p'; 1061 } else { 1062 ox[1] = 'X'; 1063 xdigs = xdigs_upper; 1064 expchar = 'P'; 1065 } 1066 if (prec >= 0) 1067 prec++; 1068 #ifdef WIDE_DOUBLE 1069 if (flags & LONGDBL) { 1070 fparg.ldbl = GETARG(long double); 1071 dtoaresult = 1072 __hldtoa(fparg.ldbl, xdigs, prec, 1073 &expt, &signflag, &dtoaend); 1074 } else 1075 #endif 1076 { 1077 fparg.dbl = GETARG(double); 1078 dtoaresult = 1079 __hdtoa(fparg.dbl, xdigs, prec, 1080 &expt, &signflag, &dtoaend); 1081 } 1082 if (dtoaresult == NULL) 1083 goto oomem; 1084 1085 if (prec < 0) { 1086 _DIAGASSERT(__type_fit(int, 1087 dtoaend - dtoaresult)); 1088 prec = (int)(dtoaend - dtoaresult); 1089 } 1090 if (expt == INT_MAX) 1091 ox[1] = '\0'; 1092 _DIAGASSERT(__type_fit(int, dtoaend - dtoaresult)); 1093 ndig = (int)(dtoaend - dtoaresult); 1094 if (convbuf != NULL) 1095 free(convbuf); 1096 #ifndef NARROW 1097 result = convbuf = __mbsconv(dtoaresult, -1, loc); 1098 #else 1099 /*XXX inefficient*/ 1100 result = convbuf = strdup(dtoaresult); 1101 #endif 1102 if (result == NULL) 1103 goto oomem; 1104 __freedtoa(dtoaresult); 1105 goto fp_common; 1106 case 'e': 1107 case 'E': 1108 expchar = ch; 1109 if (prec < 0) /* account for digit before decpt */ 1110 prec = DEFPREC + 1; 1111 else 1112 prec++; 1113 goto fp_begin; 1114 case 'f': 1115 case 'F': 1116 expchar = '\0'; 1117 goto fp_begin; 1118 case 'g': 1119 case 'G': 1120 expchar = ch - ('g' - 'e'); 1121 if (prec == 0) 1122 prec = 1; 1123 fp_begin: 1124 if (prec < 0) 1125 prec = DEFPREC; 1126 #ifdef WIDE_DOUBLE 1127 if (flags & LONGDBL) { 1128 fparg.ldbl = GETARG(long double); 1129 dtoaresult = 1130 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, 1131 &expt, &signflag, &dtoaend); 1132 } else 1133 #endif 1134 { 1135 fparg.dbl = GETARG(double); 1136 dtoaresult = 1137 __dtoa(fparg.dbl, expchar ? 2 : 3, prec, 1138 &expt, &signflag, &dtoaend); 1139 if (expt == 9999) 1140 expt = INT_MAX; 1141 } 1142 if (dtoaresult == NULL) 1143 goto oomem; 1144 _DIAGASSERT(__type_fit(int, dtoaend - dtoaresult)); 1145 ndig = (int)(dtoaend - dtoaresult); 1146 if (convbuf != NULL) 1147 free(convbuf); 1148 #ifndef NARROW 1149 result = convbuf = __mbsconv(dtoaresult, -1, loc); 1150 #else 1151 /*XXX inefficient*/ 1152 result = convbuf = strdup(dtoaresult); 1153 #endif 1154 if (result == NULL) 1155 goto oomem; 1156 __freedtoa(dtoaresult); 1157 fp_common: 1158 if (signflag) 1159 sign = '-'; 1160 if (expt == INT_MAX) { /* inf or nan */ 1161 if (*result == 'N') { 1162 result = (ch >= 'a') ? STRCONST("nan") : 1163 STRCONST("NAN"); 1164 sign = '\0'; 1165 } else 1166 result = (ch >= 'a') ? STRCONST("inf") : 1167 STRCONST("INF"); 1168 size = 3; 1169 flags &= ~ZEROPAD; 1170 break; 1171 } 1172 1173 flags |= FPT; 1174 if (ch == 'g' || ch == 'G') { 1175 if (expt > -4 && expt <= prec) { 1176 /* Make %[gG] smell like %[fF] */ 1177 expchar = '\0'; 1178 if (flags & ALT) 1179 prec -= expt; 1180 else 1181 prec = ndig - expt; 1182 if (prec < 0) 1183 prec = 0; 1184 } else { 1185 /* 1186 * Make %[gG] smell like %[eE], but 1187 * trim trailing zeroes if no # flag. 1188 */ 1189 if (!(flags & ALT)) 1190 prec = ndig; 1191 } 1192 } 1193 if (expchar) { 1194 expsize = exponent(expstr, expt - 1, expchar); 1195 size = expsize + prec; 1196 if (prec > 1 || flags & ALT) 1197 ++size; 1198 } else { 1199 /* space for digits before decimal point */ 1200 if (expt > 0) 1201 size = expt; 1202 else /* "0" */ 1203 size = 1; 1204 /* space for decimal pt and following digits */ 1205 if (prec || flags & ALT) 1206 size += prec + 1; 1207 if (grouping && expt > 0) { 1208 /* space for thousands' grouping */ 1209 nseps = nrepeats = 0; 1210 lead = expt; 1211 while ((unsigned char)*grouping 1212 != (unsigned char)CHAR_MAX) { 1213 if (lead <= *grouping) 1214 break; 1215 lead -= *grouping; 1216 if (*(grouping+1)) { 1217 nseps++; 1218 grouping++; 1219 } else 1220 nrepeats++; 1221 } 1222 size += nseps + nrepeats; 1223 } else 1224 lead = expt; 1225 } 1226 break; 1227 #endif /* !NO_FLOATING_POINT */ 1228 case 'n': 1229 /* 1230 * Assignment-like behavior is specified if the 1231 * value overflows or is otherwise unrepresentable. 1232 * C99 says to use `signed char' for %hhn conversions. 1233 */ 1234 if (flags & LLONGINT) 1235 *GETARG(long long *) = ret; 1236 else if (flags & SIZET) 1237 *GETARG(ssize_t *) = (ssize_t)ret; 1238 else if (flags & PTRDIFFT) 1239 *GETARG(ptrdiff_t *) = ret; 1240 else if (flags & INTMAXT) 1241 *GETARG(intmax_t *) = ret; 1242 else if (flags & LONGINT) 1243 *GETARG(long *) = ret; 1244 else if (flags & SHORTINT) 1245 *GETARG(short *) = ret; 1246 else if (flags & CHARINT) 1247 *GETARG(signed char *) = ret; 1248 else 1249 *GETARG(int *) = ret; 1250 continue; /* no output */ 1251 case 'O': 1252 flags |= LONGINT; 1253 /*FALLTHROUGH*/ 1254 case 'o': 1255 if (flags & INTMAX_SIZE) 1256 ujval = UJARG(); 1257 else 1258 ulval = UARG(); 1259 base = 8; 1260 goto nosign; 1261 case 'p': 1262 /*- 1263 * ``The argument shall be a pointer to void. The 1264 * value of the pointer is converted to a sequence 1265 * of printable characters, in an implementation- 1266 * defined manner.'' 1267 * -- ANSI X3J11 1268 */ 1269 ujval = (uintmax_t)(uintptr_t)GETARG(void *); 1270 base = 16; 1271 xdigs = xdigs_lower; 1272 flags = flags | INTMAXT; 1273 ox[1] = 'x'; 1274 goto nosign; 1275 case 'S': 1276 flags |= LONGINT; 1277 /*FALLTHROUGH*/ 1278 case 's': 1279 if ((flags & LONGINT) != MULTI) { 1280 if ((result = GETARG(CHAR_T *)) == NULL) 1281 result = STRCONST("(null)"); 1282 } else { 1283 MCHAR_T *mc; 1284 1285 if (convbuf != NULL) 1286 free(convbuf); 1287 if ((mc = GETARG(MCHAR_T *)) == NULL) 1288 result = STRCONST("(null)"); 1289 else { 1290 convbuf = SCONV(mc, prec, loc); 1291 if (convbuf == NULL) { 1292 fp->_flags |= __SERR; 1293 goto error; 1294 } 1295 result = convbuf; 1296 } 1297 } 1298 1299 if (prec >= 0) { 1300 /* 1301 * can't use STRLEN; can only look for the 1302 * NUL in the first `prec' characters, and 1303 * STRLEN() will go further. 1304 */ 1305 CHAR_T *p = MEMCHR(result, 0, (size_t)prec); 1306 1307 if (p != NULL) { 1308 _DIAGASSERT(__type_fit(int, 1309 p - result)); 1310 size = (int)(p - result); 1311 if (size > prec) 1312 size = prec; 1313 } else 1314 size = prec; 1315 } else { 1316 size_t rlen = STRLEN(result); 1317 _DIAGASSERT(__type_fit(int, rlen)); 1318 size = (int)rlen; 1319 } 1320 sign = '\0'; 1321 break; 1322 case 'U': 1323 flags |= LONGINT; 1324 /*FALLTHROUGH*/ 1325 case 'u': 1326 if (flags & INTMAX_SIZE) 1327 ujval = UJARG(); 1328 else 1329 ulval = UARG(); 1330 base = 10; 1331 goto nosign; 1332 case 'X': 1333 xdigs = xdigs_upper; 1334 goto hex; 1335 case 'x': 1336 xdigs = xdigs_lower; 1337 hex: 1338 if (flags & INTMAX_SIZE) 1339 ujval = UJARG(); 1340 else 1341 ulval = UARG(); 1342 base = 16; 1343 /* leading 0x/X only if non-zero */ 1344 if (flags & ALT && 1345 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 1346 ox[1] = ch; 1347 1348 flags &= ~GROUPING; 1349 /* unsigned conversions */ 1350 nosign: sign = '\0'; 1351 /*- 1352 * ``... diouXx conversions ... if a precision is 1353 * specified, the 0 flag will be ignored.'' 1354 * -- ANSI X3J11 1355 */ 1356 number: if ((dprec = prec) >= 0) 1357 flags &= ~ZEROPAD; 1358 1359 /*- 1360 * ``The result of converting a zero value with an 1361 * explicit precision of zero is no characters.'' 1362 * -- ANSI X3J11 1363 * 1364 * ``The C Standard is clear enough as is. The call 1365 * printf("%#.0o", 0) should print 0.'' 1366 * -- Defect Report #151 1367 */ 1368 result = cp = buf + BUF; 1369 if (flags & INTMAX_SIZE) { 1370 if (ujval != 0 || prec != 0 || 1371 (flags & ALT && base == 8)) 1372 result = __ujtoa(ujval, cp, base, 1373 flags & ALT, xdigs, 1374 flags & GROUPING, thousands_sep, 1375 grouping); 1376 } else { 1377 if (ulval != 0 || prec != 0 || 1378 (flags & ALT && base == 8)) 1379 result = __ultoa(ulval, cp, base, 1380 flags & ALT, xdigs, 1381 flags & GROUPING, thousands_sep, 1382 grouping); 1383 } 1384 _DIAGASSERT(__type_fit(int, buf + BUF - result)); 1385 size = (int)(buf + BUF - result); 1386 if (size > BUF) /* should never happen */ 1387 abort(); 1388 break; 1389 default: /* "%?" prints ?, unless ? is NUL */ 1390 if (ch == '\0') 1391 goto done; 1392 /* pretend it was %c with argument ch */ 1393 *buf = ch; 1394 result = buf; 1395 size = 1; 1396 sign = '\0'; 1397 break; 1398 } 1399 1400 /* 1401 * All reasonable formats wind up here. At this point, `result' 1402 * points to a string which (if not flags&LADJUST) should be 1403 * padded out to `width' places. If flags&ZEROPAD, it should 1404 * first be prefixed by any sign or other prefix; otherwise, 1405 * it should be blank padded before the prefix is emitted. 1406 * After any left-hand padding and prefixing, emit zeroes 1407 * required by a decimal [diouxX] precision, then print the 1408 * string proper, then emit zeroes required by any leftover 1409 * floating precision; finally, if LADJUST, pad with blanks. 1410 * 1411 * Compute actual size, so we know how much to pad. 1412 * size excludes decimal prec; realsz includes it. 1413 */ 1414 realsz = dprec > size ? dprec : size; 1415 if (sign) 1416 realsz++; 1417 if (ox[1]) 1418 realsz += 2; 1419 1420 prsize = width > realsz ? width : realsz; 1421 if ((unsigned)ret + prsize > INT_MAX) { 1422 ret = END_OF_FILE; 1423 goto error; 1424 } 1425 1426 /* right-adjusting blank padding */ 1427 if ((flags & (LADJUST|ZEROPAD)) == 0) 1428 PAD(width - realsz, blanks); 1429 1430 /* prefix */ 1431 if (sign) 1432 PRINT(&sign, 1); 1433 1434 if (ox[1]) { /* ox[1] is either x, X, or \0 */ 1435 ox[0] = '0'; 1436 PRINT(ox, 2); 1437 } 1438 1439 /* right-adjusting zero padding */ 1440 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 1441 PAD(width - realsz, zeroes); 1442 1443 /* leading zeroes from decimal precision */ 1444 PAD(dprec - size, zeroes); 1445 1446 /* the string or number proper */ 1447 #ifndef NO_FLOATING_POINT 1448 if ((flags & FPT) == 0) { 1449 PRINT(result, size); 1450 } else { /* glue together f_p fragments */ 1451 if (!expchar) { /* %[fF] or sufficiently short %[gG] */ 1452 if (expt <= 0) { 1453 PRINT(zeroes, 1); 1454 if (prec || flags & ALT) 1455 PRINT(decimal_point, 1); 1456 PAD(-expt, zeroes); 1457 /* already handled initial 0's */ 1458 prec += expt; 1459 } else { 1460 PRINTANDPAD(result, convbuf + ndig, 1461 lead, zeroes); 1462 result += lead; 1463 if (grouping) { 1464 while (nseps>0 || nrepeats>0) { 1465 if (nrepeats > 0) 1466 nrepeats--; 1467 else { 1468 grouping--; 1469 nseps--; 1470 } 1471 PRINT(&thousands_sep, 1472 1); 1473 PRINTANDPAD(result, 1474 convbuf + ndig, 1475 *grouping, zeroes); 1476 result += *grouping; 1477 } 1478 if (result > convbuf + ndig) 1479 result = convbuf + ndig; 1480 } 1481 if (prec || flags & ALT) { 1482 buf[0] = *decimal_point; 1483 PRINT(buf, 1); 1484 } 1485 } 1486 PRINTANDPAD(result, convbuf + ndig, prec, 1487 zeroes); 1488 } else { /* %[eE] or sufficiently long %[gG] */ 1489 if (prec > 1 || flags & ALT) { 1490 buf[0] = *result++; 1491 buf[1] = *decimal_point; 1492 PRINT(buf, 2); 1493 PRINT(result, ndig-1); 1494 PAD(prec - ndig, zeroes); 1495 } else /* XeYYY */ 1496 PRINT(result, 1); 1497 PRINT(expstr, expsize); 1498 } 1499 } 1500 #else 1501 PRINT(result, size); 1502 #endif 1503 /* left-adjusting padding (always blank) */ 1504 if (flags & LADJUST) 1505 PAD(width - realsz, blanks); 1506 1507 /* finally, adjust ret */ 1508 ret += prsize; 1509 FLUSH(); 1510 } 1511 done: 1512 FLUSH(); 1513 error: 1514 va_end(orgap); 1515 if (convbuf != NULL) 1516 free(convbuf); 1517 if (__sferror(fp)) 1518 ret = END_OF_FILE; 1519 if ((argtable != NULL) && (argtable != statargtable)) 1520 free (argtable); 1521 return ret; 1522 /* NOTREACHED */ 1523 oomem: 1524 errno = ENOMEM; 1525 ret = END_OF_FILE; 1526 goto error; 1527 } 1528 1529 /* 1530 * Find all arguments when a positional parameter is encountered. Returns a 1531 * table, indexed by argument number, of pointers to each arguments. The 1532 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries. 1533 * It will be replaces with a malloc-ed one if it overflows. 1534 */ 1535 static int 1536 __find_arguments(const CHAR_T *fmt0, va_list ap, union arg **argtable) 1537 { 1538 CHAR_T *fmt; /* format string */ 1539 int ch; /* character from fmt */ 1540 size_t n, n2; /* handy index (short term usage) */ 1541 CHAR_T *cp; /* handy char pointer (short term usage) */ 1542 int flags; /* flags as above */ 1543 enum typeid *typetable; /* table of types */ 1544 enum typeid stattypetable [STATIC_ARG_TBL_SIZE]; 1545 size_t tablesize; /* current size of type table */ 1546 size_t tablemax; /* largest used index in table */ 1547 size_t nextarg; /* 1-based argument index */ 1548 size_t nitems; /* number of items we picked from the stack */ 1549 1550 /* 1551 * Add an argument type to the table, expanding if necessary. 1552 * Check for overflow. 1553 */ 1554 #define ADDTYPE(type) \ 1555 do { \ 1556 if (nextarg > SIZE_MAX / sizeof(**argtable)) { \ 1557 if (typetable != stattypetable) \ 1558 free(typetable); \ 1559 return -1; \ 1560 } \ 1561 if (nextarg >= tablesize) \ 1562 if (__grow_type_table(nextarg, &typetable, \ 1563 &tablesize) == -1) \ 1564 return -1; \ 1565 if (nextarg > tablemax) \ 1566 tablemax = nextarg; \ 1567 typetable[nextarg++] = type; \ 1568 nitems++; \ 1569 } while (0) 1570 1571 #define ADDSARG() \ 1572 do { \ 1573 if (flags & INTMAXT) \ 1574 ADDTYPE(T_INTMAXT); \ 1575 else if (flags & SIZET) \ 1576 ADDTYPE(T_SSIZET); \ 1577 else if (flags & PTRDIFFT) \ 1578 ADDTYPE(T_PTRDIFFT); \ 1579 else if (flags & LLONGINT) \ 1580 ADDTYPE(T_LLONG); \ 1581 else if (flags & LONGINT) \ 1582 ADDTYPE(T_LONG); \ 1583 else \ 1584 ADDTYPE(T_INT); \ 1585 } while (0) 1586 1587 #define ADDUARG() \ 1588 do { \ 1589 if (flags & INTMAXT) \ 1590 ADDTYPE(T_UINTMAXT); \ 1591 else if (flags & SIZET) \ 1592 ADDTYPE(T_SIZET); \ 1593 else if (flags & PTRDIFFT) \ 1594 ADDTYPE(T_PTRDIFFT); \ 1595 else if (flags & LLONGINT) \ 1596 ADDTYPE(T_U_LLONG); \ 1597 else if (flags & LONGINT) \ 1598 ADDTYPE(T_U_LONG); \ 1599 else \ 1600 ADDTYPE(T_U_INT); \ 1601 } while (0) 1602 /* 1603 * Add * arguments to the type array. 1604 */ 1605 #define ADDASTER() \ 1606 n2 = 0; \ 1607 cp = fmt; \ 1608 while (is_digit(*cp)) { \ 1609 n2 = 10 * n2 + to_digit(*cp); \ 1610 cp++; \ 1611 } \ 1612 if (*cp == '$') { \ 1613 size_t hold = nextarg; \ 1614 nextarg = n2; \ 1615 ADDTYPE(T_INT); \ 1616 nextarg = hold; \ 1617 fmt = ++cp; \ 1618 } else { \ 1619 ADDTYPE(T_INT); \ 1620 } 1621 fmt = (CHAR_T *)__UNCONST(fmt0); 1622 memset(stattypetable, 0, sizeof(stattypetable)); 1623 typetable = stattypetable; 1624 tablesize = STATIC_ARG_TBL_SIZE; 1625 tablemax = 0; 1626 nextarg = 1; 1627 nitems = 1; 1628 1629 /* 1630 * Scan the format for conversions (`%' character). 1631 */ 1632 for (;;) { 1633 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 1634 /* void */; 1635 if (ch == '\0') 1636 goto done; 1637 fmt++; /* skip over '%' */ 1638 1639 flags = 0; 1640 1641 rflag: ch = *fmt++; 1642 reswitch: switch (ch) { 1643 case ' ': 1644 case '#': 1645 goto rflag; 1646 case '*': 1647 ADDASTER (); 1648 goto rflag; 1649 case '-': 1650 case '+': 1651 case '\'': 1652 goto rflag; 1653 case '.': 1654 if ((ch = *fmt++) == '*') { 1655 ADDASTER (); 1656 goto rflag; 1657 } 1658 while (is_digit(ch)) { 1659 ch = *fmt++; 1660 } 1661 goto reswitch; 1662 case '0': 1663 goto rflag; 1664 case '1': case '2': case '3': case '4': 1665 case '5': case '6': case '7': case '8': case '9': 1666 n = 0; 1667 do { 1668 n = 10 * n + to_digit(ch); 1669 ch = *fmt++; 1670 } while (is_digit(ch)); 1671 if (ch == '$') { 1672 nextarg = n; 1673 goto rflag; 1674 } 1675 goto reswitch; 1676 #ifndef NO_FLOATING_POINT 1677 case 'L': 1678 flags |= LONGDBL; 1679 goto rflag; 1680 #endif 1681 case 'h': 1682 if (flags & SHORTINT) { 1683 flags &= ~SHORTINT; 1684 flags |= CHARINT; 1685 } else 1686 flags |= SHORTINT; 1687 goto rflag; 1688 case 'j': 1689 flags |= INTMAXT; 1690 goto rflag; 1691 case 'l': 1692 if (flags & LONGINT) { 1693 flags &= ~LONGINT; 1694 flags |= LLONGINT; 1695 } else 1696 flags |= LONGINT; 1697 goto rflag; 1698 case 'q': 1699 flags |= LLONGINT; /* not necessarily */ 1700 goto rflag; 1701 case 't': 1702 flags |= PTRDIFFT; 1703 goto rflag; 1704 case 'z': 1705 flags |= SIZET; 1706 goto rflag; 1707 case 'C': 1708 flags |= LONGINT; 1709 /*FALLTHROUGH*/ 1710 case 'c': 1711 if (flags & LONGINT) 1712 ADDTYPE(T_WINT); 1713 else 1714 ADDTYPE(T_INT); 1715 break; 1716 case 'D': 1717 flags |= LONGINT; 1718 /*FALLTHROUGH*/ 1719 case 'd': 1720 case 'i': 1721 ADDSARG(); 1722 break; 1723 #ifndef NO_FLOATING_POINT 1724 case 'a': 1725 case 'A': 1726 case 'e': 1727 case 'E': 1728 case 'f': 1729 case 'g': 1730 case 'G': 1731 if (flags & LONGDBL) 1732 ADDTYPE(T_LONG_DOUBLE); 1733 else 1734 ADDTYPE(T_DOUBLE); 1735 break; 1736 #endif /* !NO_FLOATING_POINT */ 1737 case 'n': 1738 if (flags & INTMAXT) 1739 ADDTYPE(TP_INTMAXT); 1740 else if (flags & PTRDIFFT) 1741 ADDTYPE(TP_PTRDIFFT); 1742 else if (flags & SIZET) 1743 ADDTYPE(TP_SIZET); 1744 else if (flags & LLONGINT) 1745 ADDTYPE(TP_LLONG); 1746 else if (flags & LONGINT) 1747 ADDTYPE(TP_LONG); 1748 else if (flags & SHORTINT) 1749 ADDTYPE(TP_SHORT); 1750 else if (flags & CHARINT) 1751 ADDTYPE(TP_SCHAR); 1752 else 1753 ADDTYPE(TP_INT); 1754 continue; /* no output */ 1755 case 'O': 1756 flags |= LONGINT; 1757 /*FALLTHROUGH*/ 1758 case 'o': 1759 ADDUARG(); 1760 break; 1761 case 'p': 1762 ADDTYPE(TP_VOID); 1763 break; 1764 case 'S': 1765 flags |= LONGINT; 1766 /*FALLTHROUGH*/ 1767 case 's': 1768 if (flags & LONGINT) 1769 ADDTYPE(TP_WCHAR); 1770 else 1771 ADDTYPE(TP_CHAR); 1772 break; 1773 case 'U': 1774 flags |= LONGINT; 1775 /*FALLTHROUGH*/ 1776 case 'u': 1777 case 'X': 1778 case 'x': 1779 ADDUARG(); 1780 break; 1781 default: /* "%?" prints ?, unless ? is NUL */ 1782 if (ch == '\0') 1783 goto done; 1784 break; 1785 } 1786 } 1787 done: 1788 /* 1789 * nitems contains the number of arguments we picked from the stack. 1790 * If tablemax is larger, this means that some positional argument, 1791 * tried to pick an argument the number of arguments possibly supplied. 1792 * Since positional arguments are typically used to swap the order of 1793 * the printf arguments and not to pick random arguments from strange 1794 * positions in the stack, we assume that if the positional argument 1795 * is trying to pick beyond the end of arguments, then this is wrong. 1796 * Alternatively we could find a way to figure out when va_arg() runs 1797 * out, but how to do that? 1798 */ 1799 if (nitems < tablemax) { 1800 if (typetable != stattypetable) 1801 free(typetable); 1802 return -1; 1803 } 1804 /* 1805 * Build the argument table. 1806 */ 1807 if (tablemax >= STATIC_ARG_TBL_SIZE) { 1808 *argtable = malloc(sizeof(**argtable) * (tablemax + 1)); 1809 if (*argtable == NULL) { 1810 free(typetable); 1811 return -1; 1812 } 1813 } 1814 1815 (*argtable) [0].intarg = 0; 1816 for (n = 1; n <= tablemax; n++) { 1817 switch (typetable [n]) { 1818 case T_UNUSED: /* whoops! */ 1819 (*argtable) [n].intarg = va_arg (ap, int); 1820 break; 1821 case TP_SCHAR: 1822 (*argtable) [n].pschararg = va_arg (ap, signed char *); 1823 break; 1824 case TP_SHORT: 1825 (*argtable) [n].pshortarg = va_arg (ap, short *); 1826 break; 1827 case T_INT: 1828 (*argtable) [n].intarg = va_arg (ap, int); 1829 break; 1830 case T_U_INT: 1831 (*argtable) [n].uintarg = va_arg (ap, unsigned int); 1832 break; 1833 case TP_INT: 1834 (*argtable) [n].pintarg = va_arg (ap, int *); 1835 break; 1836 case T_LONG: 1837 (*argtable) [n].longarg = va_arg (ap, long); 1838 break; 1839 case T_U_LONG: 1840 (*argtable) [n].ulongarg = va_arg (ap, unsigned long); 1841 break; 1842 case TP_LONG: 1843 (*argtable) [n].plongarg = va_arg (ap, long *); 1844 break; 1845 case T_LLONG: 1846 (*argtable) [n].longlongarg = va_arg (ap, long long); 1847 break; 1848 case T_U_LLONG: 1849 (*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long); 1850 break; 1851 case TP_LLONG: 1852 (*argtable) [n].plonglongarg = va_arg (ap, long long *); 1853 break; 1854 case T_PTRDIFFT: 1855 (*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t); 1856 break; 1857 case TP_PTRDIFFT: 1858 (*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *); 1859 break; 1860 case T_SSIZET: 1861 (*argtable) [n].ssizearg = va_arg (ap, ssize_t); 1862 break; 1863 case T_SIZET: 1864 (*argtable) [n].sizearg = va_arg (ap, size_t); 1865 break; 1866 case TP_SIZET: 1867 (*argtable) [n].psizearg = va_arg (ap, size_t *); 1868 break; 1869 case T_INTMAXT: 1870 (*argtable) [n].intmaxarg = va_arg (ap, intmax_t); 1871 break; 1872 case T_UINTMAXT: 1873 (*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t); 1874 break; 1875 case TP_INTMAXT: 1876 (*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *); 1877 break; 1878 case T_DOUBLE: 1879 #ifndef NO_FLOATING_POINT 1880 (*argtable) [n].doublearg = va_arg (ap, double); 1881 #endif 1882 break; 1883 case T_LONG_DOUBLE: 1884 #ifndef NO_FLOATING_POINT 1885 (*argtable) [n].longdoublearg = va_arg (ap, long double); 1886 #endif 1887 break; 1888 case TP_CHAR: 1889 (*argtable) [n].pchararg = va_arg (ap, char *); 1890 break; 1891 case TP_VOID: 1892 (*argtable) [n].pvoidarg = va_arg (ap, void *); 1893 break; 1894 case T_WINT: 1895 (*argtable) [n].wintarg = va_arg (ap, wint_t); 1896 break; 1897 case TP_WCHAR: 1898 (*argtable) [n].pwchararg = va_arg (ap, wchar_t *); 1899 break; 1900 } 1901 } 1902 1903 if (typetable != stattypetable) 1904 free (typetable); 1905 return 0; 1906 } 1907 1908 /* 1909 * Increase the size of the type table. 1910 */ 1911 static int 1912 __grow_type_table (size_t nextarg, enum typeid **typetable, size_t *tablesize) 1913 { 1914 enum typeid *const oldtable = *typetable; 1915 const size_t oldsize = *tablesize; 1916 enum typeid *newtable; 1917 size_t newsize = oldsize * 2; 1918 1919 if (newsize < nextarg + 1) 1920 newsize = nextarg + 1; 1921 if (oldsize == STATIC_ARG_TBL_SIZE) { 1922 newtable = NULL; 1923 errno = reallocarr(&newtable, newsize, sizeof(*newtable)); 1924 if (errno) 1925 return -1; 1926 memcpy(newtable, oldtable, oldsize * sizeof(*newtable)); 1927 } else { 1928 newtable = oldtable; 1929 errno = reallocarr(&newtable, newsize, sizeof(*newtable)); 1930 if (errno) { 1931 int serrno = errno; 1932 free(oldtable); 1933 errno = serrno; 1934 return -1; 1935 } 1936 } 1937 memset(&newtable[oldsize], 0, (newsize - oldsize) * sizeof(*newtable)); 1938 1939 *typetable = newtable; 1940 *tablesize = newsize; 1941 return 0; 1942 } 1943 1944 #ifndef NO_FLOATING_POINT 1945 1946 static int 1947 exponent(CHAR_T *p0, int expo, int fmtch) 1948 { 1949 CHAR_T *p, *t; 1950 CHAR_T expbuf[MAXEXPDIG]; 1951 1952 p = p0; 1953 *p++ = fmtch; 1954 if (expo < 0) { 1955 expo = -expo; 1956 *p++ = '-'; 1957 } 1958 else 1959 *p++ = '+'; 1960 t = expbuf + MAXEXPDIG; 1961 if (expo > 9) { 1962 do { 1963 *--t = to_char(expo % 10); 1964 } while ((expo /= 10) > 9); 1965 *--t = to_char(expo); 1966 for (; t < expbuf + MAXEXPDIG; *p++ = *t++); 1967 } 1968 else { 1969 /* 1970 * Exponents for decimal floating point conversions 1971 * (%[eEgG]) must be at least two characters long, 1972 * whereas exponents for hexadecimal conversions can 1973 * be only one character long. 1974 */ 1975 if (fmtch == 'e' || fmtch == 'E') 1976 *p++ = '0'; 1977 *p++ = to_char(expo); 1978 } 1979 _DIAGASSERT(__type_fit(int, p - p0)); 1980 return (int)(p - p0); 1981 } 1982 #endif /* !NO_FLOATING_POINT */ 1983