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