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