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