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