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 * 4. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#if 0 34#if defined(LIBC_SCCS) && !defined(lint) 35static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 36#endif /* LIBC_SCCS and not lint */ 37#endif 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.42 2009/11/25 04:27:55 wollman Exp $"); 40 41#include "xlocale_private.h" 42 43/* 44 * Actual wprintf innards. 45 * 46 * Avoid making gratuitous changes to this source file; it should be kept 47 * as close as possible to vfprintf.c for ease of maintenance. 48 */ 49 50#include "namespace.h" 51#include <sys/types.h> 52 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 <wchar.h> 63#include <wctype.h> 64#include <errno.h> 65#include "un-namespace.h" 66 67#include "libc_private.h" 68#include "local.h" 69#include "fvwrite.h" 70#include "printflocal.h" 71 72static int __sprint(FILE *, locale_t, struct __suio *); 73static int __sbprintf(FILE *, locale_t, const wchar_t *, va_list); 74static wint_t __xfputwc(wchar_t, FILE *, locale_t); 75static wchar_t *__mbsconv(char *, int, locale_t); 76__private_extern__ const char *__fix_nogrouping(const char *); 77 78#define CHAR wchar_t 79#include "printfcommon.h" 80 81struct grouping_state { 82 wchar_t thousands_sep; /* locale-specific thousands separator */ 83 const char *grouping; /* locale-specific numeric grouping rules */ 84 int lead; /* sig figs before decimal or group sep */ 85 int nseps; /* number of group separators with ' */ 86 int nrepeats; /* number of repeats of the last group */ 87}; 88 89static const mbstate_t initial_mbs; 90 91static inline wchar_t 92get_decpt(locale_t loc) 93{ 94 mbstate_t mbs; 95 wchar_t decpt; 96 int nconv; 97 98 mbs = initial_mbs; 99 nconv = mbrtowc_l(&decpt, localeconv_l(loc)->decimal_point, MB_CUR_MAX_L(loc), &mbs, loc); 100 if (nconv == (size_t)-1 || nconv == (size_t)-2) 101 decpt = '.'; /* failsafe */ 102 return (decpt); 103} 104 105static inline wchar_t 106get_thousep(locale_t loc) 107{ 108 mbstate_t mbs; 109 wchar_t thousep; 110 int nconv; 111 112 mbs = initial_mbs; 113 nconv = mbrtowc_l(&thousep, localeconv_l(loc)->thousands_sep, 114 MB_CUR_MAX_L(loc), &mbs, loc); 115 if (nconv == (size_t)-1 || nconv == (size_t)-2) 116 thousep = '\0'; /* failsafe */ 117 return (thousep); 118} 119 120/* 121 * Initialize the thousands' grouping state in preparation to print a 122 * number with ndigits digits. This routine returns the total number 123 * of wide characters that will be printed. 124 */ 125static int 126grouping_init(struct grouping_state *gs, int ndigits, locale_t loc) 127{ 128 129 gs->grouping = __fix_nogrouping(localeconv_l(loc)->grouping); 130 gs->thousands_sep = get_thousep(loc); 131 132 gs->nseps = gs->nrepeats = 0; 133 gs->lead = ndigits; 134 while (*gs->grouping != CHAR_MAX) { 135 if (gs->lead <= *gs->grouping) 136 break; 137 gs->lead -= *gs->grouping; 138 if (*(gs->grouping+1)) { 139 gs->nseps++; 140 gs->grouping++; 141 } else 142 gs->nrepeats++; 143 } 144 return (gs->nseps + gs->nrepeats); 145} 146 147/* 148 * Print a number with thousands' separators. 149 */ 150static int 151grouping_print(struct grouping_state *gs, struct io_state *iop, 152 const CHAR *cp, const CHAR *ep, locale_t loc) 153{ 154 const CHAR *cp0 = cp; 155 156 if (io_printandpad(iop, cp, ep, gs->lead, zeroes, loc)) 157 return (-1); 158 cp += gs->lead; 159 while (gs->nseps > 0 || gs->nrepeats > 0) { 160 if (gs->nrepeats > 0) 161 gs->nrepeats--; 162 else { 163 gs->grouping--; 164 gs->nseps--; 165 } 166 if (io_print(iop, &gs->thousands_sep, 1, loc)) 167 return (-1); 168 if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, loc)) 169 return (-1); 170 cp += *gs->grouping; 171 } 172 if (cp > ep) 173 cp = ep; 174 return (cp - cp0); 175} 176 177 178/* 179 * Flush out all the vectors defined by the given uio, 180 * then reset it so that it can be reused. 181 * 182 * XXX The fact that we do this a character at a time and convert to a 183 * multibyte character sequence even if the destination is a wide 184 * string eclipses the benefits of buffering. 185 */ 186static int 187__sprint(FILE *fp, locale_t loc, struct __suio *uio) 188{ 189 struct __siov *iov; 190 wchar_t *p; 191 int i, len; 192 193 iov = uio->uio_iov; 194 for (; uio->uio_resid != 0; uio->uio_resid -= len, iov++) { 195 p = (wchar_t *)iov->iov_base; 196 len = iov->iov_len; 197 for (i = 0; i < len; i++) { 198 if (__xfputwc(p[i], fp, loc) == WEOF) 199 return (-1); 200 } 201 } 202 uio->uio_iovcnt = 0; 203 return (0); 204} 205 206/* 207 * Helper function for `fprintf to unbuffered unix file': creates a 208 * temporary buffer. We only work on write-only files; this avoids 209 * worries about ungetc buffers and so forth. 210 */ 211static int 212__sbprintf(FILE *fp, locale_t loc, const wchar_t *fmt, va_list ap) 213{ 214 int ret; 215 FILE fake; 216 unsigned char buf[BUFSIZ]; 217 struct __sFILEX ext; 218 fake._extra = &ext; 219 INITEXTRA(&fake); 220 221 /* XXX This is probably not needed. */ 222 if (prepwrite(fp) != 0) 223 return (EOF); 224 225 /* copy the important variables */ 226 fake._flags = fp->_flags & ~__SNBF; 227 fake._file = fp->_file; 228 fake._cookie = fp->_cookie; 229 fake._write = fp->_write; 230 fake._orientation = fp->_orientation; 231 fake._mbstate = fp->_mbstate; 232 233 /* set up the buffer */ 234 fake._bf._base = fake._p = buf; 235 fake._bf._size = fake._w = sizeof(buf); 236 fake._lbfsize = 0; /* not actually used, but Just In Case */ 237 238 /* do the work, then copy any error status */ 239 ret = __vfwprintf(&fake, loc, fmt, ap); 240 if (ret >= 0 && __fflush(&fake)) 241 ret = WEOF; 242 if (fake._flags & __SERR) 243 fp->_flags |= __SERR; 244 return (ret); 245} 246 247/* 248 * Like __fputwc, but handles fake string (__SSTR) files properly. 249 * File must already be locked. 250 */ 251static wint_t 252__xfputwc(wchar_t wc, FILE *fp, locale_t loc) 253{ 254 mbstate_t mbs; 255 char buf[MB_LEN_MAX]; 256 struct __suio uio; 257 struct __siov iov; 258 size_t len; 259 260 if ((fp->_flags & __SSTR) == 0) 261 return (__fputwc(wc, fp, loc)); 262 263 mbs = initial_mbs; 264 if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) { 265 fp->_flags |= __SERR; 266 return (WEOF); 267 } 268 uio.uio_iov = &iov; 269 uio.uio_resid = len; 270 uio.uio_iovcnt = 1; 271 iov.iov_base = buf; 272 iov.iov_len = len; 273 return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF); 274} 275 276/* 277 * Convert a multibyte character string argument for the %s format to a wide 278 * string representation. ``prec'' specifies the maximum number of bytes 279 * to output. If ``prec'' is greater than or equal to zero, we can't assume 280 * that the multibyte char. string ends in a null character. 281 */ 282static wchar_t * 283__mbsconv(char *mbsarg, int prec, locale_t loc) 284{ 285 mbstate_t mbs; 286 wchar_t *convbuf, *wcp; 287 const char *p; 288 size_t insize, nchars, nconv = 0; 289 int mb_cur_max = MB_CUR_MAX_L(loc); 290 291 if (mbsarg == NULL) 292 return (NULL); 293 294 /* 295 * Supplied argument is a multibyte string; convert it to wide 296 * characters first. 297 */ 298 if (prec >= 0) { 299 /* 300 * String is not guaranteed to be NUL-terminated. Find the 301 * number of characters to print. 302 */ 303 p = mbsarg; 304 insize = nchars = nconv = 0; 305 mbs = initial_mbs; 306 while (nchars != (size_t)prec) { 307 nconv = mbrlen_l(p, mb_cur_max, &mbs, loc); 308 if (nconv == 0 || nconv == (size_t)-1 || 309 nconv == (size_t)-2) 310 break; 311 p += nconv; 312 nchars++; 313 insize += nconv; 314 } 315 if (nconv == (size_t)-1 || nconv == (size_t)-2) 316 return (NULL); 317 } else { 318 insize = strlen(mbsarg); 319 nconv = 0; 320 } 321 322 /* 323 * Allocate buffer for the result and perform the conversion, 324 * converting at most `size' bytes of the input multibyte string to 325 * wide characters for printing. 326 */ 327 convbuf = malloc((insize + 1) * sizeof(*convbuf)); 328 if (convbuf == NULL) 329 return (NULL); 330 wcp = convbuf; 331 p = mbsarg; 332 mbs = initial_mbs; 333 while (insize != 0) { 334 nconv = mbrtowc_l(wcp, p, insize, &mbs, loc); 335 if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2) 336 break; 337 wcp++; 338 p += nconv; 339 insize -= nconv; 340 } 341 if (nconv == (size_t)-1 || nconv == (size_t)-2) { 342 free(convbuf); 343 return (NULL); 344 } 345 *wcp = L'\0'; 346 347 return (convbuf); 348} 349 350/* 351 * MT-safe version 352 */ 353int 354vfwprintf_l(FILE * __restrict fp, locale_t loc, const wchar_t * __restrict fmt0, va_list ap) 355{ 356 int ret; 357 358 NORMALIZE_LOCALE(loc); 359 FLOCKFILE(fp); 360 /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 361 if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 362 fp->_file >= 0) 363 ret = __sbprintf(fp, loc, fmt0, ap); 364 else 365 ret = __vfwprintf(fp, loc, fmt0, ap); 366 FUNLOCKFILE(fp); 367 return (ret); 368} 369 370int 371vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap) 372{ 373 return vfwprintf_l(fp, __current_locale(), fmt0, ap); 374} 375 376/* 377 * The size of the buffer we use as scratch space for integer 378 * conversions, among other things. We need enough space to 379 * write a uintmax_t in octal (plus one byte). 380 */ 381#if UINTMAX_MAX <= UINT64_MAX 382#define BUF 32 383#else 384#error "BUF must be large enough to format a uintmax_t" 385#endif 386 387/* 388 * Non-MT-safe version 389 */ 390__private_extern__ int 391__vfwprintf(FILE *fp, locale_t loc, const wchar_t *fmt0, va_list ap) 392{ 393 wchar_t *fmt; /* format string */ 394 wchar_t ch; /* character from fmt */ 395 int n, n2; /* handy integer (short term usage) */ 396 wchar_t *cp; /* handy char pointer (short term usage) */ 397 int flags; /* flags as above */ 398 int ret; /* return value accumulator */ 399 int width; /* width from format (%8d), or 0 */ 400 int prec; /* precision from format; <0 for N/A */ 401 wchar_t sign; /* sign prefix (' ', '+', '-', or \0) */ 402 struct grouping_state gs; /* thousands' grouping info */ 403#ifndef NO_FLOATING_POINT 404 /* 405 * We can decompose the printed representation of floating 406 * point numbers into several parts, some of which may be empty: 407 * 408 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ 409 * A B ---C--- D E F 410 * 411 * A: 'sign' holds this value if present; '\0' otherwise 412 * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal 413 * C: cp points to the string MMMNNN. Leading and trailing 414 * zeros are not in the string and must be added. 415 * D: expchar holds this character; '\0' if no exponent, e.g. %f 416 * F: at least two digits for decimal, at least one digit for hex 417 */ 418 wchar_t decimal_point; /* locale specific decimal point */ 419 int signflag; /* true if float is negative */ 420 union { /* floating point arguments %[aAeEfFgG] */ 421 double dbl; 422 long double ldbl; 423 } fparg; 424 int expt; /* integer value of exponent */ 425 char expchar; /* exponent character: [eEpP\0] */ 426 char *dtoaend; /* pointer to end of converted digits */ 427 int expsize; /* character count for expstr */ 428 int ndig; /* actual number of digits returned by dtoa */ 429 wchar_t expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ 430 char *dtoaresult; /* buffer allocated by dtoa */ 431#endif 432#ifdef VECTORS 433 union arg vval; /* Vector argument. */ 434 wchar_t *pct; /* Pointer to '%' at beginning of specifier. */ 435 wchar_t vsep; /* Vector separator character. */ 436#endif 437 u_long ulval; /* integer arguments %[diouxX] */ 438 uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 439 int base; /* base for [diouxX] conversion */ 440 int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 441 int realsz; /* field size expanded by dprec, sign, etc */ 442 int size; /* size of converted field or string */ 443 int prsize; /* max size of printed field */ 444 const char *xdigs; /* digits for [xX] conversion */ 445 struct io_state io; /* I/O buffering state */ 446 wchar_t buf[BUF]; /* buffer with space for digits of uintmax_t */ 447 wchar_t ox[2]; /* space for 0x hex-prefix */ 448 union arg *argtable; /* args, built due to positional arg */ 449 union arg statargtable [STATIC_ARG_TBL_SIZE]; 450 int nextarg; /* 1-based argument index */ 451 va_list orgap; /* original argument pointer */ 452 wchar_t *convbuf; /* multibyte to wide conversion result */ 453 454 static const char xdigs_lower[16] = "0123456789abcdef"; 455 static const char xdigs_upper[16] = "0123456789ABCDEF"; 456 457 /* BEWARE, these `goto error' on error. */ 458#define PRINT(ptr, len) do { \ 459 if (io_print(&io, (ptr), (len), loc)) \ 460 goto error; \ 461} while (0) 462#define PAD(howmany, with) { \ 463 if (io_pad(&io, (howmany), (with), loc)) \ 464 goto error; \ 465} 466#define PRINTANDPAD(p, ep, len, with) { \ 467 if (io_printandpad(&io, (p), (ep), (len), (with), loc)) \ 468 goto error; \ 469} 470#define FLUSH() { \ 471 if (io_flush(&io, loc)) \ 472 goto error; \ 473} 474 475 /* 476 * Get the argument indexed by nextarg. If the argument table is 477 * built, use it to get the argument. If its not, get the next 478 * argument (and arguments must be gotten sequentially). 479 */ 480#define GETARG(type) \ 481 ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ 482 (nextarg++, va_arg(ap, type))) 483 484 /* 485 * To extend shorts properly, we need both signed and unsigned 486 * argument extraction methods. 487 */ 488#define SARG() \ 489 (flags&LONGINT ? GETARG(long) : \ 490 flags&SHORTINT ? (long)(short)GETARG(int) : \ 491 flags&CHARINT ? (long)(signed char)GETARG(int) : \ 492 (long)GETARG(int)) 493#define UARG() \ 494 (flags&LONGINT ? GETARG(u_long) : \ 495 flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 496 flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 497 (u_long)GETARG(u_int)) 498#define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 499#define SJARG() \ 500 (flags&INTMAXT ? GETARG(intmax_t) : \ 501 flags&SIZET ? (intmax_t)GETARG(ssize_t) : \ 502 flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 503 (intmax_t)GETARG(long long)) 504#define UJARG() \ 505 (flags&INTMAXT ? GETARG(uintmax_t) : \ 506 flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 507 flags&PTRDIFFT ? (uintmax_t)(unsigned long)GETARG(ptrdiff_t) : \ 508 (uintmax_t)GETARG(unsigned long long)) 509 510 /* 511 * Get * arguments, including the form *nn$. Preserve the nextarg 512 * that the argument can be gotten once the type is determined. 513 */ 514#define GETASTER(val) \ 515 n2 = 0; \ 516 cp = fmt; \ 517 while (is_digit(*cp)) { \ 518 n2 = 10 * n2 + to_digit(*cp); \ 519 cp++; \ 520 } \ 521 if (*cp == '$') { \ 522 int hold = nextarg; \ 523 if (argtable == NULL) { \ 524 argtable = statargtable; \ 525 if (__find_warguments (fmt0, orgap, &argtable)) { \ 526 ret = EOF; \ 527 goto error; \ 528 } \ 529 } \ 530 nextarg = n2; \ 531 val = GETARG (int); \ 532 nextarg = hold; \ 533 fmt = ++cp; \ 534 } else { \ 535 val = GETARG (int); \ 536 } 537 538 539 /* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */ 540 if (prepwrite(fp) != 0) { 541 errno = EBADF; 542 return (EOF); 543 } 544 ORIENT(fp, 1); 545 546 convbuf = NULL; 547 fmt = (wchar_t *)fmt0; 548 argtable = NULL; 549 nextarg = 1; 550 va_copy(orgap, ap); 551 io_init(&io, fp); 552 ret = 0; 553#ifndef NO_FLOATING_POINT 554 decimal_point = get_decpt(loc); 555#endif 556 557 /* 558 * Scan the format for conversions (`%' character). 559 */ 560 for (;;) { 561 for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 562 /* void */; 563 if ((n = fmt - cp) != 0) { 564 if ((unsigned)ret + n > INT_MAX) { 565 ret = EOF; 566 goto error; 567 } 568 PRINT(cp, n); 569 ret += n; 570 } 571 if (ch == '\0') 572 goto done; 573#ifdef VECTORS 574 pct = fmt; 575#endif /* VECTORS */ 576 fmt++; /* skip over '%' */ 577 578 flags = 0; 579 dprec = 0; 580 width = 0; 581 prec = -1; 582 gs.grouping = NULL; 583 sign = '\0'; 584 ox[1] = '\0'; 585#ifdef VECTORS 586 vsep = 'X'; /* Illegal value, changed to defaults later. */ 587#endif /* VECTORS */ 588 589rflag: ch = *fmt++; 590reswitch: switch (ch) { 591 case ' ': 592 /*- 593 * ``If the space and + flags both appear, the space 594 * flag will be ignored.'' 595 * -- ANSI X3J11 596 */ 597 if (!sign) 598 sign = ' '; 599 goto rflag; 600 case '#': 601 flags |= ALT; 602 goto rflag; 603#ifdef VECTORS 604 case ',': case ';': case ':': case '_': 605 vsep = ch; 606 goto rflag; 607#endif /* VECTORS */ 608 case '*': 609 /*- 610 * ``A negative field width argument is taken as a 611 * - flag followed by a positive field width.'' 612 * -- ANSI X3J11 613 * They don't exclude field widths read from args. 614 */ 615 GETASTER (width); 616 if (width >= 0) 617 goto rflag; 618 width = -width; 619 /* FALLTHROUGH */ 620 case '-': 621 flags |= LADJUST; 622 goto rflag; 623 case '+': 624 sign = '+'; 625 goto rflag; 626 case '\'': 627 flags |= GROUPING; 628 goto rflag; 629 case '.': 630 if ((ch = *fmt++) == '*') { 631 GETASTER (prec); 632 goto rflag; 633 } 634 prec = 0; 635 while (is_digit(ch)) { 636 prec = 10 * prec + to_digit(ch); 637 ch = *fmt++; 638 } 639 goto reswitch; 640 case '0': 641 /*- 642 * ``Note that 0 is taken as a flag, not as the 643 * beginning of a field width.'' 644 * -- ANSI X3J11 645 */ 646 flags |= ZEROPAD; 647 goto rflag; 648 case '1': case '2': case '3': case '4': 649 case '5': case '6': case '7': case '8': case '9': 650 n = 0; 651 do { 652 n = 10 * n + to_digit(ch); 653 ch = *fmt++; 654 } while (is_digit(ch)); 655 if (ch == '$') { 656 nextarg = n; 657 if (argtable == NULL) { 658 argtable = statargtable; 659 if (__find_warguments (fmt0, orgap, 660 &argtable)) { 661 ret = EOF; 662 goto error; 663 } 664 } 665 goto rflag; 666 } 667 width = n; 668 goto reswitch; 669#ifndef NO_FLOATING_POINT 670 case 'L': 671 flags |= LONGDBL; 672 goto rflag; 673#endif 674 case 'h': 675 if (flags & SHORTINT) { 676 flags &= ~SHORTINT; 677 flags |= CHARINT; 678 } else 679 flags |= SHORTINT; 680 goto rflag; 681 case 'j': 682 flags |= INTMAXT; 683 goto rflag; 684 case 'l': 685 if (flags & LONGINT) { 686 flags &= ~LONGINT; 687 flags |= LLONGINT; 688 } else 689 flags |= LONGINT; 690 goto rflag; 691 case 'q': 692 flags |= LLONGINT; /* not necessarily */ 693 goto rflag; 694 case 't': 695 flags |= PTRDIFFT; 696 goto rflag; 697 case 'z': 698 flags |= SIZET; 699 goto rflag; 700 case 'C': 701 flags |= LONGINT; 702 /*FALLTHROUGH*/ 703 case 'c': 704#ifdef VECTORS 705 if (flags & VECTOR) 706 break; 707#endif /* VECTORS */ 708 if (flags & LONGINT) 709 *(cp = buf) = (wchar_t)GETARG(wint_t); 710 else 711 *(cp = buf) = (wchar_t)btowc_l(GETARG(int), loc); 712 size = 1; 713 sign = '\0'; 714 break; 715 case 'D': 716 flags |= LONGINT; 717 /*FALLTHROUGH*/ 718 case 'd': 719 case 'i': 720#ifdef VECTORS 721 if (flags & VECTOR) 722 break; 723#endif /* VECTORS */ 724 if (flags & INTMAX_SIZE) { 725 ujval = SJARG(); 726 if ((intmax_t)ujval < 0) { 727 ujval = -ujval; 728 sign = '-'; 729 } 730 } else { 731 ulval = SARG(); 732 if ((long)ulval < 0) { 733 ulval = -ulval; 734 sign = '-'; 735 } 736 } 737 base = 10; 738 goto number; 739#ifndef NO_FLOATING_POINT 740 case 'a': 741 case 'A': 742#ifdef VECTORS 743 if (flags & VECTOR) { 744 flags |= FPT; 745 break; 746 } 747#endif /* VECTORS */ 748 if (ch == 'a') { 749 ox[1] = 'x'; 750 xdigs = xdigs_lower; 751 expchar = 'p'; 752 } else { 753 ox[1] = 'X'; 754 xdigs = xdigs_upper; 755 expchar = 'P'; 756 } 757 if (prec >= 0) 758 prec++; 759 if (flags & LONGDBL) { 760 fparg.ldbl = GETARG(long double); 761 dtoaresult = 762 __hldtoa(fparg.ldbl, xdigs, prec, 763 &expt, &signflag, &dtoaend); 764 } else { 765 fparg.dbl = GETARG(double); 766 dtoaresult = 767 __hdtoa(fparg.dbl, xdigs, prec, 768 &expt, &signflag, &dtoaend); 769 } 770 if (prec < 0) 771 prec = dtoaend - dtoaresult; 772 if (expt == INT_MAX) 773 ox[1] = '\0'; 774 if (convbuf != NULL) 775 free(convbuf); 776 ndig = dtoaend - dtoaresult; 777 cp = convbuf = __mbsconv(dtoaresult, -1, loc); 778 freedtoa(dtoaresult); 779 goto fp_common; 780 case 'e': 781 case 'E': 782#ifdef VECTORS 783 if (flags & VECTOR) { 784 flags |= FPT; 785 break; 786 } 787#endif /* VECTORS */ 788 expchar = ch; 789 if (prec < 0) /* account for digit before decpt */ 790 prec = DEFPREC + 1; 791 else 792 prec++; 793 goto fp_begin; 794 case 'f': 795 case 'F': 796#ifdef VECTORS 797 if (flags & VECTOR) { 798 flags |= FPT; 799 break; 800 } 801#endif /* VECTORS */ 802 expchar = '\0'; 803 goto fp_begin; 804 case 'g': 805 case 'G': 806#ifdef VECTORS 807 if (flags & VECTOR) { 808 flags |= FPT; 809 break; 810 } 811#endif /* VECTORS */ 812 expchar = ch - ('g' - 'e'); 813 if (prec == 0) 814 prec = 1; 815fp_begin: 816 if (prec < 0) 817 prec = DEFPREC; 818 if (convbuf != NULL) 819 free(convbuf); 820 if (flags & LONGDBL) { 821 fparg.ldbl = GETARG(long double); 822 dtoaresult = 823 __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, 824 &expt, &signflag, &dtoaend); 825 } else { 826 fparg.dbl = GETARG(double); 827 dtoaresult = 828 dtoa(fparg.dbl, expchar ? 2 : 3, prec, 829 &expt, &signflag, &dtoaend); 830 if (expt == 9999) 831 expt = INT_MAX; 832 } 833 ndig = dtoaend - dtoaresult; 834 cp = convbuf = __mbsconv(dtoaresult, -1, loc); 835 freedtoa(dtoaresult); 836fp_common: 837 if (signflag) 838 sign = '-'; 839 if (expt == INT_MAX) { /* inf or nan */ 840 if (*cp == 'N') { 841 cp = (ch >= 'a') ? L"nan" : L"NAN"; 842 sign = '\0'; 843 } else 844 cp = (ch >= 'a') ? L"inf" : L"INF"; 845 size = 3; 846 flags &= ~ZEROPAD; 847 break; 848 } 849 flags |= FPT; 850 if (ch == 'g' || ch == 'G') { 851 if (expt > -4 && expt <= prec) { 852 /* Make %[gG] smell like %[fF] */ 853 expchar = '\0'; 854 if (flags & ALT) 855 prec -= expt; 856 else 857 prec = ndig - expt; 858 if (prec < 0) 859 prec = 0; 860 } else { 861 /* 862 * Make %[gG] smell like %[eE], but 863 * trim trailing zeroes if no # flag. 864 */ 865 if (!(flags & ALT)) 866 prec = ndig; 867 } 868 } 869 if (expchar) { 870 expsize = exponent(expstr, expt - 1, expchar); 871 size = expsize + prec; 872 if (prec > 1 || flags & ALT) 873 ++size; 874 } else { 875 /* space for digits before decimal point */ 876 if (expt > 0) 877 size = expt; 878 else /* "0" */ 879 size = 1; 880 /* space for decimal pt and following digits */ 881 if (prec || flags & ALT) 882 size += prec + 1; 883 if ((flags & GROUPING) && expt > 0) 884 size += grouping_init(&gs, expt, loc); 885 } 886 break; 887#endif /* !NO_FLOATING_POINT */ 888 case 'n': 889 { 890 /* 891 * Assignment-like behavior is specified if the 892 * value overflows or is otherwise unrepresentable. 893 * C99 says to use `signed char' for %hhn conversions. 894 */ 895 void *ptr = GETARG(void *); 896 if (ptr == NULL) 897 continue; 898 else if (flags & LLONGINT) 899 *(long long *)ptr = ret; 900 else if (flags & SIZET) 901 *(ssize_t *)ptr = (ssize_t)ret; 902 else if (flags & PTRDIFFT) 903 *(ptrdiff_t *)ptr = ret; 904 else if (flags & INTMAXT) 905 *(intmax_t *)ptr = ret; 906 else if (flags & LONGINT) 907 *(long *)ptr = ret; 908 else if (flags & SHORTINT) 909 *(short *)ptr = ret; 910 else if (flags & CHARINT) 911 *(signed char *)ptr = ret; 912 else 913 *(int *)ptr = ret; 914 continue; /* no output */ 915 } 916 case 'O': 917 flags |= LONGINT; 918 /*FALLTHROUGH*/ 919 case 'o': 920#ifdef VECTORS 921 if (flags & VECTOR) 922 break; 923#endif /* VECTORS */ 924 if (flags & INTMAX_SIZE) 925 ujval = UJARG(); 926 else 927 ulval = UARG(); 928 base = 8; 929 goto nosign; 930 case 'p': 931 /*- 932 * ``The argument shall be a pointer to void. The 933 * value of the pointer is converted to a sequence 934 * of printable characters, in an implementation- 935 * defined manner.'' 936 * -- ANSI X3J11 937 */ 938#ifdef VECTORS 939 if (flags & VECTOR) 940 break; 941#endif /* VECTORS */ 942 ujval = (uintmax_t)(uintptr_t)GETARG(void *); 943 base = 16; 944 xdigs = xdigs_lower; 945 flags = flags | INTMAXT; 946 ox[1] = 'x'; 947 goto nosign; 948 case 'S': 949 flags |= LONGINT; 950 /*FALLTHROUGH*/ 951 case 's': 952 if (flags & LONGINT) { 953 if ((cp = GETARG(wchar_t *)) == NULL) 954 cp = L"(null)"; 955 } else { 956 char *mbp; 957 958 if (convbuf != NULL) 959 free(convbuf); 960 if ((mbp = GETARG(char *)) == NULL) 961 cp = L"(null)"; 962 else { 963 convbuf = __mbsconv(mbp, prec, loc); 964 if (convbuf == NULL) { 965 fp->_flags |= __SERR; 966 goto error; 967 } 968 cp = convbuf; 969 } 970 } 971#if 0 // wcsnlen needs API review first 972 size = (prec >= 0) ? wcsnlen(cp, prec) : wcslen(cp); 973#else 974 size = wcslen(cp); 975 if(prec >= 0 && prec < size) 976 size = prec; 977#endif 978 sign = '\0'; 979 break; 980 case 'U': 981 flags |= LONGINT; 982 /*FALLTHROUGH*/ 983 case 'u': 984#ifdef VECTORS 985 if (flags & VECTOR) 986 break; 987#endif /* VECTORS */ 988 if (flags & INTMAX_SIZE) 989 ujval = UJARG(); 990 else 991 ulval = UARG(); 992 base = 10; 993 goto nosign; 994 case 'X': 995 xdigs = xdigs_upper; 996 goto hex; 997 case 'x': 998 xdigs = xdigs_lower; 999hex: 1000#ifdef VECTORS 1001 if (flags & VECTOR) 1002 break; 1003#endif /* VECTORS */ 1004 if (flags & INTMAX_SIZE) 1005 ujval = UJARG(); 1006 else 1007 ulval = UARG(); 1008 base = 16; 1009 /* leading 0x/X only if non-zero */ 1010 if (flags & ALT && 1011 (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 1012 ox[1] = ch; 1013 1014 flags &= ~GROUPING; 1015 /* unsigned conversions */ 1016nosign: sign = '\0'; 1017 /*- 1018 * ``... diouXx conversions ... if a precision is 1019 * specified, the 0 flag will be ignored.'' 1020 * -- ANSI X3J11 1021 * except for %#.0o and zero value 1022 */ 1023number: if ((dprec = prec) >= 0) 1024 flags &= ~ZEROPAD; 1025 1026 /*- 1027 * ``The result of converting a zero value with an 1028 * explicit precision of zero is no characters.'' 1029 * -- ANSI X3J11 1030 * 1031 * ``The C Standard is clear enough as is. The call 1032 * printf("%#.0o", 0) should print 0.'' 1033 * -- Defect Report #151 1034 */ 1035 cp = buf + BUF; 1036 if (flags & INTMAX_SIZE) { 1037 if (ujval != 0 || prec != 0 || 1038 (flags & ALT && base == 8)) 1039 cp = __ujtoa(ujval, cp, base, 1040 flags & ALT, xdigs); 1041 } else { 1042 if (ulval != 0 || prec != 0 || 1043 (flags & ALT && base == 8)) 1044 cp = __ultoa(ulval, cp, base, 1045 flags & ALT, xdigs); 1046 } 1047 size = buf + BUF - cp; 1048 if (size > BUF) /* should never happen */ 1049 LIBC_ABORT("size (%d) > BUF (%d)", size, BUF); 1050 if ((flags & GROUPING) && size != 0) 1051 size += grouping_init(&gs, size, loc); 1052 break; 1053#ifdef VECTORS 1054 case 'v': 1055 flags |= VECTOR; 1056 goto rflag; 1057#endif /* VECTORS */ 1058 default: /* "%?" prints ?, unless ? is NUL */ 1059 if (ch == '\0') 1060 goto done; 1061 /* pretend it was %c with argument ch */ 1062 cp = buf; 1063 *cp = ch; 1064 size = 1; 1065 sign = '\0'; 1066 break; 1067 } 1068 1069#ifdef VECTORS 1070 if (flags & VECTOR) { 1071 /* 1072 * Do the minimum amount of work necessary to construct 1073 * a format specifier that can be used to recursively 1074 * call vfprintf() for each element in the vector. 1075 */ 1076 int i, j; /* Counter. */ 1077 int vcnt; /* Number of elements in vector. */ 1078 char *vfmt; /* Pointer to format specifier. */ 1079#define EXTRAHH 2 1080 char vfmt_buf[32 + EXTRAHH]; /* Static buffer for format spec. */ 1081 int vwidth = 0; /* Width specified via '*'. */ 1082 int vprec = 0; /* Precision specified via '*'. */ 1083 char *vstr; /* Used for asprintf(). */ 1084 int vlen; /* Length returned by asprintf(). */ 1085 enum { 1086 V_CHAR, V_SHORT, V_INT, 1087 V_PCHAR, V_PSHORT, V_PINT, 1088 V_FLOAT, 1089#ifdef V64TYPE 1090 V_LONGLONG, V_PLONGLONG, 1091 V_DOUBLE, 1092#endif /* V64TYPE */ 1093 } vtype; 1094 1095 vval.vectorarg = GETARG(VECTORTYPE); 1096 /* 1097 * Set vfmt. If vfmt_buf may not be big enough, 1098 * malloc() space, taking care to free it later. 1099 * (EXTRAHH is for possible extra "hh") 1100 */ 1101 if (&fmt[-1] - pct + EXTRAHH < sizeof(vfmt_buf)) 1102 vfmt = vfmt_buf; 1103 else 1104 vfmt = (char *)malloc(&fmt[-1] - pct + EXTRAHH + 1); 1105 1106 /* Set the separator character, if not specified. */ 1107 if (vsep == 'X') { 1108 if (ch == 'c') 1109 vsep = '\0'; 1110 else 1111 vsep = ' '; 1112 } 1113 1114 /* Create the format specifier. */ 1115 for (i = j = 0; i < &fmt[-1] - pct; i++) { 1116 switch (pct[i]) { 1117 case ',': case ';': case ':': case '_': 1118 case 'v': case 'h': case 'l': 1119 /* Ignore. */ 1120 break; 1121 case '*': 1122 if (pct[i - 1] != '.') 1123 vwidth = 1; 1124 else 1125 vprec = 1; 1126 /* FALLTHROUGH */ 1127 default: 1128 vfmt[j++] = pct[i]; 1129 } 1130 } 1131 1132 /* 1133 * Determine the number of elements in the vector and 1134 * finish up the format specifier. 1135 */ 1136 if (flags & SHORTINT) { 1137 switch (ch) { 1138 case 'c': 1139 vtype = V_SHORT; 1140 break; 1141 case 'p': 1142 vtype = V_PSHORT; 1143 break; 1144 default: 1145 vfmt[j++] = 'h'; 1146 vtype = V_SHORT; 1147 break; 1148 } 1149 vcnt = 8; 1150 } else if (flags & LONGINT) { 1151 vcnt = 4; 1152 vtype = (ch == 'p') ? V_PINT : V_INT; 1153#ifdef V64TYPE 1154 } else if (flags & LLONGINT) { 1155 switch (ch) { 1156 case 'a': 1157 case 'A': 1158 case 'e': 1159 case 'E': 1160 case 'f': 1161 case 'g': 1162 case 'G': 1163 vcnt = 2; 1164 vtype = V_DOUBLE; 1165 break; 1166 case 'd': 1167 case 'i': 1168 case 'u': 1169 case 'o': 1170 case 'p': 1171 case 'x': 1172 case 'X': 1173 vfmt[j++] = 'l'; 1174 vfmt[j++] = 'l'; 1175 vcnt = 2; 1176 vtype = (ch == 'p') ? V_PLONGLONG : V_LONGLONG; 1177 break; 1178 default: 1179 /* 1180 * The default case should never 1181 * happen. 1182 */ 1183 case 'c': 1184 vcnt = 16; 1185 vtype = V_CHAR; 1186 } 1187#endif /* V64TYPE */ 1188 } else { 1189 switch (ch) { 1190 case 'a': 1191 case 'A': 1192 case 'e': 1193 case 'E': 1194 case 'f': 1195 case 'g': 1196 case 'G': 1197 vcnt = 4; 1198 vtype = V_FLOAT; 1199 break; 1200 default: 1201 /* 1202 * The default case should never 1203 * happen. 1204 */ 1205 case 'd': 1206 case 'i': 1207 case 'u': 1208 case 'o': 1209 case 'x': 1210 case 'X': 1211 vfmt[j++] = 'h'; 1212 vfmt[j++] = 'h'; 1213 /* drop through */ 1214 case 'p': 1215 case 'c': 1216 vcnt = 16; 1217 vtype = (ch == 'p') ? V_PCHAR : V_CHAR; 1218 } 1219 } 1220 vfmt[j++] = ch; 1221 vfmt[j++] = '\0'; 1222 1223/* Get a vector element. */ 1224#ifdef V64TYPE 1225#define VPRINT(type, ind, args...) do { \ 1226 switch (type) { \ 1227 case V_CHAR: \ 1228 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \ 1229 break; \ 1230 case V_PCHAR: \ 1231 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \ 1232 break; \ 1233 case V_SHORT: \ 1234 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \ 1235 break; \ 1236 case V_PSHORT: \ 1237 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \ 1238 break; \ 1239 case V_INT: \ 1240 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \ 1241 break; \ 1242 case V_PINT: \ 1243 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \ 1244 break; \ 1245 case V_LONGLONG: \ 1246 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vulonglongarg[ind]); \ 1247 break; \ 1248 case V_PLONGLONG: \ 1249 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vulonglongarg[ind]); \ 1250 break; \ 1251 case V_FLOAT: \ 1252 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \ 1253 break; \ 1254 case V_DOUBLE: \ 1255 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vdoublearg[ind]); \ 1256 break; \ 1257 } \ 1258 ret += vlen; \ 1259 PRINT(vstr, vlen); \ 1260 free(vstr); \ 1261} while (0) 1262#else /* !V64TYPE */ 1263#define VPRINT(type, ind, args...) do { \ 1264 switch (type) { \ 1265 case V_CHAR: \ 1266 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuchararg[ind]); \ 1267 break; \ 1268 case V_PCHAR: \ 1269 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuchararg[ind]); \ 1270 break; \ 1271 case V_SHORT: \ 1272 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vushortarg[ind]); \ 1273 break; \ 1274 case V_PSHORT: \ 1275 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vushortarg[ind]); \ 1276 break; \ 1277 case V_INT: \ 1278 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vuintarg[ind]); \ 1279 break; \ 1280 case V_PINT: \ 1281 vlen = asprintf_l(&vstr, loc, vfmt , ## args, (void *)(uintptr_t)vval.vuintarg[ind]); \ 1282 break; \ 1283 case V_FLOAT: \ 1284 vlen = asprintf_l(&vstr, loc, vfmt , ## args, vval.vfloatarg[ind]); \ 1285 break; \ 1286 } \ 1287 ret += vlen; \ 1288 PRINT(vstr, vlen); \ 1289 free(vstr); \ 1290} while (0) 1291#endif /* V64TYPE */ 1292 1293 /* Actually print. */ 1294 if (vwidth == 0) { 1295 if (vprec == 0) { 1296 /* First element. */ 1297 VPRINT(vtype, 0); 1298 for (i = 1; i < vcnt; i++) { 1299 /* Separator. */ 1300 if(vsep) 1301 PRINT(&vsep, 1); 1302 1303 /* Element. */ 1304 VPRINT(vtype, i); 1305 } 1306 } else { 1307 /* First element. */ 1308 VPRINT(vtype, 0, prec); 1309 for (i = 1; i < vcnt; i++) { 1310 /* Separator. */ 1311 if(vsep) 1312 PRINT(&vsep, 1); 1313 1314 /* Element. */ 1315 VPRINT(vtype, i, prec); 1316 } 1317 } 1318 } else { 1319 if (vprec == 0) { 1320 /* First element. */ 1321 VPRINT(vtype, 0, width); 1322 for (i = 1; i < vcnt; i++) { 1323 /* Separator. */ 1324 if(vsep) 1325 PRINT(&vsep, 1); 1326 1327 /* Element. */ 1328 VPRINT(vtype, i, width); 1329 } 1330 } else { 1331 /* First element. */ 1332 VPRINT(vtype, 0, width, prec); 1333 for (i = 1; i < vcnt; i++) { 1334 /* Separator. */ 1335 if(vsep) 1336 PRINT(&vsep, 1); 1337 1338 /* Element. */ 1339 VPRINT(vtype, i, width, prec); 1340 } 1341 } 1342 } 1343#undef VPRINT 1344 1345 if (vfmt != vfmt_buf) 1346 free(vfmt); 1347 1348 continue; 1349 } 1350#endif /* VECTORS */ 1351 /* 1352 * All reasonable formats wind up here. At this point, `cp' 1353 * points to a string which (if not flags&LADJUST) should be 1354 * padded out to `width' places. If flags&ZEROPAD, it should 1355 * first be prefixed by any sign or other prefix; otherwise, 1356 * it should be blank padded before the prefix is emitted. 1357 * After any left-hand padding and prefixing, emit zeroes 1358 * required by a decimal [diouxX] precision, then print the 1359 * string proper, then emit zeroes required by any leftover 1360 * floating precision; finally, if LADJUST, pad with blanks. 1361 * 1362 * Compute actual size, so we know how much to pad. 1363 * size excludes decimal prec; realsz includes it. 1364 */ 1365 realsz = dprec > size ? dprec : size; 1366 if (sign) 1367 realsz++; 1368 if (ox[1]) 1369 realsz += 2; 1370 1371 prsize = width > realsz ? width : realsz; 1372 if ((unsigned)ret + prsize > INT_MAX) { 1373 ret = EOF; 1374 goto error; 1375 } 1376 1377 /* right-adjusting blank padding */ 1378 if ((flags & (LADJUST|ZEROPAD)) == 0) 1379 PAD(width - realsz, blanks); 1380 1381 /* prefix */ 1382 if (sign) 1383 PRINT(&sign, 1); 1384 1385 if (ox[1]) { /* ox[1] is either x, X, or \0 */ 1386 ox[0] = '0'; 1387 PRINT(ox, 2); 1388 } 1389 1390 /* right-adjusting zero padding */ 1391 if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 1392 PAD(width - realsz, zeroes); 1393 1394 /* the string or number proper */ 1395#ifndef NO_FLOATING_POINT 1396 if ((flags & FPT) == 0) { 1397#endif 1398 /* leading zeroes from decimal precision */ 1399 PAD(dprec - size, zeroes); 1400 if (gs.grouping) { 1401 if (grouping_print(&gs, &io, cp, buf+BUF, loc) < 0) 1402 goto error; 1403 } else { 1404 PRINT(cp, size); 1405 } 1406#ifndef NO_FLOATING_POINT 1407 } else { /* glue together f_p fragments */ 1408 if (!expchar) { /* %[fF] or sufficiently short %[gG] */ 1409 if (expt <= 0) { 1410 PRINT(zeroes, 1); 1411 if (prec || flags & ALT) 1412 PRINT(&decimal_point, 1); 1413 PAD(-expt, zeroes); 1414 /* already handled initial 0's */ 1415 prec += expt; 1416 } else { 1417 if (gs.grouping) { 1418 n = grouping_print(&gs, &io, 1419 cp, convbuf + ndig, loc); 1420 if (n < 0) 1421 goto error; 1422 cp += n; 1423 } else { 1424 PRINTANDPAD(cp, convbuf + ndig, 1425 expt, zeroes); 1426 cp += expt; 1427 } 1428 if (prec || flags & ALT) 1429 PRINT(&decimal_point, 1); 1430 } 1431 PRINTANDPAD(cp, convbuf + ndig, prec, zeroes); 1432 } else { /* %[eE] or sufficiently long %[gG] */ 1433 if (prec > 1 || flags & ALT) { 1434 buf[0] = *cp++; 1435 buf[1] = decimal_point; 1436 PRINT(buf, 2); 1437 PRINT(cp, ndig-1); 1438 PAD(prec - ndig, zeroes); 1439 } else /* XeYYY */ 1440 PRINT(cp, 1); 1441 PRINT(expstr, expsize); 1442 } 1443 } 1444#endif 1445 /* left-adjusting padding (always blank) */ 1446 if (flags & LADJUST) 1447 PAD(width - realsz, blanks); 1448 1449 /* finally, adjust ret */ 1450 ret += prsize; 1451 1452 FLUSH(); /* copy out the I/O vectors */ 1453 } 1454done: 1455 FLUSH(); 1456error: 1457 va_end(orgap); 1458 if (convbuf != NULL) 1459 free(convbuf); 1460 if (__sferror(fp)) 1461 ret = EOF; 1462 if ((argtable != NULL) && (argtable != statargtable)) 1463 free (argtable); 1464 return (ret); 1465 /* NOTREACHED */ 1466} 1467