158f0484fSRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 458f0484fSRodney W. Grimes * Copyright (c) 1990, 1993 558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 658f0484fSRodney W. Grimes * 758f0484fSRodney W. Grimes * This code is derived from software contributed to Berkeley by 858f0484fSRodney W. Grimes * Chris Torek. 958f0484fSRodney W. Grimes * 103c87aa1dSDavid Chisnall * Copyright (c) 2011 The FreeBSD Foundation 115b5fa75aSEd Maste * 123c87aa1dSDavid Chisnall * Portions of this software were developed by David Chisnall 133c87aa1dSDavid Chisnall * under sponsorship from the FreeBSD Foundation. 143c87aa1dSDavid Chisnall * 1558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 1658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1758f0484fSRodney W. Grimes * are met: 1858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 2058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 2158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 2258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 231d8053c5SEd Maste * 3. Neither the name of the University nor the names of its contributors 2458f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2558f0484fSRodney W. Grimes * without specific prior written permission. 2658f0484fSRodney W. Grimes * 2758f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2858f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2958f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3058f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 3158f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 3258f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3358f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3458f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3558f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3658f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3758f0484fSRodney W. Grimes * SUCH DAMAGE. 3858f0484fSRodney W. Grimes */ 3958f0484fSRodney W. Grimes 4058f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 4158f0484fSRodney W. Grimes static char sccsid[] = "@(#)vfprintf.c 8.1 (Berkeley) 6/4/93"; 4258f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 43333fc21eSDavid E. O'Brien #include <sys/cdefs.h> 4458f0484fSRodney W. Grimes /* 4558f0484fSRodney W. Grimes * Actual printf innards. 4658f0484fSRodney W. Grimes * 4758f0484fSRodney W. Grimes * This code is large and complicated... 4858f0484fSRodney W. Grimes */ 4958f0484fSRodney W. Grimes 50d201fe46SDaniel Eischen #include "namespace.h" 5158f0484fSRodney W. Grimes #include <sys/types.h> 5258f0484fSRodney W. Grimes 537735bb0fSBill Fenner #include <ctype.h> 54666d00d3SDavid Schultz #include <errno.h> 5558f0484fSRodney W. Grimes #include <limits.h> 567ae5c679SAlexey Zelkin #include <locale.h> 577735bb0fSBill Fenner #include <stddef.h> 587735bb0fSBill Fenner #include <stdint.h> 5958f0484fSRodney W. Grimes #include <stdio.h> 6058f0484fSRodney W. Grimes #include <stdlib.h> 6158f0484fSRodney W. Grimes #include <string.h> 62b9aac308STim J. Robbins #include <wchar.h> 6375067f4fSPoul-Henning Kamp #include <printf.h> 6458f0484fSRodney W. Grimes 6558f0484fSRodney W. Grimes #include <stdarg.h> 663c87aa1dSDavid Chisnall #include "xlocale_private.h" 67d201fe46SDaniel Eischen #include "un-namespace.h" 6858f0484fSRodney W. Grimes 69d201fe46SDaniel Eischen #include "libc_private.h" 7058f0484fSRodney W. Grimes #include "local.h" 7158f0484fSRodney W. Grimes #include "fvwrite.h" 722591efccSDavid Schultz #include "printflocal.h" 73e5abb5e6SDavid Schultz 743c87aa1dSDavid Chisnall static int __sprint(FILE *, struct __suio *, locale_t); 753c87aa1dSDavid Chisnall static int __sbprintf(FILE *, locale_t, const char *, va_list) __printflike(3, 0) 76a1805f7bSDavid Schultz __noinline; 77b9aac308STim J. Robbins static char *__wcsconv(wchar_t *, int); 78ce51cf03SJames Raynard 79814d1bc9SDavid Schultz #define CHAR char 80814d1bc9SDavid Schultz #include "printfcommon.h" 81814d1bc9SDavid Schultz 8221ca178eSDavid Schultz struct grouping_state { 8321ca178eSDavid Schultz char *thousands_sep; /* locale-specific thousands separator */ 8421ca178eSDavid Schultz int thousep_len; /* length of thousands_sep */ 8521ca178eSDavid Schultz const char *grouping; /* locale-specific numeric grouping rules */ 8621ca178eSDavid Schultz int lead; /* sig figs before decimal or group sep */ 8721ca178eSDavid Schultz int nseps; /* number of group separators with ' */ 8821ca178eSDavid Schultz int nrepeats; /* number of repeats of the last group */ 8921ca178eSDavid Schultz }; 9021ca178eSDavid Schultz 9121ca178eSDavid Schultz /* 9221ca178eSDavid Schultz * Initialize the thousands' grouping state in preparation to print a 9321ca178eSDavid Schultz * number with ndigits digits. This routine returns the total number 9421ca178eSDavid Schultz * of bytes that will be needed. 9521ca178eSDavid Schultz */ 9621ca178eSDavid Schultz static int 973c87aa1dSDavid Chisnall grouping_init(struct grouping_state *gs, int ndigits, locale_t loc) 9821ca178eSDavid Schultz { 9921ca178eSDavid Schultz struct lconv *locale; 10021ca178eSDavid Schultz 1013c87aa1dSDavid Chisnall locale = localeconv_l(loc); 10221ca178eSDavid Schultz gs->grouping = locale->grouping; 10321ca178eSDavid Schultz gs->thousands_sep = locale->thousands_sep; 10421ca178eSDavid Schultz gs->thousep_len = strlen(gs->thousands_sep); 10521ca178eSDavid Schultz 10621ca178eSDavid Schultz gs->nseps = gs->nrepeats = 0; 10721ca178eSDavid Schultz gs->lead = ndigits; 10821ca178eSDavid Schultz while (*gs->grouping != CHAR_MAX) { 10921ca178eSDavid Schultz if (gs->lead <= *gs->grouping) 11021ca178eSDavid Schultz break; 11121ca178eSDavid Schultz gs->lead -= *gs->grouping; 11221ca178eSDavid Schultz if (*(gs->grouping+1)) { 11321ca178eSDavid Schultz gs->nseps++; 11421ca178eSDavid Schultz gs->grouping++; 11521ca178eSDavid Schultz } else 11621ca178eSDavid Schultz gs->nrepeats++; 11721ca178eSDavid Schultz } 11821ca178eSDavid Schultz return ((gs->nseps + gs->nrepeats) * gs->thousep_len); 11921ca178eSDavid Schultz } 12021ca178eSDavid Schultz 12121ca178eSDavid Schultz /* 12221ca178eSDavid Schultz * Print a number with thousands' separators. 12321ca178eSDavid Schultz */ 12421ca178eSDavid Schultz static int 12521ca178eSDavid Schultz grouping_print(struct grouping_state *gs, struct io_state *iop, 1263c87aa1dSDavid Chisnall const CHAR *cp, const CHAR *ep, locale_t locale) 12721ca178eSDavid Schultz { 12821ca178eSDavid Schultz const CHAR *cp0 = cp; 12921ca178eSDavid Schultz 1303c87aa1dSDavid Chisnall if (io_printandpad(iop, cp, ep, gs->lead, zeroes, locale)) 13121ca178eSDavid Schultz return (-1); 13221ca178eSDavid Schultz cp += gs->lead; 13321ca178eSDavid Schultz while (gs->nseps > 0 || gs->nrepeats > 0) { 13421ca178eSDavid Schultz if (gs->nrepeats > 0) 13521ca178eSDavid Schultz gs->nrepeats--; 13621ca178eSDavid Schultz else { 13721ca178eSDavid Schultz gs->grouping--; 13821ca178eSDavid Schultz gs->nseps--; 13921ca178eSDavid Schultz } 1403c87aa1dSDavid Chisnall if (io_print(iop, gs->thousands_sep, gs->thousep_len, locale)) 14121ca178eSDavid Schultz return (-1); 1423c87aa1dSDavid Chisnall if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, locale)) 14321ca178eSDavid Schultz return (-1); 14421ca178eSDavid Schultz cp += *gs->grouping; 14521ca178eSDavid Schultz } 14621ca178eSDavid Schultz if (cp > ep) 14721ca178eSDavid Schultz cp = ep; 14821ca178eSDavid Schultz return (cp - cp0); 14921ca178eSDavid Schultz } 15021ca178eSDavid Schultz 15158f0484fSRodney W. Grimes /* 15258f0484fSRodney W. Grimes * Flush out all the vectors defined by the given uio, 15358f0484fSRodney W. Grimes * then reset it so that it can be reused. 15458f0484fSRodney W. Grimes */ 15558f0484fSRodney W. Grimes static int 1563c87aa1dSDavid Chisnall __sprint(FILE *fp, struct __suio *uio, locale_t locale) 15758f0484fSRodney W. Grimes { 158d201fe46SDaniel Eischen int err; 15958f0484fSRodney W. Grimes 16058f0484fSRodney W. Grimes if (uio->uio_resid == 0) { 16158f0484fSRodney W. Grimes uio->uio_iovcnt = 0; 16258f0484fSRodney W. Grimes return (0); 16358f0484fSRodney W. Grimes } 16458f0484fSRodney W. Grimes err = __sfvwrite(fp, uio); 16558f0484fSRodney W. Grimes uio->uio_resid = 0; 16658f0484fSRodney W. Grimes uio->uio_iovcnt = 0; 16758f0484fSRodney W. Grimes return (err); 16858f0484fSRodney W. Grimes } 16958f0484fSRodney W. Grimes 17058f0484fSRodney W. Grimes /* 17158f0484fSRodney W. Grimes * Helper function for `fprintf to unbuffered unix file': creates a 17258f0484fSRodney W. Grimes * temporary buffer. We only work on write-only files; this avoids 17358f0484fSRodney W. Grimes * worries about ungetc buffers and so forth. 17458f0484fSRodney W. Grimes */ 17558f0484fSRodney W. Grimes static int 1763c87aa1dSDavid Chisnall __sbprintf(FILE *fp, locale_t locale, const char *fmt, va_list ap) 17758f0484fSRodney W. Grimes { 17858f0484fSRodney W. Grimes int ret; 1791b0181dfSJohn Baldwin FILE fake = FAKE_FILE; 18058f0484fSRodney W. Grimes unsigned char buf[BUFSIZ]; 18158f0484fSRodney W. Grimes 182a1805f7bSDavid Schultz /* XXX This is probably not needed. */ 183a1805f7bSDavid Schultz if (prepwrite(fp) != 0) 184a1805f7bSDavid Schultz return (EOF); 185a1805f7bSDavid Schultz 18658f0484fSRodney W. Grimes /* copy the important variables */ 18758f0484fSRodney W. Grimes fake._flags = fp->_flags & ~__SNBF; 18858f0484fSRodney W. Grimes fake._file = fp->_file; 18958f0484fSRodney W. Grimes fake._cookie = fp->_cookie; 19058f0484fSRodney W. Grimes fake._write = fp->_write; 1911e98f887SJohn Baldwin fake._orientation = fp->_orientation; 1921e98f887SJohn Baldwin fake._mbstate = fp->_mbstate; 19358f0484fSRodney W. Grimes 19458f0484fSRodney W. Grimes /* set up the buffer */ 19558f0484fSRodney W. Grimes fake._bf._base = fake._p = buf; 19658f0484fSRodney W. Grimes fake._bf._size = fake._w = sizeof(buf); 19758f0484fSRodney W. Grimes fake._lbfsize = 0; /* not actually used, but Just In Case */ 19858f0484fSRodney W. Grimes 19958f0484fSRodney W. Grimes /* do the work, then copy any error status */ 2003c87aa1dSDavid Chisnall ret = __vfprintf(&fake, locale, fmt, ap); 201d201fe46SDaniel Eischen if (ret >= 0 && __fflush(&fake)) 20258f0484fSRodney W. Grimes ret = EOF; 20358f0484fSRodney W. Grimes if (fake._flags & __SERR) 20458f0484fSRodney W. Grimes fp->_flags |= __SERR; 20558f0484fSRodney W. Grimes return (ret); 20658f0484fSRodney W. Grimes } 20758f0484fSRodney W. Grimes 20858f0484fSRodney W. Grimes /* 209b9aac308STim J. Robbins * Convert a wide character string argument for the %ls format to a multibyte 210d48c77b5STim J. Robbins * string representation. If not -1, prec specifies the maximum number of 211d48c77b5STim J. Robbins * bytes to output, and also means that we can't assume that the wide char. 212d48c77b5STim J. Robbins * string ends is null-terminated. 213b9aac308STim J. Robbins */ 214b9aac308STim J. Robbins static char * 215b9aac308STim J. Robbins __wcsconv(wchar_t *wcsarg, int prec) 216b9aac308STim J. Robbins { 21793996f6dSTim J. Robbins static const mbstate_t initial; 21893996f6dSTim J. Robbins mbstate_t mbs; 219b9aac308STim J. Robbins char buf[MB_LEN_MAX]; 220b9aac308STim J. Robbins wchar_t *p; 221d48c77b5STim J. Robbins char *convbuf; 222b9aac308STim J. Robbins size_t clen, nbytes; 223b9aac308STim J. Robbins 224d48c77b5STim J. Robbins /* Allocate space for the maximum number of bytes we could output. */ 225d48c77b5STim J. Robbins if (prec < 0) { 226d48c77b5STim J. Robbins p = wcsarg; 227d48c77b5STim J. Robbins mbs = initial; 228d48c77b5STim J. Robbins nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs); 229d48c77b5STim J. Robbins if (nbytes == (size_t)-1) 230d48c77b5STim J. Robbins return (NULL); 231d48c77b5STim J. Robbins } else { 232b9aac308STim J. Robbins /* 233d48c77b5STim J. Robbins * Optimisation: if the output precision is small enough, 234d48c77b5STim J. Robbins * just allocate enough memory for the maximum instead of 235d48c77b5STim J. Robbins * scanning the string. 236b9aac308STim J. Robbins */ 237d48c77b5STim J. Robbins if (prec < 128) 238d48c77b5STim J. Robbins nbytes = prec; 239d48c77b5STim J. Robbins else { 240b9aac308STim J. Robbins nbytes = 0; 241b9aac308STim J. Robbins p = wcsarg; 24293996f6dSTim J. Robbins mbs = initial; 243b9aac308STim J. Robbins for (;;) { 24493996f6dSTim J. Robbins clen = wcrtomb(buf, *p++, &mbs); 245b9aac308STim J. Robbins if (clen == 0 || clen == (size_t)-1 || 246b9aac308STim J. Robbins nbytes + clen > prec) 247b9aac308STim J. Robbins break; 248b9aac308STim J. Robbins nbytes += clen; 249b9aac308STim J. Robbins } 250d48c77b5STim J. Robbins } 251b9aac308STim J. Robbins } 252b9aac308STim J. Robbins if ((convbuf = malloc(nbytes + 1)) == NULL) 253b9aac308STim J. Robbins return (NULL); 254b9aac308STim J. Robbins 255d48c77b5STim J. Robbins /* Fill the output buffer. */ 256b9aac308STim J. Robbins p = wcsarg; 25793996f6dSTim J. Robbins mbs = initial; 258d48c77b5STim J. Robbins if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p, 259d48c77b5STim J. Robbins nbytes, &mbs)) == (size_t)-1) { 2606f098a48SAndrey A. Chernov free(convbuf); 261b9aac308STim J. Robbins return (NULL); 2626f098a48SAndrey A. Chernov } 263d48c77b5STim J. Robbins convbuf[nbytes] = '\0'; 264b9aac308STim J. Robbins return (convbuf); 265b9aac308STim J. Robbins } 266b9aac308STim J. Robbins 267b9aac308STim J. Robbins /* 268d201fe46SDaniel Eischen * MT-safe version 269d201fe46SDaniel Eischen */ 270d201fe46SDaniel Eischen int 2713c87aa1dSDavid Chisnall vfprintf_l(FILE * __restrict fp, locale_t locale, const char * __restrict fmt0, 2723c87aa1dSDavid Chisnall va_list ap) 273d201fe46SDaniel Eischen { 274d201fe46SDaniel Eischen int ret; 2753c87aa1dSDavid Chisnall FIX_LOCALE(locale); 276d201fe46SDaniel Eischen 277fda0a14fSKonstantin Belousov FLOCKFILE_CANCELSAFE(fp); 278a1805f7bSDavid Schultz /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 279a1805f7bSDavid Schultz if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 280a1805f7bSDavid Schultz fp->_file >= 0) 2813c87aa1dSDavid Chisnall ret = __sbprintf(fp, locale, fmt0, ap); 282a1805f7bSDavid Schultz else 2833c87aa1dSDavid Chisnall ret = __vfprintf(fp, locale, fmt0, ap); 284fda0a14fSKonstantin Belousov FUNLOCKFILE_CANCELSAFE(); 285d201fe46SDaniel Eischen return (ret); 286d201fe46SDaniel Eischen } 2873c87aa1dSDavid Chisnall int 2883c87aa1dSDavid Chisnall vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap) 2893c87aa1dSDavid Chisnall { 2903c87aa1dSDavid Chisnall return vfprintf_l(fp, __get_locale(), fmt0, ap); 2913c87aa1dSDavid Chisnall } 292d201fe46SDaniel Eischen 29338cac8f8SDavid Schultz /* 29438cac8f8SDavid Schultz * The size of the buffer we use as scratch space for integer 29521ca178eSDavid Schultz * conversions, among other things. We need enough space to 29621ca178eSDavid Schultz * write a uintmax_t in octal (plus one byte). 29738cac8f8SDavid Schultz */ 29821ca178eSDavid Schultz #if UINTMAX_MAX <= UINT64_MAX 29921ca178eSDavid Schultz #define BUF 32 30021ca178eSDavid Schultz #else 30121ca178eSDavid Schultz #error "BUF must be large enough to format a uintmax_t" 30221ca178eSDavid Schultz #endif 30338cac8f8SDavid Schultz 30458f0484fSRodney W. Grimes /* 305d201fe46SDaniel Eischen * Non-MT-safe version 306d201fe46SDaniel Eischen */ 30758f0484fSRodney W. Grimes int 3083c87aa1dSDavid Chisnall __vfprintf(FILE *fp, locale_t locale, const char *fmt0, va_list ap) 30958f0484fSRodney W. Grimes { 310d201fe46SDaniel Eischen char *fmt; /* format string */ 311d201fe46SDaniel Eischen int ch; /* character from fmt */ 312d201fe46SDaniel Eischen int n, n2; /* handy integer (short term usage) */ 313d201fe46SDaniel Eischen char *cp; /* handy char pointer (short term usage) */ 314d201fe46SDaniel Eischen int flags; /* flags as above */ 31558f0484fSRodney W. Grimes int ret; /* return value accumulator */ 31658f0484fSRodney W. Grimes int width; /* width from format (%8d), or 0 */ 317ebbad5ecSDavid Schultz int prec; /* precision from format; <0 for N/A */ 318e95725feSKonstantin Belousov int saved_errno; 31958f0484fSRodney W. Grimes char sign; /* sign prefix (' ', '+', '-', or \0) */ 32021ca178eSDavid Schultz struct grouping_state gs; /* thousands' grouping info */ 321814d1bc9SDavid Schultz 3228de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 323ebbad5ecSDavid Schultz /* 324ebbad5ecSDavid Schultz * We can decompose the printed representation of floating 325ebbad5ecSDavid Schultz * point numbers into several parts, some of which may be empty: 326ebbad5ecSDavid Schultz * 327ebbad5ecSDavid Schultz * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ 328ebbad5ecSDavid Schultz * A B ---C--- D E F 329ebbad5ecSDavid Schultz * 330ebbad5ecSDavid Schultz * A: 'sign' holds this value if present; '\0' otherwise 331ebbad5ecSDavid Schultz * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal 332ebbad5ecSDavid Schultz * C: cp points to the string MMMNNN. Leading and trailing 333ebbad5ecSDavid Schultz * zeros are not in the string and must be added. 334ebbad5ecSDavid Schultz * D: expchar holds this character; '\0' if no exponent, e.g. %f 335ebbad5ecSDavid Schultz * F: at least two digits for decimal, at least one digit for hex 336ebbad5ecSDavid Schultz */ 3377ae5c679SAlexey Zelkin char *decimal_point; /* locale specific decimal point */ 3385004a238SDavid Schultz int decpt_len; /* length of decimal_point */ 339ebbad5ecSDavid Schultz int signflag; /* true if float is negative */ 340ebbad5ecSDavid Schultz union { /* floating point arguments %[aAeEfFgG] */ 341ebbad5ecSDavid Schultz double dbl; 342ebbad5ecSDavid Schultz long double ldbl; 343ebbad5ecSDavid Schultz } fparg; 34458f0484fSRodney W. Grimes int expt; /* integer value of exponent */ 345ebbad5ecSDavid Schultz char expchar; /* exponent character: [eEpP\0] */ 346ebbad5ecSDavid Schultz char *dtoaend; /* pointer to end of converted digits */ 34758f0484fSRodney W. Grimes int expsize; /* character count for expstr */ 348ebbad5ecSDavid Schultz int ndig; /* actual number of digits returned by dtoa */ 349ebbad5ecSDavid Schultz char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ 3502ffc61baSTor Egge char *dtoaresult; /* buffer allocated by dtoa */ 35158f0484fSRodney W. Grimes #endif 35258f0484fSRodney W. Grimes u_long ulval; /* integer arguments %[diouxX] */ 3537735bb0fSBill Fenner uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 35458f0484fSRodney W. Grimes int base; /* base for [diouxX] conversion */ 35558f0484fSRodney W. Grimes int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 356261a532aSBill Fenner int realsz; /* field size expanded by dprec, sign, etc */ 35758f0484fSRodney W. Grimes int size; /* size of converted field or string */ 35892e88f87SAndrey A. Chernov int prsize; /* max size of printed field */ 359ebbad5ecSDavid Schultz const char *xdigs; /* digits for %[xX] conversion */ 360814d1bc9SDavid Schultz struct io_state io; /* I/O buffering state */ 36138cac8f8SDavid Schultz char buf[BUF]; /* buffer with space for digits of uintmax_t */ 362ebbad5ecSDavid Schultz char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ 363a387081cSDoug Rabson union arg *argtable; /* args, built due to positional arg */ 364a387081cSDoug Rabson union arg statargtable [STATIC_ARG_TBL_SIZE]; 365efb7e53dSJordan K. Hubbard int nextarg; /* 1-based argument index */ 366efb7e53dSJordan K. Hubbard va_list orgap; /* original argument pointer */ 367b9aac308STim J. Robbins char *convbuf; /* wide to multibyte conversion result */ 3681bf6c5f1SAndrey A. Chernov int savserr; 36958f0484fSRodney W. Grimes 370ac9913a7SDavid Schultz static const char xdigs_lower[16] = "0123456789abcdef"; 371ac9913a7SDavid Schultz static const char xdigs_upper[16] = "0123456789ABCDEF"; 372ebbad5ecSDavid Schultz 373814d1bc9SDavid Schultz /* BEWARE, these `goto error' on error. */ 37458f0484fSRodney W. Grimes #define PRINT(ptr, len) { \ 3753c87aa1dSDavid Chisnall if (io_print(&io, (ptr), (len), locale)) \ 37658f0484fSRodney W. Grimes goto error; \ 37758f0484fSRodney W. Grimes } 37858f0484fSRodney W. Grimes #define PAD(howmany, with) { \ 3793c87aa1dSDavid Chisnall if (io_pad(&io, (howmany), (with), locale)) \ 38058f0484fSRodney W. Grimes goto error; \ 381814d1bc9SDavid Schultz } 382814d1bc9SDavid Schultz #define PRINTANDPAD(p, ep, len, with) { \ 3833c87aa1dSDavid Chisnall if (io_printandpad(&io, (p), (ep), (len), (with), locale)) \ 384814d1bc9SDavid Schultz goto error; \ 385814d1bc9SDavid Schultz } 386814d1bc9SDavid Schultz #define FLUSH() { \ 3873c87aa1dSDavid Chisnall if (io_flush(&io, locale)) \ 388814d1bc9SDavid Schultz goto error; \ 38958f0484fSRodney W. Grimes } 39058f0484fSRodney W. Grimes 39158f0484fSRodney W. Grimes /* 392efb7e53dSJordan K. Hubbard * Get the argument indexed by nextarg. If the argument table is 393efb7e53dSJordan K. Hubbard * built, use it to get the argument. If its not, get the next 394efb7e53dSJordan K. Hubbard * argument (and arguments must be gotten sequentially). 395efb7e53dSJordan K. Hubbard */ 396efb7e53dSJordan K. Hubbard #define GETARG(type) \ 397a387081cSDoug Rabson ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ 398efb7e53dSJordan K. Hubbard (nextarg++, va_arg(ap, type))) 399efb7e53dSJordan K. Hubbard 400efb7e53dSJordan K. Hubbard /* 40158f0484fSRodney W. Grimes * To extend shorts properly, we need both signed and unsigned 40258f0484fSRodney W. Grimes * argument extraction methods. 40358f0484fSRodney W. Grimes */ 40458f0484fSRodney W. Grimes #define SARG() \ 405efb7e53dSJordan K. Hubbard (flags&LONGINT ? GETARG(long) : \ 406efb7e53dSJordan K. Hubbard flags&SHORTINT ? (long)(short)GETARG(int) : \ 4077735bb0fSBill Fenner flags&CHARINT ? (long)(signed char)GETARG(int) : \ 408efb7e53dSJordan K. Hubbard (long)GETARG(int)) 40958f0484fSRodney W. Grimes #define UARG() \ 410efb7e53dSJordan K. Hubbard (flags&LONGINT ? GETARG(u_long) : \ 411efb7e53dSJordan K. Hubbard flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 4127735bb0fSBill Fenner flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 413efb7e53dSJordan K. Hubbard (u_long)GETARG(u_int)) 4147735bb0fSBill Fenner #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 4157735bb0fSBill Fenner #define SJARG() \ 4167735bb0fSBill Fenner (flags&INTMAXT ? GETARG(intmax_t) : \ 4170881683bSDavid Schultz flags&SIZET ? (intmax_t)GETARG(ssize_t) : \ 4187735bb0fSBill Fenner flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 4197735bb0fSBill Fenner (intmax_t)GETARG(long long)) 4207735bb0fSBill Fenner #define UJARG() \ 4217735bb0fSBill Fenner (flags&INTMAXT ? GETARG(uintmax_t) : \ 4227735bb0fSBill Fenner flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 4237735bb0fSBill Fenner flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \ 4247735bb0fSBill Fenner (uintmax_t)GETARG(unsigned long long)) 425efb7e53dSJordan K. Hubbard 426efb7e53dSJordan K. Hubbard /* 427efb7e53dSJordan K. Hubbard * Get * arguments, including the form *nn$. Preserve the nextarg 428efb7e53dSJordan K. Hubbard * that the argument can be gotten once the type is determined. 429efb7e53dSJordan K. Hubbard */ 430efb7e53dSJordan K. Hubbard #define GETASTER(val) \ 431efb7e53dSJordan K. Hubbard n2 = 0; \ 432efb7e53dSJordan K. Hubbard cp = fmt; \ 433efb7e53dSJordan K. Hubbard while (is_digit(*cp)) { \ 434efb7e53dSJordan K. Hubbard n2 = 10 * n2 + to_digit(*cp); \ 435efb7e53dSJordan K. Hubbard cp++; \ 436efb7e53dSJordan K. Hubbard } \ 437efb7e53dSJordan K. Hubbard if (*cp == '$') { \ 438efb7e53dSJordan K. Hubbard int hold = nextarg; \ 439efb7e53dSJordan K. Hubbard if (argtable == NULL) { \ 440efb7e53dSJordan K. Hubbard argtable = statargtable; \ 441e62e5ff9SDavid Schultz if (__find_arguments (fmt0, orgap, &argtable)) { \ 442e62e5ff9SDavid Schultz ret = EOF; \ 443e62e5ff9SDavid Schultz goto error; \ 444e62e5ff9SDavid Schultz } \ 445efb7e53dSJordan K. Hubbard } \ 446efb7e53dSJordan K. Hubbard nextarg = n2; \ 447efb7e53dSJordan K. Hubbard val = GETARG (int); \ 448efb7e53dSJordan K. Hubbard nextarg = hold; \ 449efb7e53dSJordan K. Hubbard fmt = ++cp; \ 450efb7e53dSJordan K. Hubbard } else { \ 451efb7e53dSJordan K. Hubbard val = GETARG (int); \ 452efb7e53dSJordan K. Hubbard } 453efb7e53dSJordan K. Hubbard 45433bff5d3SDavid Schultz if (__use_xprintf == 0 && getenv("USE_XPRINTF")) 45533bff5d3SDavid Schultz __use_xprintf = 1; 45633bff5d3SDavid Schultz if (__use_xprintf > 0) 45733bff5d3SDavid Schultz return (__xvprintf(fp, fmt0, ap)); 45858f0484fSRodney W. Grimes 45958f0484fSRodney W. Grimes /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 460450ead86SPedro F. Giffuni if (prepwrite(fp) != 0) { 461450ead86SPedro F. Giffuni errno = EBADF; 46258f0484fSRodney W. Grimes return (EOF); 463450ead86SPedro F. Giffuni } 46458f0484fSRodney W. Grimes 4651bf6c5f1SAndrey A. Chernov savserr = fp->_flags & __SERR; 4661bf6c5f1SAndrey A. Chernov fp->_flags &= ~__SERR; 4671bf6c5f1SAndrey A. Chernov 468e95725feSKonstantin Belousov saved_errno = errno; 469e18701f4SDavid Schultz convbuf = NULL; 47058f0484fSRodney W. Grimes fmt = (char *)fmt0; 471efb7e53dSJordan K. Hubbard argtable = NULL; 472efb7e53dSJordan K. Hubbard nextarg = 1; 473d07090a8STim J. Robbins va_copy(orgap, ap); 474814d1bc9SDavid Schultz io_init(&io, fp); 47558f0484fSRodney W. Grimes ret = 0; 476e18701f4SDavid Schultz #ifndef NO_FLOATING_POINT 477e18701f4SDavid Schultz dtoaresult = NULL; 4783c87aa1dSDavid Chisnall decimal_point = localeconv_l(locale)->decimal_point; 4795004a238SDavid Schultz /* The overwhelmingly common case is decpt_len == 1. */ 4805004a238SDavid Schultz decpt_len = (decimal_point[1] == '\0' ? 1 : strlen(decimal_point)); 481e18701f4SDavid Schultz #endif 48258f0484fSRodney W. Grimes 48358f0484fSRodney W. Grimes /* 48458f0484fSRodney W. Grimes * Scan the format for conversions (`%' character). 48558f0484fSRodney W. Grimes */ 48658f0484fSRodney W. Grimes for (;;) { 48758f0484fSRodney W. Grimes for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++) 48858f0484fSRodney W. Grimes /* void */; 48958f0484fSRodney W. Grimes if ((n = fmt - cp) != 0) { 490b250f248SAndrey A. Chernov if ((unsigned)ret + n > INT_MAX) { 49192e88f87SAndrey A. Chernov ret = EOF; 492666d00d3SDavid Schultz errno = EOVERFLOW; 49392e88f87SAndrey A. Chernov goto error; 49492e88f87SAndrey A. Chernov } 49558f0484fSRodney W. Grimes PRINT(cp, n); 49658f0484fSRodney W. Grimes ret += n; 49758f0484fSRodney W. Grimes } 49858f0484fSRodney W. Grimes if (ch == '\0') 49958f0484fSRodney W. Grimes goto done; 50058f0484fSRodney W. Grimes fmt++; /* skip over '%' */ 50158f0484fSRodney W. Grimes 50258f0484fSRodney W. Grimes flags = 0; 50358f0484fSRodney W. Grimes dprec = 0; 50458f0484fSRodney W. Grimes width = 0; 50558f0484fSRodney W. Grimes prec = -1; 50621ca178eSDavid Schultz gs.grouping = NULL; 50758f0484fSRodney W. Grimes sign = '\0'; 508ebbad5ecSDavid Schultz ox[1] = '\0'; 50958f0484fSRodney W. Grimes 51058f0484fSRodney W. Grimes rflag: ch = *fmt++; 51158f0484fSRodney W. Grimes reswitch: switch (ch) { 51258f0484fSRodney W. Grimes case ' ': 5132e394b2fSAlexey Zelkin /*- 51458f0484fSRodney W. Grimes * ``If the space and + flags both appear, the space 51558f0484fSRodney W. Grimes * flag will be ignored.'' 51658f0484fSRodney W. Grimes * -- ANSI X3J11 51758f0484fSRodney W. Grimes */ 51858f0484fSRodney W. Grimes if (!sign) 51958f0484fSRodney W. Grimes sign = ' '; 52058f0484fSRodney W. Grimes goto rflag; 52158f0484fSRodney W. Grimes case '#': 52258f0484fSRodney W. Grimes flags |= ALT; 52358f0484fSRodney W. Grimes goto rflag; 52458f0484fSRodney W. Grimes case '*': 5252e394b2fSAlexey Zelkin /*- 52658f0484fSRodney W. Grimes * ``A negative field width argument is taken as a 52758f0484fSRodney W. Grimes * - flag followed by a positive field width.'' 52858f0484fSRodney W. Grimes * -- ANSI X3J11 52958f0484fSRodney W. Grimes * They don't exclude field widths read from args. 53058f0484fSRodney W. Grimes */ 531efb7e53dSJordan K. Hubbard GETASTER (width); 532efb7e53dSJordan K. Hubbard if (width >= 0) 53358f0484fSRodney W. Grimes goto rflag; 53458f0484fSRodney W. Grimes width = -width; 53558f0484fSRodney W. Grimes /* FALLTHROUGH */ 53658f0484fSRodney W. Grimes case '-': 53758f0484fSRodney W. Grimes flags |= LADJUST; 53858f0484fSRodney W. Grimes goto rflag; 53958f0484fSRodney W. Grimes case '+': 54058f0484fSRodney W. Grimes sign = '+'; 54158f0484fSRodney W. Grimes goto rflag; 5427735bb0fSBill Fenner case '\'': 54398ee7635SAlexey Zelkin flags |= GROUPING; 5447735bb0fSBill Fenner goto rflag; 54558f0484fSRodney W. Grimes case '.': 54658f0484fSRodney W. Grimes if ((ch = *fmt++) == '*') { 5473b204b7dSDavid Schultz GETASTER (prec); 54858f0484fSRodney W. Grimes goto rflag; 54958f0484fSRodney W. Grimes } 5503b204b7dSDavid Schultz prec = 0; 55158f0484fSRodney W. Grimes while (is_digit(ch)) { 5523b204b7dSDavid Schultz prec = 10 * prec + to_digit(ch); 55358f0484fSRodney W. Grimes ch = *fmt++; 55458f0484fSRodney W. Grimes } 55558f0484fSRodney W. Grimes goto reswitch; 55658f0484fSRodney W. Grimes case '0': 5572e394b2fSAlexey Zelkin /*- 55858f0484fSRodney W. Grimes * ``Note that 0 is taken as a flag, not as the 55958f0484fSRodney W. Grimes * beginning of a field width.'' 56058f0484fSRodney W. Grimes * -- ANSI X3J11 56158f0484fSRodney W. Grimes */ 56258f0484fSRodney W. Grimes flags |= ZEROPAD; 56358f0484fSRodney W. Grimes goto rflag; 56458f0484fSRodney W. Grimes case '1': case '2': case '3': case '4': 56558f0484fSRodney W. Grimes case '5': case '6': case '7': case '8': case '9': 56658f0484fSRodney W. Grimes n = 0; 56758f0484fSRodney W. Grimes do { 56858f0484fSRodney W. Grimes n = 10 * n + to_digit(ch); 56958f0484fSRodney W. Grimes ch = *fmt++; 57058f0484fSRodney W. Grimes } while (is_digit(ch)); 571efb7e53dSJordan K. Hubbard if (ch == '$') { 572efb7e53dSJordan K. Hubbard nextarg = n; 573efb7e53dSJordan K. Hubbard if (argtable == NULL) { 574efb7e53dSJordan K. Hubbard argtable = statargtable; 575e62e5ff9SDavid Schultz if (__find_arguments (fmt0, orgap, 576e62e5ff9SDavid Schultz &argtable)) { 577e62e5ff9SDavid Schultz ret = EOF; 578e62e5ff9SDavid Schultz goto error; 579e62e5ff9SDavid Schultz } 580efb7e53dSJordan K. Hubbard } 581efb7e53dSJordan K. Hubbard goto rflag; 582efb7e53dSJordan K. Hubbard } 58358f0484fSRodney W. Grimes width = n; 58458f0484fSRodney W. Grimes goto reswitch; 5858de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 58658f0484fSRodney W. Grimes case 'L': 58758f0484fSRodney W. Grimes flags |= LONGDBL; 58858f0484fSRodney W. Grimes goto rflag; 58958f0484fSRodney W. Grimes #endif 59058f0484fSRodney W. Grimes case 'h': 5917735bb0fSBill Fenner if (flags & SHORTINT) { 5927735bb0fSBill Fenner flags &= ~SHORTINT; 5937735bb0fSBill Fenner flags |= CHARINT; 5947735bb0fSBill Fenner } else 59558f0484fSRodney W. Grimes flags |= SHORTINT; 59658f0484fSRodney W. Grimes goto rflag; 5977735bb0fSBill Fenner case 'j': 5987735bb0fSBill Fenner flags |= INTMAXT; 5997735bb0fSBill Fenner goto rflag; 60058f0484fSRodney W. Grimes case 'l': 6017735bb0fSBill Fenner if (flags & LONGINT) { 6027735bb0fSBill Fenner flags &= ~LONGINT; 6037735bb0fSBill Fenner flags |= LLONGINT; 6047735bb0fSBill Fenner } else 60558f0484fSRodney W. Grimes flags |= LONGINT; 60658f0484fSRodney W. Grimes goto rflag; 60758f0484fSRodney W. Grimes case 'q': 6087735bb0fSBill Fenner flags |= LLONGINT; /* not necessarily */ 6097735bb0fSBill Fenner goto rflag; 6107735bb0fSBill Fenner case 't': 6117735bb0fSBill Fenner flags |= PTRDIFFT; 6127735bb0fSBill Fenner goto rflag; 6137735bb0fSBill Fenner case 'z': 6147735bb0fSBill Fenner flags |= SIZET; 61558f0484fSRodney W. Grimes goto rflag; 616*d9dc1603SDag-Erling Smørgrav case 'B': 617*d9dc1603SDag-Erling Smørgrav case 'b': 618*d9dc1603SDag-Erling Smørgrav if (flags & INTMAX_SIZE) 619*d9dc1603SDag-Erling Smørgrav ujval = UJARG(); 620*d9dc1603SDag-Erling Smørgrav else 621*d9dc1603SDag-Erling Smørgrav ulval = UARG(); 622*d9dc1603SDag-Erling Smørgrav base = 2; 623*d9dc1603SDag-Erling Smørgrav /* leading 0b/B only if non-zero */ 624*d9dc1603SDag-Erling Smørgrav if (flags & ALT && 625*d9dc1603SDag-Erling Smørgrav (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 626*d9dc1603SDag-Erling Smørgrav ox[1] = ch; 627*d9dc1603SDag-Erling Smørgrav goto nosign; 628*d9dc1603SDag-Erling Smørgrav break; 629927ecbf3STim J. Robbins case 'C': 630927ecbf3STim J. Robbins flags |= LONGINT; 631927ecbf3STim J. Robbins /*FALLTHROUGH*/ 63258f0484fSRodney W. Grimes case 'c': 633b9aac308STim J. Robbins if (flags & LONGINT) { 63493996f6dSTim J. Robbins static const mbstate_t initial; 63593996f6dSTim J. Robbins mbstate_t mbs; 636b9aac308STim J. Robbins size_t mbseqlen; 637b9aac308STim J. Robbins 63893996f6dSTim J. Robbins mbs = initial; 639b9aac308STim J. Robbins mbseqlen = wcrtomb(cp = buf, 64093996f6dSTim J. Robbins (wchar_t)GETARG(wint_t), &mbs); 6416180233fSTim J. Robbins if (mbseqlen == (size_t)-1) { 6426180233fSTim J. Robbins fp->_flags |= __SERR; 643b9aac308STim J. Robbins goto error; 6446180233fSTim J. Robbins } 645b9aac308STim J. Robbins size = (int)mbseqlen; 646b9aac308STim J. Robbins } else { 647efb7e53dSJordan K. Hubbard *(cp = buf) = GETARG(int); 64858f0484fSRodney W. Grimes size = 1; 649b9aac308STim J. Robbins } 65058f0484fSRodney W. Grimes sign = '\0'; 65158f0484fSRodney W. Grimes break; 65258f0484fSRodney W. Grimes case 'D': 65358f0484fSRodney W. Grimes flags |= LONGINT; 65458f0484fSRodney W. Grimes /*FALLTHROUGH*/ 65558f0484fSRodney W. Grimes case 'd': 65658f0484fSRodney W. Grimes case 'i': 6577735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 6587735bb0fSBill Fenner ujval = SJARG(); 6597735bb0fSBill Fenner if ((intmax_t)ujval < 0) { 6607735bb0fSBill Fenner ujval = -ujval; 66158f0484fSRodney W. Grimes sign = '-'; 66258f0484fSRodney W. Grimes } 66358f0484fSRodney W. Grimes } else { 66458f0484fSRodney W. Grimes ulval = SARG(); 66558f0484fSRodney W. Grimes if ((long)ulval < 0) { 66658f0484fSRodney W. Grimes ulval = -ulval; 66758f0484fSRodney W. Grimes sign = '-'; 66858f0484fSRodney W. Grimes } 66958f0484fSRodney W. Grimes } 67058f0484fSRodney W. Grimes base = 10; 67158f0484fSRodney W. Grimes goto number; 6728de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 6737735bb0fSBill Fenner case 'a': 6747735bb0fSBill Fenner case 'A': 675ebbad5ecSDavid Schultz if (ch == 'a') { 676ebbad5ecSDavid Schultz ox[1] = 'x'; 677ebbad5ecSDavid Schultz xdigs = xdigs_lower; 678ebbad5ecSDavid Schultz expchar = 'p'; 679ebbad5ecSDavid Schultz } else { 680ebbad5ecSDavid Schultz ox[1] = 'X'; 681ebbad5ecSDavid Schultz xdigs = xdigs_upper; 682ebbad5ecSDavid Schultz expchar = 'P'; 683ebbad5ecSDavid Schultz } 684904322a5SDavid Schultz if (prec >= 0) 685904322a5SDavid Schultz prec++; 686904322a5SDavid Schultz if (dtoaresult != NULL) 687904322a5SDavid Schultz freedtoa(dtoaresult); 688ebbad5ecSDavid Schultz if (flags & LONGDBL) { 689904322a5SDavid Schultz fparg.ldbl = GETARG(long double); 690ebbad5ecSDavid Schultz dtoaresult = cp = 691ebbad5ecSDavid Schultz __hldtoa(fparg.ldbl, xdigs, prec, 692ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 693ebbad5ecSDavid Schultz } else { 694ebbad5ecSDavid Schultz fparg.dbl = GETARG(double); 695ebbad5ecSDavid Schultz dtoaresult = cp = 696ebbad5ecSDavid Schultz __hdtoa(fparg.dbl, xdigs, prec, 697ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 698ebbad5ecSDavid Schultz } 699904322a5SDavid Schultz if (prec < 0) 700904322a5SDavid Schultz prec = dtoaend - cp; 701904322a5SDavid Schultz if (expt == INT_MAX) 702904322a5SDavid Schultz ox[1] = '\0'; 703904322a5SDavid Schultz goto fp_common; 704d26be6f0SBruce Evans case 'e': 70558f0484fSRodney W. Grimes case 'E': 706ebbad5ecSDavid Schultz expchar = ch; 707ebbad5ecSDavid Schultz if (prec < 0) /* account for digit before decpt */ 708ebbad5ecSDavid Schultz prec = DEFPREC + 1; 709ebbad5ecSDavid Schultz else 710ebbad5ecSDavid Schultz prec++; 711ebbad5ecSDavid Schultz goto fp_begin; 712d26be6f0SBruce Evans case 'f': 7137735bb0fSBill Fenner case 'F': 714ebbad5ecSDavid Schultz expchar = '\0'; 715d26be6f0SBruce Evans goto fp_begin; 71658f0484fSRodney W. Grimes case 'g': 71758f0484fSRodney W. Grimes case 'G': 718ebbad5ecSDavid Schultz expchar = ch - ('g' - 'e'); 719d26be6f0SBruce Evans if (prec == 0) 720d26be6f0SBruce Evans prec = 1; 721ebbad5ecSDavid Schultz fp_begin: 722ebbad5ecSDavid Schultz if (prec < 0) 72358f0484fSRodney W. Grimes prec = DEFPREC; 724ebbad5ecSDavid Schultz if (dtoaresult != NULL) 725ebbad5ecSDavid Schultz freedtoa(dtoaresult); 726ebbad5ecSDavid Schultz if (flags & LONGDBL) { 727ebbad5ecSDavid Schultz fparg.ldbl = GETARG(long double); 728ebbad5ecSDavid Schultz dtoaresult = cp = 729ebbad5ecSDavid Schultz __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, 730ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 731ebbad5ecSDavid Schultz } else { 732ebbad5ecSDavid Schultz fparg.dbl = GETARG(double); 733ebbad5ecSDavid Schultz dtoaresult = cp = 734ebbad5ecSDavid Schultz dtoa(fparg.dbl, expchar ? 2 : 3, prec, 735ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 736ebbad5ecSDavid Schultz if (expt == 9999) 737ebbad5ecSDavid Schultz expt = INT_MAX; 73858f0484fSRodney W. Grimes } 739904322a5SDavid Schultz fp_common: 740ebbad5ecSDavid Schultz if (signflag) 741ebbad5ecSDavid Schultz sign = '-'; 742ebbad5ecSDavid Schultz if (expt == INT_MAX) { /* inf or nan */ 743ebbad5ecSDavid Schultz if (*cp == 'N') { 744ebbad5ecSDavid Schultz cp = (ch >= 'a') ? "nan" : "NAN"; 745ebbad5ecSDavid Schultz sign = '\0'; 746ebbad5ecSDavid Schultz } else 747ebbad5ecSDavid Schultz cp = (ch >= 'a') ? "inf" : "INF"; 74858f0484fSRodney W. Grimes size = 3; 749970a466cSDavid Schultz flags &= ~ZEROPAD; 75058f0484fSRodney W. Grimes break; 75158f0484fSRodney W. Grimes } 75258f0484fSRodney W. Grimes flags |= FPT; 753ebbad5ecSDavid Schultz ndig = dtoaend - cp; 75458f0484fSRodney W. Grimes if (ch == 'g' || ch == 'G') { 755ebbad5ecSDavid Schultz if (expt > -4 && expt <= prec) { 756ebbad5ecSDavid Schultz /* Make %[gG] smell like %[fF] */ 757ebbad5ecSDavid Schultz expchar = '\0'; 758ebbad5ecSDavid Schultz if (flags & ALT) 759ebbad5ecSDavid Schultz prec -= expt; 76058f0484fSRodney W. Grimes else 761ebbad5ecSDavid Schultz prec = ndig - expt; 762ebbad5ecSDavid Schultz if (prec < 0) 763ebbad5ecSDavid Schultz prec = 0; 7641f2a0cdfSDavid Schultz } else { 7651f2a0cdfSDavid Schultz /* 7661f2a0cdfSDavid Schultz * Make %[gG] smell like %[eE], but 7671f2a0cdfSDavid Schultz * trim trailing zeroes if no # flag. 7681f2a0cdfSDavid Schultz */ 7691f2a0cdfSDavid Schultz if (!(flags & ALT)) 7701f2a0cdfSDavid Schultz prec = ndig; 77158f0484fSRodney W. Grimes } 772ebbad5ecSDavid Schultz } 773ebbad5ecSDavid Schultz if (expchar) { 774ebbad5ecSDavid Schultz expsize = exponent(expstr, expt - 1, expchar); 775ebbad5ecSDavid Schultz size = expsize + prec; 7763b204b7dSDavid Schultz if (prec > 1 || flags & ALT) 7775004a238SDavid Schultz size += decpt_len; 778ebbad5ecSDavid Schultz } else { 77981ae2e9aSDavid Schultz /* space for digits before decimal point */ 78081ae2e9aSDavid Schultz if (expt > 0) 78158f0484fSRodney W. Grimes size = expt; 78281ae2e9aSDavid Schultz else /* "0" */ 78381ae2e9aSDavid Schultz size = 1; 78481ae2e9aSDavid Schultz /* space for decimal pt and following digits */ 78558f0484fSRodney W. Grimes if (prec || flags & ALT) 7865004a238SDavid Schultz size += prec + decpt_len; 78721ca178eSDavid Schultz if ((flags & GROUPING) && expt > 0) 7883c87aa1dSDavid Chisnall size += grouping_init(&gs, expt, locale); 789ebbad5ecSDavid Schultz } 79058f0484fSRodney W. Grimes break; 7918de9e897SDavid Schultz #endif /* !NO_FLOATING_POINT */ 792e95725feSKonstantin Belousov case 'm': 793e95725feSKonstantin Belousov cp = strerror(saved_errno); 794e95725feSKonstantin Belousov size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp); 795e95725feSKonstantin Belousov sign = '\0'; 796e95725feSKonstantin Belousov break; 79758f0484fSRodney W. Grimes case 'n': 7987735bb0fSBill Fenner /* 7997735bb0fSBill Fenner * Assignment-like behavior is specified if the 8007735bb0fSBill Fenner * value overflows or is otherwise unrepresentable. 8017735bb0fSBill Fenner * C99 says to use `signed char' for %hhn conversions. 8027735bb0fSBill Fenner */ 8037735bb0fSBill Fenner if (flags & LLONGINT) 8047735bb0fSBill Fenner *GETARG(long long *) = ret; 8057735bb0fSBill Fenner else if (flags & SIZET) 8067735bb0fSBill Fenner *GETARG(ssize_t *) = (ssize_t)ret; 8077735bb0fSBill Fenner else if (flags & PTRDIFFT) 8087735bb0fSBill Fenner *GETARG(ptrdiff_t *) = ret; 8097735bb0fSBill Fenner else if (flags & INTMAXT) 8107735bb0fSBill Fenner *GETARG(intmax_t *) = ret; 81158f0484fSRodney W. Grimes else if (flags & LONGINT) 8126e690ad4SAndrey A. Chernov *GETARG(long *) = ret; 81358f0484fSRodney W. Grimes else if (flags & SHORTINT) 8146e690ad4SAndrey A. Chernov *GETARG(short *) = ret; 8157735bb0fSBill Fenner else if (flags & CHARINT) 8167735bb0fSBill Fenner *GETARG(signed char *) = ret; 81758f0484fSRodney W. Grimes else 8186e690ad4SAndrey A. Chernov *GETARG(int *) = ret; 81958f0484fSRodney W. Grimes continue; /* no output */ 82058f0484fSRodney W. Grimes case 'O': 82158f0484fSRodney W. Grimes flags |= LONGINT; 82258f0484fSRodney W. Grimes /*FALLTHROUGH*/ 82358f0484fSRodney W. Grimes case 'o': 8247735bb0fSBill Fenner if (flags & INTMAX_SIZE) 8257735bb0fSBill Fenner ujval = UJARG(); 82658f0484fSRodney W. Grimes else 82758f0484fSRodney W. Grimes ulval = UARG(); 82858f0484fSRodney W. Grimes base = 8; 82958f0484fSRodney W. Grimes goto nosign; 83058f0484fSRodney W. Grimes case 'p': 8312e394b2fSAlexey Zelkin /*- 83258f0484fSRodney W. Grimes * ``The argument shall be a pointer to void. The 83358f0484fSRodney W. Grimes * value of the pointer is converted to a sequence 83458f0484fSRodney W. Grimes * of printable characters, in an implementation- 83558f0484fSRodney W. Grimes * defined manner.'' 83658f0484fSRodney W. Grimes * -- ANSI X3J11 83758f0484fSRodney W. Grimes */ 8387735bb0fSBill Fenner ujval = (uintmax_t)(uintptr_t)GETARG(void *); 83958f0484fSRodney W. Grimes base = 16; 840ebbad5ecSDavid Schultz xdigs = xdigs_lower; 841ebbad5ecSDavid Schultz flags = flags | INTMAXT; 842ebbad5ecSDavid Schultz ox[1] = 'x'; 84358f0484fSRodney W. Grimes goto nosign; 844927ecbf3STim J. Robbins case 'S': 845927ecbf3STim J. Robbins flags |= LONGINT; 846927ecbf3STim J. Robbins /*FALLTHROUGH*/ 84758f0484fSRodney W. Grimes case 's': 848b9aac308STim J. Robbins if (flags & LONGINT) { 849b9aac308STim J. Robbins wchar_t *wcp; 850b9aac308STim J. Robbins 851b9aac308STim J. Robbins if (convbuf != NULL) 852b9aac308STim J. Robbins free(convbuf); 853b9aac308STim J. Robbins if ((wcp = GETARG(wchar_t *)) == NULL) 854b9aac308STim J. Robbins cp = "(null)"; 855b9aac308STim J. Robbins else { 856b9aac308STim J. Robbins convbuf = __wcsconv(wcp, prec); 8576180233fSTim J. Robbins if (convbuf == NULL) { 8586180233fSTim J. Robbins fp->_flags |= __SERR; 859b9aac308STim J. Robbins goto error; 8606180233fSTim J. Robbins } 861b9aac308STim J. Robbins cp = convbuf; 862b9aac308STim J. Robbins } 863b9aac308STim J. Robbins } else if ((cp = GETARG(char *)) == NULL) 86458f0484fSRodney W. Grimes cp = "(null)"; 865353ce11cSDavid Schultz size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp); 86658f0484fSRodney W. Grimes sign = '\0'; 86758f0484fSRodney W. Grimes break; 86858f0484fSRodney W. Grimes case 'U': 86958f0484fSRodney W. Grimes flags |= LONGINT; 87058f0484fSRodney W. Grimes /*FALLTHROUGH*/ 87158f0484fSRodney W. Grimes case 'u': 8727735bb0fSBill Fenner if (flags & INTMAX_SIZE) 8737735bb0fSBill Fenner ujval = UJARG(); 87458f0484fSRodney W. Grimes else 87558f0484fSRodney W. Grimes ulval = UARG(); 87658f0484fSRodney W. Grimes base = 10; 87758f0484fSRodney W. Grimes goto nosign; 87858f0484fSRodney W. Grimes case 'X': 879ebbad5ecSDavid Schultz xdigs = xdigs_upper; 88058f0484fSRodney W. Grimes goto hex; 88158f0484fSRodney W. Grimes case 'x': 882ebbad5ecSDavid Schultz xdigs = xdigs_lower; 8837735bb0fSBill Fenner hex: 8847735bb0fSBill Fenner if (flags & INTMAX_SIZE) 8857735bb0fSBill Fenner ujval = UJARG(); 88658f0484fSRodney W. Grimes else 88758f0484fSRodney W. Grimes ulval = UARG(); 88858f0484fSRodney W. Grimes base = 16; 88958f0484fSRodney W. Grimes /* leading 0x/X only if non-zero */ 89058f0484fSRodney W. Grimes if (flags & ALT && 8917735bb0fSBill Fenner (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 892ebbad5ecSDavid Schultz ox[1] = ch; 89358f0484fSRodney W. Grimes 89498ee7635SAlexey Zelkin flags &= ~GROUPING; 89558f0484fSRodney W. Grimes /* unsigned conversions */ 89658f0484fSRodney W. Grimes nosign: sign = '\0'; 8972e394b2fSAlexey Zelkin /*- 89858f0484fSRodney W. Grimes * ``... diouXx conversions ... if a precision is 89958f0484fSRodney W. Grimes * specified, the 0 flag will be ignored.'' 90058f0484fSRodney W. Grimes * -- ANSI X3J11 90158f0484fSRodney W. Grimes */ 90258f0484fSRodney W. Grimes number: if ((dprec = prec) >= 0) 90358f0484fSRodney W. Grimes flags &= ~ZEROPAD; 90458f0484fSRodney W. Grimes 9052e394b2fSAlexey Zelkin /*- 90658f0484fSRodney W. Grimes * ``The result of converting a zero value with an 90758f0484fSRodney W. Grimes * explicit precision of zero is no characters.'' 90858f0484fSRodney W. Grimes * -- ANSI X3J11 9091be5319aSDavid Schultz * 9101be5319aSDavid Schultz * ``The C Standard is clear enough as is. The call 9111be5319aSDavid Schultz * printf("%#.0o", 0) should print 0.'' 9121be5319aSDavid Schultz * -- Defect Report #151 91358f0484fSRodney W. Grimes */ 91458f0484fSRodney W. Grimes cp = buf + BUF; 9157735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 9161be5319aSDavid Schultz if (ujval != 0 || prec != 0 || 9171be5319aSDavid Schultz (flags & ALT && base == 8)) 9187735bb0fSBill Fenner cp = __ujtoa(ujval, cp, base, 91921ca178eSDavid Schultz flags & ALT, xdigs); 92058f0484fSRodney W. Grimes } else { 9211be5319aSDavid Schultz if (ulval != 0 || prec != 0 || 9221be5319aSDavid Schultz (flags & ALT && base == 8)) 92358f0484fSRodney W. Grimes cp = __ultoa(ulval, cp, base, 92421ca178eSDavid Schultz flags & ALT, xdigs); 92558f0484fSRodney W. Grimes } 92658f0484fSRodney W. Grimes size = buf + BUF - cp; 92738cac8f8SDavid Schultz if (size > BUF) /* should never happen */ 92838cac8f8SDavid Schultz abort(); 92921ca178eSDavid Schultz if ((flags & GROUPING) && size != 0) 9303c87aa1dSDavid Chisnall size += grouping_init(&gs, size, locale); 93158f0484fSRodney W. Grimes break; 93258f0484fSRodney W. Grimes default: /* "%?" prints ?, unless ? is NUL */ 93358f0484fSRodney W. Grimes if (ch == '\0') 93458f0484fSRodney W. Grimes goto done; 93558f0484fSRodney W. Grimes /* pretend it was %c with argument ch */ 93658f0484fSRodney W. Grimes cp = buf; 93758f0484fSRodney W. Grimes *cp = ch; 93858f0484fSRodney W. Grimes size = 1; 93958f0484fSRodney W. Grimes sign = '\0'; 94058f0484fSRodney W. Grimes break; 94158f0484fSRodney W. Grimes } 94258f0484fSRodney W. Grimes 94358f0484fSRodney W. Grimes /* 94458f0484fSRodney W. Grimes * All reasonable formats wind up here. At this point, `cp' 94558f0484fSRodney W. Grimes * points to a string which (if not flags&LADJUST) should be 94658f0484fSRodney W. Grimes * padded out to `width' places. If flags&ZEROPAD, it should 94758f0484fSRodney W. Grimes * first be prefixed by any sign or other prefix; otherwise, 94858f0484fSRodney W. Grimes * it should be blank padded before the prefix is emitted. 94958f0484fSRodney W. Grimes * After any left-hand padding and prefixing, emit zeroes 95058f0484fSRodney W. Grimes * required by a decimal [diouxX] precision, then print the 95158f0484fSRodney W. Grimes * string proper, then emit zeroes required by any leftover 95258f0484fSRodney W. Grimes * floating precision; finally, if LADJUST, pad with blanks. 95358f0484fSRodney W. Grimes * 95458f0484fSRodney W. Grimes * Compute actual size, so we know how much to pad. 955261a532aSBill Fenner * size excludes decimal prec; realsz includes it. 95658f0484fSRodney W. Grimes */ 957261a532aSBill Fenner realsz = dprec > size ? dprec : size; 95858f0484fSRodney W. Grimes if (sign) 959261a532aSBill Fenner realsz++; 960904322a5SDavid Schultz if (ox[1]) 961261a532aSBill Fenner realsz += 2; 96258f0484fSRodney W. Grimes 96392e88f87SAndrey A. Chernov prsize = width > realsz ? width : realsz; 964b250f248SAndrey A. Chernov if ((unsigned)ret + prsize > INT_MAX) { 96592e88f87SAndrey A. Chernov ret = EOF; 966666d00d3SDavid Schultz errno = EOVERFLOW; 96792e88f87SAndrey A. Chernov goto error; 96892e88f87SAndrey A. Chernov } 96992e88f87SAndrey A. Chernov 97058f0484fSRodney W. Grimes /* right-adjusting blank padding */ 97158f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == 0) 97258f0484fSRodney W. Grimes PAD(width - realsz, blanks); 97358f0484fSRodney W. Grimes 97458f0484fSRodney W. Grimes /* prefix */ 975904322a5SDavid Schultz if (sign) 97658f0484fSRodney W. Grimes PRINT(&sign, 1); 977904322a5SDavid Schultz 978904322a5SDavid Schultz if (ox[1]) { /* ox[1] is either x, X, or \0 */ 97958f0484fSRodney W. Grimes ox[0] = '0'; 98058f0484fSRodney W. Grimes PRINT(ox, 2); 98158f0484fSRodney W. Grimes } 98258f0484fSRodney W. Grimes 98358f0484fSRodney W. Grimes /* right-adjusting zero padding */ 98458f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 98558f0484fSRodney W. Grimes PAD(width - realsz, zeroes); 98658f0484fSRodney W. Grimes 98758f0484fSRodney W. Grimes /* the string or number proper */ 9888de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 98958f0484fSRodney W. Grimes if ((flags & FPT) == 0) { 99021ca178eSDavid Schultz #endif 99121ca178eSDavid Schultz /* leading zeroes from decimal precision */ 99221ca178eSDavid Schultz PAD(dprec - size, zeroes); 99321ca178eSDavid Schultz if (gs.grouping) { 9943c87aa1dSDavid Chisnall if (grouping_print(&gs, &io, cp, buf+BUF, locale) < 0) 99521ca178eSDavid Schultz goto error; 99621ca178eSDavid Schultz } else { 99758f0484fSRodney W. Grimes PRINT(cp, size); 99821ca178eSDavid Schultz } 99921ca178eSDavid Schultz #ifndef NO_FLOATING_POINT 100058f0484fSRodney W. Grimes } else { /* glue together f_p fragments */ 1001ebbad5ecSDavid Schultz if (!expchar) { /* %[fF] or sufficiently short %[gG] */ 1002ebbad5ecSDavid Schultz if (expt <= 0) { 100381ae2e9aSDavid Schultz PRINT(zeroes, 1); 100481ae2e9aSDavid Schultz if (prec || flags & ALT) 10055004a238SDavid Schultz PRINT(decimal_point,decpt_len); 100658f0484fSRodney W. Grimes PAD(-expt, zeroes); 10073b204b7dSDavid Schultz /* already handled initial 0's */ 10083b204b7dSDavid Schultz prec += expt; 100958f0484fSRodney W. Grimes } else { 101021ca178eSDavid Schultz if (gs.grouping) { 101121ca178eSDavid Schultz n = grouping_print(&gs, &io, 10123c87aa1dSDavid Chisnall cp, dtoaend, locale); 101321ca178eSDavid Schultz if (n < 0) 101421ca178eSDavid Schultz goto error; 101521ca178eSDavid Schultz cp += n; 101621ca178eSDavid Schultz } else { 10173b204b7dSDavid Schultz PRINTANDPAD(cp, dtoaend, 101821ca178eSDavid Schultz expt, zeroes); 101921ca178eSDavid Schultz cp += expt; 1020ebbad5ecSDavid Schultz } 1021ebbad5ecSDavid Schultz if (prec || flags & ALT) 10225004a238SDavid Schultz PRINT(decimal_point,decpt_len); 1023ebbad5ecSDavid Schultz } 10243b204b7dSDavid Schultz PRINTANDPAD(cp, dtoaend, prec, zeroes); 1025ebbad5ecSDavid Schultz } else { /* %[eE] or sufficiently long %[gG] */ 10263b204b7dSDavid Schultz if (prec > 1 || flags & ALT) { 10275004a238SDavid Schultz PRINT(cp++, 1); 10285004a238SDavid Schultz PRINT(decimal_point, decpt_len); 102958f0484fSRodney W. Grimes PRINT(cp, ndig-1); 1030ebbad5ecSDavid Schultz PAD(prec - ndig, zeroes); 103158f0484fSRodney W. Grimes } else /* XeYYY */ 103258f0484fSRodney W. Grimes PRINT(cp, 1); 103358f0484fSRodney W. Grimes PRINT(expstr, expsize); 103458f0484fSRodney W. Grimes } 103558f0484fSRodney W. Grimes } 103658f0484fSRodney W. Grimes #endif 103758f0484fSRodney W. Grimes /* left-adjusting padding (always blank) */ 103858f0484fSRodney W. Grimes if (flags & LADJUST) 103958f0484fSRodney W. Grimes PAD(width - realsz, blanks); 104058f0484fSRodney W. Grimes 104158f0484fSRodney W. Grimes /* finally, adjust ret */ 104292e88f87SAndrey A. Chernov ret += prsize; 104358f0484fSRodney W. Grimes 104458f0484fSRodney W. Grimes FLUSH(); /* copy out the I/O vectors */ 104558f0484fSRodney W. Grimes } 104658f0484fSRodney W. Grimes done: 104758f0484fSRodney W. Grimes FLUSH(); 104858f0484fSRodney W. Grimes error: 1049096ad104SDag-Erling Smørgrav va_end(orgap); 10508de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 10512ffc61baSTor Egge if (dtoaresult != NULL) 1052ebbad5ecSDavid Schultz freedtoa(dtoaresult); 10532ffc61baSTor Egge #endif 1054b9aac308STim J. Robbins if (convbuf != NULL) 1055b9aac308STim J. Robbins free(convbuf); 1056f70177e7SJulian Elischer if (__sferror(fp)) 1057f70177e7SJulian Elischer ret = EOF; 10581bf6c5f1SAndrey A. Chernov else 10591bf6c5f1SAndrey A. Chernov fp->_flags |= savserr; 1060efb7e53dSJordan K. Hubbard if ((argtable != NULL) && (argtable != statargtable)) 1061efb7e53dSJordan K. Hubbard free (argtable); 1062f70177e7SJulian Elischer return (ret); 106358f0484fSRodney W. Grimes /* NOTREACHED */ 106458f0484fSRodney W. Grimes } 106558f0484fSRodney W. Grimes 1066