1/*	$NetBSD: vfwprintf.c,v 1.41 2024/01/20 14:52:49 christos Exp $	*/
2
3/*-
4 * Copyright (c) 1990, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Chris Torek.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 *    may be used to endorse or promote products derived from this software
20 *    without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36#if defined(LIBC_SCCS) && !defined(lint)
37#if 0
38static char sccsid[] = "@(#)vfprintf.c	8.1 (Berkeley) 6/4/93";
39__FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.27 2007/01/09 00:28:08 imp Exp $");
40#else
41__RCSID("$NetBSD: vfwprintf.c,v 1.41 2024/01/20 14:52:49 christos Exp $");
42#endif
43#endif /* LIBC_SCCS and not lint */
44
45/*
46 * Actual {w,}printf innards.
47 */
48
49#include "namespace.h"
50#include <sys/types.h>
51
52#include <assert.h>
53#include <ctype.h>
54#include <limits.h>
55#include <locale.h>
56#include <stdarg.h>
57#include <stddef.h>
58#include <stdint.h>
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
62#include <errno.h>
63#include <wchar.h>
64#include <wctype.h>
65
66#include "reentrant.h"
67#include "setlocale_local.h"
68#include "local.h"
69#include "extern.h"
70#include "fvwrite.h"
71
72#ifndef NARROW
73#define MCHAR_T		char
74#define CHAR_T		wchar_t
75#define STRLEN(a)	wcslen(a)
76#define MEMCHR(a, b, c)	wmemchr(a, b, c)
77#define SCONV(a, b, loc)	__mbsconv(a, b, loc)
78#define STRCONST(a)	L ## a
79#define WDECL(a, b)	a ## w ## b
80#define END_OF_FILE	WEOF
81#define MULTI		0
82#else
83#define MCHAR_T		wchar_t
84#define CHAR_T		char
85#define STRLEN(a)	strlen(a)
86#define MEMCHR(a, b, c)	memchr(a, b, c)
87#define SCONV(a, b, loc)	__wcsconv(a, b, loc)
88#define STRCONST(a)	a
89#define WDECL(a, b)	a ## b
90#define END_OF_FILE	EOF
91#define MULTI		LONGINT
92#endif
93
94union arg {
95	int	intarg;
96	u_int	uintarg;
97	long	longarg;
98	u_long	ulongarg;
99	long long longlongarg;
100	unsigned long long ulonglongarg;
101	ptrdiff_t ptrdiffarg;
102	ssize_t ssizearg;
103	size_t	sizearg;
104	intmax_t intmaxarg;
105	uintmax_t uintmaxarg;
106	void	*pvoidarg;
107	char	*pchararg;
108	signed char *pschararg;
109	short	*pshortarg;
110	int	*pintarg;
111	long	*plongarg;
112	long long *plonglongarg;
113	ptrdiff_t *pptrdiffarg;
114	size_t	*psizearg;
115	intmax_t *pintmaxarg;
116#ifndef NO_FLOATING_POINT
117	double	doublearg;
118	long double longdoublearg;
119#endif
120	wint_t	wintarg;
121	wchar_t	*pwchararg;
122};
123
124/*
125 * Type ids for argument type table.
126 */
127enum typeid {
128	T_UNUSED = 0, TP_SHORT, T_INT, T_U_INT, TP_INT,
129	T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
130	T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SIZET,
131	T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
132	T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
133};
134
135#ifdef NARROW
136__printflike(3, 0)
137#endif
138static int	__sbprintf(FILE *, locale_t, const CHAR_T *, va_list);
139
140static CHAR_T	*__ujtoa(uintmax_t, CHAR_T *, int, int, const char *, int,
141		    char, const char *);
142static CHAR_T	*__ultoa(u_long, CHAR_T *, int, int, const char *, int,
143		    char, const char *);
144#ifndef NARROW
145static CHAR_T	*__mbsconv(char *, int, locale_t);
146static wint_t	__xfputwc(CHAR_T, FILE *, locale_t);
147#else
148static char	*__wcsconv(wchar_t *, int, locale_t);
149static int	__sprint(FILE *, struct __suio *);
150#endif
151static int	__find_arguments(const CHAR_T *, va_list, union arg **);
152static int	__grow_type_table(size_t, enum typeid **, size_t *);
153
154/*
155 * Helper function for `fprintf to unbuffered unix file': creates a
156 * temporary buffer.  We only work on write-only files; this avoids
157 * worries about ungetc buffers and so forth.
158 */
159static int
160__sbprintf(FILE *fp, locale_t loc, const CHAR_T *fmt, va_list ap)
161{
162	int ret;
163	FILE fake;
164	struct __sfileext fakeext;
165	unsigned char buf[BUFSIZ];
166
167	_DIAGASSERT(fp != NULL);
168	_DIAGASSERT(fmt != NULL);
169
170	_FILEEXT_SETUP(&fake, &fakeext);
171	memset(WCIO_GET(&fake), 0, sizeof(struct wchar_io_data));
172
173	/* copy the important variables */
174	fake._flags = fp->_flags & ~__SNBF;
175	fake._file = fp->_file;
176	fake._cookie = fp->_cookie;
177	fake._write = fp->_write;
178	fake._flush = fp->_flush;
179
180	/* set up the buffer */
181	fake._bf._base = fake._p = buf;
182	fake._bf._size = fake._w = sizeof(buf);
183	fake._lbfsize = 0;	/* not actually used, but Just In Case */
184
185	/* do the work, then copy any error status */
186	ret = WDECL(__vf,printf_unlocked_l)(&fake, loc, fmt, ap);
187	if (ret >= 0 && fflush(&fake))
188		ret = END_OF_FILE;
189	if (fake._flags & __SERR)
190		fp->_flags |= __SERR;
191	return ret;
192}
193
194#ifndef NARROW
195/*
196 * Like __fputwc, but handles fake string (__SSTR) files properly.
197 * File must already be locked.
198 */
199static wint_t
200__xfputwc(wchar_t wc, FILE *fp, locale_t loc)
201{
202	static const mbstate_t initial;
203	mbstate_t mbs;
204	char buf[MB_LEN_MAX];
205	struct __suio uio;
206	struct __siov iov;
207	size_t len;
208
209	if ((fp->_flags & __SSTR) == 0)
210		return __fputwc_unlock(wc, fp);
211
212	mbs = initial;
213	if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) {
214		fp->_flags |= __SERR;
215		return END_OF_FILE;
216	}
217	uio.uio_iov = &iov;
218	uio.uio_resid = len;
219	uio.uio_iovcnt = 1;
220	iov.iov_base = buf;
221	iov.iov_len = len;
222	return __sfvwrite(fp, &uio) != EOF ? (wint_t)wc : END_OF_FILE;
223}
224#else
225/*
226 * Flush out all the vectors defined by the given uio,
227 * then reset it so that it can be reused.
228 */
229static int
230__sprint(FILE *fp, struct __suio *uio)
231{
232	int err;
233
234	_DIAGASSERT(fp != NULL);
235	_DIAGASSERT(uio != NULL);
236
237	if (uio->uio_resid == 0) {
238		uio->uio_iovcnt = 0;
239		return 0;
240	}
241	err = __sfvwrite(fp, uio);
242	uio->uio_resid = 0;
243	uio->uio_iovcnt = 0;
244	return err;
245}
246#endif
247
248/*
249 * Macros for converting digits to letters and vice versa
250 */
251#define	to_digit(c)	((c) - '0')
252#define is_digit(c)	((unsigned)to_digit(c) <= 9)
253#define	to_char(n)	(CHAR_T)((n) + '0')
254
255/*
256 * Convert an unsigned long to ASCII for printf purposes, returning
257 * a pointer to the first character of the string representation.
258 * Octal numbers can be forced to have a leading zero; hex numbers
259 * use the given digits.
260 */
261static CHAR_T *
262__ultoa(u_long val, CHAR_T *endp, int base, int octzero, const char *xdigs,
263    int needgrp, char thousep, const char *grp)
264{
265	CHAR_T *cp = endp;
266	long sval;
267	int ndig;
268
269	/*
270	 * Handle the three cases separately, in the hope of getting
271	 * better/faster code.
272	 */
273	switch (base) {
274	case 10:
275		if (val < 10) {	/* many numbers are 1 digit */
276			*--cp = to_char(val);
277			return cp;
278		}
279		ndig = 0;
280		/*
281		 * On many machines, unsigned arithmetic is harder than
282		 * signed arithmetic, so we do at most one unsigned mod and
283		 * divide; this is sufficient to reduce the range of
284		 * the incoming value to where signed arithmetic works.
285		 */
286		if (val > LONG_MAX) {
287			*--cp = to_char(val % 10);
288			ndig++;
289			sval = val / 10;
290		} else
291			sval = val;
292		do {
293			*--cp = to_char(sval % 10);
294			ndig++;
295			/*
296			 * If (*grp == CHAR_MAX) then no more grouping
297			 * should be performed.
298			 */
299			if (needgrp && ndig == *grp
300			    && (unsigned char)*grp != (unsigned char)CHAR_MAX
301			    && sval > 9) {
302				*--cp = thousep;
303				ndig = 0;
304				/*
305				 * If (*(grp+1) == '\0') then we have to
306				 * use *grp character (last grouping rule)
307				 * for all next cases
308				 */
309				if (*(grp+1) != '\0')
310					grp++;
311			}
312			sval /= 10;
313		} while (sval != 0);
314		break;
315
316	case 8:
317		do {
318			*--cp = to_char(val & 7);
319			val >>= 3;
320		} while (val);
321		if (octzero && *cp != '0')
322			*--cp = '0';
323		break;
324
325	case 16:
326		do {
327			*--cp = xdigs[(size_t)val & 15];
328			val >>= 4;
329		} while (val);
330		break;
331
332	default:			/* oops */
333		abort();
334	}
335	return cp;
336}
337
338/* Identical to __ultoa, but for intmax_t. */
339static CHAR_T *
340__ujtoa(uintmax_t val, CHAR_T *endp, int base, int octzero,
341    const char *xdigs, int needgrp, char thousep, const char *grp)
342{
343	CHAR_T *cp = endp;
344	intmax_t sval;
345	int ndig;
346
347	/* quick test for small values; __ultoa is typically much faster */
348	/* (perhaps instead we should run until small, then call __ultoa?) */
349	if (val <= ULONG_MAX)
350		return __ultoa((u_long)val, endp, base, octzero, xdigs,
351		    needgrp, thousep, grp);
352	switch (base) {
353	case 10:
354		if (val < 10) {
355			*--cp = to_char(val % 10);
356			return cp;
357		}
358		ndig = 0;
359		if (val > INTMAX_MAX) {
360			*--cp = to_char(val % 10);
361			ndig++;
362			sval = val / 10;
363		} else
364			sval = val;
365		do {
366			*--cp = to_char(sval % 10);
367			ndig++;
368			/*
369			 * If (*grp == CHAR_MAX) then no more grouping
370			 * should be performed.
371			 */
372			if (needgrp
373			    && (unsigned char)*grp != (unsigned char)CHAR_MAX
374			    && ndig == *grp
375			    && sval > 9) {
376				*--cp = thousep;
377				ndig = 0;
378				/*
379				 * If (*(grp+1) == '\0') then we have to
380				 * use *grp character (last grouping rule)
381				 * for all next cases
382				 */
383				if (*(grp+1) != '\0')
384					grp++;
385			}
386			sval /= 10;
387		} while (sval != 0);
388		break;
389
390	case 8:
391		do {
392			*--cp = to_char(val & 7);
393			val >>= 3;
394		} while (val);
395		if (octzero && *cp != '0')
396			*--cp = '0';
397		break;
398
399	case 16:
400		do {
401			*--cp = xdigs[(size_t)val & 15];
402			val >>= 4;
403		} while (val);
404		break;
405
406	default:
407		abort();
408	}
409	return cp;
410}
411
412#ifndef NARROW
413/*
414 * Convert a multibyte character string argument for the %s format to a wide
415 * string representation. ``prec'' specifies the maximum number of bytes
416 * to output. If ``prec'' is greater than or equal to zero, we can't assume
417 * that the multibyte char. string ends in a null character.
418 */
419static wchar_t *
420__mbsconv(char *mbsarg, int prec, locale_t loc)
421{
422	static const mbstate_t initial;
423	mbstate_t mbs;
424	wchar_t *convbuf, *wcp;
425	const char *p;
426	size_t insize, nchars, nconv;
427
428	if (mbsarg == NULL)
429		return NULL;
430
431	/*
432	 * Supplied argument is a multibyte string; convert it to wide
433	 * characters first.
434	 */
435	if (prec >= 0) {
436		/*
437		 * String is not guaranteed to be NUL-terminated. Find the
438		 * number of characters to print.
439		 */
440		p = mbsarg;
441		insize = nchars = nconv = 0;
442		mbs = initial;
443		while (nchars != (size_t)prec) {
444			nconv = mbrlen_l(p, MB_CUR_MAX_L(loc), &mbs, loc);
445			if (nconv == 0 || nconv == (size_t)-1 ||
446			    nconv == (size_t)-2)
447				break;
448			p += nconv;
449			nchars++;
450			insize += nconv;
451		}
452		if (nconv == (size_t)-1 || nconv == (size_t)-2)
453			return NULL;
454	} else
455		insize = strlen(mbsarg);
456
457	/*
458	 * Allocate buffer for the result and perform the conversion,
459	 * converting at most `size' bytes of the input multibyte string to
460	 * wide characters for printing.
461	 */
462	convbuf = NULL;
463	errno = reallocarr(&convbuf, insize + 1, sizeof(*convbuf));
464	if (errno)
465		return NULL;
466	wcp = convbuf;
467	p = mbsarg;
468	mbs = initial;
469	nconv = 0;
470	while (insize != 0) {
471		nconv = mbrtowc_l(wcp, p, insize, &mbs, loc);
472		if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
473			break;
474		wcp++;
475		p += nconv;
476		insize -= nconv;
477	}
478	if (nconv == (size_t)-1 || nconv == (size_t)-2) {
479		int serrno = errno;
480		free(convbuf);
481		errno = serrno;
482		return NULL;
483	}
484	*wcp = L'\0';
485
486	return convbuf;
487}
488#else
489/*
490 * Convert a wide-character string argument for the %ls format to a multibyte
491 * string representation. If not -1, prec specifies the maximum number of
492 * bytes to output, and also means that we can't assume that the wide-char.
493 * string ends is null-terminated.
494 */
495static char *
496__wcsconv(wchar_t *wcsarg, int prec, locale_t loc)
497{
498	static const mbstate_t initial;
499	mbstate_t mbs;
500	char buf[MB_LEN_MAX];
501	wchar_t *p;
502	char *convbuf;
503	size_t clen, nbytes;
504
505	/* Allocate space for the maximum number of bytes we could output. */
506	if (prec < 0) {
507		p = wcsarg;
508		mbs = initial;
509		nbytes = wcsrtombs_l(NULL, (void *)&p, 0, &mbs, loc);
510		if (nbytes == (size_t)-1)
511			return NULL;
512	} else {
513		/*
514		 * Optimisation: if the output precision is small enough,
515		 * just allocate enough memory for the maximum instead of
516		 * scanning the string.
517		 */
518		if (prec < 128)
519			nbytes = prec;
520		else {
521			nbytes = 0;
522			p = wcsarg;
523			mbs = initial;
524			for (;;) {
525				clen = wcrtomb_l(buf, *p++, &mbs, loc);
526				if (clen == 0 || clen == (size_t)-1 ||
527				    nbytes + clen > (size_t)prec)
528					break;
529				nbytes += clen;
530			}
531		}
532	}
533	if ((convbuf = malloc(nbytes + 1)) == NULL)
534		return NULL;
535
536	/* Fill the output buffer. */
537	p = wcsarg;
538	mbs = initial;
539	if ((nbytes = wcsrtombs_l(convbuf, (void *)&p,
540	    nbytes, &mbs, loc)) == (size_t)-1) {
541		free(convbuf);
542		return NULL;
543	}
544	convbuf[nbytes] = '\0';
545	return convbuf;
546}
547#endif
548
549/*
550 * MT-safe version
551 */
552int
553WDECL(vf,printf)(FILE * __restrict fp, const CHAR_T * __restrict fmt0, va_list ap)
554{
555	int ret;
556
557	FLOCKFILE(fp);
558	ret = WDECL(__vf,printf_unlocked_l)(fp, _current_locale(), fmt0, ap);
559	FUNLOCKFILE(fp);
560	return ret;
561}
562
563int
564WDECL(vf,printf_l)(FILE * __restrict fp, locale_t loc, const CHAR_T * __restrict fmt0,
565    va_list ap)
566{
567	int ret;
568
569	FLOCKFILE(fp);
570	ret = WDECL(__vf,printf_unlocked_l)(fp, loc, fmt0, ap);
571	FUNLOCKFILE(fp);
572	return ret;
573}
574
575#ifndef NO_FLOATING_POINT
576
577#include <float.h>
578#include <math.h>
579#include "floatio.h"
580
581#define	DEFPREC		6
582
583static int exponent(CHAR_T *, int, int);
584
585#endif /* !NO_FLOATING_POINT */
586
587/*
588 * The size of the buffer we use as scratch space for integer
589 * conversions, among other things.  Technically, we would need the
590 * most space for base 10 conversions with thousands' grouping
591 * characters between each pair of digits.  100 bytes is a
592 * conservative overestimate even for a 128-bit uintmax_t.
593 */
594#define	BUF	100
595
596#define STATIC_ARG_TBL_SIZE 8           /* Size of static argument table. */
597
598/*
599 * Flags used during conversion.
600 */
601#define	ALT		0x001		/* alternate form */
602#define	LADJUST		0x004		/* left adjustment */
603#define	LONGDBL		0x008		/* long double */
604#define	LONGINT		0x010		/* long integer */
605#define	LLONGINT	0x020		/* long long integer */
606#define	SHORTINT	0x040		/* short integer */
607#define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
608#define	FPT		0x100		/* Floating point number */
609#define	GROUPING	0x200		/* use grouping ("'" flag) */
610					/* C99 additional size modifiers: */
611#define	SIZET		0x400		/* size_t */
612#define	PTRDIFFT	0x800		/* ptrdiff_t */
613#define	INTMAXT		0x1000		/* intmax_t */
614#define	CHARINT		0x2000		/* print char using int format */
615
616/*
617 * Non-MT-safe version
618 */
619int
620WDECL(__vf,printf_unlocked_l)(FILE *fp, locale_t loc, const CHAR_T *fmt0, va_list ap)
621{
622	CHAR_T *fmt;		/* format string */
623	int ch;			/* character from fmt */
624	int n, n2;		/* handy integer (short term usage) */
625	CHAR_T *cp;		/* handy char pointer (short term usage) */
626	int flags;		/* flags as above */
627	int ret;		/* return value accumulator */
628	int width;		/* width from format (%8d), or 0 */
629	int prec;		/* precision from format; <0 for N/A */
630	CHAR_T sign;		/* sign prefix (' ', '+', '-', or \0) */
631	char thousands_sep;	/* locale specific thousands separator */
632	const char *grouping;	/* locale specific numeric grouping rules */
633#ifndef NO_FLOATING_POINT
634	/*
635	 * We can decompose the printed representation of floating
636	 * point numbers into several parts, some of which may be empty:
637	 *
638	 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
639	 *    A       B     ---C---      D       E   F
640	 *
641	 * A:	'sign' holds this value if present; '\0' otherwise
642	 * B:	ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
643	 * C:	cp points to the string MMMNNN.  Leading and trailing
644	 *	zeros are not in the string and must be added.
645	 * D:	expchar holds this character; '\0' if no exponent, e.g. %f
646	 * F:	at least two digits for decimal, at least one digit for hex
647	 */
648	char *decimal_point;	/* locale specific decimal point */
649	int signflag;		/* true if float is negative */
650	union {			/* floating point arguments %[aAeEfFgG] */
651		double dbl;
652#ifdef WIDE_DOUBLE
653		long double ldbl;
654#endif
655	} fparg;
656	char *dtoaend;		/* pointer to end of converted digits */
657	char *dtoaresult;	/* buffer allocated by dtoa */
658	int expt;		/* integer value of exponent */
659	char expchar;		/* exponent character: [eEpP\0] */
660	int expsize;		/* character count for expstr */
661	int lead;		/* sig figs before decimal or group sep */
662	int ndig;		/* actual number of digits returned by dtoa */
663	CHAR_T expstr[MAXEXPDIG+2];	/* buffer for exponent string: e+ZZZ */
664	int nseps;		/* number of group separators with ' */
665	int nrepeats;		/* number of repeats of the last group */
666#endif
667	u_long	ulval;		/* integer arguments %[diouxX] */
668	uintmax_t ujval;	/* %j, %ll, %q, %t, %z integers */
669	int base;		/* base for [diouxX] conversion */
670	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
671	int realsz;		/* field size expanded by dprec, sign, etc */
672	int size;		/* size of converted field or string */
673	int prsize;             /* max size of printed field */
674	const char *xdigs;	/* digits for %[xX] conversion */
675#ifdef NARROW
676#define NIOV 8
677	struct __siov *iovp;	/* for PRINT macro */
678	struct __suio uio;	/* output information: summary */
679	struct __siov iov[NIOV];/* ... and individual io vectors */
680#else
681	int n3;
682#endif
683	CHAR_T buf[BUF];	/* buffer with space for digits of uintmax_t */
684	CHAR_T ox[2];		/* space for 0x hex-prefix */
685	union arg *argtable;	/* args, built due to positional arg */
686	union arg statargtable [STATIC_ARG_TBL_SIZE];
687	int nextarg;		/* 1-based argument index */
688	va_list orgap;		/* original argument pointer */
689	CHAR_T *convbuf;	/* multibyte to wide conversion result */
690
691	/*
692	 * Choose PADSIZE to trade efficiency vs. size.  If larger printf
693	 * fields occur frequently, increase PADSIZE and make the initialisers
694	 * below longer.
695	 */
696#define	PADSIZE	16		/* pad chunk size */
697	static CHAR_T blanks[PADSIZE] =
698	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
699	static CHAR_T zeroes[PADSIZE] =
700	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
701
702	static const char xdigs_lower[16] = "0123456789abcdef";
703	static const char xdigs_upper[16] = "0123456789ABCDEF";
704
705	/*
706	 * BEWARE, these `goto error' on error, PRINT uses `n2' and
707	 * PAD uses `n'.
708	 */
709#ifndef NARROW
710#define	PRINT(ptr, len)	do {			\
711	for (n3 = 0; n3 < (len); n3++)		\
712		if (__xfputwc((ptr)[n3], fp, loc) == END_OF_FILE) { \
713			fp->_flags |= __SERR;	\
714			goto error;		\
715		}				\
716} while (0)
717#define FLUSH()
718#else
719#define	PRINT(ptr, len) do { \
720	iovp->iov_base = __UNCONST(ptr); \
721	iovp->iov_len = (len); \
722	uio.uio_resid += (len); \
723	iovp++; \
724	if (++uio.uio_iovcnt >= NIOV) { \
725		if (__sprint(fp, &uio)) \
726			goto error; \
727		iovp = iov; \
728	} \
729} while (0)
730#define	FLUSH() do { \
731	if (uio.uio_resid && __sprint(fp, &uio)) \
732		goto error; \
733	uio.uio_iovcnt = 0; \
734	iovp = iov; \
735} while (0)
736#endif /* NARROW */
737
738#define	PAD(howmany, with)	do {		\
739	if ((n = (howmany)) > 0) {		\
740		while (n > PADSIZE) {		\
741			PRINT(with, PADSIZE);	\
742			n -= PADSIZE;		\
743		}				\
744		PRINT(with, n);			\
745	}					\
746} while (0)
747#define	PRINTANDPAD(p, ep, len, with) do {	\
748	ptrdiff_t td = (ep) - (p);		\
749	_DIAGASSERT(__type_fit(int, td));	\
750	n2 = (int)td;       			\
751	if (n2 > (len))				\
752		n2 = (len);			\
753	if (n2 > 0)				\
754		PRINT((p), n2);			\
755	PAD((len) - (n2 > 0 ? n2 : 0), (with));	\
756} while (0)
757
758	/*
759	 * Get the argument indexed by nextarg.   If the argument table is
760	 * built, use it to get the argument.  If its not, get the next
761	 * argument (and arguments must be gotten sequentially).
762	 */
763#define GETARG(type) \
764	((/*CONSTCOND*/argtable != NULL) ? *((type*)(void*)(&argtable[nextarg++])) : \
765	    (nextarg++, va_arg(ap, type)))
766
767	/*
768	 * To extend shorts properly, we need both signed and unsigned
769	 * argument extraction methods.
770	 */
771#define	SARG() \
772	(flags&LONGINT ? GETARG(long) : \
773	    flags&SHORTINT ? (long)(short)GETARG(int) : \
774	    flags&CHARINT ? (long)(signed char)GETARG(int) : \
775	    (long)GETARG(int))
776#define	UARG() \
777	(flags&LONGINT ? GETARG(u_long) : \
778	    flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
779	    flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
780	    (u_long)GETARG(u_int))
781#define	INTMAX_SIZE	(INTMAXT|SIZET|PTRDIFFT|LLONGINT)
782#define SJARG() \
783	(flags&INTMAXT ? GETARG(intmax_t) : \
784	    flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
785	    flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
786	    (intmax_t)GETARG(long long))
787#define	UJARG() \
788	(flags&INTMAXT ? GETARG(uintmax_t) : \
789	    flags&SIZET ? (uintmax_t)GETARG(size_t) : \
790	    flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
791	    (uintmax_t)GETARG(unsigned long long))
792
793	/*
794	 * Get * arguments, including the form *nn$.  Preserve the nextarg
795	 * that the argument can be gotten once the type is determined.
796	 */
797#define GETASTER(val) \
798	n2 = 0; \
799	cp = fmt; \
800	while (is_digit(*cp)) { \
801		n2 = 10 * n2 + to_digit(*cp); \
802		cp++; \
803	} \
804	if (*cp == '$') { \
805		int hold = nextarg; \
806		if (argtable == NULL) { \
807			argtable = statargtable; \
808			if (__find_arguments(fmt0, orgap, &argtable) == -1) \
809				goto oomem; \
810		} \
811		nextarg = n2; \
812		val = GETARG (int); \
813		nextarg = hold; \
814		fmt = ++cp; \
815	} else { \
816		val = GETARG (int); \
817	}
818
819	_DIAGASSERT(fp != NULL);
820	_DIAGASSERT(fmt0 != NULL);
821
822	_SET_ORIENTATION(fp, -1);
823
824	thousands_sep = '\0';
825	grouping = NULL;
826#ifndef NO_FLOATING_POINT
827	decimal_point = localeconv_l(loc)->decimal_point;
828	expsize = 0;		/* XXXGCC -Wuninitialized [sh3,m68000] */
829	ndig = -1;	/* XXX gcc */
830#endif
831	convbuf = NULL;
832	/* sorry, f{w,}printf(read_only_file, L"") returns {W,}EOF, not 0 */
833	if (cantwrite(fp)) {
834		errno = EBADF;
835		return END_OF_FILE;
836	}
837
838	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
839	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
840	    __sfileno(fp) != -1)
841		return __sbprintf(fp, loc, fmt0, ap);
842
843	fmt = (CHAR_T *)__UNCONST(fmt0);
844	argtable = NULL;
845	nextarg = 1;
846	va_copy(orgap, ap);
847#ifdef NARROW
848	uio.uio_iov = iovp = iov;
849	uio.uio_resid = 0;
850	uio.uio_iovcnt = 0;
851#endif
852	ret = 0;
853
854	/*
855	 * Scan the format for conversions (`%' character).
856	 */
857	for (;;) {
858		const CHAR_T *result;
859
860		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
861			continue;
862		_DIAGASSERT(__type_fit(int, fmt - cp));
863		if ((n = (int)(fmt - cp)) != 0) {
864			if ((unsigned)ret + n > INT_MAX) {
865				ret = END_OF_FILE;
866				goto error;
867			}
868			PRINT(cp, n);
869			ret += n;
870		}
871		if (ch == '\0')
872			goto done;
873		fmt++;		/* skip over '%' */
874
875		flags = 0;
876		dprec = 0;
877		width = 0;
878		prec = -1;
879		sign = '\0';
880		ox[1] = '\0';
881#ifndef NO_FLOATING_POINT
882		expchar = '\0';
883		lead = 0;
884		nseps = nrepeats = 0;
885#endif
886		ulval = 0;
887		ujval = 0;
888		xdigs = NULL;
889
890rflag:		ch = *fmt++;
891reswitch:	switch (ch) {
892		case ' ':
893			/*-
894			 * ``If the space and + flags both appear, the space
895			 * flag will be ignored.''
896			 *	-- ANSI X3J11
897			 */
898			if (!sign)
899				sign = ' ';
900			goto rflag;
901		case '#':
902			flags |= ALT;
903			goto rflag;
904		case '*':
905			/*-
906			 * ``A negative field width argument is taken as a
907			 * - flag followed by a positive field width.''
908			 *	-- ANSI X3J11
909			 * They don't exclude field widths read from args.
910			 */
911			GETASTER (width);
912			if (width >= 0)
913				goto rflag;
914			width = -width;
915			/* FALLTHROUGH */
916		case '-':
917			flags |= LADJUST;
918			goto rflag;
919		case '+':
920			sign = '+';
921			goto rflag;
922		case '\'':
923			thousands_sep = *(localeconv_l(loc)->thousands_sep);
924			grouping = localeconv_l(loc)->grouping;
925			/* Use grouping if defined by locale */
926			if (thousands_sep && grouping && *grouping)
927				flags |= GROUPING;
928			else {
929				thousands_sep = '\0';
930				grouping = NULL;
931			}
932			goto rflag;
933		case '.':
934			if ((ch = *fmt++) == '*') {
935				GETASTER (prec);
936				goto rflag;
937			}
938			prec = 0;
939			while (is_digit(ch)) {
940				prec = 10 * prec + to_digit(ch);
941				ch = *fmt++;
942			}
943			goto reswitch;
944		case '0':
945			/*-
946			 * ``Note that 0 is taken as a flag, not as the
947			 * beginning of a field width.''
948			 *	-- ANSI X3J11
949			 */
950			flags |= ZEROPAD;
951			goto rflag;
952		case '1': case '2': case '3': case '4':
953		case '5': case '6': case '7': case '8': case '9':
954			n = 0;
955			do {
956				n = 10 * n + to_digit(ch);
957				ch = *fmt++;
958			} while (is_digit(ch));
959			if (ch == '$') {
960				nextarg = n;
961				if (argtable == NULL) {
962					argtable = statargtable;
963					if (__find_arguments(fmt0, orgap,
964					    &argtable) == -1)
965						goto oomem;
966				}
967				goto rflag;
968			}
969			width = n;
970			goto reswitch;
971#ifndef NO_FLOATING_POINT
972		case 'L':
973			flags |= LONGDBL;
974			goto rflag;
975#endif
976		case 'h':
977			if (flags & SHORTINT) {
978				flags &= ~SHORTINT;
979				flags |= CHARINT;
980			} else
981				flags |= SHORTINT;
982			goto rflag;
983		case 'j':
984			flags |= INTMAXT;
985			goto rflag;
986		case 'l':
987			if (flags & LONGINT) {
988				flags &= ~LONGINT;
989				flags |= LLONGINT;
990			} else
991				flags |= LONGINT;
992			goto rflag;
993		case 'q':
994			flags |= LLONGINT;	/* not necessarily */
995			goto rflag;
996		case 't':
997			flags |= PTRDIFFT;
998			goto rflag;
999		case 'z':
1000			flags |= SIZET;
1001			goto rflag;
1002		case 'C':
1003			flags |= LONGINT;
1004			/*FALLTHROUGH*/
1005		case 'c':
1006#ifdef NARROW
1007			if (flags & LONGINT) {
1008				static const mbstate_t initial;
1009				mbstate_t mbs;
1010				size_t mbseqlen;
1011
1012				mbs = initial;
1013				mbseqlen = wcrtomb_l(buf,
1014				    (wchar_t)GETARG(wint_t), &mbs, loc);
1015				if (mbseqlen == (size_t)-1) {
1016					fp->_flags |= __SERR;
1017					goto error;
1018				}
1019				size = (int)mbseqlen;
1020			} else {
1021				*buf = GETARG(int);
1022				size = 1;
1023			}
1024#else
1025			if (flags & LONGINT)
1026				*buf = (wchar_t)GETARG(wint_t);
1027			else
1028				*buf = (wchar_t)btowc_l(GETARG(int), loc);
1029			size = 1;
1030#endif
1031			result = buf;
1032			sign = '\0';
1033			break;
1034		case 'D':
1035			flags |= LONGINT;
1036			/*FALLTHROUGH*/
1037		case 'd':
1038		case 'i':
1039			if (flags & INTMAX_SIZE) {
1040				ujval = SJARG();
1041				if ((intmax_t)ujval < 0) {
1042					ujval = -ujval;
1043					sign = '-';
1044				}
1045			} else {
1046				ulval = SARG();
1047				if ((long)ulval < 0) {
1048					ulval = -ulval;
1049					sign = '-';
1050				}
1051			}
1052			base = 10;
1053			goto number;
1054#ifndef NO_FLOATING_POINT
1055		case 'a':
1056		case 'A':
1057			if (ch == 'a') {
1058				ox[1] = 'x';
1059				xdigs = xdigs_lower;
1060				expchar = 'p';
1061			} else {
1062				ox[1] = 'X';
1063				xdigs = xdigs_upper;
1064				expchar = 'P';
1065			}
1066			if (prec >= 0)
1067				prec++;
1068#ifdef WIDE_DOUBLE
1069			if (flags & LONGDBL) {
1070				fparg.ldbl = GETARG(long double);
1071				dtoaresult =
1072				    __hldtoa(fparg.ldbl, xdigs, prec,
1073				        &expt, &signflag, &dtoaend);
1074			} else
1075#endif
1076			{
1077				fparg.dbl = GETARG(double);
1078				dtoaresult =
1079				    __hdtoa(fparg.dbl, xdigs, prec,
1080				        &expt, &signflag, &dtoaend);
1081			}
1082			if (dtoaresult == NULL)
1083				goto oomem;
1084
1085			if (prec < 0) {
1086				_DIAGASSERT(__type_fit(int,
1087				    dtoaend - dtoaresult));
1088				prec = (int)(dtoaend - dtoaresult);
1089			}
1090			if (expt == INT_MAX)
1091				ox[1] = '\0';
1092			_DIAGASSERT(__type_fit(int, dtoaend - dtoaresult));
1093			ndig = (int)(dtoaend - dtoaresult);
1094			if (convbuf != NULL)
1095				free(convbuf);
1096#ifndef NARROW
1097			result = convbuf = __mbsconv(dtoaresult, -1, loc);
1098#else
1099			/*XXX inefficient*/
1100			result = convbuf = strdup(dtoaresult);
1101#endif
1102			if (result == NULL)
1103				goto oomem;
1104			__freedtoa(dtoaresult);
1105			goto fp_common;
1106		case 'e':
1107		case 'E':
1108			expchar = ch;
1109			if (prec < 0)	/* account for digit before decpt */
1110				prec = DEFPREC + 1;
1111			else
1112				prec++;
1113			goto fp_begin;
1114		case 'f':
1115		case 'F':
1116			expchar = '\0';
1117			goto fp_begin;
1118		case 'g':
1119		case 'G':
1120			expchar = ch - ('g' - 'e');
1121			if (prec == 0)
1122				prec = 1;
1123fp_begin:
1124			if (prec < 0)
1125				prec = DEFPREC;
1126#ifdef WIDE_DOUBLE
1127			if (flags & LONGDBL) {
1128				fparg.ldbl = GETARG(long double);
1129				dtoaresult =
1130				    __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
1131				    &expt, &signflag, &dtoaend);
1132			} else
1133#endif
1134			{
1135				fparg.dbl = GETARG(double);
1136				dtoaresult =
1137				    __dtoa(fparg.dbl, expchar ? 2 : 3, prec,
1138				    &expt, &signflag, &dtoaend);
1139				if (expt == 9999)
1140					expt = INT_MAX;
1141			}
1142			if (dtoaresult == NULL)
1143				goto oomem;
1144			_DIAGASSERT(__type_fit(int, dtoaend - dtoaresult));
1145			ndig = (int)(dtoaend - dtoaresult);
1146			if (convbuf != NULL)
1147				free(convbuf);
1148#ifndef NARROW
1149			result = convbuf = __mbsconv(dtoaresult, -1, loc);
1150#else
1151			/*XXX inefficient*/
1152			result = convbuf = strdup(dtoaresult);
1153#endif
1154			if (result == NULL)
1155				goto oomem;
1156			__freedtoa(dtoaresult);
1157fp_common:
1158			if (signflag)
1159				sign = '-';
1160			if (expt == INT_MAX) {	/* inf or nan */
1161				if (*result == 'N') {
1162					result = (ch >= 'a') ? STRCONST("nan") :
1163					    STRCONST("NAN");
1164					sign = '\0';
1165				} else
1166					result = (ch >= 'a') ? STRCONST("inf") :
1167					    STRCONST("INF");
1168				size = 3;
1169				flags &= ~ZEROPAD;
1170				break;
1171			}
1172
1173			flags |= FPT;
1174			if (ch == 'g' || ch == 'G') {
1175				if (expt > -4 && expt <= prec) {
1176					/* Make %[gG] smell like %[fF] */
1177					expchar = '\0';
1178					if (flags & ALT)
1179						prec -= expt;
1180					else
1181						prec = ndig - expt;
1182					if (prec < 0)
1183						prec = 0;
1184				} else {
1185					/*
1186					 * Make %[gG] smell like %[eE], but
1187					 * trim trailing zeroes if no # flag.
1188					 */
1189					if (!(flags & ALT))
1190						prec = ndig;
1191				}
1192			}
1193			if (expchar) {
1194				expsize = exponent(expstr, expt - 1, expchar);
1195				size = expsize + prec;
1196				if (prec > 1 || flags & ALT)
1197					++size;
1198			} else {
1199				/* space for digits before decimal point */
1200				if (expt > 0)
1201					size = expt;
1202				else	/* "0" */
1203					size = 1;
1204				/* space for decimal pt and following digits */
1205				if (prec || flags & ALT)
1206					size += prec + 1;
1207				if (grouping && expt > 0) {
1208					/* space for thousands' grouping */
1209					nseps = nrepeats = 0;
1210					lead = expt;
1211					while ((unsigned char)*grouping
1212					    != (unsigned char)CHAR_MAX) {
1213						if (lead <= *grouping)
1214							break;
1215						lead -= *grouping;
1216						if (*(grouping+1)) {
1217							nseps++;
1218							grouping++;
1219						} else
1220							nrepeats++;
1221					}
1222					size += nseps + nrepeats;
1223				} else
1224					lead = expt;
1225			}
1226			break;
1227#endif /* !NO_FLOATING_POINT */
1228		case 'n':
1229			/*
1230			 * Assignment-like behavior is specified if the
1231			 * value overflows or is otherwise unrepresentable.
1232			 * C99 says to use `signed char' for %hhn conversions.
1233			 */
1234			if (flags & LLONGINT)
1235				*GETARG(long long *) = ret;
1236			else if (flags & SIZET)
1237				*GETARG(ssize_t *) = (ssize_t)ret;
1238			else if (flags & PTRDIFFT)
1239				*GETARG(ptrdiff_t *) = ret;
1240			else if (flags & INTMAXT)
1241				*GETARG(intmax_t *) = ret;
1242			else if (flags & LONGINT)
1243				*GETARG(long *) = ret;
1244			else if (flags & SHORTINT)
1245				*GETARG(short *) = ret;
1246			else if (flags & CHARINT)
1247				*GETARG(signed char *) = ret;
1248			else
1249				*GETARG(int *) = ret;
1250			continue;	/* no output */
1251		case 'O':
1252			flags |= LONGINT;
1253			/*FALLTHROUGH*/
1254		case 'o':
1255			if (flags & INTMAX_SIZE)
1256				ujval = UJARG();
1257			else
1258				ulval = UARG();
1259			base = 8;
1260			goto nosign;
1261		case 'p':
1262			/*-
1263			 * ``The argument shall be a pointer to void.  The
1264			 * value of the pointer is converted to a sequence
1265			 * of printable characters, in an implementation-
1266			 * defined manner.''
1267			 *	-- ANSI X3J11
1268			 */
1269			ujval = (uintmax_t)(uintptr_t)GETARG(void *);
1270			base = 16;
1271			xdigs = xdigs_lower;
1272			flags = flags | INTMAXT;
1273			ox[1] = 'x';
1274			goto nosign;
1275		case 'S':
1276			flags |= LONGINT;
1277			/*FALLTHROUGH*/
1278		case 's':
1279			if ((flags & LONGINT) != MULTI) {
1280				if ((result = GETARG(CHAR_T *)) == NULL)
1281					result = STRCONST("(null)");
1282			} else {
1283				MCHAR_T *mc;
1284
1285				if (convbuf != NULL)
1286					free(convbuf);
1287				if ((mc = GETARG(MCHAR_T *)) == NULL)
1288					result = STRCONST("(null)");
1289				else {
1290					convbuf = SCONV(mc, prec, loc);
1291					if (convbuf == NULL) {
1292						fp->_flags |= __SERR;
1293						goto error;
1294					}
1295					result = convbuf;
1296				}
1297			}
1298
1299			if (prec >= 0) {
1300				/*
1301				 * can't use STRLEN; can only look for the
1302				 * NUL in the first `prec' characters, and
1303				 * STRLEN() will go further.
1304				 */
1305				const CHAR_T *p =
1306				    MEMCHR(result, 0, (size_t)prec);
1307
1308				if (p != NULL) {
1309					_DIAGASSERT(__type_fit(int,
1310					    p - result));
1311					size = (int)(p - result);
1312					if (size > prec)
1313						size = prec;
1314				} else
1315					size = prec;
1316			} else {
1317				size_t rlen = STRLEN(result);
1318				_DIAGASSERT(__type_fit(int, rlen));
1319				size = (int)rlen;
1320			}
1321			sign = '\0';
1322			break;
1323		case 'U':
1324			flags |= LONGINT;
1325			/*FALLTHROUGH*/
1326		case 'u':
1327			if (flags & INTMAX_SIZE)
1328				ujval = UJARG();
1329			else
1330				ulval = UARG();
1331			base = 10;
1332			goto nosign;
1333		case 'X':
1334			xdigs = xdigs_upper;
1335			goto hex;
1336		case 'x':
1337			xdigs = xdigs_lower;
1338hex:
1339			if (flags & INTMAX_SIZE)
1340				ujval = UJARG();
1341			else
1342				ulval = UARG();
1343			base = 16;
1344			/* leading 0x/X only if non-zero */
1345			if (flags & ALT &&
1346			    (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1347				ox[1] = ch;
1348
1349			flags &= ~GROUPING;
1350			/* unsigned conversions */
1351nosign:			sign = '\0';
1352			/*-
1353			 * ``... diouXx conversions ... if a precision is
1354			 * specified, the 0 flag will be ignored.''
1355			 *	-- ANSI X3J11
1356			 */
1357number:			if ((dprec = prec) >= 0)
1358				flags &= ~ZEROPAD;
1359
1360			/*-
1361			 * ``The result of converting a zero value with an
1362			 * explicit precision of zero is no characters.''
1363			 *	-- ANSI X3J11
1364			 *
1365			 * ``The C Standard is clear enough as is.  The call
1366			 * printf("%#.0o", 0) should print 0.''
1367			 *	-- Defect Report #151
1368			 */
1369			result = cp = buf + BUF;
1370			if (flags & INTMAX_SIZE) {
1371				if (ujval != 0 || prec != 0 ||
1372				    (flags & ALT && base == 8))
1373					result = __ujtoa(ujval, cp, base,
1374					    flags & ALT, xdigs,
1375					    flags & GROUPING, thousands_sep,
1376					    grouping);
1377			} else {
1378				if (ulval != 0 || prec != 0 ||
1379				    (flags & ALT && base == 8))
1380					result = __ultoa(ulval, cp, base,
1381					    flags & ALT, xdigs,
1382					    flags & GROUPING, thousands_sep,
1383					    grouping);
1384			}
1385			_DIAGASSERT(__type_fit(int, buf + BUF - result));
1386			size = (int)(buf + BUF - result);
1387			if (size > BUF)	/* should never happen */
1388				abort();
1389			break;
1390		default:	/* "%?" prints ?, unless ? is NUL */
1391			if (ch == '\0')
1392				goto done;
1393			/* pretend it was %c with argument ch */
1394			*buf = ch;
1395			result = buf;
1396			size = 1;
1397			sign = '\0';
1398			break;
1399		}
1400
1401		/*
1402		 * All reasonable formats wind up here.  At this point, `result'
1403		 * points to a string which (if not flags&LADJUST) should be
1404		 * padded out to `width' places.  If flags&ZEROPAD, it should
1405		 * first be prefixed by any sign or other prefix; otherwise,
1406		 * it should be blank padded before the prefix is emitted.
1407		 * After any left-hand padding and prefixing, emit zeroes
1408		 * required by a decimal [diouxX] precision, then print the
1409		 * string proper, then emit zeroes required by any leftover
1410		 * floating precision; finally, if LADJUST, pad with blanks.
1411		 *
1412		 * Compute actual size, so we know how much to pad.
1413		 * size excludes decimal prec; realsz includes it.
1414		 */
1415		realsz = dprec > size ? dprec : size;
1416		if (sign)
1417			realsz++;
1418		if (ox[1])
1419			realsz += 2;
1420
1421		prsize = width > realsz ? width : realsz;
1422		if ((unsigned)ret + prsize > INT_MAX) {
1423			ret = END_OF_FILE;
1424			goto error;
1425		}
1426
1427		/* right-adjusting blank padding */
1428		if ((flags & (LADJUST|ZEROPAD)) == 0)
1429			PAD(width - realsz, blanks);
1430
1431		/* prefix */
1432		if (sign)
1433			PRINT(&sign, 1);
1434
1435		if (ox[1]) {	/* ox[1] is either x, X, or \0 */
1436			ox[0] = '0';
1437			PRINT(ox, 2);
1438		}
1439
1440		/* right-adjusting zero padding */
1441		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1442			PAD(width - realsz, zeroes);
1443
1444		/* leading zeroes from decimal precision */
1445		PAD(dprec - size, zeroes);
1446
1447		/* the string or number proper */
1448#ifndef NO_FLOATING_POINT
1449		if ((flags & FPT) == 0) {
1450			PRINT(result, size);
1451		} else {	/* glue together f_p fragments */
1452			if (!expchar) {	/* %[fF] or sufficiently short %[gG] */
1453				if (expt <= 0) {
1454					PRINT(zeroes, 1);
1455					if (prec || flags & ALT)
1456						PRINT(decimal_point, 1);
1457					PAD(-expt, zeroes);
1458					/* already handled initial 0's */
1459					prec += expt;
1460				} else {
1461					PRINTANDPAD(result, convbuf + ndig,
1462					    lead, zeroes);
1463					result += lead;
1464					if (grouping) {
1465						while (nseps>0 || nrepeats>0) {
1466							if (nrepeats > 0)
1467								nrepeats--;
1468							else {
1469								grouping--;
1470								nseps--;
1471							}
1472							PRINT(&thousands_sep,
1473							    1);
1474							PRINTANDPAD(result,
1475							    convbuf + ndig,
1476							    *grouping, zeroes);
1477							result += *grouping;
1478						}
1479						if (result > convbuf + ndig)
1480							result = convbuf + ndig;
1481					}
1482					if (prec || flags & ALT) {
1483						buf[0] = *decimal_point;
1484						PRINT(buf, 1);
1485					}
1486				}
1487				PRINTANDPAD(result, convbuf + ndig, prec,
1488				    zeroes);
1489			} else {	/* %[eE] or sufficiently long %[gG] */
1490				if (prec > 1 || flags & ALT) {
1491					buf[0] = *result++;
1492					buf[1] = *decimal_point;
1493					PRINT(buf, 2);
1494					PRINT(result, ndig-1);
1495					PAD(prec - ndig, zeroes);
1496				} else	/* XeYYY */
1497					PRINT(result, 1);
1498				PRINT(expstr, expsize);
1499			}
1500		}
1501#else
1502		PRINT(result, size);
1503#endif
1504		/* left-adjusting padding (always blank) */
1505		if (flags & LADJUST)
1506			PAD(width - realsz, blanks);
1507
1508		/* finally, adjust ret */
1509		ret += prsize;
1510		FLUSH();
1511	}
1512done:
1513	FLUSH();
1514error:
1515	va_end(orgap);
1516	if (convbuf != NULL)
1517		free(convbuf);
1518	if (__sferror(fp))
1519		ret = END_OF_FILE;
1520	if ((argtable != NULL) && (argtable != statargtable))
1521		free (argtable);
1522	return ret;
1523	/* NOTREACHED */
1524oomem:
1525	errno = ENOMEM;
1526	ret = END_OF_FILE;
1527	goto error;
1528}
1529
1530/*
1531 * Find all arguments when a positional parameter is encountered.  Returns a
1532 * table, indexed by argument number, of pointers to each arguments.  The
1533 * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1534 * It will be replaces with a malloc-ed one if it overflows.
1535 */
1536static int
1537__find_arguments(const CHAR_T *fmt0, va_list ap, union arg **argtable)
1538{
1539	CHAR_T *fmt;		/* format string */
1540	int ch;			/* character from fmt */
1541	size_t n, n2;		/* handy index (short term usage) */
1542	CHAR_T *cp;		/* handy char pointer (short term usage) */
1543	int flags;		/* flags as above */
1544	enum typeid *typetable; /* table of types */
1545	enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1546	size_t tablesize;	/* current size of type table */
1547	size_t tablemax;	/* largest used index in table */
1548	size_t nextarg;		/* 1-based argument index */
1549	size_t nitems;		/* number of items we picked from the stack */
1550
1551	/*
1552	 * Add an argument type to the table, expanding if necessary.
1553	 * Check for overflow.
1554	 */
1555#define ADDTYPE(type) \
1556	do { \
1557		if (nextarg > SIZE_MAX / sizeof(**argtable)) { \
1558			if (typetable != stattypetable) \
1559				free(typetable); \
1560			return -1; \
1561		} \
1562		if (nextarg >= tablesize) \
1563			if (__grow_type_table(nextarg, &typetable, \
1564			    &tablesize) == -1) \
1565				return -1; \
1566		if (nextarg > tablemax) \
1567			tablemax = nextarg; \
1568		typetable[nextarg++] = type; \
1569		nitems++; \
1570	} while (0)
1571
1572#define	ADDSARG() \
1573	do { \
1574		if (flags & INTMAXT)  \
1575			ADDTYPE(T_INTMAXT); \
1576		else if (flags & SIZET)  \
1577			ADDTYPE(T_SSIZET); \
1578		else if (flags & PTRDIFFT) \
1579			ADDTYPE(T_PTRDIFFT); \
1580		else if (flags & LLONGINT) \
1581			ADDTYPE(T_LLONG); \
1582		else if (flags & LONGINT) \
1583			ADDTYPE(T_LONG); \
1584		else \
1585			ADDTYPE(T_INT); \
1586	} while (0)
1587
1588#define	ADDUARG() \
1589	do { \
1590		if (flags & INTMAXT)  \
1591			ADDTYPE(T_UINTMAXT); \
1592		else if (flags & SIZET)  \
1593			ADDTYPE(T_SIZET); \
1594		else if (flags & PTRDIFFT) \
1595			ADDTYPE(T_PTRDIFFT); \
1596		else if (flags & LLONGINT) \
1597			ADDTYPE(T_U_LLONG); \
1598		else if (flags & LONGINT) \
1599			ADDTYPE(T_U_LONG); \
1600		else \
1601			ADDTYPE(T_U_INT); \
1602	} while (0)
1603	/*
1604	 * Add * arguments to the type array.
1605	 */
1606#define ADDASTER() \
1607	n2 = 0; \
1608	cp = fmt; \
1609	while (is_digit(*cp)) { \
1610		n2 = 10 * n2 + to_digit(*cp); \
1611		cp++; \
1612	} \
1613	if (*cp == '$') { \
1614		size_t hold = nextarg; \
1615		nextarg = n2; \
1616		ADDTYPE(T_INT); \
1617		nextarg = hold; \
1618		fmt = ++cp; \
1619	} else { \
1620		ADDTYPE(T_INT); \
1621	}
1622	fmt = (CHAR_T *)__UNCONST(fmt0);
1623	memset(stattypetable, 0, sizeof(stattypetable));
1624	typetable = stattypetable;
1625	tablesize = STATIC_ARG_TBL_SIZE;
1626	tablemax = 0;
1627	nextarg = 1;
1628	nitems = 1;
1629
1630	/*
1631	 * Scan the format for conversions (`%' character).
1632	 */
1633	for (;;) {
1634		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1635			/* void */;
1636		if (ch == '\0')
1637			goto done;
1638		fmt++;		/* skip over '%' */
1639
1640		flags = 0;
1641
1642rflag:		ch = *fmt++;
1643reswitch:	switch (ch) {
1644		case ' ':
1645		case '#':
1646			goto rflag;
1647		case '*':
1648			ADDASTER ();
1649			goto rflag;
1650		case '-':
1651		case '+':
1652		case '\'':
1653			goto rflag;
1654		case '.':
1655			if ((ch = *fmt++) == '*') {
1656				ADDASTER ();
1657				goto rflag;
1658			}
1659			while (is_digit(ch)) {
1660				ch = *fmt++;
1661			}
1662			goto reswitch;
1663		case '0':
1664			goto rflag;
1665		case '1': case '2': case '3': case '4':
1666		case '5': case '6': case '7': case '8': case '9':
1667			n = 0;
1668			do {
1669				n = 10 * n + to_digit(ch);
1670				ch = *fmt++;
1671			} while (is_digit(ch));
1672			if (ch == '$') {
1673				nextarg = n;
1674				goto rflag;
1675			}
1676			goto reswitch;
1677#ifndef NO_FLOATING_POINT
1678		case 'L':
1679			flags |= LONGDBL;
1680			goto rflag;
1681#endif
1682		case 'h':
1683			if (flags & SHORTINT) {
1684				flags &= ~SHORTINT;
1685				flags |= CHARINT;
1686			} else
1687				flags |= SHORTINT;
1688			goto rflag;
1689		case 'j':
1690			flags |= INTMAXT;
1691			goto rflag;
1692		case 'l':
1693			if (flags & LONGINT) {
1694				flags &= ~LONGINT;
1695				flags |= LLONGINT;
1696			} else
1697				flags |= LONGINT;
1698			goto rflag;
1699		case 'q':
1700			flags |= LLONGINT;	/* not necessarily */
1701			goto rflag;
1702		case 't':
1703			flags |= PTRDIFFT;
1704			goto rflag;
1705		case 'z':
1706			flags |= SIZET;
1707			goto rflag;
1708		case 'C':
1709			flags |= LONGINT;
1710			/*FALLTHROUGH*/
1711		case 'c':
1712			if (flags & LONGINT)
1713				ADDTYPE(T_WINT);
1714			else
1715				ADDTYPE(T_INT);
1716			break;
1717		case 'D':
1718			flags |= LONGINT;
1719			/*FALLTHROUGH*/
1720		case 'd':
1721		case 'i':
1722			ADDSARG();
1723			break;
1724#ifndef NO_FLOATING_POINT
1725		case 'a':
1726		case 'A':
1727		case 'e':
1728		case 'E':
1729		case 'f':
1730		case 'g':
1731		case 'G':
1732			if (flags & LONGDBL)
1733				ADDTYPE(T_LONG_DOUBLE);
1734			else
1735				ADDTYPE(T_DOUBLE);
1736			break;
1737#endif /* !NO_FLOATING_POINT */
1738		case 'n':
1739			if (flags & INTMAXT)
1740				ADDTYPE(TP_INTMAXT);
1741			else if (flags & PTRDIFFT)
1742				ADDTYPE(TP_PTRDIFFT);
1743			else if (flags & SIZET)
1744				ADDTYPE(TP_SIZET);
1745			else if (flags & LLONGINT)
1746				ADDTYPE(TP_LLONG);
1747			else if (flags & LONGINT)
1748				ADDTYPE(TP_LONG);
1749			else if (flags & SHORTINT)
1750				ADDTYPE(TP_SHORT);
1751			else if (flags & CHARINT)
1752				ADDTYPE(TP_SCHAR);
1753			else
1754				ADDTYPE(TP_INT);
1755			continue;	/* no output */
1756		case 'O':
1757			flags |= LONGINT;
1758			/*FALLTHROUGH*/
1759		case 'o':
1760			ADDUARG();
1761			break;
1762		case 'p':
1763			ADDTYPE(TP_VOID);
1764			break;
1765		case 'S':
1766			flags |= LONGINT;
1767			/*FALLTHROUGH*/
1768		case 's':
1769			if (flags & LONGINT)
1770				ADDTYPE(TP_WCHAR);
1771			else
1772				ADDTYPE(TP_CHAR);
1773			break;
1774		case 'U':
1775			flags |= LONGINT;
1776			/*FALLTHROUGH*/
1777		case 'u':
1778		case 'X':
1779		case 'x':
1780			ADDUARG();
1781			break;
1782		default:	/* "%?" prints ?, unless ? is NUL */
1783			if (ch == '\0')
1784				goto done;
1785			break;
1786		}
1787	}
1788done:
1789	/*
1790	 * nitems contains the number of arguments we picked from the stack.
1791	 * If tablemax is larger, this means that some positional argument,
1792	 * tried to pick an argument the number of arguments possibly supplied.
1793	 * Since positional arguments are typically used to swap the order of
1794	 * the printf arguments and not to pick random arguments from strange
1795	 * positions in the stack, we assume that if the positional argument
1796	 * is trying to pick beyond the end of arguments, then this is wrong.
1797	 * Alternatively we could find a way to figure out when va_arg() runs
1798	 * out, but how to do that?
1799	 */
1800	if (nitems < tablemax) {
1801		if (typetable != stattypetable)
1802			free(typetable);
1803		return -1;
1804	}
1805	/*
1806	 * Build the argument table.
1807	 */
1808	if (tablemax >= STATIC_ARG_TBL_SIZE) {
1809		*argtable = malloc(sizeof(**argtable) * (tablemax + 1));
1810		if (*argtable == NULL) {
1811			free(typetable);
1812			return -1;
1813		}
1814	}
1815
1816	(*argtable) [0].intarg = 0;
1817	for (n = 1; n <= tablemax; n++) {
1818		switch (typetable [n]) {
1819		    case T_UNUSED: /* whoops! */
1820			(*argtable) [n].intarg = va_arg (ap, int);
1821			break;
1822		    case TP_SCHAR:
1823			(*argtable) [n].pschararg = va_arg (ap, signed char *);
1824			break;
1825		    case TP_SHORT:
1826			(*argtable) [n].pshortarg = va_arg (ap, short *);
1827			break;
1828		    case T_INT:
1829			(*argtable) [n].intarg = va_arg (ap, int);
1830			break;
1831		    case T_U_INT:
1832			(*argtable) [n].uintarg = va_arg (ap, unsigned int);
1833			break;
1834		    case TP_INT:
1835			(*argtable) [n].pintarg = va_arg (ap, int *);
1836			break;
1837		    case T_LONG:
1838			(*argtable) [n].longarg = va_arg (ap, long);
1839			break;
1840		    case T_U_LONG:
1841			(*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1842			break;
1843		    case TP_LONG:
1844			(*argtable) [n].plongarg = va_arg (ap, long *);
1845			break;
1846		    case T_LLONG:
1847			(*argtable) [n].longlongarg = va_arg (ap, long long);
1848			break;
1849		    case T_U_LLONG:
1850			(*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1851			break;
1852		    case TP_LLONG:
1853			(*argtable) [n].plonglongarg = va_arg (ap, long long *);
1854			break;
1855		    case T_PTRDIFFT:
1856			(*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1857			break;
1858		    case TP_PTRDIFFT:
1859			(*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1860			break;
1861		    case T_SSIZET:
1862			(*argtable) [n].ssizearg = va_arg (ap, ssize_t);
1863			break;
1864		    case T_SIZET:
1865			(*argtable) [n].sizearg = va_arg (ap, size_t);
1866			break;
1867		    case TP_SIZET:
1868			(*argtable) [n].psizearg = va_arg (ap, size_t *);
1869			break;
1870		    case T_INTMAXT:
1871			(*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1872			break;
1873		    case T_UINTMAXT:
1874			(*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1875			break;
1876		    case TP_INTMAXT:
1877			(*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1878			break;
1879		    case T_DOUBLE:
1880#ifndef NO_FLOATING_POINT
1881			(*argtable) [n].doublearg = va_arg (ap, double);
1882#endif
1883			break;
1884		    case T_LONG_DOUBLE:
1885#ifndef NO_FLOATING_POINT
1886			(*argtable) [n].longdoublearg = va_arg (ap, long double);
1887#endif
1888			break;
1889		    case TP_CHAR:
1890			(*argtable) [n].pchararg = va_arg (ap, char *);
1891			break;
1892		    case TP_VOID:
1893			(*argtable) [n].pvoidarg = va_arg (ap, void *);
1894			break;
1895		    case T_WINT:
1896			(*argtable) [n].wintarg = va_arg (ap, wint_t);
1897			break;
1898		    case TP_WCHAR:
1899			(*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
1900			break;
1901		}
1902	}
1903
1904	if (typetable != stattypetable)
1905		free (typetable);
1906	return 0;
1907}
1908
1909/*
1910 * Increase the size of the type table.
1911 */
1912static int
1913__grow_type_table (size_t nextarg, enum typeid **typetable, size_t *tablesize)
1914{
1915	enum typeid *const oldtable = *typetable;
1916	const size_t oldsize = *tablesize;
1917	enum typeid *newtable;
1918	size_t newsize = oldsize * 2;
1919
1920	if (newsize < nextarg + 1)
1921		newsize = nextarg + 1;
1922	if (oldsize == STATIC_ARG_TBL_SIZE) {
1923		newtable = NULL;
1924		errno = reallocarr(&newtable, newsize, sizeof(*newtable));
1925		if (errno)
1926			return -1;
1927		memcpy(newtable, oldtable, oldsize * sizeof(*newtable));
1928	} else {
1929		newtable = oldtable;
1930		errno = reallocarr(&newtable, newsize, sizeof(*newtable));
1931		if (errno) {
1932			int serrno = errno;
1933			free(oldtable);
1934			errno = serrno;
1935			return -1;
1936		}
1937	}
1938	memset(&newtable[oldsize], 0, (newsize - oldsize) * sizeof(*newtable));
1939
1940	*typetable = newtable;
1941	*tablesize = newsize;
1942	return 0;
1943}
1944
1945#ifndef NO_FLOATING_POINT
1946
1947static int
1948exponent(CHAR_T *p0, int expo, int fmtch)
1949{
1950	CHAR_T *p, *t;
1951	CHAR_T expbuf[MAXEXPDIG];
1952
1953	p = p0;
1954	*p++ = fmtch;
1955	if (expo < 0) {
1956		expo = -expo;
1957		*p++ = '-';
1958	}
1959	else
1960		*p++ = '+';
1961	t = expbuf + MAXEXPDIG;
1962	if (expo > 9) {
1963		do {
1964			*--t = to_char(expo % 10);
1965		} while ((expo /= 10) > 9);
1966		*--t = to_char(expo);
1967		for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1968	}
1969	else {
1970		/*
1971		 * Exponents for decimal floating point conversions
1972		 * (%[eEgG]) must be at least two characters long,
1973		 * whereas exponents for hexadecimal conversions can
1974		 * be only one character long.
1975		 */
1976		if (fmtch == 'e' || fmtch == 'E')
1977			*p++ = '0';
1978		*p++ = to_char(expo);
1979	}
1980	_DIAGASSERT(__type_fit(int, p - p0));
1981	return (int)(p - p0);
1982}
1983#endif /* !NO_FLOATING_POINT */
1984