xref: /freebsd/lib/libc/stdio/vfprintf.c (revision e95725feca4ad8142d472293c519687ec3098c6e)
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
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 */
320*e95725feSKonstantin Belousov 	int saved_errno;
32158f0484fSRodney W. Grimes 	char sign;		/* sign prefix (' ', '+', '-', or \0) */
32221ca178eSDavid Schultz 	struct grouping_state gs; /* thousands' grouping info */
323814d1bc9SDavid Schultz 
3248de9e897SDavid Schultz #ifndef NO_FLOATING_POINT
325ebbad5ecSDavid Schultz 	/*
326ebbad5ecSDavid Schultz 	 * We can decompose the printed representation of floating
327ebbad5ecSDavid Schultz 	 * point numbers into several parts, some of which may be empty:
328ebbad5ecSDavid Schultz 	 *
329ebbad5ecSDavid Schultz 	 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
330ebbad5ecSDavid Schultz 	 *    A       B     ---C---      D       E   F
331ebbad5ecSDavid Schultz 	 *
332ebbad5ecSDavid Schultz 	 * A:	'sign' holds this value if present; '\0' otherwise
333ebbad5ecSDavid Schultz 	 * B:	ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
334ebbad5ecSDavid Schultz 	 * C:	cp points to the string MMMNNN.  Leading and trailing
335ebbad5ecSDavid Schultz 	 *	zeros are not in the string and must be added.
336ebbad5ecSDavid Schultz 	 * D:	expchar holds this character; '\0' if no exponent, e.g. %f
337ebbad5ecSDavid Schultz 	 * F:	at least two digits for decimal, at least one digit for hex
338ebbad5ecSDavid Schultz 	 */
3397ae5c679SAlexey Zelkin 	char *decimal_point;	/* locale specific decimal point */
3405004a238SDavid Schultz 	int decpt_len;		/* length of decimal_point */
341ebbad5ecSDavid Schultz 	int signflag;		/* true if float is negative */
342ebbad5ecSDavid Schultz 	union {			/* floating point arguments %[aAeEfFgG] */
343ebbad5ecSDavid Schultz 		double dbl;
344ebbad5ecSDavid Schultz 		long double ldbl;
345ebbad5ecSDavid Schultz 	} fparg;
34658f0484fSRodney W. Grimes 	int expt;		/* integer value of exponent */
347ebbad5ecSDavid Schultz 	char expchar;		/* exponent character: [eEpP\0] */
348ebbad5ecSDavid Schultz 	char *dtoaend;		/* pointer to end of converted digits */
34958f0484fSRodney W. Grimes 	int expsize;		/* character count for expstr */
350ebbad5ecSDavid Schultz 	int ndig;		/* actual number of digits returned by dtoa */
351ebbad5ecSDavid Schultz 	char expstr[MAXEXPDIG+2];	/* buffer for exponent string: e+ZZZ */
3522ffc61baSTor Egge 	char *dtoaresult;	/* buffer allocated by dtoa */
35358f0484fSRodney W. Grimes #endif
35458f0484fSRodney W. Grimes 	u_long	ulval;		/* integer arguments %[diouxX] */
3557735bb0fSBill Fenner 	uintmax_t ujval;	/* %j, %ll, %q, %t, %z integers */
35658f0484fSRodney W. Grimes 	int base;		/* base for [diouxX] conversion */
35758f0484fSRodney W. Grimes 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
358261a532aSBill Fenner 	int realsz;		/* field size expanded by dprec, sign, etc */
35958f0484fSRodney W. Grimes 	int size;		/* size of converted field or string */
36092e88f87SAndrey A. Chernov 	int prsize;             /* max size of printed field */
361ebbad5ecSDavid Schultz 	const char *xdigs;     	/* digits for %[xX] conversion */
362814d1bc9SDavid Schultz 	struct io_state io;	/* I/O buffering state */
36338cac8f8SDavid Schultz 	char buf[BUF];		/* buffer with space for digits of uintmax_t */
364ebbad5ecSDavid Schultz 	char ox[2];		/* space for 0x; ox[1] is either x, X, or \0 */
365a387081cSDoug Rabson 	union arg *argtable;    /* args, built due to positional arg */
366a387081cSDoug Rabson 	union arg statargtable [STATIC_ARG_TBL_SIZE];
367efb7e53dSJordan K. Hubbard 	int nextarg;            /* 1-based argument index */
368efb7e53dSJordan K. Hubbard 	va_list orgap;          /* original argument pointer */
369b9aac308STim J. Robbins 	char *convbuf;		/* wide to multibyte conversion result */
3701bf6c5f1SAndrey A. Chernov 	int savserr;
37158f0484fSRodney W. Grimes 
372ac9913a7SDavid Schultz 	static const char xdigs_lower[16] = "0123456789abcdef";
373ac9913a7SDavid Schultz 	static const char xdigs_upper[16] = "0123456789ABCDEF";
374ebbad5ecSDavid Schultz 
375814d1bc9SDavid Schultz 	/* BEWARE, these `goto error' on error. */
37658f0484fSRodney W. Grimes #define	PRINT(ptr, len) { \
3773c87aa1dSDavid Chisnall 	if (io_print(&io, (ptr), (len), locale))	\
37858f0484fSRodney W. Grimes 		goto error; \
37958f0484fSRodney W. Grimes }
38058f0484fSRodney W. Grimes #define	PAD(howmany, with) { \
3813c87aa1dSDavid Chisnall 	if (io_pad(&io, (howmany), (with), locale)) \
38258f0484fSRodney W. Grimes 		goto error; \
383814d1bc9SDavid Schultz }
384814d1bc9SDavid Schultz #define	PRINTANDPAD(p, ep, len, with) {	\
3853c87aa1dSDavid Chisnall 	if (io_printandpad(&io, (p), (ep), (len), (with), locale)) \
386814d1bc9SDavid Schultz 		goto error; \
387814d1bc9SDavid Schultz }
388814d1bc9SDavid Schultz #define	FLUSH() { \
3893c87aa1dSDavid Chisnall 	if (io_flush(&io, locale)) \
390814d1bc9SDavid Schultz 		goto error; \
39158f0484fSRodney W. Grimes }
39258f0484fSRodney W. Grimes 
39358f0484fSRodney W. Grimes 	/*
394efb7e53dSJordan K. Hubbard 	 * Get the argument indexed by nextarg.   If the argument table is
395efb7e53dSJordan K. Hubbard 	 * built, use it to get the argument.  If its not, get the next
396efb7e53dSJordan K. Hubbard 	 * argument (and arguments must be gotten sequentially).
397efb7e53dSJordan K. Hubbard 	 */
398efb7e53dSJordan K. Hubbard #define GETARG(type) \
399a387081cSDoug Rabson 	((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
400efb7e53dSJordan K. Hubbard 	    (nextarg++, va_arg(ap, type)))
401efb7e53dSJordan K. Hubbard 
402efb7e53dSJordan K. Hubbard 	/*
40358f0484fSRodney W. Grimes 	 * To extend shorts properly, we need both signed and unsigned
40458f0484fSRodney W. Grimes 	 * argument extraction methods.
40558f0484fSRodney W. Grimes 	 */
40658f0484fSRodney W. Grimes #define	SARG() \
407efb7e53dSJordan K. Hubbard 	(flags&LONGINT ? GETARG(long) : \
408efb7e53dSJordan K. Hubbard 	    flags&SHORTINT ? (long)(short)GETARG(int) : \
4097735bb0fSBill Fenner 	    flags&CHARINT ? (long)(signed char)GETARG(int) : \
410efb7e53dSJordan K. Hubbard 	    (long)GETARG(int))
41158f0484fSRodney W. Grimes #define	UARG() \
412efb7e53dSJordan K. Hubbard 	(flags&LONGINT ? GETARG(u_long) : \
413efb7e53dSJordan K. Hubbard 	    flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
4147735bb0fSBill Fenner 	    flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
415efb7e53dSJordan K. Hubbard 	    (u_long)GETARG(u_int))
4167735bb0fSBill Fenner #define	INTMAX_SIZE	(INTMAXT|SIZET|PTRDIFFT|LLONGINT)
4177735bb0fSBill Fenner #define SJARG() \
4187735bb0fSBill Fenner 	(flags&INTMAXT ? GETARG(intmax_t) : \
4190881683bSDavid Schultz 	    flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
4207735bb0fSBill Fenner 	    flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
4217735bb0fSBill Fenner 	    (intmax_t)GETARG(long long))
4227735bb0fSBill Fenner #define	UJARG() \
4237735bb0fSBill Fenner 	(flags&INTMAXT ? GETARG(uintmax_t) : \
4247735bb0fSBill Fenner 	    flags&SIZET ? (uintmax_t)GETARG(size_t) : \
4257735bb0fSBill Fenner 	    flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
4267735bb0fSBill Fenner 	    (uintmax_t)GETARG(unsigned long long))
427efb7e53dSJordan K. Hubbard 
428efb7e53dSJordan K. Hubbard 	/*
429efb7e53dSJordan K. Hubbard 	 * Get * arguments, including the form *nn$.  Preserve the nextarg
430efb7e53dSJordan K. Hubbard 	 * that the argument can be gotten once the type is determined.
431efb7e53dSJordan K. Hubbard 	 */
432efb7e53dSJordan K. Hubbard #define GETASTER(val) \
433efb7e53dSJordan K. Hubbard 	n2 = 0; \
434efb7e53dSJordan K. Hubbard 	cp = fmt; \
435efb7e53dSJordan K. Hubbard 	while (is_digit(*cp)) { \
436efb7e53dSJordan K. Hubbard 		n2 = 10 * n2 + to_digit(*cp); \
437efb7e53dSJordan K. Hubbard 		cp++; \
438efb7e53dSJordan K. Hubbard 	} \
439efb7e53dSJordan K. Hubbard 	if (*cp == '$') { \
440efb7e53dSJordan K. Hubbard 		int hold = nextarg; \
441efb7e53dSJordan K. Hubbard 		if (argtable == NULL) { \
442efb7e53dSJordan K. Hubbard 			argtable = statargtable; \
443e62e5ff9SDavid Schultz 			if (__find_arguments (fmt0, orgap, &argtable)) { \
444e62e5ff9SDavid Schultz 				ret = EOF; \
445e62e5ff9SDavid Schultz 				goto error; \
446e62e5ff9SDavid Schultz 			} \
447efb7e53dSJordan K. Hubbard 		} \
448efb7e53dSJordan K. Hubbard 		nextarg = n2; \
449efb7e53dSJordan K. Hubbard 		val = GETARG (int); \
450efb7e53dSJordan K. Hubbard 		nextarg = hold; \
451efb7e53dSJordan K. Hubbard 		fmt = ++cp; \
452efb7e53dSJordan K. Hubbard 	} else { \
453efb7e53dSJordan K. Hubbard 		val = GETARG (int); \
454efb7e53dSJordan K. Hubbard 	}
455efb7e53dSJordan K. Hubbard 
45633bff5d3SDavid Schultz 	if (__use_xprintf == 0 && getenv("USE_XPRINTF"))
45733bff5d3SDavid Schultz 		__use_xprintf = 1;
45833bff5d3SDavid Schultz 	if (__use_xprintf > 0)
45933bff5d3SDavid Schultz 		return (__xvprintf(fp, fmt0, ap));
46058f0484fSRodney W. Grimes 
46158f0484fSRodney W. Grimes 	/* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
462450ead86SPedro F. Giffuni 	if (prepwrite(fp) != 0) {
463450ead86SPedro F. Giffuni 		errno = EBADF;
46458f0484fSRodney W. Grimes 		return (EOF);
465450ead86SPedro F. Giffuni 	}
46658f0484fSRodney W. Grimes 
4671bf6c5f1SAndrey A. Chernov 	savserr = fp->_flags & __SERR;
4681bf6c5f1SAndrey A. Chernov 	fp->_flags &= ~__SERR;
4691bf6c5f1SAndrey A. Chernov 
470*e95725feSKonstantin Belousov 	saved_errno = errno;
471e18701f4SDavid Schultz 	convbuf = NULL;
47258f0484fSRodney W. Grimes 	fmt = (char *)fmt0;
473efb7e53dSJordan K. Hubbard 	argtable = NULL;
474efb7e53dSJordan K. Hubbard 	nextarg = 1;
475d07090a8STim J. Robbins 	va_copy(orgap, ap);
476814d1bc9SDavid Schultz 	io_init(&io, fp);
47758f0484fSRodney W. Grimes 	ret = 0;
478e18701f4SDavid Schultz #ifndef NO_FLOATING_POINT
479e18701f4SDavid Schultz 	dtoaresult = NULL;
4803c87aa1dSDavid Chisnall 	decimal_point = localeconv_l(locale)->decimal_point;
4815004a238SDavid Schultz 	/* The overwhelmingly common case is decpt_len == 1. */
4825004a238SDavid Schultz 	decpt_len = (decimal_point[1] == '\0' ? 1 : strlen(decimal_point));
483e18701f4SDavid Schultz #endif
48458f0484fSRodney W. Grimes 
48558f0484fSRodney W. Grimes 	/*
48658f0484fSRodney W. Grimes 	 * Scan the format for conversions (`%' character).
48758f0484fSRodney W. Grimes 	 */
48858f0484fSRodney W. Grimes 	for (;;) {
48958f0484fSRodney W. Grimes 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
49058f0484fSRodney W. Grimes 			/* void */;
49158f0484fSRodney W. Grimes 		if ((n = fmt - cp) != 0) {
492b250f248SAndrey A. Chernov 			if ((unsigned)ret + n > INT_MAX) {
49392e88f87SAndrey A. Chernov 				ret = EOF;
494666d00d3SDavid Schultz 				errno = EOVERFLOW;
49592e88f87SAndrey A. Chernov 				goto error;
49692e88f87SAndrey A. Chernov 			}
49758f0484fSRodney W. Grimes 			PRINT(cp, n);
49858f0484fSRodney W. Grimes 			ret += n;
49958f0484fSRodney W. Grimes 		}
50058f0484fSRodney W. Grimes 		if (ch == '\0')
50158f0484fSRodney W. Grimes 			goto done;
50258f0484fSRodney W. Grimes 		fmt++;		/* skip over '%' */
50358f0484fSRodney W. Grimes 
50458f0484fSRodney W. Grimes 		flags = 0;
50558f0484fSRodney W. Grimes 		dprec = 0;
50658f0484fSRodney W. Grimes 		width = 0;
50758f0484fSRodney W. Grimes 		prec = -1;
50821ca178eSDavid Schultz 		gs.grouping = NULL;
50958f0484fSRodney W. Grimes 		sign = '\0';
510ebbad5ecSDavid Schultz 		ox[1] = '\0';
51158f0484fSRodney W. Grimes 
51258f0484fSRodney W. Grimes rflag:		ch = *fmt++;
51358f0484fSRodney W. Grimes reswitch:	switch (ch) {
51458f0484fSRodney W. Grimes 		case ' ':
5152e394b2fSAlexey Zelkin 			/*-
51658f0484fSRodney W. Grimes 			 * ``If the space and + flags both appear, the space
51758f0484fSRodney W. Grimes 			 * flag will be ignored.''
51858f0484fSRodney W. Grimes 			 *	-- ANSI X3J11
51958f0484fSRodney W. Grimes 			 */
52058f0484fSRodney W. Grimes 			if (!sign)
52158f0484fSRodney W. Grimes 				sign = ' ';
52258f0484fSRodney W. Grimes 			goto rflag;
52358f0484fSRodney W. Grimes 		case '#':
52458f0484fSRodney W. Grimes 			flags |= ALT;
52558f0484fSRodney W. Grimes 			goto rflag;
52658f0484fSRodney W. Grimes 		case '*':
5272e394b2fSAlexey Zelkin 			/*-
52858f0484fSRodney W. Grimes 			 * ``A negative field width argument is taken as a
52958f0484fSRodney W. Grimes 			 * - flag followed by a positive field width.''
53058f0484fSRodney W. Grimes 			 *	-- ANSI X3J11
53158f0484fSRodney W. Grimes 			 * They don't exclude field widths read from args.
53258f0484fSRodney W. Grimes 			 */
533efb7e53dSJordan K. Hubbard 			GETASTER (width);
534efb7e53dSJordan K. Hubbard 			if (width >= 0)
53558f0484fSRodney W. Grimes 				goto rflag;
53658f0484fSRodney W. Grimes 			width = -width;
53758f0484fSRodney W. Grimes 			/* FALLTHROUGH */
53858f0484fSRodney W. Grimes 		case '-':
53958f0484fSRodney W. Grimes 			flags |= LADJUST;
54058f0484fSRodney W. Grimes 			goto rflag;
54158f0484fSRodney W. Grimes 		case '+':
54258f0484fSRodney W. Grimes 			sign = '+';
54358f0484fSRodney W. Grimes 			goto rflag;
5447735bb0fSBill Fenner 		case '\'':
54598ee7635SAlexey Zelkin 			flags |= GROUPING;
5467735bb0fSBill Fenner 			goto rflag;
54758f0484fSRodney W. Grimes 		case '.':
54858f0484fSRodney W. Grimes 			if ((ch = *fmt++) == '*') {
5493b204b7dSDavid Schultz 				GETASTER (prec);
55058f0484fSRodney W. Grimes 				goto rflag;
55158f0484fSRodney W. Grimes 			}
5523b204b7dSDavid Schultz 			prec = 0;
55358f0484fSRodney W. Grimes 			while (is_digit(ch)) {
5543b204b7dSDavid Schultz 				prec = 10 * prec + to_digit(ch);
55558f0484fSRodney W. Grimes 				ch = *fmt++;
55658f0484fSRodney W. Grimes 			}
55758f0484fSRodney W. Grimes 			goto reswitch;
55858f0484fSRodney W. Grimes 		case '0':
5592e394b2fSAlexey Zelkin 			/*-
56058f0484fSRodney W. Grimes 			 * ``Note that 0 is taken as a flag, not as the
56158f0484fSRodney W. Grimes 			 * beginning of a field width.''
56258f0484fSRodney W. Grimes 			 *	-- ANSI X3J11
56358f0484fSRodney W. Grimes 			 */
56458f0484fSRodney W. Grimes 			flags |= ZEROPAD;
56558f0484fSRodney W. Grimes 			goto rflag;
56658f0484fSRodney W. Grimes 		case '1': case '2': case '3': case '4':
56758f0484fSRodney W. Grimes 		case '5': case '6': case '7': case '8': case '9':
56858f0484fSRodney W. Grimes 			n = 0;
56958f0484fSRodney W. Grimes 			do {
57058f0484fSRodney W. Grimes 				n = 10 * n + to_digit(ch);
57158f0484fSRodney W. Grimes 				ch = *fmt++;
57258f0484fSRodney W. Grimes 			} while (is_digit(ch));
573efb7e53dSJordan K. Hubbard 			if (ch == '$') {
574efb7e53dSJordan K. Hubbard 				nextarg = n;
575efb7e53dSJordan K. Hubbard 				if (argtable == NULL) {
576efb7e53dSJordan K. Hubbard 					argtable = statargtable;
577e62e5ff9SDavid Schultz 					if (__find_arguments (fmt0, orgap,
578e62e5ff9SDavid Schultz 							      &argtable)) {
579e62e5ff9SDavid Schultz 						ret = EOF;
580e62e5ff9SDavid Schultz 						goto error;
581e62e5ff9SDavid Schultz 					}
582efb7e53dSJordan K. Hubbard 				}
583efb7e53dSJordan K. Hubbard 				goto rflag;
584efb7e53dSJordan K. Hubbard 			}
58558f0484fSRodney W. Grimes 			width = n;
58658f0484fSRodney W. Grimes 			goto reswitch;
5878de9e897SDavid Schultz #ifndef NO_FLOATING_POINT
58858f0484fSRodney W. Grimes 		case 'L':
58958f0484fSRodney W. Grimes 			flags |= LONGDBL;
59058f0484fSRodney W. Grimes 			goto rflag;
59158f0484fSRodney W. Grimes #endif
59258f0484fSRodney W. Grimes 		case 'h':
5937735bb0fSBill Fenner 			if (flags & SHORTINT) {
5947735bb0fSBill Fenner 				flags &= ~SHORTINT;
5957735bb0fSBill Fenner 				flags |= CHARINT;
5967735bb0fSBill Fenner 			} else
59758f0484fSRodney W. Grimes 				flags |= SHORTINT;
59858f0484fSRodney W. Grimes 			goto rflag;
5997735bb0fSBill Fenner 		case 'j':
6007735bb0fSBill Fenner 			flags |= INTMAXT;
6017735bb0fSBill Fenner 			goto rflag;
60258f0484fSRodney W. Grimes 		case 'l':
6037735bb0fSBill Fenner 			if (flags & LONGINT) {
6047735bb0fSBill Fenner 				flags &= ~LONGINT;
6057735bb0fSBill Fenner 				flags |= LLONGINT;
6067735bb0fSBill Fenner 			} else
60758f0484fSRodney W. Grimes 				flags |= LONGINT;
60858f0484fSRodney W. Grimes 			goto rflag;
60958f0484fSRodney W. Grimes 		case 'q':
6107735bb0fSBill Fenner 			flags |= LLONGINT;	/* not necessarily */
6117735bb0fSBill Fenner 			goto rflag;
6127735bb0fSBill Fenner 		case 't':
6137735bb0fSBill Fenner 			flags |= PTRDIFFT;
6147735bb0fSBill Fenner 			goto rflag;
6157735bb0fSBill Fenner 		case 'z':
6167735bb0fSBill Fenner 			flags |= SIZET;
61758f0484fSRodney W. Grimes 			goto rflag;
618927ecbf3STim J. Robbins 		case 'C':
619927ecbf3STim J. Robbins 			flags |= LONGINT;
620927ecbf3STim J. Robbins 			/*FALLTHROUGH*/
62158f0484fSRodney W. Grimes 		case 'c':
622b9aac308STim J. Robbins 			if (flags & LONGINT) {
62393996f6dSTim J. Robbins 				static const mbstate_t initial;
62493996f6dSTim J. Robbins 				mbstate_t mbs;
625b9aac308STim J. Robbins 				size_t mbseqlen;
626b9aac308STim J. Robbins 
62793996f6dSTim J. Robbins 				mbs = initial;
628b9aac308STim J. Robbins 				mbseqlen = wcrtomb(cp = buf,
62993996f6dSTim J. Robbins 				    (wchar_t)GETARG(wint_t), &mbs);
6306180233fSTim J. Robbins 				if (mbseqlen == (size_t)-1) {
6316180233fSTim J. Robbins 					fp->_flags |= __SERR;
632b9aac308STim J. Robbins 					goto error;
6336180233fSTim J. Robbins 				}
634b9aac308STim J. Robbins 				size = (int)mbseqlen;
635b9aac308STim J. Robbins 			} else {
636efb7e53dSJordan K. Hubbard 				*(cp = buf) = GETARG(int);
63758f0484fSRodney W. Grimes 				size = 1;
638b9aac308STim J. Robbins 			}
63958f0484fSRodney W. Grimes 			sign = '\0';
64058f0484fSRodney W. Grimes 			break;
64158f0484fSRodney W. Grimes 		case 'D':
64258f0484fSRodney W. Grimes 			flags |= LONGINT;
64358f0484fSRodney W. Grimes 			/*FALLTHROUGH*/
64458f0484fSRodney W. Grimes 		case 'd':
64558f0484fSRodney W. Grimes 		case 'i':
6467735bb0fSBill Fenner 			if (flags & INTMAX_SIZE) {
6477735bb0fSBill Fenner 				ujval = SJARG();
6487735bb0fSBill Fenner 				if ((intmax_t)ujval < 0) {
6497735bb0fSBill Fenner 					ujval = -ujval;
65058f0484fSRodney W. Grimes 					sign = '-';
65158f0484fSRodney W. Grimes 				}
65258f0484fSRodney W. Grimes 			} else {
65358f0484fSRodney W. Grimes 				ulval = SARG();
65458f0484fSRodney W. Grimes 				if ((long)ulval < 0) {
65558f0484fSRodney W. Grimes 					ulval = -ulval;
65658f0484fSRodney W. Grimes 					sign = '-';
65758f0484fSRodney W. Grimes 				}
65858f0484fSRodney W. Grimes 			}
65958f0484fSRodney W. Grimes 			base = 10;
66058f0484fSRodney W. Grimes 			goto number;
6618de9e897SDavid Schultz #ifndef NO_FLOATING_POINT
6627735bb0fSBill Fenner 		case 'a':
6637735bb0fSBill Fenner 		case 'A':
664ebbad5ecSDavid Schultz 			if (ch == 'a') {
665ebbad5ecSDavid Schultz 				ox[1] = 'x';
666ebbad5ecSDavid Schultz 				xdigs = xdigs_lower;
667ebbad5ecSDavid Schultz 				expchar = 'p';
668ebbad5ecSDavid Schultz 			} else {
669ebbad5ecSDavid Schultz 				ox[1] = 'X';
670ebbad5ecSDavid Schultz 				xdigs = xdigs_upper;
671ebbad5ecSDavid Schultz 				expchar = 'P';
672ebbad5ecSDavid Schultz 			}
673904322a5SDavid Schultz 			if (prec >= 0)
674904322a5SDavid Schultz 				prec++;
675904322a5SDavid Schultz 			if (dtoaresult != NULL)
676904322a5SDavid Schultz 				freedtoa(dtoaresult);
677ebbad5ecSDavid Schultz 			if (flags & LONGDBL) {
678904322a5SDavid Schultz 				fparg.ldbl = GETARG(long double);
679ebbad5ecSDavid Schultz 				dtoaresult = cp =
680ebbad5ecSDavid Schultz 				    __hldtoa(fparg.ldbl, xdigs, prec,
681ebbad5ecSDavid Schultz 				    &expt, &signflag, &dtoaend);
682ebbad5ecSDavid Schultz 			} else {
683ebbad5ecSDavid Schultz 				fparg.dbl = GETARG(double);
684ebbad5ecSDavid Schultz 				dtoaresult = cp =
685ebbad5ecSDavid Schultz 				    __hdtoa(fparg.dbl, xdigs, prec,
686ebbad5ecSDavid Schultz 				    &expt, &signflag, &dtoaend);
687ebbad5ecSDavid Schultz 			}
688904322a5SDavid Schultz 			if (prec < 0)
689904322a5SDavid Schultz 				prec = dtoaend - cp;
690904322a5SDavid Schultz 			if (expt == INT_MAX)
691904322a5SDavid Schultz 				ox[1] = '\0';
692904322a5SDavid Schultz 			goto fp_common;
693d26be6f0SBruce Evans 		case 'e':
69458f0484fSRodney W. Grimes 		case 'E':
695ebbad5ecSDavid Schultz 			expchar = ch;
696ebbad5ecSDavid Schultz 			if (prec < 0)	/* account for digit before decpt */
697ebbad5ecSDavid Schultz 				prec = DEFPREC + 1;
698ebbad5ecSDavid Schultz 			else
699ebbad5ecSDavid Schultz 				prec++;
700ebbad5ecSDavid Schultz 			goto fp_begin;
701d26be6f0SBruce Evans 		case 'f':
7027735bb0fSBill Fenner 		case 'F':
703ebbad5ecSDavid Schultz 			expchar = '\0';
704d26be6f0SBruce Evans 			goto fp_begin;
70558f0484fSRodney W. Grimes 		case 'g':
70658f0484fSRodney W. Grimes 		case 'G':
707ebbad5ecSDavid Schultz 			expchar = ch - ('g' - 'e');
708d26be6f0SBruce Evans 			if (prec == 0)
709d26be6f0SBruce Evans 				prec = 1;
710ebbad5ecSDavid Schultz fp_begin:
711ebbad5ecSDavid Schultz 			if (prec < 0)
71258f0484fSRodney W. Grimes 				prec = DEFPREC;
713ebbad5ecSDavid Schultz 			if (dtoaresult != NULL)
714ebbad5ecSDavid Schultz 				freedtoa(dtoaresult);
715ebbad5ecSDavid Schultz 			if (flags & LONGDBL) {
716ebbad5ecSDavid Schultz 				fparg.ldbl = GETARG(long double);
717ebbad5ecSDavid Schultz 				dtoaresult = cp =
718ebbad5ecSDavid Schultz 				    __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
719ebbad5ecSDavid Schultz 				    &expt, &signflag, &dtoaend);
720ebbad5ecSDavid Schultz 			} else {
721ebbad5ecSDavid Schultz 				fparg.dbl = GETARG(double);
722ebbad5ecSDavid Schultz 				dtoaresult = cp =
723ebbad5ecSDavid Schultz 				    dtoa(fparg.dbl, expchar ? 2 : 3, prec,
724ebbad5ecSDavid Schultz 				    &expt, &signflag, &dtoaend);
725ebbad5ecSDavid Schultz 				if (expt == 9999)
726ebbad5ecSDavid Schultz 					expt = INT_MAX;
72758f0484fSRodney W. Grimes 			}
728904322a5SDavid Schultz fp_common:
729ebbad5ecSDavid Schultz 			if (signflag)
730ebbad5ecSDavid Schultz 				sign = '-';
731ebbad5ecSDavid Schultz 			if (expt == INT_MAX) {	/* inf or nan */
732ebbad5ecSDavid Schultz 				if (*cp == 'N') {
733ebbad5ecSDavid Schultz 					cp = (ch >= 'a') ? "nan" : "NAN";
734ebbad5ecSDavid Schultz 					sign = '\0';
735ebbad5ecSDavid Schultz 				} else
736ebbad5ecSDavid Schultz 					cp = (ch >= 'a') ? "inf" : "INF";
73758f0484fSRodney W. Grimes 				size = 3;
738970a466cSDavid Schultz 				flags &= ~ZEROPAD;
73958f0484fSRodney W. Grimes 				break;
74058f0484fSRodney W. Grimes 			}
74158f0484fSRodney W. Grimes 			flags |= FPT;
742ebbad5ecSDavid Schultz 			ndig = dtoaend - cp;
74358f0484fSRodney W. Grimes 			if (ch == 'g' || ch == 'G') {
744ebbad5ecSDavid Schultz 				if (expt > -4 && expt <= prec) {
745ebbad5ecSDavid Schultz 					/* Make %[gG] smell like %[fF] */
746ebbad5ecSDavid Schultz 					expchar = '\0';
747ebbad5ecSDavid Schultz 					if (flags & ALT)
748ebbad5ecSDavid Schultz 						prec -= expt;
74958f0484fSRodney W. Grimes 					else
750ebbad5ecSDavid Schultz 						prec = ndig - expt;
751ebbad5ecSDavid Schultz 					if (prec < 0)
752ebbad5ecSDavid Schultz 						prec = 0;
7531f2a0cdfSDavid Schultz 				} else {
7541f2a0cdfSDavid Schultz 					/*
7551f2a0cdfSDavid Schultz 					 * Make %[gG] smell like %[eE], but
7561f2a0cdfSDavid Schultz 					 * trim trailing zeroes if no # flag.
7571f2a0cdfSDavid Schultz 					 */
7581f2a0cdfSDavid Schultz 					if (!(flags & ALT))
7591f2a0cdfSDavid Schultz 						prec = ndig;
76058f0484fSRodney W. Grimes 				}
761ebbad5ecSDavid Schultz 			}
762ebbad5ecSDavid Schultz 			if (expchar) {
763ebbad5ecSDavid Schultz 				expsize = exponent(expstr, expt - 1, expchar);
764ebbad5ecSDavid Schultz 				size = expsize + prec;
7653b204b7dSDavid Schultz 				if (prec > 1 || flags & ALT)
7665004a238SDavid Schultz 					size += decpt_len;
767ebbad5ecSDavid Schultz 			} else {
76881ae2e9aSDavid Schultz 				/* space for digits before decimal point */
76981ae2e9aSDavid Schultz 				if (expt > 0)
77058f0484fSRodney W. Grimes 					size = expt;
77181ae2e9aSDavid Schultz 				else	/* "0" */
77281ae2e9aSDavid Schultz 					size = 1;
77381ae2e9aSDavid Schultz 				/* space for decimal pt and following digits */
77458f0484fSRodney W. Grimes 				if (prec || flags & ALT)
7755004a238SDavid Schultz 					size += prec + decpt_len;
77621ca178eSDavid Schultz 				if ((flags & GROUPING) && expt > 0)
7773c87aa1dSDavid Chisnall 					size += grouping_init(&gs, expt, locale);
778ebbad5ecSDavid Schultz 			}
77958f0484fSRodney W. Grimes 			break;
7808de9e897SDavid Schultz #endif /* !NO_FLOATING_POINT */
781*e95725feSKonstantin Belousov 		case 'm':
782*e95725feSKonstantin Belousov 			cp = strerror(saved_errno);
783*e95725feSKonstantin Belousov 			size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp);
784*e95725feSKonstantin Belousov 			sign = '\0';
785*e95725feSKonstantin Belousov 			break;
78658f0484fSRodney W. Grimes 		case 'n':
7877735bb0fSBill Fenner 			/*
7887735bb0fSBill Fenner 			 * Assignment-like behavior is specified if the
7897735bb0fSBill Fenner 			 * value overflows or is otherwise unrepresentable.
7907735bb0fSBill Fenner 			 * C99 says to use `signed char' for %hhn conversions.
7917735bb0fSBill Fenner 			 */
7927735bb0fSBill Fenner 			if (flags & LLONGINT)
7937735bb0fSBill Fenner 				*GETARG(long long *) = ret;
7947735bb0fSBill Fenner 			else if (flags & SIZET)
7957735bb0fSBill Fenner 				*GETARG(ssize_t *) = (ssize_t)ret;
7967735bb0fSBill Fenner 			else if (flags & PTRDIFFT)
7977735bb0fSBill Fenner 				*GETARG(ptrdiff_t *) = ret;
7987735bb0fSBill Fenner 			else if (flags & INTMAXT)
7997735bb0fSBill Fenner 				*GETARG(intmax_t *) = ret;
80058f0484fSRodney W. Grimes 			else if (flags & LONGINT)
8016e690ad4SAndrey A. Chernov 				*GETARG(long *) = ret;
80258f0484fSRodney W. Grimes 			else if (flags & SHORTINT)
8036e690ad4SAndrey A. Chernov 				*GETARG(short *) = ret;
8047735bb0fSBill Fenner 			else if (flags & CHARINT)
8057735bb0fSBill Fenner 				*GETARG(signed char *) = ret;
80658f0484fSRodney W. Grimes 			else
8076e690ad4SAndrey A. Chernov 				*GETARG(int *) = ret;
80858f0484fSRodney W. Grimes 			continue;	/* no output */
80958f0484fSRodney W. Grimes 		case 'O':
81058f0484fSRodney W. Grimes 			flags |= LONGINT;
81158f0484fSRodney W. Grimes 			/*FALLTHROUGH*/
81258f0484fSRodney W. Grimes 		case 'o':
8137735bb0fSBill Fenner 			if (flags & INTMAX_SIZE)
8147735bb0fSBill Fenner 				ujval = UJARG();
81558f0484fSRodney W. Grimes 			else
81658f0484fSRodney W. Grimes 				ulval = UARG();
81758f0484fSRodney W. Grimes 			base = 8;
81858f0484fSRodney W. Grimes 			goto nosign;
81958f0484fSRodney W. Grimes 		case 'p':
8202e394b2fSAlexey Zelkin 			/*-
82158f0484fSRodney W. Grimes 			 * ``The argument shall be a pointer to void.  The
82258f0484fSRodney W. Grimes 			 * value of the pointer is converted to a sequence
82358f0484fSRodney W. Grimes 			 * of printable characters, in an implementation-
82458f0484fSRodney W. Grimes 			 * defined manner.''
82558f0484fSRodney W. Grimes 			 *	-- ANSI X3J11
82658f0484fSRodney W. Grimes 			 */
8277735bb0fSBill Fenner 			ujval = (uintmax_t)(uintptr_t)GETARG(void *);
82858f0484fSRodney W. Grimes 			base = 16;
829ebbad5ecSDavid Schultz 			xdigs = xdigs_lower;
830ebbad5ecSDavid Schultz 			flags = flags | INTMAXT;
831ebbad5ecSDavid Schultz 			ox[1] = 'x';
83258f0484fSRodney W. Grimes 			goto nosign;
833927ecbf3STim J. Robbins 		case 'S':
834927ecbf3STim J. Robbins 			flags |= LONGINT;
835927ecbf3STim J. Robbins 			/*FALLTHROUGH*/
83658f0484fSRodney W. Grimes 		case 's':
837b9aac308STim J. Robbins 			if (flags & LONGINT) {
838b9aac308STim J. Robbins 				wchar_t *wcp;
839b9aac308STim J. Robbins 
840b9aac308STim J. Robbins 				if (convbuf != NULL)
841b9aac308STim J. Robbins 					free(convbuf);
842b9aac308STim J. Robbins 				if ((wcp = GETARG(wchar_t *)) == NULL)
843b9aac308STim J. Robbins 					cp = "(null)";
844b9aac308STim J. Robbins 				else {
845b9aac308STim J. Robbins 					convbuf = __wcsconv(wcp, prec);
8466180233fSTim J. Robbins 					if (convbuf == NULL) {
8476180233fSTim J. Robbins 						fp->_flags |= __SERR;
848b9aac308STim J. Robbins 						goto error;
8496180233fSTim J. Robbins 					}
850b9aac308STim J. Robbins 					cp = convbuf;
851b9aac308STim J. Robbins 				}
852b9aac308STim J. Robbins 			} else if ((cp = GETARG(char *)) == NULL)
85358f0484fSRodney W. Grimes 				cp = "(null)";
854353ce11cSDavid Schultz 			size = (prec >= 0) ? strnlen(cp, prec) : strlen(cp);
85558f0484fSRodney W. Grimes 			sign = '\0';
85658f0484fSRodney W. Grimes 			break;
85758f0484fSRodney W. Grimes 		case 'U':
85858f0484fSRodney W. Grimes 			flags |= LONGINT;
85958f0484fSRodney W. Grimes 			/*FALLTHROUGH*/
86058f0484fSRodney W. Grimes 		case 'u':
8617735bb0fSBill Fenner 			if (flags & INTMAX_SIZE)
8627735bb0fSBill Fenner 				ujval = UJARG();
86358f0484fSRodney W. Grimes 			else
86458f0484fSRodney W. Grimes 				ulval = UARG();
86558f0484fSRodney W. Grimes 			base = 10;
86658f0484fSRodney W. Grimes 			goto nosign;
86758f0484fSRodney W. Grimes 		case 'X':
868ebbad5ecSDavid Schultz 			xdigs = xdigs_upper;
86958f0484fSRodney W. Grimes 			goto hex;
87058f0484fSRodney W. Grimes 		case 'x':
871ebbad5ecSDavid Schultz 			xdigs = xdigs_lower;
8727735bb0fSBill Fenner hex:
8737735bb0fSBill Fenner 			if (flags & INTMAX_SIZE)
8747735bb0fSBill Fenner 				ujval = UJARG();
87558f0484fSRodney W. Grimes 			else
87658f0484fSRodney W. Grimes 				ulval = UARG();
87758f0484fSRodney W. Grimes 			base = 16;
87858f0484fSRodney W. Grimes 			/* leading 0x/X only if non-zero */
87958f0484fSRodney W. Grimes 			if (flags & ALT &&
8807735bb0fSBill Fenner 			    (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
881ebbad5ecSDavid Schultz 				ox[1] = ch;
88258f0484fSRodney W. Grimes 
88398ee7635SAlexey Zelkin 			flags &= ~GROUPING;
88458f0484fSRodney W. Grimes 			/* unsigned conversions */
88558f0484fSRodney W. Grimes nosign:			sign = '\0';
8862e394b2fSAlexey Zelkin 			/*-
88758f0484fSRodney W. Grimes 			 * ``... diouXx conversions ... if a precision is
88858f0484fSRodney W. Grimes 			 * specified, the 0 flag will be ignored.''
88958f0484fSRodney W. Grimes 			 *	-- ANSI X3J11
89058f0484fSRodney W. Grimes 			 */
89158f0484fSRodney W. Grimes number:			if ((dprec = prec) >= 0)
89258f0484fSRodney W. Grimes 				flags &= ~ZEROPAD;
89358f0484fSRodney W. Grimes 
8942e394b2fSAlexey Zelkin 			/*-
89558f0484fSRodney W. Grimes 			 * ``The result of converting a zero value with an
89658f0484fSRodney W. Grimes 			 * explicit precision of zero is no characters.''
89758f0484fSRodney W. Grimes 			 *	-- ANSI X3J11
8981be5319aSDavid Schultz 			 *
8991be5319aSDavid Schultz 			 * ``The C Standard is clear enough as is.  The call
9001be5319aSDavid Schultz 			 * printf("%#.0o", 0) should print 0.''
9011be5319aSDavid Schultz 			 *	-- Defect Report #151
90258f0484fSRodney W. Grimes 			 */
90358f0484fSRodney W. Grimes 			cp = buf + BUF;
9047735bb0fSBill Fenner 			if (flags & INTMAX_SIZE) {
9051be5319aSDavid Schultz 				if (ujval != 0 || prec != 0 ||
9061be5319aSDavid Schultz 				    (flags & ALT && base == 8))
9077735bb0fSBill Fenner 					cp = __ujtoa(ujval, cp, base,
90821ca178eSDavid Schultz 					    flags & ALT, xdigs);
90958f0484fSRodney W. Grimes 			} else {
9101be5319aSDavid Schultz 				if (ulval != 0 || prec != 0 ||
9111be5319aSDavid Schultz 				    (flags & ALT && base == 8))
91258f0484fSRodney W. Grimes 					cp = __ultoa(ulval, cp, base,
91321ca178eSDavid Schultz 					    flags & ALT, xdigs);
91458f0484fSRodney W. Grimes 			}
91558f0484fSRodney W. Grimes 			size = buf + BUF - cp;
91638cac8f8SDavid Schultz 			if (size > BUF)	/* should never happen */
91738cac8f8SDavid Schultz 				abort();
91821ca178eSDavid Schultz 			if ((flags & GROUPING) && size != 0)
9193c87aa1dSDavid Chisnall 				size += grouping_init(&gs, size, locale);
92058f0484fSRodney W. Grimes 			break;
92158f0484fSRodney W. Grimes 		default:	/* "%?" prints ?, unless ? is NUL */
92258f0484fSRodney W. Grimes 			if (ch == '\0')
92358f0484fSRodney W. Grimes 				goto done;
92458f0484fSRodney W. Grimes 			/* pretend it was %c with argument ch */
92558f0484fSRodney W. Grimes 			cp = buf;
92658f0484fSRodney W. Grimes 			*cp = ch;
92758f0484fSRodney W. Grimes 			size = 1;
92858f0484fSRodney W. Grimes 			sign = '\0';
92958f0484fSRodney W. Grimes 			break;
93058f0484fSRodney W. Grimes 		}
93158f0484fSRodney W. Grimes 
93258f0484fSRodney W. Grimes 		/*
93358f0484fSRodney W. Grimes 		 * All reasonable formats wind up here.  At this point, `cp'
93458f0484fSRodney W. Grimes 		 * points to a string which (if not flags&LADJUST) should be
93558f0484fSRodney W. Grimes 		 * padded out to `width' places.  If flags&ZEROPAD, it should
93658f0484fSRodney W. Grimes 		 * first be prefixed by any sign or other prefix; otherwise,
93758f0484fSRodney W. Grimes 		 * it should be blank padded before the prefix is emitted.
93858f0484fSRodney W. Grimes 		 * After any left-hand padding and prefixing, emit zeroes
93958f0484fSRodney W. Grimes 		 * required by a decimal [diouxX] precision, then print the
94058f0484fSRodney W. Grimes 		 * string proper, then emit zeroes required by any leftover
94158f0484fSRodney W. Grimes 		 * floating precision; finally, if LADJUST, pad with blanks.
94258f0484fSRodney W. Grimes 		 *
94358f0484fSRodney W. Grimes 		 * Compute actual size, so we know how much to pad.
944261a532aSBill Fenner 		 * size excludes decimal prec; realsz includes it.
94558f0484fSRodney W. Grimes 		 */
946261a532aSBill Fenner 		realsz = dprec > size ? dprec : size;
94758f0484fSRodney W. Grimes 		if (sign)
948261a532aSBill Fenner 			realsz++;
949904322a5SDavid Schultz 		if (ox[1])
950261a532aSBill Fenner 			realsz += 2;
95158f0484fSRodney W. Grimes 
95292e88f87SAndrey A. Chernov 		prsize = width > realsz ? width : realsz;
953b250f248SAndrey A. Chernov 		if ((unsigned)ret + prsize > INT_MAX) {
95492e88f87SAndrey A. Chernov 			ret = EOF;
955666d00d3SDavid Schultz 			errno = EOVERFLOW;
95692e88f87SAndrey A. Chernov 			goto error;
95792e88f87SAndrey A. Chernov 		}
95892e88f87SAndrey A. Chernov 
95958f0484fSRodney W. Grimes 		/* right-adjusting blank padding */
96058f0484fSRodney W. Grimes 		if ((flags & (LADJUST|ZEROPAD)) == 0)
96158f0484fSRodney W. Grimes 			PAD(width - realsz, blanks);
96258f0484fSRodney W. Grimes 
96358f0484fSRodney W. Grimes 		/* prefix */
964904322a5SDavid Schultz 		if (sign)
96558f0484fSRodney W. Grimes 			PRINT(&sign, 1);
966904322a5SDavid Schultz 
967904322a5SDavid Schultz 		if (ox[1]) {	/* ox[1] is either x, X, or \0 */
96858f0484fSRodney W. Grimes 			ox[0] = '0';
96958f0484fSRodney W. Grimes 			PRINT(ox, 2);
97058f0484fSRodney W. Grimes 		}
97158f0484fSRodney W. Grimes 
97258f0484fSRodney W. Grimes 		/* right-adjusting zero padding */
97358f0484fSRodney W. Grimes 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
97458f0484fSRodney W. Grimes 			PAD(width - realsz, zeroes);
97558f0484fSRodney W. Grimes 
97658f0484fSRodney W. Grimes 		/* the string or number proper */
9778de9e897SDavid Schultz #ifndef NO_FLOATING_POINT
97858f0484fSRodney W. Grimes 		if ((flags & FPT) == 0) {
97921ca178eSDavid Schultz #endif
98021ca178eSDavid Schultz 			/* leading zeroes from decimal precision */
98121ca178eSDavid Schultz 			PAD(dprec - size, zeroes);
98221ca178eSDavid Schultz 			if (gs.grouping) {
9833c87aa1dSDavid Chisnall 				if (grouping_print(&gs, &io, cp, buf+BUF, locale) < 0)
98421ca178eSDavid Schultz 					goto error;
98521ca178eSDavid Schultz 			} else {
98658f0484fSRodney W. Grimes 				PRINT(cp, size);
98721ca178eSDavid Schultz 			}
98821ca178eSDavid Schultz #ifndef NO_FLOATING_POINT
98958f0484fSRodney W. Grimes 		} else {	/* glue together f_p fragments */
990ebbad5ecSDavid Schultz 			if (!expchar) {	/* %[fF] or sufficiently short %[gG] */
991ebbad5ecSDavid Schultz 				if (expt <= 0) {
99281ae2e9aSDavid Schultz 					PRINT(zeroes, 1);
99381ae2e9aSDavid Schultz 					if (prec || flags & ALT)
9945004a238SDavid Schultz 						PRINT(decimal_point,decpt_len);
99558f0484fSRodney W. Grimes 					PAD(-expt, zeroes);
9963b204b7dSDavid Schultz 					/* already handled initial 0's */
9973b204b7dSDavid Schultz 					prec += expt;
99858f0484fSRodney W. Grimes 				} else {
99921ca178eSDavid Schultz 					if (gs.grouping) {
100021ca178eSDavid Schultz 						n = grouping_print(&gs, &io,
10013c87aa1dSDavid Chisnall 						    cp, dtoaend, locale);
100221ca178eSDavid Schultz 						if (n < 0)
100321ca178eSDavid Schultz 							goto error;
100421ca178eSDavid Schultz 						cp += n;
100521ca178eSDavid Schultz 					} else {
10063b204b7dSDavid Schultz 						PRINTANDPAD(cp, dtoaend,
100721ca178eSDavid Schultz 						    expt, zeroes);
100821ca178eSDavid Schultz 						cp += expt;
1009ebbad5ecSDavid Schultz 					}
1010ebbad5ecSDavid Schultz 					if (prec || flags & ALT)
10115004a238SDavid Schultz 						PRINT(decimal_point,decpt_len);
1012ebbad5ecSDavid Schultz 				}
10133b204b7dSDavid Schultz 				PRINTANDPAD(cp, dtoaend, prec, zeroes);
1014ebbad5ecSDavid Schultz 			} else {	/* %[eE] or sufficiently long %[gG] */
10153b204b7dSDavid Schultz 				if (prec > 1 || flags & ALT) {
10165004a238SDavid Schultz 					PRINT(cp++, 1);
10175004a238SDavid Schultz 					PRINT(decimal_point, decpt_len);
101858f0484fSRodney W. Grimes 					PRINT(cp, ndig-1);
1019ebbad5ecSDavid Schultz 					PAD(prec - ndig, zeroes);
102058f0484fSRodney W. Grimes 				} else	/* XeYYY */
102158f0484fSRodney W. Grimes 					PRINT(cp, 1);
102258f0484fSRodney W. Grimes 				PRINT(expstr, expsize);
102358f0484fSRodney W. Grimes 			}
102458f0484fSRodney W. Grimes 		}
102558f0484fSRodney W. Grimes #endif
102658f0484fSRodney W. Grimes 		/* left-adjusting padding (always blank) */
102758f0484fSRodney W. Grimes 		if (flags & LADJUST)
102858f0484fSRodney W. Grimes 			PAD(width - realsz, blanks);
102958f0484fSRodney W. Grimes 
103058f0484fSRodney W. Grimes 		/* finally, adjust ret */
103192e88f87SAndrey A. Chernov 		ret += prsize;
103258f0484fSRodney W. Grimes 
103358f0484fSRodney W. Grimes 		FLUSH();	/* copy out the I/O vectors */
103458f0484fSRodney W. Grimes 	}
103558f0484fSRodney W. Grimes done:
103658f0484fSRodney W. Grimes 	FLUSH();
103758f0484fSRodney W. Grimes error:
1038096ad104SDag-Erling Smørgrav 	va_end(orgap);
10398de9e897SDavid Schultz #ifndef NO_FLOATING_POINT
10402ffc61baSTor Egge 	if (dtoaresult != NULL)
1041ebbad5ecSDavid Schultz 		freedtoa(dtoaresult);
10422ffc61baSTor Egge #endif
1043b9aac308STim J. Robbins 	if (convbuf != NULL)
1044b9aac308STim J. Robbins 		free(convbuf);
1045f70177e7SJulian Elischer 	if (__sferror(fp))
1046f70177e7SJulian Elischer 		ret = EOF;
10471bf6c5f1SAndrey A. Chernov 	else
10481bf6c5f1SAndrey A. Chernov 		fp->_flags |= savserr;
1049efb7e53dSJordan K. Hubbard 	if ((argtable != NULL) && (argtable != statargtable))
1050efb7e53dSJordan K. Hubbard 		free (argtable);
1051f70177e7SJulian Elischer 	return (ret);
105258f0484fSRodney W. Grimes 	/* NOTREACHED */
105358f0484fSRodney W. Grimes }
105458f0484fSRodney W. Grimes 
1055