158f0484fSRodney W. Grimes /*- 2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 3*8a16b7a1SPedro 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 113c87aa1dSDavid Chisnall * All rights reserved. 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> 44333fc21eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 4558f0484fSRodney W. Grimes 4658f0484fSRodney W. Grimes /* 4758f0484fSRodney W. Grimes * Actual printf innards. 4858f0484fSRodney W. Grimes * 4958f0484fSRodney W. Grimes * This code is large and complicated... 5058f0484fSRodney W. Grimes */ 5158f0484fSRodney W. Grimes 52d201fe46SDaniel Eischen #include "namespace.h" 5358f0484fSRodney W. Grimes #include <sys/types.h> 5458f0484fSRodney W. Grimes 557735bb0fSBill Fenner #include <ctype.h> 56666d00d3SDavid Schultz #include <errno.h> 5758f0484fSRodney W. Grimes #include <limits.h> 587ae5c679SAlexey Zelkin #include <locale.h> 597735bb0fSBill Fenner #include <stddef.h> 607735bb0fSBill Fenner #include <stdint.h> 6158f0484fSRodney W. Grimes #include <stdio.h> 6258f0484fSRodney W. Grimes #include <stdlib.h> 6358f0484fSRodney W. Grimes #include <string.h> 64b9aac308STim J. Robbins #include <wchar.h> 6575067f4fSPoul-Henning Kamp #include <printf.h> 6658f0484fSRodney W. Grimes 6758f0484fSRodney W. Grimes #include <stdarg.h> 683c87aa1dSDavid Chisnall #include "xlocale_private.h" 69d201fe46SDaniel Eischen #include "un-namespace.h" 7058f0484fSRodney W. Grimes 71d201fe46SDaniel Eischen #include "libc_private.h" 7258f0484fSRodney W. Grimes #include "local.h" 7358f0484fSRodney W. Grimes #include "fvwrite.h" 742591efccSDavid Schultz #include "printflocal.h" 75e5abb5e6SDavid Schultz 763c87aa1dSDavid Chisnall static int __sprint(FILE *, struct __suio *, locale_t); 773c87aa1dSDavid Chisnall static int __sbprintf(FILE *, locale_t, const char *, va_list) __printflike(3, 0) 78a1805f7bSDavid Schultz __noinline; 79b9aac308STim J. Robbins static char *__wcsconv(wchar_t *, int); 80ce51cf03SJames Raynard 81814d1bc9SDavid Schultz #define CHAR char 82814d1bc9SDavid Schultz #include "printfcommon.h" 83814d1bc9SDavid Schultz 8421ca178eSDavid Schultz struct grouping_state { 8521ca178eSDavid Schultz char *thousands_sep; /* locale-specific thousands separator */ 8621ca178eSDavid Schultz int thousep_len; /* length of thousands_sep */ 8721ca178eSDavid Schultz const char *grouping; /* locale-specific numeric grouping rules */ 8821ca178eSDavid Schultz int lead; /* sig figs before decimal or group sep */ 8921ca178eSDavid Schultz int nseps; /* number of group separators with ' */ 9021ca178eSDavid Schultz int nrepeats; /* number of repeats of the last group */ 9121ca178eSDavid Schultz }; 9221ca178eSDavid Schultz 9321ca178eSDavid Schultz /* 9421ca178eSDavid Schultz * Initialize the thousands' grouping state in preparation to print a 9521ca178eSDavid Schultz * number with ndigits digits. This routine returns the total number 9621ca178eSDavid Schultz * of bytes that will be needed. 9721ca178eSDavid Schultz */ 9821ca178eSDavid Schultz static int 993c87aa1dSDavid Chisnall grouping_init(struct grouping_state *gs, int ndigits, locale_t loc) 10021ca178eSDavid Schultz { 10121ca178eSDavid Schultz struct lconv *locale; 10221ca178eSDavid Schultz 1033c87aa1dSDavid Chisnall locale = localeconv_l(loc); 10421ca178eSDavid Schultz gs->grouping = locale->grouping; 10521ca178eSDavid Schultz gs->thousands_sep = locale->thousands_sep; 10621ca178eSDavid Schultz gs->thousep_len = strlen(gs->thousands_sep); 10721ca178eSDavid Schultz 10821ca178eSDavid Schultz gs->nseps = gs->nrepeats = 0; 10921ca178eSDavid Schultz gs->lead = ndigits; 11021ca178eSDavid Schultz while (*gs->grouping != CHAR_MAX) { 11121ca178eSDavid Schultz if (gs->lead <= *gs->grouping) 11221ca178eSDavid Schultz break; 11321ca178eSDavid Schultz gs->lead -= *gs->grouping; 11421ca178eSDavid Schultz if (*(gs->grouping+1)) { 11521ca178eSDavid Schultz gs->nseps++; 11621ca178eSDavid Schultz gs->grouping++; 11721ca178eSDavid Schultz } else 11821ca178eSDavid Schultz gs->nrepeats++; 11921ca178eSDavid Schultz } 12021ca178eSDavid Schultz return ((gs->nseps + gs->nrepeats) * gs->thousep_len); 12121ca178eSDavid Schultz } 12221ca178eSDavid Schultz 12321ca178eSDavid Schultz /* 12421ca178eSDavid Schultz * Print a number with thousands' separators. 12521ca178eSDavid Schultz */ 12621ca178eSDavid Schultz static int 12721ca178eSDavid Schultz grouping_print(struct grouping_state *gs, struct io_state *iop, 1283c87aa1dSDavid Chisnall const CHAR *cp, const CHAR *ep, locale_t locale) 12921ca178eSDavid Schultz { 13021ca178eSDavid Schultz const CHAR *cp0 = cp; 13121ca178eSDavid Schultz 1323c87aa1dSDavid Chisnall if (io_printandpad(iop, cp, ep, gs->lead, zeroes, locale)) 13321ca178eSDavid Schultz return (-1); 13421ca178eSDavid Schultz cp += gs->lead; 13521ca178eSDavid Schultz while (gs->nseps > 0 || gs->nrepeats > 0) { 13621ca178eSDavid Schultz if (gs->nrepeats > 0) 13721ca178eSDavid Schultz gs->nrepeats--; 13821ca178eSDavid Schultz else { 13921ca178eSDavid Schultz gs->grouping--; 14021ca178eSDavid Schultz gs->nseps--; 14121ca178eSDavid Schultz } 1423c87aa1dSDavid Chisnall if (io_print(iop, gs->thousands_sep, gs->thousep_len, locale)) 14321ca178eSDavid Schultz return (-1); 1443c87aa1dSDavid Chisnall if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, locale)) 14521ca178eSDavid Schultz return (-1); 14621ca178eSDavid Schultz cp += *gs->grouping; 14721ca178eSDavid Schultz } 14821ca178eSDavid Schultz if (cp > ep) 14921ca178eSDavid Schultz cp = ep; 15021ca178eSDavid Schultz return (cp - cp0); 15121ca178eSDavid Schultz } 15221ca178eSDavid Schultz 15358f0484fSRodney W. Grimes /* 15458f0484fSRodney W. Grimes * Flush out all the vectors defined by the given uio, 15558f0484fSRodney W. Grimes * then reset it so that it can be reused. 15658f0484fSRodney W. Grimes */ 15758f0484fSRodney W. Grimes static int 1583c87aa1dSDavid Chisnall __sprint(FILE *fp, struct __suio *uio, locale_t locale) 15958f0484fSRodney W. Grimes { 160d201fe46SDaniel Eischen int err; 16158f0484fSRodney W. Grimes 16258f0484fSRodney W. Grimes if (uio->uio_resid == 0) { 16358f0484fSRodney W. Grimes uio->uio_iovcnt = 0; 16458f0484fSRodney W. Grimes return (0); 16558f0484fSRodney W. Grimes } 16658f0484fSRodney W. Grimes err = __sfvwrite(fp, uio); 16758f0484fSRodney W. Grimes uio->uio_resid = 0; 16858f0484fSRodney W. Grimes uio->uio_iovcnt = 0; 16958f0484fSRodney W. Grimes return (err); 17058f0484fSRodney W. Grimes } 17158f0484fSRodney W. Grimes 17258f0484fSRodney W. Grimes /* 17358f0484fSRodney W. Grimes * Helper function for `fprintf to unbuffered unix file': creates a 17458f0484fSRodney W. Grimes * temporary buffer. We only work on write-only files; this avoids 17558f0484fSRodney W. Grimes * worries about ungetc buffers and so forth. 17658f0484fSRodney W. Grimes */ 17758f0484fSRodney W. Grimes static int 1783c87aa1dSDavid Chisnall __sbprintf(FILE *fp, locale_t locale, const char *fmt, va_list ap) 17958f0484fSRodney W. Grimes { 18058f0484fSRodney W. Grimes int ret; 1811b0181dfSJohn Baldwin FILE fake = FAKE_FILE; 18258f0484fSRodney W. Grimes unsigned char buf[BUFSIZ]; 18358f0484fSRodney W. Grimes 184a1805f7bSDavid Schultz /* XXX This is probably not needed. */ 185a1805f7bSDavid Schultz if (prepwrite(fp) != 0) 186a1805f7bSDavid Schultz return (EOF); 187a1805f7bSDavid Schultz 18858f0484fSRodney W. Grimes /* copy the important variables */ 18958f0484fSRodney W. Grimes fake._flags = fp->_flags & ~__SNBF; 19058f0484fSRodney W. Grimes fake._file = fp->_file; 19158f0484fSRodney W. Grimes fake._cookie = fp->_cookie; 19258f0484fSRodney W. Grimes fake._write = fp->_write; 1931e98f887SJohn Baldwin fake._orientation = fp->_orientation; 1941e98f887SJohn Baldwin fake._mbstate = fp->_mbstate; 19558f0484fSRodney W. Grimes 19658f0484fSRodney W. Grimes /* set up the buffer */ 19758f0484fSRodney W. Grimes fake._bf._base = fake._p = buf; 19858f0484fSRodney W. Grimes fake._bf._size = fake._w = sizeof(buf); 19958f0484fSRodney W. Grimes fake._lbfsize = 0; /* not actually used, but Just In Case */ 20058f0484fSRodney W. Grimes 20158f0484fSRodney W. Grimes /* do the work, then copy any error status */ 2023c87aa1dSDavid Chisnall ret = __vfprintf(&fake, locale, fmt, ap); 203d201fe46SDaniel Eischen if (ret >= 0 && __fflush(&fake)) 20458f0484fSRodney W. Grimes ret = EOF; 20558f0484fSRodney W. Grimes if (fake._flags & __SERR) 20658f0484fSRodney W. Grimes fp->_flags |= __SERR; 20758f0484fSRodney W. Grimes return (ret); 20858f0484fSRodney W. Grimes } 20958f0484fSRodney W. Grimes 21058f0484fSRodney W. Grimes /* 211b9aac308STim J. Robbins * Convert a wide character string argument for the %ls format to a multibyte 212d48c77b5STim J. Robbins * string representation. If not -1, prec specifies the maximum number of 213d48c77b5STim J. Robbins * bytes to output, and also means that we can't assume that the wide char. 214d48c77b5STim J. Robbins * string ends is null-terminated. 215b9aac308STim J. Robbins */ 216b9aac308STim J. Robbins static char * 217b9aac308STim J. Robbins __wcsconv(wchar_t *wcsarg, int prec) 218b9aac308STim J. Robbins { 21993996f6dSTim J. Robbins static const mbstate_t initial; 22093996f6dSTim J. Robbins mbstate_t mbs; 221b9aac308STim J. Robbins char buf[MB_LEN_MAX]; 222b9aac308STim J. Robbins wchar_t *p; 223d48c77b5STim J. Robbins char *convbuf; 224b9aac308STim J. Robbins size_t clen, nbytes; 225b9aac308STim J. Robbins 226d48c77b5STim J. Robbins /* Allocate space for the maximum number of bytes we could output. */ 227d48c77b5STim J. Robbins if (prec < 0) { 228d48c77b5STim J. Robbins p = wcsarg; 229d48c77b5STim J. Robbins mbs = initial; 230d48c77b5STim J. Robbins nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs); 231d48c77b5STim J. Robbins if (nbytes == (size_t)-1) 232d48c77b5STim J. Robbins return (NULL); 233d48c77b5STim J. Robbins } else { 234b9aac308STim J. Robbins /* 235d48c77b5STim J. Robbins * Optimisation: if the output precision is small enough, 236d48c77b5STim J. Robbins * just allocate enough memory for the maximum instead of 237d48c77b5STim J. Robbins * scanning the string. 238b9aac308STim J. Robbins */ 239d48c77b5STim J. Robbins if (prec < 128) 240d48c77b5STim J. Robbins nbytes = prec; 241d48c77b5STim J. Robbins else { 242b9aac308STim J. Robbins nbytes = 0; 243b9aac308STim J. Robbins p = wcsarg; 24493996f6dSTim J. Robbins mbs = initial; 245b9aac308STim J. Robbins for (;;) { 24693996f6dSTim J. Robbins clen = wcrtomb(buf, *p++, &mbs); 247b9aac308STim J. Robbins if (clen == 0 || clen == (size_t)-1 || 248b9aac308STim J. Robbins nbytes + clen > prec) 249b9aac308STim J. Robbins break; 250b9aac308STim J. Robbins nbytes += clen; 251b9aac308STim J. Robbins } 252d48c77b5STim J. Robbins } 253b9aac308STim J. Robbins } 254b9aac308STim J. Robbins if ((convbuf = malloc(nbytes + 1)) == NULL) 255b9aac308STim J. Robbins return (NULL); 256b9aac308STim J. Robbins 257d48c77b5STim J. Robbins /* Fill the output buffer. */ 258b9aac308STim J. Robbins p = wcsarg; 25993996f6dSTim J. Robbins mbs = initial; 260d48c77b5STim J. Robbins if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p, 261d48c77b5STim J. Robbins nbytes, &mbs)) == (size_t)-1) { 2626f098a48SAndrey A. Chernov free(convbuf); 263b9aac308STim J. Robbins return (NULL); 2646f098a48SAndrey A. Chernov } 265d48c77b5STim J. Robbins convbuf[nbytes] = '\0'; 266b9aac308STim J. Robbins return (convbuf); 267b9aac308STim J. Robbins } 268b9aac308STim J. Robbins 269b9aac308STim J. Robbins /* 270d201fe46SDaniel Eischen * MT-safe version 271d201fe46SDaniel Eischen */ 272d201fe46SDaniel Eischen int 2733c87aa1dSDavid Chisnall vfprintf_l(FILE * __restrict fp, locale_t locale, const char * __restrict fmt0, 2743c87aa1dSDavid Chisnall va_list ap) 275d201fe46SDaniel Eischen { 276d201fe46SDaniel Eischen int ret; 2773c87aa1dSDavid Chisnall FIX_LOCALE(locale); 278d201fe46SDaniel Eischen 279fda0a14fSKonstantin Belousov FLOCKFILE_CANCELSAFE(fp); 280a1805f7bSDavid Schultz /* optimise fprintf(stderr) (and other unbuffered Unix files) */ 281a1805f7bSDavid Schultz if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) && 282a1805f7bSDavid Schultz fp->_file >= 0) 2833c87aa1dSDavid Chisnall ret = __sbprintf(fp, locale, fmt0, ap); 284a1805f7bSDavid Schultz else 2853c87aa1dSDavid Chisnall ret = __vfprintf(fp, locale, fmt0, ap); 286fda0a14fSKonstantin Belousov FUNLOCKFILE_CANCELSAFE(); 287d201fe46SDaniel Eischen return (ret); 288d201fe46SDaniel Eischen } 2893c87aa1dSDavid Chisnall int 2903c87aa1dSDavid Chisnall vfprintf(FILE * __restrict fp, const char * __restrict fmt0, va_list ap) 2913c87aa1dSDavid Chisnall { 2923c87aa1dSDavid Chisnall return vfprintf_l(fp, __get_locale(), fmt0, ap); 2933c87aa1dSDavid Chisnall } 294d201fe46SDaniel Eischen 29538cac8f8SDavid Schultz /* 29638cac8f8SDavid Schultz * The size of the buffer we use as scratch space for integer 29721ca178eSDavid Schultz * conversions, among other things. We need enough space to 29821ca178eSDavid Schultz * write a uintmax_t in octal (plus one byte). 29938cac8f8SDavid Schultz */ 30021ca178eSDavid Schultz #if UINTMAX_MAX <= UINT64_MAX 30121ca178eSDavid Schultz #define BUF 32 30221ca178eSDavid Schultz #else 30321ca178eSDavid Schultz #error "BUF must be large enough to format a uintmax_t" 30421ca178eSDavid Schultz #endif 30538cac8f8SDavid Schultz 30658f0484fSRodney W. Grimes /* 307d201fe46SDaniel Eischen * Non-MT-safe version 308d201fe46SDaniel Eischen */ 30958f0484fSRodney W. Grimes int 3103c87aa1dSDavid Chisnall __vfprintf(FILE *fp, locale_t locale, const char *fmt0, va_list ap) 31158f0484fSRodney W. Grimes { 312d201fe46SDaniel Eischen char *fmt; /* format string */ 313d201fe46SDaniel Eischen int ch; /* character from fmt */ 314d201fe46SDaniel Eischen int n, n2; /* handy integer (short term usage) */ 315d201fe46SDaniel Eischen char *cp; /* handy char pointer (short term usage) */ 316d201fe46SDaniel Eischen int flags; /* flags as above */ 31758f0484fSRodney W. Grimes int ret; /* return value accumulator */ 31858f0484fSRodney W. Grimes int width; /* width from format (%8d), or 0 */ 319ebbad5ecSDavid Schultz int prec; /* precision from format; <0 for N/A */ 32058f0484fSRodney W. Grimes char sign; /* sign prefix (' ', '+', '-', or \0) */ 32121ca178eSDavid Schultz struct grouping_state gs; /* thousands' grouping info */ 322814d1bc9SDavid Schultz 3238de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 324ebbad5ecSDavid Schultz /* 325ebbad5ecSDavid Schultz * We can decompose the printed representation of floating 326ebbad5ecSDavid Schultz * point numbers into several parts, some of which may be empty: 327ebbad5ecSDavid Schultz * 328ebbad5ecSDavid Schultz * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ 329ebbad5ecSDavid Schultz * A B ---C--- D E F 330ebbad5ecSDavid Schultz * 331ebbad5ecSDavid Schultz * A: 'sign' holds this value if present; '\0' otherwise 332ebbad5ecSDavid Schultz * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal 333ebbad5ecSDavid Schultz * C: cp points to the string MMMNNN. Leading and trailing 334ebbad5ecSDavid Schultz * zeros are not in the string and must be added. 335ebbad5ecSDavid Schultz * D: expchar holds this character; '\0' if no exponent, e.g. %f 336ebbad5ecSDavid Schultz * F: at least two digits for decimal, at least one digit for hex 337ebbad5ecSDavid Schultz */ 3387ae5c679SAlexey Zelkin char *decimal_point; /* locale specific decimal point */ 3395004a238SDavid Schultz int decpt_len; /* length of decimal_point */ 340ebbad5ecSDavid Schultz int signflag; /* true if float is negative */ 341ebbad5ecSDavid Schultz union { /* floating point arguments %[aAeEfFgG] */ 342ebbad5ecSDavid Schultz double dbl; 343ebbad5ecSDavid Schultz long double ldbl; 344ebbad5ecSDavid Schultz } fparg; 34558f0484fSRodney W. Grimes int expt; /* integer value of exponent */ 346ebbad5ecSDavid Schultz char expchar; /* exponent character: [eEpP\0] */ 347ebbad5ecSDavid Schultz char *dtoaend; /* pointer to end of converted digits */ 34858f0484fSRodney W. Grimes int expsize; /* character count for expstr */ 349ebbad5ecSDavid Schultz int ndig; /* actual number of digits returned by dtoa */ 350ebbad5ecSDavid Schultz char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */ 3512ffc61baSTor Egge char *dtoaresult; /* buffer allocated by dtoa */ 35258f0484fSRodney W. Grimes #endif 35358f0484fSRodney W. Grimes u_long ulval; /* integer arguments %[diouxX] */ 3547735bb0fSBill Fenner uintmax_t ujval; /* %j, %ll, %q, %t, %z integers */ 35558f0484fSRodney W. Grimes int base; /* base for [diouxX] conversion */ 35658f0484fSRodney W. Grimes int dprec; /* a copy of prec if [diouxX], 0 otherwise */ 357261a532aSBill Fenner int realsz; /* field size expanded by dprec, sign, etc */ 35858f0484fSRodney W. Grimes int size; /* size of converted field or string */ 35992e88f87SAndrey A. Chernov int prsize; /* max size of printed field */ 360ebbad5ecSDavid Schultz const char *xdigs; /* digits for %[xX] conversion */ 361814d1bc9SDavid Schultz struct io_state io; /* I/O buffering state */ 36238cac8f8SDavid Schultz char buf[BUF]; /* buffer with space for digits of uintmax_t */ 363ebbad5ecSDavid Schultz char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */ 364a387081cSDoug Rabson union arg *argtable; /* args, built due to positional arg */ 365a387081cSDoug Rabson union arg statargtable [STATIC_ARG_TBL_SIZE]; 366efb7e53dSJordan K. Hubbard int nextarg; /* 1-based argument index */ 367efb7e53dSJordan K. Hubbard va_list orgap; /* original argument pointer */ 368b9aac308STim J. Robbins char *convbuf; /* wide to multibyte conversion result */ 3691bf6c5f1SAndrey A. Chernov int savserr; 37058f0484fSRodney W. Grimes 371ac9913a7SDavid Schultz static const char xdigs_lower[16] = "0123456789abcdef"; 372ac9913a7SDavid Schultz static const char xdigs_upper[16] = "0123456789ABCDEF"; 373ebbad5ecSDavid Schultz 374814d1bc9SDavid Schultz /* BEWARE, these `goto error' on error. */ 37558f0484fSRodney W. Grimes #define PRINT(ptr, len) { \ 3763c87aa1dSDavid Chisnall if (io_print(&io, (ptr), (len), locale)) \ 37758f0484fSRodney W. Grimes goto error; \ 37858f0484fSRodney W. Grimes } 37958f0484fSRodney W. Grimes #define PAD(howmany, with) { \ 3803c87aa1dSDavid Chisnall if (io_pad(&io, (howmany), (with), locale)) \ 38158f0484fSRodney W. Grimes goto error; \ 382814d1bc9SDavid Schultz } 383814d1bc9SDavid Schultz #define PRINTANDPAD(p, ep, len, with) { \ 3843c87aa1dSDavid Chisnall if (io_printandpad(&io, (p), (ep), (len), (with), locale)) \ 385814d1bc9SDavid Schultz goto error; \ 386814d1bc9SDavid Schultz } 387814d1bc9SDavid Schultz #define FLUSH() { \ 3883c87aa1dSDavid Chisnall if (io_flush(&io, locale)) \ 389814d1bc9SDavid Schultz goto error; \ 39058f0484fSRodney W. Grimes } 39158f0484fSRodney W. Grimes 39258f0484fSRodney W. Grimes /* 393efb7e53dSJordan K. Hubbard * Get the argument indexed by nextarg. If the argument table is 394efb7e53dSJordan K. Hubbard * built, use it to get the argument. If its not, get the next 395efb7e53dSJordan K. Hubbard * argument (and arguments must be gotten sequentially). 396efb7e53dSJordan K. Hubbard */ 397efb7e53dSJordan K. Hubbard #define GETARG(type) \ 398a387081cSDoug Rabson ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \ 399efb7e53dSJordan K. Hubbard (nextarg++, va_arg(ap, type))) 400efb7e53dSJordan K. Hubbard 401efb7e53dSJordan K. Hubbard /* 40258f0484fSRodney W. Grimes * To extend shorts properly, we need both signed and unsigned 40358f0484fSRodney W. Grimes * argument extraction methods. 40458f0484fSRodney W. Grimes */ 40558f0484fSRodney W. Grimes #define SARG() \ 406efb7e53dSJordan K. Hubbard (flags&LONGINT ? GETARG(long) : \ 407efb7e53dSJordan K. Hubbard flags&SHORTINT ? (long)(short)GETARG(int) : \ 4087735bb0fSBill Fenner flags&CHARINT ? (long)(signed char)GETARG(int) : \ 409efb7e53dSJordan K. Hubbard (long)GETARG(int)) 41058f0484fSRodney W. Grimes #define UARG() \ 411efb7e53dSJordan K. Hubbard (flags&LONGINT ? GETARG(u_long) : \ 412efb7e53dSJordan K. Hubbard flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \ 4137735bb0fSBill Fenner flags&CHARINT ? (u_long)(u_char)GETARG(int) : \ 414efb7e53dSJordan K. Hubbard (u_long)GETARG(u_int)) 4157735bb0fSBill Fenner #define INTMAX_SIZE (INTMAXT|SIZET|PTRDIFFT|LLONGINT) 4167735bb0fSBill Fenner #define SJARG() \ 4177735bb0fSBill Fenner (flags&INTMAXT ? GETARG(intmax_t) : \ 4180881683bSDavid Schultz flags&SIZET ? (intmax_t)GETARG(ssize_t) : \ 4197735bb0fSBill Fenner flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \ 4207735bb0fSBill Fenner (intmax_t)GETARG(long long)) 4217735bb0fSBill Fenner #define UJARG() \ 4227735bb0fSBill Fenner (flags&INTMAXT ? GETARG(uintmax_t) : \ 4237735bb0fSBill Fenner flags&SIZET ? (uintmax_t)GETARG(size_t) : \ 4247735bb0fSBill Fenner flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \ 4257735bb0fSBill Fenner (uintmax_t)GETARG(unsigned long long)) 426efb7e53dSJordan K. Hubbard 427efb7e53dSJordan K. Hubbard /* 428efb7e53dSJordan K. Hubbard * Get * arguments, including the form *nn$. Preserve the nextarg 429efb7e53dSJordan K. Hubbard * that the argument can be gotten once the type is determined. 430efb7e53dSJordan K. Hubbard */ 431efb7e53dSJordan K. Hubbard #define GETASTER(val) \ 432efb7e53dSJordan K. Hubbard n2 = 0; \ 433efb7e53dSJordan K. Hubbard cp = fmt; \ 434efb7e53dSJordan K. Hubbard while (is_digit(*cp)) { \ 435efb7e53dSJordan K. Hubbard n2 = 10 * n2 + to_digit(*cp); \ 436efb7e53dSJordan K. Hubbard cp++; \ 437efb7e53dSJordan K. Hubbard } \ 438efb7e53dSJordan K. Hubbard if (*cp == '$') { \ 439efb7e53dSJordan K. Hubbard int hold = nextarg; \ 440efb7e53dSJordan K. Hubbard if (argtable == NULL) { \ 441efb7e53dSJordan K. Hubbard argtable = statargtable; \ 442e62e5ff9SDavid Schultz if (__find_arguments (fmt0, orgap, &argtable)) { \ 443e62e5ff9SDavid Schultz ret = EOF; \ 444e62e5ff9SDavid Schultz goto error; \ 445e62e5ff9SDavid Schultz } \ 446efb7e53dSJordan K. Hubbard } \ 447efb7e53dSJordan K. Hubbard nextarg = n2; \ 448efb7e53dSJordan K. Hubbard val = GETARG (int); \ 449efb7e53dSJordan K. Hubbard nextarg = hold; \ 450efb7e53dSJordan K. Hubbard fmt = ++cp; \ 451efb7e53dSJordan K. Hubbard } else { \ 452efb7e53dSJordan K. Hubbard val = GETARG (int); \ 453efb7e53dSJordan K. Hubbard } 454efb7e53dSJordan K. Hubbard 45533bff5d3SDavid Schultz if (__use_xprintf == 0 && getenv("USE_XPRINTF")) 45633bff5d3SDavid Schultz __use_xprintf = 1; 45733bff5d3SDavid Schultz if (__use_xprintf > 0) 45833bff5d3SDavid Schultz return (__xvprintf(fp, fmt0, ap)); 45958f0484fSRodney W. Grimes 46058f0484fSRodney W. Grimes /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */ 461450ead86SPedro F. Giffuni if (prepwrite(fp) != 0) { 462450ead86SPedro F. Giffuni errno = EBADF; 46358f0484fSRodney W. Grimes return (EOF); 464450ead86SPedro F. Giffuni } 46558f0484fSRodney W. Grimes 4661bf6c5f1SAndrey A. Chernov savserr = fp->_flags & __SERR; 4671bf6c5f1SAndrey A. Chernov fp->_flags &= ~__SERR; 4681bf6c5f1SAndrey A. Chernov 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; 616927ecbf3STim J. Robbins case 'C': 617927ecbf3STim J. Robbins flags |= LONGINT; 618927ecbf3STim J. Robbins /*FALLTHROUGH*/ 61958f0484fSRodney W. Grimes case 'c': 620b9aac308STim J. Robbins if (flags & LONGINT) { 62193996f6dSTim J. Robbins static const mbstate_t initial; 62293996f6dSTim J. Robbins mbstate_t mbs; 623b9aac308STim J. Robbins size_t mbseqlen; 624b9aac308STim J. Robbins 62593996f6dSTim J. Robbins mbs = initial; 626b9aac308STim J. Robbins mbseqlen = wcrtomb(cp = buf, 62793996f6dSTim J. Robbins (wchar_t)GETARG(wint_t), &mbs); 6286180233fSTim J. Robbins if (mbseqlen == (size_t)-1) { 6296180233fSTim J. Robbins fp->_flags |= __SERR; 630b9aac308STim J. Robbins goto error; 6316180233fSTim J. Robbins } 632b9aac308STim J. Robbins size = (int)mbseqlen; 633b9aac308STim J. Robbins } else { 634efb7e53dSJordan K. Hubbard *(cp = buf) = GETARG(int); 63558f0484fSRodney W. Grimes size = 1; 636b9aac308STim J. Robbins } 63758f0484fSRodney W. Grimes sign = '\0'; 63858f0484fSRodney W. Grimes break; 63958f0484fSRodney W. Grimes case 'D': 64058f0484fSRodney W. Grimes flags |= LONGINT; 64158f0484fSRodney W. Grimes /*FALLTHROUGH*/ 64258f0484fSRodney W. Grimes case 'd': 64358f0484fSRodney W. Grimes case 'i': 6447735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 6457735bb0fSBill Fenner ujval = SJARG(); 6467735bb0fSBill Fenner if ((intmax_t)ujval < 0) { 6477735bb0fSBill Fenner ujval = -ujval; 64858f0484fSRodney W. Grimes sign = '-'; 64958f0484fSRodney W. Grimes } 65058f0484fSRodney W. Grimes } else { 65158f0484fSRodney W. Grimes ulval = SARG(); 65258f0484fSRodney W. Grimes if ((long)ulval < 0) { 65358f0484fSRodney W. Grimes ulval = -ulval; 65458f0484fSRodney W. Grimes sign = '-'; 65558f0484fSRodney W. Grimes } 65658f0484fSRodney W. Grimes } 65758f0484fSRodney W. Grimes base = 10; 65858f0484fSRodney W. Grimes goto number; 6598de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 6607735bb0fSBill Fenner case 'a': 6617735bb0fSBill Fenner case 'A': 662ebbad5ecSDavid Schultz if (ch == 'a') { 663ebbad5ecSDavid Schultz ox[1] = 'x'; 664ebbad5ecSDavid Schultz xdigs = xdigs_lower; 665ebbad5ecSDavid Schultz expchar = 'p'; 666ebbad5ecSDavid Schultz } else { 667ebbad5ecSDavid Schultz ox[1] = 'X'; 668ebbad5ecSDavid Schultz xdigs = xdigs_upper; 669ebbad5ecSDavid Schultz expchar = 'P'; 670ebbad5ecSDavid Schultz } 671904322a5SDavid Schultz if (prec >= 0) 672904322a5SDavid Schultz prec++; 673904322a5SDavid Schultz if (dtoaresult != NULL) 674904322a5SDavid Schultz freedtoa(dtoaresult); 675ebbad5ecSDavid Schultz if (flags & LONGDBL) { 676904322a5SDavid Schultz fparg.ldbl = GETARG(long double); 677ebbad5ecSDavid Schultz dtoaresult = cp = 678ebbad5ecSDavid Schultz __hldtoa(fparg.ldbl, xdigs, prec, 679ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 680ebbad5ecSDavid Schultz } else { 681ebbad5ecSDavid Schultz fparg.dbl = GETARG(double); 682ebbad5ecSDavid Schultz dtoaresult = cp = 683ebbad5ecSDavid Schultz __hdtoa(fparg.dbl, xdigs, prec, 684ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 685ebbad5ecSDavid Schultz } 686904322a5SDavid Schultz if (prec < 0) 687904322a5SDavid Schultz prec = dtoaend - cp; 688904322a5SDavid Schultz if (expt == INT_MAX) 689904322a5SDavid Schultz ox[1] = '\0'; 690904322a5SDavid Schultz goto fp_common; 691d26be6f0SBruce Evans case 'e': 69258f0484fSRodney W. Grimes case 'E': 693ebbad5ecSDavid Schultz expchar = ch; 694ebbad5ecSDavid Schultz if (prec < 0) /* account for digit before decpt */ 695ebbad5ecSDavid Schultz prec = DEFPREC + 1; 696ebbad5ecSDavid Schultz else 697ebbad5ecSDavid Schultz prec++; 698ebbad5ecSDavid Schultz goto fp_begin; 699d26be6f0SBruce Evans case 'f': 7007735bb0fSBill Fenner case 'F': 701ebbad5ecSDavid Schultz expchar = '\0'; 702d26be6f0SBruce Evans goto fp_begin; 70358f0484fSRodney W. Grimes case 'g': 70458f0484fSRodney W. Grimes case 'G': 705ebbad5ecSDavid Schultz expchar = ch - ('g' - 'e'); 706d26be6f0SBruce Evans if (prec == 0) 707d26be6f0SBruce Evans prec = 1; 708ebbad5ecSDavid Schultz fp_begin: 709ebbad5ecSDavid Schultz if (prec < 0) 71058f0484fSRodney W. Grimes prec = DEFPREC; 711ebbad5ecSDavid Schultz if (dtoaresult != NULL) 712ebbad5ecSDavid Schultz freedtoa(dtoaresult); 713ebbad5ecSDavid Schultz if (flags & LONGDBL) { 714ebbad5ecSDavid Schultz fparg.ldbl = GETARG(long double); 715ebbad5ecSDavid Schultz dtoaresult = cp = 716ebbad5ecSDavid Schultz __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec, 717ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 718ebbad5ecSDavid Schultz } else { 719ebbad5ecSDavid Schultz fparg.dbl = GETARG(double); 720ebbad5ecSDavid Schultz dtoaresult = cp = 721ebbad5ecSDavid Schultz dtoa(fparg.dbl, expchar ? 2 : 3, prec, 722ebbad5ecSDavid Schultz &expt, &signflag, &dtoaend); 723ebbad5ecSDavid Schultz if (expt == 9999) 724ebbad5ecSDavid Schultz expt = INT_MAX; 72558f0484fSRodney W. Grimes } 726904322a5SDavid Schultz fp_common: 727ebbad5ecSDavid Schultz if (signflag) 728ebbad5ecSDavid Schultz sign = '-'; 729ebbad5ecSDavid Schultz if (expt == INT_MAX) { /* inf or nan */ 730ebbad5ecSDavid Schultz if (*cp == 'N') { 731ebbad5ecSDavid Schultz cp = (ch >= 'a') ? "nan" : "NAN"; 732ebbad5ecSDavid Schultz sign = '\0'; 733ebbad5ecSDavid Schultz } else 734ebbad5ecSDavid Schultz cp = (ch >= 'a') ? "inf" : "INF"; 73558f0484fSRodney W. Grimes size = 3; 736970a466cSDavid Schultz flags &= ~ZEROPAD; 73758f0484fSRodney W. Grimes break; 73858f0484fSRodney W. Grimes } 73958f0484fSRodney W. Grimes flags |= FPT; 740ebbad5ecSDavid Schultz ndig = dtoaend - cp; 74158f0484fSRodney W. Grimes if (ch == 'g' || ch == 'G') { 742ebbad5ecSDavid Schultz if (expt > -4 && expt <= prec) { 743ebbad5ecSDavid Schultz /* Make %[gG] smell like %[fF] */ 744ebbad5ecSDavid Schultz expchar = '\0'; 745ebbad5ecSDavid Schultz if (flags & ALT) 746ebbad5ecSDavid Schultz prec -= expt; 74758f0484fSRodney W. Grimes else 748ebbad5ecSDavid Schultz prec = ndig - expt; 749ebbad5ecSDavid Schultz if (prec < 0) 750ebbad5ecSDavid Schultz prec = 0; 7511f2a0cdfSDavid Schultz } else { 7521f2a0cdfSDavid Schultz /* 7531f2a0cdfSDavid Schultz * Make %[gG] smell like %[eE], but 7541f2a0cdfSDavid Schultz * trim trailing zeroes if no # flag. 7551f2a0cdfSDavid Schultz */ 7561f2a0cdfSDavid Schultz if (!(flags & ALT)) 7571f2a0cdfSDavid Schultz prec = ndig; 75858f0484fSRodney W. Grimes } 759ebbad5ecSDavid Schultz } 760ebbad5ecSDavid Schultz if (expchar) { 761ebbad5ecSDavid Schultz expsize = exponent(expstr, expt - 1, expchar); 762ebbad5ecSDavid Schultz size = expsize + prec; 7633b204b7dSDavid Schultz if (prec > 1 || flags & ALT) 7645004a238SDavid Schultz size += decpt_len; 765ebbad5ecSDavid Schultz } else { 76681ae2e9aSDavid Schultz /* space for digits before decimal point */ 76781ae2e9aSDavid Schultz if (expt > 0) 76858f0484fSRodney W. Grimes size = expt; 76981ae2e9aSDavid Schultz else /* "0" */ 77081ae2e9aSDavid Schultz size = 1; 77181ae2e9aSDavid Schultz /* space for decimal pt and following digits */ 77258f0484fSRodney W. Grimes if (prec || flags & ALT) 7735004a238SDavid Schultz size += prec + decpt_len; 77421ca178eSDavid Schultz if ((flags & GROUPING) && expt > 0) 7753c87aa1dSDavid Chisnall size += grouping_init(&gs, expt, locale); 776ebbad5ecSDavid Schultz } 77758f0484fSRodney W. Grimes break; 7788de9e897SDavid Schultz #endif /* !NO_FLOATING_POINT */ 77958f0484fSRodney W. Grimes case 'n': 7807735bb0fSBill Fenner /* 7817735bb0fSBill Fenner * Assignment-like behavior is specified if the 7827735bb0fSBill Fenner * value overflows or is otherwise unrepresentable. 7837735bb0fSBill Fenner * C99 says to use `signed char' for %hhn conversions. 7847735bb0fSBill Fenner */ 7857735bb0fSBill Fenner if (flags & LLONGINT) 7867735bb0fSBill Fenner *GETARG(long long *) = ret; 7877735bb0fSBill Fenner else if (flags & SIZET) 7887735bb0fSBill Fenner *GETARG(ssize_t *) = (ssize_t)ret; 7897735bb0fSBill Fenner else if (flags & PTRDIFFT) 7907735bb0fSBill Fenner *GETARG(ptrdiff_t *) = ret; 7917735bb0fSBill Fenner else if (flags & INTMAXT) 7927735bb0fSBill Fenner *GETARG(intmax_t *) = ret; 79358f0484fSRodney W. Grimes else if (flags & LONGINT) 7946e690ad4SAndrey A. Chernov *GETARG(long *) = ret; 79558f0484fSRodney W. Grimes else if (flags & SHORTINT) 7966e690ad4SAndrey A. Chernov *GETARG(short *) = ret; 7977735bb0fSBill Fenner else if (flags & CHARINT) 7987735bb0fSBill Fenner *GETARG(signed char *) = ret; 79958f0484fSRodney W. Grimes else 8006e690ad4SAndrey A. Chernov *GETARG(int *) = ret; 80158f0484fSRodney W. Grimes continue; /* no output */ 80258f0484fSRodney W. Grimes case 'O': 80358f0484fSRodney W. Grimes flags |= LONGINT; 80458f0484fSRodney W. Grimes /*FALLTHROUGH*/ 80558f0484fSRodney W. Grimes case 'o': 8067735bb0fSBill Fenner if (flags & INTMAX_SIZE) 8077735bb0fSBill Fenner ujval = UJARG(); 80858f0484fSRodney W. Grimes else 80958f0484fSRodney W. Grimes ulval = UARG(); 81058f0484fSRodney W. Grimes base = 8; 81158f0484fSRodney W. Grimes goto nosign; 81258f0484fSRodney W. Grimes case 'p': 8132e394b2fSAlexey Zelkin /*- 81458f0484fSRodney W. Grimes * ``The argument shall be a pointer to void. The 81558f0484fSRodney W. Grimes * value of the pointer is converted to a sequence 81658f0484fSRodney W. Grimes * of printable characters, in an implementation- 81758f0484fSRodney W. Grimes * defined manner.'' 81858f0484fSRodney W. Grimes * -- ANSI X3J11 81958f0484fSRodney W. Grimes */ 8207735bb0fSBill Fenner ujval = (uintmax_t)(uintptr_t)GETARG(void *); 82158f0484fSRodney W. Grimes base = 16; 822ebbad5ecSDavid Schultz xdigs = xdigs_lower; 823ebbad5ecSDavid Schultz flags = flags | INTMAXT; 824ebbad5ecSDavid Schultz ox[1] = 'x'; 82558f0484fSRodney W. Grimes goto nosign; 826927ecbf3STim J. Robbins case 'S': 827927ecbf3STim J. Robbins flags |= LONGINT; 828927ecbf3STim J. Robbins /*FALLTHROUGH*/ 82958f0484fSRodney W. Grimes case 's': 830b9aac308STim J. Robbins if (flags & LONGINT) { 831b9aac308STim J. Robbins wchar_t *wcp; 832b9aac308STim J. Robbins 833b9aac308STim J. Robbins if (convbuf != NULL) 834b9aac308STim J. Robbins free(convbuf); 835b9aac308STim J. Robbins if ((wcp = GETARG(wchar_t *)) == NULL) 836b9aac308STim J. Robbins cp = "(null)"; 837b9aac308STim J. Robbins else { 838b9aac308STim J. Robbins convbuf = __wcsconv(wcp, prec); 8396180233fSTim J. Robbins if (convbuf == NULL) { 8406180233fSTim J. Robbins fp->_flags |= __SERR; 841b9aac308STim J. Robbins goto error; 8426180233fSTim J. Robbins } 843b9aac308STim J. Robbins cp = convbuf; 844b9aac308STim J. Robbins } 845b9aac308STim J. Robbins } else if ((cp = GETARG(char *)) == NULL) 84658f0484fSRodney W. Grimes cp = "(null)"; 847353ce11cSDavid Schultz size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp); 84858f0484fSRodney W. Grimes sign = '\0'; 84958f0484fSRodney W. Grimes break; 85058f0484fSRodney W. Grimes case 'U': 85158f0484fSRodney W. Grimes flags |= LONGINT; 85258f0484fSRodney W. Grimes /*FALLTHROUGH*/ 85358f0484fSRodney W. Grimes case 'u': 8547735bb0fSBill Fenner if (flags & INTMAX_SIZE) 8557735bb0fSBill Fenner ujval = UJARG(); 85658f0484fSRodney W. Grimes else 85758f0484fSRodney W. Grimes ulval = UARG(); 85858f0484fSRodney W. Grimes base = 10; 85958f0484fSRodney W. Grimes goto nosign; 86058f0484fSRodney W. Grimes case 'X': 861ebbad5ecSDavid Schultz xdigs = xdigs_upper; 86258f0484fSRodney W. Grimes goto hex; 86358f0484fSRodney W. Grimes case 'x': 864ebbad5ecSDavid Schultz xdigs = xdigs_lower; 8657735bb0fSBill Fenner hex: 8667735bb0fSBill Fenner if (flags & INTMAX_SIZE) 8677735bb0fSBill Fenner ujval = UJARG(); 86858f0484fSRodney W. Grimes else 86958f0484fSRodney W. Grimes ulval = UARG(); 87058f0484fSRodney W. Grimes base = 16; 87158f0484fSRodney W. Grimes /* leading 0x/X only if non-zero */ 87258f0484fSRodney W. Grimes if (flags & ALT && 8737735bb0fSBill Fenner (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0)) 874ebbad5ecSDavid Schultz ox[1] = ch; 87558f0484fSRodney W. Grimes 87698ee7635SAlexey Zelkin flags &= ~GROUPING; 87758f0484fSRodney W. Grimes /* unsigned conversions */ 87858f0484fSRodney W. Grimes nosign: sign = '\0'; 8792e394b2fSAlexey Zelkin /*- 88058f0484fSRodney W. Grimes * ``... diouXx conversions ... if a precision is 88158f0484fSRodney W. Grimes * specified, the 0 flag will be ignored.'' 88258f0484fSRodney W. Grimes * -- ANSI X3J11 88358f0484fSRodney W. Grimes */ 88458f0484fSRodney W. Grimes number: if ((dprec = prec) >= 0) 88558f0484fSRodney W. Grimes flags &= ~ZEROPAD; 88658f0484fSRodney W. Grimes 8872e394b2fSAlexey Zelkin /*- 88858f0484fSRodney W. Grimes * ``The result of converting a zero value with an 88958f0484fSRodney W. Grimes * explicit precision of zero is no characters.'' 89058f0484fSRodney W. Grimes * -- ANSI X3J11 8911be5319aSDavid Schultz * 8921be5319aSDavid Schultz * ``The C Standard is clear enough as is. The call 8931be5319aSDavid Schultz * printf("%#.0o", 0) should print 0.'' 8941be5319aSDavid Schultz * -- Defect Report #151 89558f0484fSRodney W. Grimes */ 89658f0484fSRodney W. Grimes cp = buf + BUF; 8977735bb0fSBill Fenner if (flags & INTMAX_SIZE) { 8981be5319aSDavid Schultz if (ujval != 0 || prec != 0 || 8991be5319aSDavid Schultz (flags & ALT && base == 8)) 9007735bb0fSBill Fenner cp = __ujtoa(ujval, cp, base, 90121ca178eSDavid Schultz flags & ALT, xdigs); 90258f0484fSRodney W. Grimes } else { 9031be5319aSDavid Schultz if (ulval != 0 || prec != 0 || 9041be5319aSDavid Schultz (flags & ALT && base == 8)) 90558f0484fSRodney W. Grimes cp = __ultoa(ulval, cp, base, 90621ca178eSDavid Schultz flags & ALT, xdigs); 90758f0484fSRodney W. Grimes } 90858f0484fSRodney W. Grimes size = buf + BUF - cp; 90938cac8f8SDavid Schultz if (size > BUF) /* should never happen */ 91038cac8f8SDavid Schultz abort(); 91121ca178eSDavid Schultz if ((flags & GROUPING) && size != 0) 9123c87aa1dSDavid Chisnall size += grouping_init(&gs, size, locale); 91358f0484fSRodney W. Grimes break; 91458f0484fSRodney W. Grimes default: /* "%?" prints ?, unless ? is NUL */ 91558f0484fSRodney W. Grimes if (ch == '\0') 91658f0484fSRodney W. Grimes goto done; 91758f0484fSRodney W. Grimes /* pretend it was %c with argument ch */ 91858f0484fSRodney W. Grimes cp = buf; 91958f0484fSRodney W. Grimes *cp = ch; 92058f0484fSRodney W. Grimes size = 1; 92158f0484fSRodney W. Grimes sign = '\0'; 92258f0484fSRodney W. Grimes break; 92358f0484fSRodney W. Grimes } 92458f0484fSRodney W. Grimes 92558f0484fSRodney W. Grimes /* 92658f0484fSRodney W. Grimes * All reasonable formats wind up here. At this point, `cp' 92758f0484fSRodney W. Grimes * points to a string which (if not flags&LADJUST) should be 92858f0484fSRodney W. Grimes * padded out to `width' places. If flags&ZEROPAD, it should 92958f0484fSRodney W. Grimes * first be prefixed by any sign or other prefix; otherwise, 93058f0484fSRodney W. Grimes * it should be blank padded before the prefix is emitted. 93158f0484fSRodney W. Grimes * After any left-hand padding and prefixing, emit zeroes 93258f0484fSRodney W. Grimes * required by a decimal [diouxX] precision, then print the 93358f0484fSRodney W. Grimes * string proper, then emit zeroes required by any leftover 93458f0484fSRodney W. Grimes * floating precision; finally, if LADJUST, pad with blanks. 93558f0484fSRodney W. Grimes * 93658f0484fSRodney W. Grimes * Compute actual size, so we know how much to pad. 937261a532aSBill Fenner * size excludes decimal prec; realsz includes it. 93858f0484fSRodney W. Grimes */ 939261a532aSBill Fenner realsz = dprec > size ? dprec : size; 94058f0484fSRodney W. Grimes if (sign) 941261a532aSBill Fenner realsz++; 942904322a5SDavid Schultz if (ox[1]) 943261a532aSBill Fenner realsz += 2; 94458f0484fSRodney W. Grimes 94592e88f87SAndrey A. Chernov prsize = width > realsz ? width : realsz; 946b250f248SAndrey A. Chernov if ((unsigned)ret + prsize > INT_MAX) { 94792e88f87SAndrey A. Chernov ret = EOF; 948666d00d3SDavid Schultz errno = EOVERFLOW; 94992e88f87SAndrey A. Chernov goto error; 95092e88f87SAndrey A. Chernov } 95192e88f87SAndrey A. Chernov 95258f0484fSRodney W. Grimes /* right-adjusting blank padding */ 95358f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == 0) 95458f0484fSRodney W. Grimes PAD(width - realsz, blanks); 95558f0484fSRodney W. Grimes 95658f0484fSRodney W. Grimes /* prefix */ 957904322a5SDavid Schultz if (sign) 95858f0484fSRodney W. Grimes PRINT(&sign, 1); 959904322a5SDavid Schultz 960904322a5SDavid Schultz if (ox[1]) { /* ox[1] is either x, X, or \0 */ 96158f0484fSRodney W. Grimes ox[0] = '0'; 96258f0484fSRodney W. Grimes PRINT(ox, 2); 96358f0484fSRodney W. Grimes } 96458f0484fSRodney W. Grimes 96558f0484fSRodney W. Grimes /* right-adjusting zero padding */ 96658f0484fSRodney W. Grimes if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD) 96758f0484fSRodney W. Grimes PAD(width - realsz, zeroes); 96858f0484fSRodney W. Grimes 96958f0484fSRodney W. Grimes /* the string or number proper */ 9708de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 97158f0484fSRodney W. Grimes if ((flags & FPT) == 0) { 97221ca178eSDavid Schultz #endif 97321ca178eSDavid Schultz /* leading zeroes from decimal precision */ 97421ca178eSDavid Schultz PAD(dprec - size, zeroes); 97521ca178eSDavid Schultz if (gs.grouping) { 9763c87aa1dSDavid Chisnall if (grouping_print(&gs, &io, cp, buf+BUF, locale) < 0) 97721ca178eSDavid Schultz goto error; 97821ca178eSDavid Schultz } else { 97958f0484fSRodney W. Grimes PRINT(cp, size); 98021ca178eSDavid Schultz } 98121ca178eSDavid Schultz #ifndef NO_FLOATING_POINT 98258f0484fSRodney W. Grimes } else { /* glue together f_p fragments */ 983ebbad5ecSDavid Schultz if (!expchar) { /* %[fF] or sufficiently short %[gG] */ 984ebbad5ecSDavid Schultz if (expt <= 0) { 98581ae2e9aSDavid Schultz PRINT(zeroes, 1); 98681ae2e9aSDavid Schultz if (prec || flags & ALT) 9875004a238SDavid Schultz PRINT(decimal_point,decpt_len); 98858f0484fSRodney W. Grimes PAD(-expt, zeroes); 9893b204b7dSDavid Schultz /* already handled initial 0's */ 9903b204b7dSDavid Schultz prec += expt; 99158f0484fSRodney W. Grimes } else { 99221ca178eSDavid Schultz if (gs.grouping) { 99321ca178eSDavid Schultz n = grouping_print(&gs, &io, 9943c87aa1dSDavid Chisnall cp, dtoaend, locale); 99521ca178eSDavid Schultz if (n < 0) 99621ca178eSDavid Schultz goto error; 99721ca178eSDavid Schultz cp += n; 99821ca178eSDavid Schultz } else { 9993b204b7dSDavid Schultz PRINTANDPAD(cp, dtoaend, 100021ca178eSDavid Schultz expt, zeroes); 100121ca178eSDavid Schultz cp += expt; 1002ebbad5ecSDavid Schultz } 1003ebbad5ecSDavid Schultz if (prec || flags & ALT) 10045004a238SDavid Schultz PRINT(decimal_point,decpt_len); 1005ebbad5ecSDavid Schultz } 10063b204b7dSDavid Schultz PRINTANDPAD(cp, dtoaend, prec, zeroes); 1007ebbad5ecSDavid Schultz } else { /* %[eE] or sufficiently long %[gG] */ 10083b204b7dSDavid Schultz if (prec > 1 || flags & ALT) { 10095004a238SDavid Schultz PRINT(cp++, 1); 10105004a238SDavid Schultz PRINT(decimal_point, decpt_len); 101158f0484fSRodney W. Grimes PRINT(cp, ndig-1); 1012ebbad5ecSDavid Schultz PAD(prec - ndig, zeroes); 101358f0484fSRodney W. Grimes } else /* XeYYY */ 101458f0484fSRodney W. Grimes PRINT(cp, 1); 101558f0484fSRodney W. Grimes PRINT(expstr, expsize); 101658f0484fSRodney W. Grimes } 101758f0484fSRodney W. Grimes } 101858f0484fSRodney W. Grimes #endif 101958f0484fSRodney W. Grimes /* left-adjusting padding (always blank) */ 102058f0484fSRodney W. Grimes if (flags & LADJUST) 102158f0484fSRodney W. Grimes PAD(width - realsz, blanks); 102258f0484fSRodney W. Grimes 102358f0484fSRodney W. Grimes /* finally, adjust ret */ 102492e88f87SAndrey A. Chernov ret += prsize; 102558f0484fSRodney W. Grimes 102658f0484fSRodney W. Grimes FLUSH(); /* copy out the I/O vectors */ 102758f0484fSRodney W. Grimes } 102858f0484fSRodney W. Grimes done: 102958f0484fSRodney W. Grimes FLUSH(); 103058f0484fSRodney W. Grimes error: 1031096ad104SDag-Erling Smørgrav va_end(orgap); 10328de9e897SDavid Schultz #ifndef NO_FLOATING_POINT 10332ffc61baSTor Egge if (dtoaresult != NULL) 1034ebbad5ecSDavid Schultz freedtoa(dtoaresult); 10352ffc61baSTor Egge #endif 1036b9aac308STim J. Robbins if (convbuf != NULL) 1037b9aac308STim J. Robbins free(convbuf); 1038f70177e7SJulian Elischer if (__sferror(fp)) 1039f70177e7SJulian Elischer ret = EOF; 10401bf6c5f1SAndrey A. Chernov else 10411bf6c5f1SAndrey A. Chernov fp->_flags |= savserr; 1042efb7e53dSJordan K. Hubbard if ((argtable != NULL) && (argtable != statargtable)) 1043efb7e53dSJordan K. Hubbard free (argtable); 1044f70177e7SJulian Elischer return (ret); 104558f0484fSRodney W. Grimes /* NOTREACHED */ 104658f0484fSRodney W. Grimes } 104758f0484fSRodney W. Grimes 1048