xref: /netbsd-src/lib/libc/stdio/vfwprintf.c (revision ace5b9b5feb0e7608bd2da7a617428d2e1cf8aa3)
1*ace5b9b5Schristos /*	$NetBSD: vfwprintf.c,v 1.41 2024/01/20 14:52:49 christos Exp $	*/
2f432bbb6Schristos 
3f432bbb6Schristos /*-
4f432bbb6Schristos  * Copyright (c) 1990, 1993
5f432bbb6Schristos  *	The Regents of the University of California.  All rights reserved.
6f432bbb6Schristos  *
7f432bbb6Schristos  * This code is derived from software contributed to Berkeley by
8f432bbb6Schristos  * Chris Torek.
9f432bbb6Schristos  *
10f432bbb6Schristos  * Redistribution and use in source and binary forms, with or without
11f432bbb6Schristos  * modification, are permitted provided that the following conditions
12f432bbb6Schristos  * are met:
13f432bbb6Schristos  * 1. Redistributions of source code must retain the above copyright
14f432bbb6Schristos  *    notice, this list of conditions and the following disclaimer.
15f432bbb6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16f432bbb6Schristos  *    notice, this list of conditions and the following disclaimer in the
17f432bbb6Schristos  *    documentation and/or other materials provided with the distribution.
1809314539Schristos  * 3. Neither the name of the University nor the names of its contributors
19f432bbb6Schristos  *    may be used to endorse or promote products derived from this software
20f432bbb6Schristos  *    without specific prior written permission.
21f432bbb6Schristos  *
22f432bbb6Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23f432bbb6Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24f432bbb6Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f432bbb6Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26f432bbb6Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27f432bbb6Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28f432bbb6Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29f432bbb6Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30f432bbb6Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31f432bbb6Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32f432bbb6Schristos  * SUCH DAMAGE.
33f432bbb6Schristos  */
34f432bbb6Schristos 
35f432bbb6Schristos #include <sys/cdefs.h>
36f432bbb6Schristos #if defined(LIBC_SCCS) && !defined(lint)
37f432bbb6Schristos #if 0
38f432bbb6Schristos static char sccsid[] = "@(#)vfprintf.c	8.1 (Berkeley) 6/4/93";
3909314539Schristos __FBSDID("$FreeBSD: src/lib/libc/stdio/vfwprintf.c,v 1.27 2007/01/09 00:28:08 imp Exp $");
40f432bbb6Schristos #else
41*ace5b9b5Schristos __RCSID("$NetBSD: vfwprintf.c,v 1.41 2024/01/20 14:52:49 christos Exp $");
42f432bbb6Schristos #endif
43f432bbb6Schristos #endif /* LIBC_SCCS and not lint */
44f432bbb6Schristos 
45f432bbb6Schristos /*
4609314539Schristos  * Actual {w,}printf innards.
47f432bbb6Schristos  */
48f432bbb6Schristos 
49f432bbb6Schristos #include "namespace.h"
50f432bbb6Schristos #include <sys/types.h>
51f432bbb6Schristos 
52f432bbb6Schristos #include <assert.h>
53f432bbb6Schristos #include <ctype.h>
54f432bbb6Schristos #include <limits.h>
55f432bbb6Schristos #include <locale.h>
56f432bbb6Schristos #include <stdarg.h>
57f432bbb6Schristos #include <stddef.h>
58f432bbb6Schristos #include <stdint.h>
59f432bbb6Schristos #include <stdio.h>
60f432bbb6Schristos #include <stdlib.h>
61f432bbb6Schristos #include <string.h>
62f432bbb6Schristos #include <errno.h>
63f432bbb6Schristos #include <wchar.h>
64f432bbb6Schristos #include <wctype.h>
65f432bbb6Schristos 
66f432bbb6Schristos #include "reentrant.h"
672561b634Sjoerg #include "setlocale_local.h"
68f432bbb6Schristos #include "local.h"
69f432bbb6Schristos #include "extern.h"
70f432bbb6Schristos #include "fvwrite.h"
71f432bbb6Schristos 
7209314539Schristos #ifndef NARROW
7309314539Schristos #define MCHAR_T		char
7409314539Schristos #define CHAR_T		wchar_t
7509314539Schristos #define STRLEN(a)	wcslen(a)
7609314539Schristos #define MEMCHR(a, b, c)	wmemchr(a, b, c)
772561b634Sjoerg #define SCONV(a, b, loc)	__mbsconv(a, b, loc)
7809314539Schristos #define STRCONST(a)	L ## a
7909314539Schristos #define WDECL(a, b)	a ## w ## b
8009314539Schristos #define END_OF_FILE	WEOF
8109314539Schristos #define MULTI		0
8209314539Schristos #else
8309314539Schristos #define MCHAR_T		wchar_t
8409314539Schristos #define CHAR_T		char
8509314539Schristos #define STRLEN(a)	strlen(a)
8609314539Schristos #define MEMCHR(a, b, c)	memchr(a, b, c)
872561b634Sjoerg #define SCONV(a, b, loc)	__wcsconv(a, b, loc)
8809314539Schristos #define STRCONST(a)	a
8909314539Schristos #define WDECL(a, b)	a ## b
9009314539Schristos #define END_OF_FILE	EOF
91b0288bdeSaymeric #define MULTI		LONGINT
9209314539Schristos #endif
9309314539Schristos 
94f432bbb6Schristos union arg {
95f432bbb6Schristos 	int	intarg;
96f432bbb6Schristos 	u_int	uintarg;
97f432bbb6Schristos 	long	longarg;
98f432bbb6Schristos 	u_long	ulongarg;
9909314539Schristos 	long long longlongarg;
10009314539Schristos 	unsigned long long ulonglongarg;
101f432bbb6Schristos 	ptrdiff_t ptrdiffarg;
102dbf72b0eSroy 	ssize_t ssizearg;
103f432bbb6Schristos 	size_t	sizearg;
104f432bbb6Schristos 	intmax_t intmaxarg;
105f432bbb6Schristos 	uintmax_t uintmaxarg;
106f432bbb6Schristos 	void	*pvoidarg;
107f432bbb6Schristos 	char	*pchararg;
108f432bbb6Schristos 	signed char *pschararg;
109f432bbb6Schristos 	short	*pshortarg;
110f432bbb6Schristos 	int	*pintarg;
111f432bbb6Schristos 	long	*plongarg;
11209314539Schristos 	long long *plonglongarg;
113f432bbb6Schristos 	ptrdiff_t *pptrdiffarg;
114f432bbb6Schristos 	size_t	*psizearg;
115f432bbb6Schristos 	intmax_t *pintmaxarg;
116f432bbb6Schristos #ifndef NO_FLOATING_POINT
117f432bbb6Schristos 	double	doublearg;
118f432bbb6Schristos 	long double longdoublearg;
119f432bbb6Schristos #endif
120f432bbb6Schristos 	wint_t	wintarg;
121f432bbb6Schristos 	wchar_t	*pwchararg;
122f432bbb6Schristos };
123f432bbb6Schristos 
124f432bbb6Schristos /*
125f432bbb6Schristos  * Type ids for argument type table.
126f432bbb6Schristos  */
127f432bbb6Schristos enum typeid {
128f325545bSchristos 	T_UNUSED = 0, TP_SHORT, T_INT, T_U_INT, TP_INT,
129f432bbb6Schristos 	T_LONG, T_U_LONG, TP_LONG, T_LLONG, T_U_LLONG, TP_LLONG,
130dbf72b0eSroy 	T_PTRDIFFT, TP_PTRDIFFT, T_SSIZET, T_SIZET, TP_SIZET,
131f432bbb6Schristos 	T_INTMAXT, T_UINTMAXT, TP_INTMAXT, TP_VOID, TP_CHAR, TP_SCHAR,
132f432bbb6Schristos 	T_DOUBLE, T_LONG_DOUBLE, T_WINT, TP_WCHAR
133f432bbb6Schristos };
134f432bbb6Schristos 
1352561b634Sjoerg #ifdef NARROW
1362561b634Sjoerg __printflike(3, 0)
1372561b634Sjoerg #endif
1382561b634Sjoerg static int	__sbprintf(FILE *, locale_t, const CHAR_T *, va_list);
1392561b634Sjoerg 
14009314539Schristos static CHAR_T	*__ujtoa(uintmax_t, CHAR_T *, int, int, const char *, int,
141f432bbb6Schristos 		    char, const char *);
14209314539Schristos static CHAR_T	*__ultoa(u_long, CHAR_T *, int, int, const char *, int,
143f432bbb6Schristos 		    char, const char *);
14409314539Schristos #ifndef NARROW
1452561b634Sjoerg static CHAR_T	*__mbsconv(char *, int, locale_t);
1462561b634Sjoerg static wint_t	__xfputwc(CHAR_T, FILE *, locale_t);
14709314539Schristos #else
1482561b634Sjoerg static char	*__wcsconv(wchar_t *, int, locale_t);
14909314539Schristos static int	__sprint(FILE *, struct __suio *);
15009314539Schristos #endif
151ab625449Schristos static int	__find_arguments(const CHAR_T *, va_list, union arg **);
152f325545bSchristos static int	__grow_type_table(size_t, enum typeid **, size_t *);
153f432bbb6Schristos 
154f432bbb6Schristos /*
155f432bbb6Schristos  * Helper function for `fprintf to unbuffered unix file': creates a
156f432bbb6Schristos  * temporary buffer.  We only work on write-only files; this avoids
157f432bbb6Schristos  * worries about ungetc buffers and so forth.
158f432bbb6Schristos  */
159f432bbb6Schristos static int
__sbprintf(FILE * fp,locale_t loc,const CHAR_T * fmt,va_list ap)1602561b634Sjoerg __sbprintf(FILE *fp, locale_t loc, const CHAR_T *fmt, va_list ap)
161f432bbb6Schristos {
162f432bbb6Schristos 	int ret;
163f432bbb6Schristos 	FILE fake;
1640490c7ebStnozaki 	struct __sfileext fakeext;
165f432bbb6Schristos 	unsigned char buf[BUFSIZ];
166f432bbb6Schristos 
1670490c7ebStnozaki 	_DIAGASSERT(fp != NULL);
1680490c7ebStnozaki 	_DIAGASSERT(fmt != NULL);
1690490c7ebStnozaki 
1700490c7ebStnozaki 	_FILEEXT_SETUP(&fake, &fakeext);
1719731566dStnozaki 	memset(WCIO_GET(&fake), 0, sizeof(struct wchar_io_data));
1720490c7ebStnozaki 
173f432bbb6Schristos 	/* copy the important variables */
174f432bbb6Schristos 	fake._flags = fp->_flags & ~__SNBF;
175f432bbb6Schristos 	fake._file = fp->_file;
176f432bbb6Schristos 	fake._cookie = fp->_cookie;
177f432bbb6Schristos 	fake._write = fp->_write;
178de001ba2Schristos 	fake._flush = fp->_flush;
179f432bbb6Schristos 
180f432bbb6Schristos 	/* set up the buffer */
181f432bbb6Schristos 	fake._bf._base = fake._p = buf;
182f432bbb6Schristos 	fake._bf._size = fake._w = sizeof(buf);
183f432bbb6Schristos 	fake._lbfsize = 0;	/* not actually used, but Just In Case */
184f432bbb6Schristos 
185f432bbb6Schristos 	/* do the work, then copy any error status */
1862561b634Sjoerg 	ret = WDECL(__vf,printf_unlocked_l)(&fake, loc, fmt, ap);
187f432bbb6Schristos 	if (ret >= 0 && fflush(&fake))
18809314539Schristos 		ret = END_OF_FILE;
189f432bbb6Schristos 	if (fake._flags & __SERR)
190f432bbb6Schristos 		fp->_flags |= __SERR;
191526d9427Schristos 	return ret;
192f432bbb6Schristos }
193f432bbb6Schristos 
19409314539Schristos #ifndef NARROW
195f432bbb6Schristos /*
196f432bbb6Schristos  * Like __fputwc, but handles fake string (__SSTR) files properly.
197f432bbb6Schristos  * File must already be locked.
198f432bbb6Schristos  */
199f432bbb6Schristos static wint_t
__xfputwc(wchar_t wc,FILE * fp,locale_t loc)2002561b634Sjoerg __xfputwc(wchar_t wc, FILE *fp, locale_t loc)
201f432bbb6Schristos {
202f432bbb6Schristos 	static const mbstate_t initial;
203f432bbb6Schristos 	mbstate_t mbs;
204f432bbb6Schristos 	char buf[MB_LEN_MAX];
205f432bbb6Schristos 	struct __suio uio;
206f432bbb6Schristos 	struct __siov iov;
207f432bbb6Schristos 	size_t len;
208f432bbb6Schristos 
209f432bbb6Schristos 	if ((fp->_flags & __SSTR) == 0)
210526d9427Schristos 		return __fputwc_unlock(wc, fp);
211f432bbb6Schristos 
212f432bbb6Schristos 	mbs = initial;
2132561b634Sjoerg 	if ((len = wcrtomb_l(buf, wc, &mbs, loc)) == (size_t)-1) {
214f432bbb6Schristos 		fp->_flags |= __SERR;
215526d9427Schristos 		return END_OF_FILE;
216f432bbb6Schristos 	}
217f432bbb6Schristos 	uio.uio_iov = &iov;
218f432bbb6Schristos 	uio.uio_resid = len;
219f432bbb6Schristos 	uio.uio_iovcnt = 1;
220f432bbb6Schristos 	iov.iov_base = buf;
221f432bbb6Schristos 	iov.iov_len = len;
222526d9427Schristos 	return __sfvwrite(fp, &uio) != EOF ? (wint_t)wc : END_OF_FILE;
223f432bbb6Schristos }
22409314539Schristos #else
22509314539Schristos /*
22609314539Schristos  * Flush out all the vectors defined by the given uio,
22709314539Schristos  * then reset it so that it can be reused.
22809314539Schristos  */
22909314539Schristos static int
__sprint(FILE * fp,struct __suio * uio)23009314539Schristos __sprint(FILE *fp, struct __suio *uio)
23109314539Schristos {
23209314539Schristos 	int err;
23309314539Schristos 
23409314539Schristos 	_DIAGASSERT(fp != NULL);
23509314539Schristos 	_DIAGASSERT(uio != NULL);
23609314539Schristos 
23709314539Schristos 	if (uio->uio_resid == 0) {
23809314539Schristos 		uio->uio_iovcnt = 0;
239526d9427Schristos 		return 0;
24009314539Schristos 	}
24109314539Schristos 	err = __sfvwrite(fp, uio);
24209314539Schristos 	uio->uio_resid = 0;
24309314539Schristos 	uio->uio_iovcnt = 0;
244526d9427Schristos 	return err;
24509314539Schristos }
24609314539Schristos #endif
247f432bbb6Schristos 
248f432bbb6Schristos /*
249f432bbb6Schristos  * Macros for converting digits to letters and vice versa
250f432bbb6Schristos  */
251f432bbb6Schristos #define	to_digit(c)	((c) - '0')
252f432bbb6Schristos #define is_digit(c)	((unsigned)to_digit(c) <= 9)
25309314539Schristos #define	to_char(n)	(CHAR_T)((n) + '0')
254f432bbb6Schristos 
255f432bbb6Schristos /*
256f432bbb6Schristos  * Convert an unsigned long to ASCII for printf purposes, returning
257f432bbb6Schristos  * a pointer to the first character of the string representation.
258f432bbb6Schristos  * Octal numbers can be forced to have a leading zero; hex numbers
259f432bbb6Schristos  * use the given digits.
260f432bbb6Schristos  */
26109314539Schristos static CHAR_T *
__ultoa(u_long val,CHAR_T * endp,int base,int octzero,const char * xdigs,int needgrp,char thousep,const char * grp)26209314539Schristos __ultoa(u_long val, CHAR_T *endp, int base, int octzero, const char *xdigs,
263f432bbb6Schristos     int needgrp, char thousep, const char *grp)
264f432bbb6Schristos {
26509314539Schristos 	CHAR_T *cp = endp;
266f432bbb6Schristos 	long sval;
267f432bbb6Schristos 	int ndig;
268f432bbb6Schristos 
269f432bbb6Schristos 	/*
270f432bbb6Schristos 	 * Handle the three cases separately, in the hope of getting
271f432bbb6Schristos 	 * better/faster code.
272f432bbb6Schristos 	 */
273f432bbb6Schristos 	switch (base) {
274f432bbb6Schristos 	case 10:
275f432bbb6Schristos 		if (val < 10) {	/* many numbers are 1 digit */
276f432bbb6Schristos 			*--cp = to_char(val);
277526d9427Schristos 			return cp;
278f432bbb6Schristos 		}
279f432bbb6Schristos 		ndig = 0;
280f432bbb6Schristos 		/*
281f432bbb6Schristos 		 * On many machines, unsigned arithmetic is harder than
282f432bbb6Schristos 		 * signed arithmetic, so we do at most one unsigned mod and
283f432bbb6Schristos 		 * divide; this is sufficient to reduce the range of
284f432bbb6Schristos 		 * the incoming value to where signed arithmetic works.
285f432bbb6Schristos 		 */
286f432bbb6Schristos 		if (val > LONG_MAX) {
287f432bbb6Schristos 			*--cp = to_char(val % 10);
288f432bbb6Schristos 			ndig++;
289f432bbb6Schristos 			sval = val / 10;
290f432bbb6Schristos 		} else
291f432bbb6Schristos 			sval = val;
292f432bbb6Schristos 		do {
293f432bbb6Schristos 			*--cp = to_char(sval % 10);
294f432bbb6Schristos 			ndig++;
295f432bbb6Schristos 			/*
296f432bbb6Schristos 			 * If (*grp == CHAR_MAX) then no more grouping
297f432bbb6Schristos 			 * should be performed.
298f432bbb6Schristos 			 */
299d9285aa4Schristos 			if (needgrp && ndig == *grp
300d9285aa4Schristos 			    && (unsigned char)*grp != (unsigned char)CHAR_MAX
301f432bbb6Schristos 			    && sval > 9) {
302f432bbb6Schristos 				*--cp = thousep;
303f432bbb6Schristos 				ndig = 0;
304f432bbb6Schristos 				/*
305f432bbb6Schristos 				 * If (*(grp+1) == '\0') then we have to
306f432bbb6Schristos 				 * use *grp character (last grouping rule)
307f432bbb6Schristos 				 * for all next cases
308f432bbb6Schristos 				 */
309f432bbb6Schristos 				if (*(grp+1) != '\0')
310f432bbb6Schristos 					grp++;
311f432bbb6Schristos 			}
312f432bbb6Schristos 			sval /= 10;
313f432bbb6Schristos 		} while (sval != 0);
314f432bbb6Schristos 		break;
315f432bbb6Schristos 
316f432bbb6Schristos 	case 8:
317f432bbb6Schristos 		do {
318f432bbb6Schristos 			*--cp = to_char(val & 7);
319f432bbb6Schristos 			val >>= 3;
320f432bbb6Schristos 		} while (val);
321f432bbb6Schristos 		if (octzero && *cp != '0')
322f432bbb6Schristos 			*--cp = '0';
323f432bbb6Schristos 		break;
324f432bbb6Schristos 
325f432bbb6Schristos 	case 16:
326f432bbb6Schristos 		do {
327cfbb35edSchristos 			*--cp = xdigs[(size_t)val & 15];
328f432bbb6Schristos 			val >>= 4;
329f432bbb6Schristos 		} while (val);
330f432bbb6Schristos 		break;
331f432bbb6Schristos 
332f432bbb6Schristos 	default:			/* oops */
333f432bbb6Schristos 		abort();
334f432bbb6Schristos 	}
335526d9427Schristos 	return cp;
336f432bbb6Schristos }
337f432bbb6Schristos 
338f432bbb6Schristos /* Identical to __ultoa, but for intmax_t. */
33909314539Schristos static CHAR_T *
__ujtoa(uintmax_t val,CHAR_T * endp,int base,int octzero,const char * xdigs,int needgrp,char thousep,const char * grp)34009314539Schristos __ujtoa(uintmax_t val, CHAR_T *endp, int base, int octzero,
341f432bbb6Schristos     const char *xdigs, int needgrp, char thousep, const char *grp)
342f432bbb6Schristos {
34309314539Schristos 	CHAR_T *cp = endp;
344f432bbb6Schristos 	intmax_t sval;
345f432bbb6Schristos 	int ndig;
346f432bbb6Schristos 
347f432bbb6Schristos 	/* quick test for small values; __ultoa is typically much faster */
348f432bbb6Schristos 	/* (perhaps instead we should run until small, then call __ultoa?) */
349f432bbb6Schristos 	if (val <= ULONG_MAX)
350526d9427Schristos 		return __ultoa((u_long)val, endp, base, octzero, xdigs,
351526d9427Schristos 		    needgrp, thousep, grp);
352f432bbb6Schristos 	switch (base) {
353f432bbb6Schristos 	case 10:
354f432bbb6Schristos 		if (val < 10) {
355f432bbb6Schristos 			*--cp = to_char(val % 10);
356526d9427Schristos 			return cp;
357f432bbb6Schristos 		}
358f432bbb6Schristos 		ndig = 0;
359f432bbb6Schristos 		if (val > INTMAX_MAX) {
360f432bbb6Schristos 			*--cp = to_char(val % 10);
361f432bbb6Schristos 			ndig++;
362f432bbb6Schristos 			sval = val / 10;
363f432bbb6Schristos 		} else
364f432bbb6Schristos 			sval = val;
365f432bbb6Schristos 		do {
366f432bbb6Schristos 			*--cp = to_char(sval % 10);
367f432bbb6Schristos 			ndig++;
368f432bbb6Schristos 			/*
369f432bbb6Schristos 			 * If (*grp == CHAR_MAX) then no more grouping
370f432bbb6Schristos 			 * should be performed.
371f432bbb6Schristos 			 */
372d9285aa4Schristos 			if (needgrp
373d9285aa4Schristos 			    && (unsigned char)*grp != (unsigned char)CHAR_MAX
374d9285aa4Schristos 			    && ndig == *grp
375f432bbb6Schristos 			    && sval > 9) {
376f432bbb6Schristos 				*--cp = thousep;
377f432bbb6Schristos 				ndig = 0;
378f432bbb6Schristos 				/*
379f432bbb6Schristos 				 * If (*(grp+1) == '\0') then we have to
380f432bbb6Schristos 				 * use *grp character (last grouping rule)
381f432bbb6Schristos 				 * for all next cases
382f432bbb6Schristos 				 */
383f432bbb6Schristos 				if (*(grp+1) != '\0')
384f432bbb6Schristos 					grp++;
385f432bbb6Schristos 			}
386f432bbb6Schristos 			sval /= 10;
387f432bbb6Schristos 		} while (sval != 0);
388f432bbb6Schristos 		break;
389f432bbb6Schristos 
390f432bbb6Schristos 	case 8:
391f432bbb6Schristos 		do {
392f432bbb6Schristos 			*--cp = to_char(val & 7);
393f432bbb6Schristos 			val >>= 3;
394f432bbb6Schristos 		} while (val);
395f432bbb6Schristos 		if (octzero && *cp != '0')
396f432bbb6Schristos 			*--cp = '0';
397f432bbb6Schristos 		break;
398f432bbb6Schristos 
399f432bbb6Schristos 	case 16:
400f432bbb6Schristos 		do {
401cfbb35edSchristos 			*--cp = xdigs[(size_t)val & 15];
402f432bbb6Schristos 			val >>= 4;
403f432bbb6Schristos 		} while (val);
404f432bbb6Schristos 		break;
405f432bbb6Schristos 
406f432bbb6Schristos 	default:
407f432bbb6Schristos 		abort();
408f432bbb6Schristos 	}
409526d9427Schristos 	return cp;
410f432bbb6Schristos }
411f432bbb6Schristos 
41209314539Schristos #ifndef NARROW
413f432bbb6Schristos /*
414f432bbb6Schristos  * Convert a multibyte character string argument for the %s format to a wide
415f432bbb6Schristos  * string representation. ``prec'' specifies the maximum number of bytes
416f432bbb6Schristos  * to output. If ``prec'' is greater than or equal to zero, we can't assume
417f432bbb6Schristos  * that the multibyte char. string ends in a null character.
418f432bbb6Schristos  */
419f432bbb6Schristos static wchar_t *
__mbsconv(char * mbsarg,int prec,locale_t loc)4202561b634Sjoerg __mbsconv(char *mbsarg, int prec, locale_t loc)
421f432bbb6Schristos {
422f432bbb6Schristos 	static const mbstate_t initial;
423f432bbb6Schristos 	mbstate_t mbs;
424f432bbb6Schristos 	wchar_t *convbuf, *wcp;
425f432bbb6Schristos 	const char *p;
426f432bbb6Schristos 	size_t insize, nchars, nconv;
427f432bbb6Schristos 
428f432bbb6Schristos 	if (mbsarg == NULL)
429526d9427Schristos 		return NULL;
430f432bbb6Schristos 
431f432bbb6Schristos 	/*
432f432bbb6Schristos 	 * Supplied argument is a multibyte string; convert it to wide
433f432bbb6Schristos 	 * characters first.
434f432bbb6Schristos 	 */
435f432bbb6Schristos 	if (prec >= 0) {
436f432bbb6Schristos 		/*
437f432bbb6Schristos 		 * String is not guaranteed to be NUL-terminated. Find the
438f432bbb6Schristos 		 * number of characters to print.
439f432bbb6Schristos 		 */
440f432bbb6Schristos 		p = mbsarg;
44181c4b0d5Slukem 		insize = nchars = nconv = 0;
442f432bbb6Schristos 		mbs = initial;
443f432bbb6Schristos 		while (nchars != (size_t)prec) {
4442561b634Sjoerg 			nconv = mbrlen_l(p, MB_CUR_MAX_L(loc), &mbs, loc);
445f432bbb6Schristos 			if (nconv == 0 || nconv == (size_t)-1 ||
446f432bbb6Schristos 			    nconv == (size_t)-2)
447f432bbb6Schristos 				break;
448f432bbb6Schristos 			p += nconv;
449f432bbb6Schristos 			nchars++;
450f432bbb6Schristos 			insize += nconv;
451f432bbb6Schristos 		}
452f432bbb6Schristos 		if (nconv == (size_t)-1 || nconv == (size_t)-2)
453526d9427Schristos 			return NULL;
454f432bbb6Schristos 	} else
455f432bbb6Schristos 		insize = strlen(mbsarg);
456f432bbb6Schristos 
457f432bbb6Schristos 	/*
458f432bbb6Schristos 	 * Allocate buffer for the result and perform the conversion,
459f432bbb6Schristos 	 * converting at most `size' bytes of the input multibyte string to
460f432bbb6Schristos 	 * wide characters for printing.
461f432bbb6Schristos 	 */
462a2c387ceSnia 	convbuf = NULL;
4633249d3dcSchristos 	errno = reallocarr(&convbuf, insize + 1, sizeof(*convbuf));
4643249d3dcSchristos 	if (errno)
465526d9427Schristos 		return NULL;
466f432bbb6Schristos 	wcp = convbuf;
467f432bbb6Schristos 	p = mbsarg;
468f432bbb6Schristos 	mbs = initial;
46981c4b0d5Slukem 	nconv = 0;
470f432bbb6Schristos 	while (insize != 0) {
4712561b634Sjoerg 		nconv = mbrtowc_l(wcp, p, insize, &mbs, loc);
472f432bbb6Schristos 		if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
473f432bbb6Schristos 			break;
474f432bbb6Schristos 		wcp++;
475f432bbb6Schristos 		p += nconv;
476f432bbb6Schristos 		insize -= nconv;
477f432bbb6Schristos 	}
478f432bbb6Schristos 	if (nconv == (size_t)-1 || nconv == (size_t)-2) {
4793249d3dcSchristos 		int serrno = errno;
480f432bbb6Schristos 		free(convbuf);
4813249d3dcSchristos 		errno = serrno;
482526d9427Schristos 		return NULL;
483f432bbb6Schristos 	}
484f432bbb6Schristos 	*wcp = L'\0';
485f432bbb6Schristos 
486526d9427Schristos 	return convbuf;
487f432bbb6Schristos }
48809314539Schristos #else
48909314539Schristos /*
4903d3c5d42Swiz  * Convert a wide-character string argument for the %ls format to a multibyte
49109314539Schristos  * string representation. If not -1, prec specifies the maximum number of
4923d3c5d42Swiz  * bytes to output, and also means that we can't assume that the wide-char.
49309314539Schristos  * string ends is null-terminated.
49409314539Schristos  */
49509314539Schristos static char *
__wcsconv(wchar_t * wcsarg,int prec,locale_t loc)4962561b634Sjoerg __wcsconv(wchar_t *wcsarg, int prec, locale_t loc)
49709314539Schristos {
49809314539Schristos 	static const mbstate_t initial;
49909314539Schristos 	mbstate_t mbs;
50009314539Schristos 	char buf[MB_LEN_MAX];
50109314539Schristos 	wchar_t *p;
50209314539Schristos 	char *convbuf;
50309314539Schristos 	size_t clen, nbytes;
50409314539Schristos 
50509314539Schristos 	/* Allocate space for the maximum number of bytes we could output. */
50609314539Schristos 	if (prec < 0) {
50709314539Schristos 		p = wcsarg;
50809314539Schristos 		mbs = initial;
5092561b634Sjoerg 		nbytes = wcsrtombs_l(NULL, (void *)&p, 0, &mbs, loc);
51009314539Schristos 		if (nbytes == (size_t)-1)
511526d9427Schristos 			return NULL;
51209314539Schristos 	} else {
51309314539Schristos 		/*
51409314539Schristos 		 * Optimisation: if the output precision is small enough,
51509314539Schristos 		 * just allocate enough memory for the maximum instead of
51609314539Schristos 		 * scanning the string.
51709314539Schristos 		 */
51809314539Schristos 		if (prec < 128)
51909314539Schristos 			nbytes = prec;
52009314539Schristos 		else {
52109314539Schristos 			nbytes = 0;
52209314539Schristos 			p = wcsarg;
52309314539Schristos 			mbs = initial;
52409314539Schristos 			for (;;) {
5252561b634Sjoerg 				clen = wcrtomb_l(buf, *p++, &mbs, loc);
52609314539Schristos 				if (clen == 0 || clen == (size_t)-1 ||
527957ba389Slukem 				    nbytes + clen > (size_t)prec)
52809314539Schristos 					break;
52909314539Schristos 				nbytes += clen;
53009314539Schristos 			}
53109314539Schristos 		}
53209314539Schristos 	}
53309314539Schristos 	if ((convbuf = malloc(nbytes + 1)) == NULL)
534526d9427Schristos 		return NULL;
53509314539Schristos 
53609314539Schristos 	/* Fill the output buffer. */
53709314539Schristos 	p = wcsarg;
53809314539Schristos 	mbs = initial;
5392561b634Sjoerg 	if ((nbytes = wcsrtombs_l(convbuf, (void *)&p,
5402561b634Sjoerg 	    nbytes, &mbs, loc)) == (size_t)-1) {
54109314539Schristos 		free(convbuf);
542526d9427Schristos 		return NULL;
54309314539Schristos 	}
54409314539Schristos 	convbuf[nbytes] = '\0';
545526d9427Schristos 	return convbuf;
54609314539Schristos }
54709314539Schristos #endif
548f432bbb6Schristos 
549f432bbb6Schristos /*
550f432bbb6Schristos  * MT-safe version
551f432bbb6Schristos  */
552f432bbb6Schristos int
WDECL(vf,printf)55309314539Schristos WDECL(vf,printf)(FILE * __restrict fp, const CHAR_T * __restrict fmt0, va_list ap)
554f432bbb6Schristos {
555f432bbb6Schristos 	int ret;
556f432bbb6Schristos 
557f432bbb6Schristos 	FLOCKFILE(fp);
558e0ac190eSjoerg 	ret = WDECL(__vf,printf_unlocked_l)(fp, _current_locale(), fmt0, ap);
5592561b634Sjoerg 	FUNLOCKFILE(fp);
5602561b634Sjoerg 	return ret;
5612561b634Sjoerg }
5622561b634Sjoerg 
5632561b634Sjoerg int
WDECL(vf,printf_l)5642561b634Sjoerg WDECL(vf,printf_l)(FILE * __restrict fp, locale_t loc, const CHAR_T * __restrict fmt0,
5652561b634Sjoerg     va_list ap)
5662561b634Sjoerg {
5672561b634Sjoerg 	int ret;
5682561b634Sjoerg 
5692561b634Sjoerg 	FLOCKFILE(fp);
5702561b634Sjoerg 	ret = WDECL(__vf,printf_unlocked_l)(fp, loc, fmt0, ap);
571f432bbb6Schristos 	FUNLOCKFILE(fp);
572526d9427Schristos 	return ret;
573f432bbb6Schristos }
574f432bbb6Schristos 
575f432bbb6Schristos #ifndef NO_FLOATING_POINT
576f432bbb6Schristos 
577f432bbb6Schristos #include <float.h>
578f432bbb6Schristos #include <math.h>
579f432bbb6Schristos #include "floatio.h"
580f432bbb6Schristos 
581f432bbb6Schristos #define	DEFPREC		6
582f432bbb6Schristos 
58309314539Schristos static int exponent(CHAR_T *, int, int);
584f432bbb6Schristos 
585f432bbb6Schristos #endif /* !NO_FLOATING_POINT */
586f432bbb6Schristos 
587f432bbb6Schristos /*
588f432bbb6Schristos  * The size of the buffer we use as scratch space for integer
589f432bbb6Schristos  * conversions, among other things.  Technically, we would need the
590f432bbb6Schristos  * most space for base 10 conversions with thousands' grouping
591f432bbb6Schristos  * characters between each pair of digits.  100 bytes is a
592f432bbb6Schristos  * conservative overestimate even for a 128-bit uintmax_t.
593f432bbb6Schristos  */
594f432bbb6Schristos #define	BUF	100
595f432bbb6Schristos 
596f432bbb6Schristos #define STATIC_ARG_TBL_SIZE 8           /* Size of static argument table. */
597f432bbb6Schristos 
598f432bbb6Schristos /*
599f432bbb6Schristos  * Flags used during conversion.
600f432bbb6Schristos  */
601f432bbb6Schristos #define	ALT		0x001		/* alternate form */
602f432bbb6Schristos #define	LADJUST		0x004		/* left adjustment */
603f432bbb6Schristos #define	LONGDBL		0x008		/* long double */
604f432bbb6Schristos #define	LONGINT		0x010		/* long integer */
60509314539Schristos #define	LLONGINT	0x020		/* long long integer */
606f432bbb6Schristos #define	SHORTINT	0x040		/* short integer */
607f432bbb6Schristos #define	ZEROPAD		0x080		/* zero (as opposed to blank) pad */
608f432bbb6Schristos #define	FPT		0x100		/* Floating point number */
609f432bbb6Schristos #define	GROUPING	0x200		/* use grouping ("'" flag) */
610f432bbb6Schristos 					/* C99 additional size modifiers: */
611f432bbb6Schristos #define	SIZET		0x400		/* size_t */
612f432bbb6Schristos #define	PTRDIFFT	0x800		/* ptrdiff_t */
613f432bbb6Schristos #define	INTMAXT		0x1000		/* intmax_t */
614f432bbb6Schristos #define	CHARINT		0x2000		/* print char using int format */
615f432bbb6Schristos 
616f432bbb6Schristos /*
617f432bbb6Schristos  * Non-MT-safe version
618f432bbb6Schristos  */
619f432bbb6Schristos int
WDECL(__vf,printf_unlocked_l)6202561b634Sjoerg WDECL(__vf,printf_unlocked_l)(FILE *fp, locale_t loc, const CHAR_T *fmt0, va_list ap)
621f432bbb6Schristos {
62209314539Schristos 	CHAR_T *fmt;		/* format string */
62309314539Schristos 	int ch;			/* character from fmt */
62409314539Schristos 	int n, n2;		/* handy integer (short term usage) */
62509314539Schristos 	CHAR_T *cp;		/* handy char pointer (short term usage) */
626f432bbb6Schristos 	int flags;		/* flags as above */
627f432bbb6Schristos 	int ret;		/* return value accumulator */
628f432bbb6Schristos 	int width;		/* width from format (%8d), or 0 */
629f432bbb6Schristos 	int prec;		/* precision from format; <0 for N/A */
63009314539Schristos 	CHAR_T sign;		/* sign prefix (' ', '+', '-', or \0) */
631f432bbb6Schristos 	char thousands_sep;	/* locale specific thousands separator */
632f432bbb6Schristos 	const char *grouping;	/* locale specific numeric grouping rules */
633f432bbb6Schristos #ifndef NO_FLOATING_POINT
634f432bbb6Schristos 	/*
635f432bbb6Schristos 	 * We can decompose the printed representation of floating
636f432bbb6Schristos 	 * point numbers into several parts, some of which may be empty:
637f432bbb6Schristos 	 *
638f432bbb6Schristos 	 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
639f432bbb6Schristos 	 *    A       B     ---C---      D       E   F
640f432bbb6Schristos 	 *
641f432bbb6Schristos 	 * A:	'sign' holds this value if present; '\0' otherwise
642f432bbb6Schristos 	 * B:	ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
643f432bbb6Schristos 	 * C:	cp points to the string MMMNNN.  Leading and trailing
644f432bbb6Schristos 	 *	zeros are not in the string and must be added.
645f432bbb6Schristos 	 * D:	expchar holds this character; '\0' if no exponent, e.g. %f
646f432bbb6Schristos 	 * F:	at least two digits for decimal, at least one digit for hex
647f432bbb6Schristos 	 */
648f432bbb6Schristos 	char *decimal_point;	/* locale specific decimal point */
649f432bbb6Schristos 	int signflag;		/* true if float is negative */
650f432bbb6Schristos 	union {			/* floating point arguments %[aAeEfFgG] */
651f432bbb6Schristos 		double dbl;
652c41e4692Schristos #ifdef WIDE_DOUBLE
653f432bbb6Schristos 		long double ldbl;
654c41e4692Schristos #endif
655f432bbb6Schristos 	} fparg;
656f432bbb6Schristos 	char *dtoaend;		/* pointer to end of converted digits */
65709314539Schristos 	char *dtoaresult;	/* buffer allocated by dtoa */
658f432bbb6Schristos 	int expt;		/* integer value of exponent */
659f432bbb6Schristos 	char expchar;		/* exponent character: [eEpP\0] */
660f432bbb6Schristos 	int expsize;		/* character count for expstr */
661f432bbb6Schristos 	int lead;		/* sig figs before decimal or group sep */
662f432bbb6Schristos 	int ndig;		/* actual number of digits returned by dtoa */
66309314539Schristos 	CHAR_T expstr[MAXEXPDIG+2];	/* buffer for exponent string: e+ZZZ */
664f432bbb6Schristos 	int nseps;		/* number of group separators with ' */
665f432bbb6Schristos 	int nrepeats;		/* number of repeats of the last group */
666f432bbb6Schristos #endif
667f432bbb6Schristos 	u_long	ulval;		/* integer arguments %[diouxX] */
668f432bbb6Schristos 	uintmax_t ujval;	/* %j, %ll, %q, %t, %z integers */
669f432bbb6Schristos 	int base;		/* base for [diouxX] conversion */
670f432bbb6Schristos 	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
671f432bbb6Schristos 	int realsz;		/* field size expanded by dprec, sign, etc */
672f432bbb6Schristos 	int size;		/* size of converted field or string */
673f432bbb6Schristos 	int prsize;             /* max size of printed field */
67409314539Schristos 	const char *xdigs;	/* digits for %[xX] conversion */
67509314539Schristos #ifdef NARROW
67609314539Schristos #define NIOV 8
67709314539Schristos 	struct __siov *iovp;	/* for PRINT macro */
67809314539Schristos 	struct __suio uio;	/* output information: summary */
67909314539Schristos 	struct __siov iov[NIOV];/* ... and individual io vectors */
68009314539Schristos #else
68109314539Schristos 	int n3;
68209314539Schristos #endif
68309314539Schristos 	CHAR_T buf[BUF];	/* buffer with space for digits of uintmax_t */
68409314539Schristos 	CHAR_T ox[2];		/* space for 0x hex-prefix */
685f432bbb6Schristos 	union arg *argtable;	/* args, built due to positional arg */
686f432bbb6Schristos 	union arg statargtable [STATIC_ARG_TBL_SIZE];
687f432bbb6Schristos 	int nextarg;		/* 1-based argument index */
688f432bbb6Schristos 	va_list orgap;		/* original argument pointer */
68909314539Schristos 	CHAR_T *convbuf;	/* multibyte to wide conversion result */
690f432bbb6Schristos 
691f432bbb6Schristos 	/*
692f432bbb6Schristos 	 * Choose PADSIZE to trade efficiency vs. size.  If larger printf
693f432bbb6Schristos 	 * fields occur frequently, increase PADSIZE and make the initialisers
694f432bbb6Schristos 	 * below longer.
695f432bbb6Schristos 	 */
696f432bbb6Schristos #define	PADSIZE	16		/* pad chunk size */
69709314539Schristos 	static CHAR_T blanks[PADSIZE] =
698f432bbb6Schristos 	 {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
69909314539Schristos 	static CHAR_T zeroes[PADSIZE] =
700f432bbb6Schristos 	 {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
701f432bbb6Schristos 
702f432bbb6Schristos 	static const char xdigs_lower[16] = "0123456789abcdef";
703f432bbb6Schristos 	static const char xdigs_upper[16] = "0123456789ABCDEF";
704f432bbb6Schristos 
705f432bbb6Schristos 	/*
706f432bbb6Schristos 	 * BEWARE, these `goto error' on error, PRINT uses `n2' and
707f432bbb6Schristos 	 * PAD uses `n'.
708f432bbb6Schristos 	 */
70909314539Schristos #ifndef NARROW
710f432bbb6Schristos #define	PRINT(ptr, len)	do {			\
711f432bbb6Schristos 	for (n3 = 0; n3 < (len); n3++)		\
71201364496Syamt 		if (__xfputwc((ptr)[n3], fp, loc) == END_OF_FILE) { \
71301364496Syamt 			fp->_flags |= __SERR;	\
71401364496Syamt 			goto error;		\
71501364496Syamt 		}				\
716388550b0Srillig } while (0)
71709314539Schristos #define FLUSH()
71809314539Schristos #else
71909314539Schristos #define	PRINT(ptr, len) do { \
72009314539Schristos 	iovp->iov_base = __UNCONST(ptr); \
72109314539Schristos 	iovp->iov_len = (len); \
72209314539Schristos 	uio.uio_resid += (len); \
72309314539Schristos 	iovp++; \
72409314539Schristos 	if (++uio.uio_iovcnt >= NIOV) { \
72509314539Schristos 		if (__sprint(fp, &uio)) \
72609314539Schristos 			goto error; \
72709314539Schristos 		iovp = iov; \
72809314539Schristos 	} \
729388550b0Srillig } while (0)
73009314539Schristos #define	FLUSH() do { \
73109314539Schristos 	if (uio.uio_resid && __sprint(fp, &uio)) \
73209314539Schristos 		goto error; \
73309314539Schristos 	uio.uio_iovcnt = 0; \
73409314539Schristos 	iovp = iov; \
735388550b0Srillig } while (0)
73609314539Schristos #endif /* NARROW */
73709314539Schristos 
738f432bbb6Schristos #define	PAD(howmany, with)	do {		\
739f432bbb6Schristos 	if ((n = (howmany)) > 0) {		\
740f432bbb6Schristos 		while (n > PADSIZE) {		\
741f432bbb6Schristos 			PRINT(with, PADSIZE);	\
742f432bbb6Schristos 			n -= PADSIZE;		\
743f432bbb6Schristos 		}				\
744f432bbb6Schristos 		PRINT(with, n);			\
745f432bbb6Schristos 	}					\
746388550b0Srillig } while (0)
747f432bbb6Schristos #define	PRINTANDPAD(p, ep, len, with) do {	\
748c5e820caSchristos 	ptrdiff_t td = (ep) - (p);		\
749c5e820caSchristos 	_DIAGASSERT(__type_fit(int, td));	\
750c5e820caSchristos 	n2 = (int)td;       			\
751f432bbb6Schristos 	if (n2 > (len))				\
752f432bbb6Schristos 		n2 = (len);			\
753f432bbb6Schristos 	if (n2 > 0)				\
754f432bbb6Schristos 		PRINT((p), n2);			\
755f432bbb6Schristos 	PAD((len) - (n2 > 0 ? n2 : 0), (with));	\
756388550b0Srillig } while (0)
757f432bbb6Schristos 
758f432bbb6Schristos 	/*
759f432bbb6Schristos 	 * Get the argument indexed by nextarg.   If the argument table is
760f432bbb6Schristos 	 * built, use it to get the argument.  If its not, get the next
761f432bbb6Schristos 	 * argument (and arguments must be gotten sequentially).
762f432bbb6Schristos 	 */
763f432bbb6Schristos #define GETARG(type) \
764f432bbb6Schristos 	((/*CONSTCOND*/argtable != NULL) ? *((type*)(void*)(&argtable[nextarg++])) : \
765f432bbb6Schristos 	    (nextarg++, va_arg(ap, type)))
766f432bbb6Schristos 
767f432bbb6Schristos 	/*
768f432bbb6Schristos 	 * To extend shorts properly, we need both signed and unsigned
769f432bbb6Schristos 	 * argument extraction methods.
770f432bbb6Schristos 	 */
771f432bbb6Schristos #define	SARG() \
772f432bbb6Schristos 	(flags&LONGINT ? GETARG(long) : \
773f432bbb6Schristos 	    flags&SHORTINT ? (long)(short)GETARG(int) : \
774f432bbb6Schristos 	    flags&CHARINT ? (long)(signed char)GETARG(int) : \
775f432bbb6Schristos 	    (long)GETARG(int))
776f432bbb6Schristos #define	UARG() \
777f432bbb6Schristos 	(flags&LONGINT ? GETARG(u_long) : \
778f432bbb6Schristos 	    flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
779f432bbb6Schristos 	    flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
780f432bbb6Schristos 	    (u_long)GETARG(u_int))
781f432bbb6Schristos #define	INTMAX_SIZE	(INTMAXT|SIZET|PTRDIFFT|LLONGINT)
782f432bbb6Schristos #define SJARG() \
783f432bbb6Schristos 	(flags&INTMAXT ? GETARG(intmax_t) : \
784dbf72b0eSroy 	    flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
785f432bbb6Schristos 	    flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
78609314539Schristos 	    (intmax_t)GETARG(long long))
787f432bbb6Schristos #define	UJARG() \
788f432bbb6Schristos 	(flags&INTMAXT ? GETARG(uintmax_t) : \
789f432bbb6Schristos 	    flags&SIZET ? (uintmax_t)GETARG(size_t) : \
790f432bbb6Schristos 	    flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
79109314539Schristos 	    (uintmax_t)GETARG(unsigned long long))
792f432bbb6Schristos 
793f432bbb6Schristos 	/*
794f432bbb6Schristos 	 * Get * arguments, including the form *nn$.  Preserve the nextarg
795f432bbb6Schristos 	 * that the argument can be gotten once the type is determined.
796f432bbb6Schristos 	 */
797f432bbb6Schristos #define GETASTER(val) \
798f432bbb6Schristos 	n2 = 0; \
799f432bbb6Schristos 	cp = fmt; \
800f432bbb6Schristos 	while (is_digit(*cp)) { \
801f432bbb6Schristos 		n2 = 10 * n2 + to_digit(*cp); \
802f432bbb6Schristos 		cp++; \
803f432bbb6Schristos 	} \
804f432bbb6Schristos 	if (*cp == '$') { \
805f432bbb6Schristos 		int hold = nextarg; \
806f432bbb6Schristos 		if (argtable == NULL) { \
807f432bbb6Schristos 			argtable = statargtable; \
808ab625449Schristos 			if (__find_arguments(fmt0, orgap, &argtable) == -1) \
809ab625449Schristos 				goto oomem; \
810f432bbb6Schristos 		} \
811f432bbb6Schristos 		nextarg = n2; \
812f432bbb6Schristos 		val = GETARG (int); \
813f432bbb6Schristos 		nextarg = hold; \
814f432bbb6Schristos 		fmt = ++cp; \
815f432bbb6Schristos 	} else { \
816f432bbb6Schristos 		val = GETARG (int); \
817f432bbb6Schristos 	}
818f432bbb6Schristos 
81909314539Schristos 	_DIAGASSERT(fp != NULL);
82009314539Schristos 	_DIAGASSERT(fmt0 != NULL);
82109314539Schristos 
82209314539Schristos 	_SET_ORIENTATION(fp, -1);
82309314539Schristos 
824f432bbb6Schristos 	thousands_sep = '\0';
825f432bbb6Schristos 	grouping = NULL;
826f432bbb6Schristos #ifndef NO_FLOATING_POINT
8272561b634Sjoerg 	decimal_point = localeconv_l(loc)->decimal_point;
82812d0c406She 	expsize = 0;		/* XXXGCC -Wuninitialized [sh3,m68000] */
829f42f5177Spooka 	ndig = -1;	/* XXX gcc */
830f432bbb6Schristos #endif
831f432bbb6Schristos 	convbuf = NULL;
83209314539Schristos 	/* sorry, f{w,}printf(read_only_file, L"") returns {W,}EOF, not 0 */
833f432bbb6Schristos 	if (cantwrite(fp)) {
834f432bbb6Schristos 		errno = EBADF;
835526d9427Schristos 		return END_OF_FILE;
836f432bbb6Schristos 	}
837f432bbb6Schristos 
838f432bbb6Schristos 	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
839f432bbb6Schristos 	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
840e4086817Schristos 	    __sfileno(fp) != -1)
8412561b634Sjoerg 		return __sbprintf(fp, loc, fmt0, ap);
842f432bbb6Schristos 
84309314539Schristos 	fmt = (CHAR_T *)__UNCONST(fmt0);
844f432bbb6Schristos 	argtable = NULL;
845f432bbb6Schristos 	nextarg = 1;
846f432bbb6Schristos 	va_copy(orgap, ap);
84709314539Schristos #ifdef NARROW
84809314539Schristos 	uio.uio_iov = iovp = iov;
84909314539Schristos 	uio.uio_resid = 0;
85009314539Schristos 	uio.uio_iovcnt = 0;
85109314539Schristos #endif
852f432bbb6Schristos 	ret = 0;
853f432bbb6Schristos 
854f432bbb6Schristos 	/*
855f432bbb6Schristos 	 * Scan the format for conversions (`%' character).
856f432bbb6Schristos 	 */
857f432bbb6Schristos 	for (;;) {
85809314539Schristos 		const CHAR_T *result;
8594470fd92Syamt 
860f432bbb6Schristos 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
86109314539Schristos 			continue;
862c5e820caSchristos 		_DIAGASSERT(__type_fit(int, fmt - cp));
863c5e820caSchristos 		if ((n = (int)(fmt - cp)) != 0) {
864f432bbb6Schristos 			if ((unsigned)ret + n > INT_MAX) {
86509314539Schristos 				ret = END_OF_FILE;
866f432bbb6Schristos 				goto error;
867f432bbb6Schristos 			}
868f432bbb6Schristos 			PRINT(cp, n);
869f432bbb6Schristos 			ret += n;
870f432bbb6Schristos 		}
871f432bbb6Schristos 		if (ch == '\0')
872f432bbb6Schristos 			goto done;
873f432bbb6Schristos 		fmt++;		/* skip over '%' */
874f432bbb6Schristos 
875f432bbb6Schristos 		flags = 0;
876f432bbb6Schristos 		dprec = 0;
877f432bbb6Schristos 		width = 0;
878f432bbb6Schristos 		prec = -1;
879f432bbb6Schristos 		sign = '\0';
880f432bbb6Schristos 		ox[1] = '\0';
881f42f5177Spooka #ifndef NO_FLOATING_POINT
88281c4b0d5Slukem 		expchar = '\0';
88381c4b0d5Slukem 		lead = 0;
88481c4b0d5Slukem 		nseps = nrepeats = 0;
885f42f5177Spooka #endif
88681c4b0d5Slukem 		ulval = 0;
88781c4b0d5Slukem 		ujval = 0;
88881c4b0d5Slukem 		xdigs = NULL;
889f432bbb6Schristos 
890f432bbb6Schristos rflag:		ch = *fmt++;
891f432bbb6Schristos reswitch:	switch (ch) {
892f432bbb6Schristos 		case ' ':
893f432bbb6Schristos 			/*-
894f432bbb6Schristos 			 * ``If the space and + flags both appear, the space
895f432bbb6Schristos 			 * flag will be ignored.''
896f432bbb6Schristos 			 *	-- ANSI X3J11
897f432bbb6Schristos 			 */
898f432bbb6Schristos 			if (!sign)
899f432bbb6Schristos 				sign = ' ';
900f432bbb6Schristos 			goto rflag;
901f432bbb6Schristos 		case '#':
902f432bbb6Schristos 			flags |= ALT;
903f432bbb6Schristos 			goto rflag;
904f432bbb6Schristos 		case '*':
905f432bbb6Schristos 			/*-
906f432bbb6Schristos 			 * ``A negative field width argument is taken as a
907f432bbb6Schristos 			 * - flag followed by a positive field width.''
908f432bbb6Schristos 			 *	-- ANSI X3J11
909f432bbb6Schristos 			 * They don't exclude field widths read from args.
910f432bbb6Schristos 			 */
911f432bbb6Schristos 			GETASTER (width);
912f432bbb6Schristos 			if (width >= 0)
913f432bbb6Schristos 				goto rflag;
914f432bbb6Schristos 			width = -width;
915f432bbb6Schristos 			/* FALLTHROUGH */
916f432bbb6Schristos 		case '-':
917f432bbb6Schristos 			flags |= LADJUST;
918f432bbb6Schristos 			goto rflag;
919f432bbb6Schristos 		case '+':
920f432bbb6Schristos 			sign = '+';
921f432bbb6Schristos 			goto rflag;
922f432bbb6Schristos 		case '\'':
9232561b634Sjoerg 			thousands_sep = *(localeconv_l(loc)->thousands_sep);
9242561b634Sjoerg 			grouping = localeconv_l(loc)->grouping;
9259648a4c2Sperseant 			/* Use grouping if defined by locale */
9269648a4c2Sperseant 			if (thousands_sep && grouping && *grouping)
9279648a4c2Sperseant 				flags |= GROUPING;
9289648a4c2Sperseant 			else {
9299648a4c2Sperseant 				thousands_sep = '\0';
930b22c29c1Sperseant 				grouping = NULL;
9319648a4c2Sperseant 			}
932f432bbb6Schristos 			goto rflag;
933f432bbb6Schristos 		case '.':
934f432bbb6Schristos 			if ((ch = *fmt++) == '*') {
935f432bbb6Schristos 				GETASTER (prec);
936f432bbb6Schristos 				goto rflag;
937f432bbb6Schristos 			}
938f432bbb6Schristos 			prec = 0;
939f432bbb6Schristos 			while (is_digit(ch)) {
940f432bbb6Schristos 				prec = 10 * prec + to_digit(ch);
941f432bbb6Schristos 				ch = *fmt++;
942f432bbb6Schristos 			}
943f432bbb6Schristos 			goto reswitch;
944f432bbb6Schristos 		case '0':
945f432bbb6Schristos 			/*-
946f432bbb6Schristos 			 * ``Note that 0 is taken as a flag, not as the
947f432bbb6Schristos 			 * beginning of a field width.''
948f432bbb6Schristos 			 *	-- ANSI X3J11
949f432bbb6Schristos 			 */
950f432bbb6Schristos 			flags |= ZEROPAD;
951f432bbb6Schristos 			goto rflag;
952f432bbb6Schristos 		case '1': case '2': case '3': case '4':
953f432bbb6Schristos 		case '5': case '6': case '7': case '8': case '9':
954f432bbb6Schristos 			n = 0;
955f432bbb6Schristos 			do {
956f432bbb6Schristos 				n = 10 * n + to_digit(ch);
957f432bbb6Schristos 				ch = *fmt++;
958f432bbb6Schristos 			} while (is_digit(ch));
959f432bbb6Schristos 			if (ch == '$') {
960f432bbb6Schristos 				nextarg = n;
961f432bbb6Schristos 				if (argtable == NULL) {
962f432bbb6Schristos 					argtable = statargtable;
963ab625449Schristos 					if (__find_arguments(fmt0, orgap,
964ab625449Schristos 					    &argtable) == -1)
965ab625449Schristos 						goto oomem;
966f432bbb6Schristos 				}
967f432bbb6Schristos 				goto rflag;
968f432bbb6Schristos 			}
969f432bbb6Schristos 			width = n;
970f432bbb6Schristos 			goto reswitch;
971f432bbb6Schristos #ifndef NO_FLOATING_POINT
972f432bbb6Schristos 		case 'L':
973f432bbb6Schristos 			flags |= LONGDBL;
974f432bbb6Schristos 			goto rflag;
975f432bbb6Schristos #endif
976f432bbb6Schristos 		case 'h':
977f432bbb6Schristos 			if (flags & SHORTINT) {
978f432bbb6Schristos 				flags &= ~SHORTINT;
979f432bbb6Schristos 				flags |= CHARINT;
980f432bbb6Schristos 			} else
981f432bbb6Schristos 				flags |= SHORTINT;
982f432bbb6Schristos 			goto rflag;
983f432bbb6Schristos 		case 'j':
984f432bbb6Schristos 			flags |= INTMAXT;
985f432bbb6Schristos 			goto rflag;
986f432bbb6Schristos 		case 'l':
987f432bbb6Schristos 			if (flags & LONGINT) {
988f432bbb6Schristos 				flags &= ~LONGINT;
989f432bbb6Schristos 				flags |= LLONGINT;
990f432bbb6Schristos 			} else
991f432bbb6Schristos 				flags |= LONGINT;
992f432bbb6Schristos 			goto rflag;
993f432bbb6Schristos 		case 'q':
994f432bbb6Schristos 			flags |= LLONGINT;	/* not necessarily */
995f432bbb6Schristos 			goto rflag;
996f432bbb6Schristos 		case 't':
997f432bbb6Schristos 			flags |= PTRDIFFT;
998f432bbb6Schristos 			goto rflag;
999f432bbb6Schristos 		case 'z':
1000f432bbb6Schristos 			flags |= SIZET;
1001f432bbb6Schristos 			goto rflag;
1002f432bbb6Schristos 		case 'C':
1003f432bbb6Schristos 			flags |= LONGINT;
1004f432bbb6Schristos 			/*FALLTHROUGH*/
1005f432bbb6Schristos 		case 'c':
100609314539Schristos #ifdef NARROW
100709314539Schristos 			if (flags & LONGINT) {
100809314539Schristos 				static const mbstate_t initial;
100909314539Schristos 				mbstate_t mbs;
101009314539Schristos 				size_t mbseqlen;
101109314539Schristos 
101209314539Schristos 				mbs = initial;
10132561b634Sjoerg 				mbseqlen = wcrtomb_l(buf,
10142561b634Sjoerg 				    (wchar_t)GETARG(wint_t), &mbs, loc);
101509314539Schristos 				if (mbseqlen == (size_t)-1) {
101609314539Schristos 					fp->_flags |= __SERR;
101709314539Schristos 					goto error;
101809314539Schristos 				}
101909314539Schristos 				size = (int)mbseqlen;
102009314539Schristos 			} else {
102109314539Schristos 				*buf = GETARG(int);
102209314539Schristos 				size = 1;
102309314539Schristos 			}
102409314539Schristos #else
1025f432bbb6Schristos 			if (flags & LONGINT)
10264470fd92Syamt 				*buf = (wchar_t)GETARG(wint_t);
1027f432bbb6Schristos 			else
10282561b634Sjoerg 				*buf = (wchar_t)btowc_l(GETARG(int), loc);
1029f432bbb6Schristos 			size = 1;
103009314539Schristos #endif
103109314539Schristos 			result = buf;
1032f432bbb6Schristos 			sign = '\0';
1033f432bbb6Schristos 			break;
1034f432bbb6Schristos 		case 'D':
1035f432bbb6Schristos 			flags |= LONGINT;
1036f432bbb6Schristos 			/*FALLTHROUGH*/
1037f432bbb6Schristos 		case 'd':
1038f432bbb6Schristos 		case 'i':
1039f432bbb6Schristos 			if (flags & INTMAX_SIZE) {
1040f432bbb6Schristos 				ujval = SJARG();
1041f432bbb6Schristos 				if ((intmax_t)ujval < 0) {
1042f432bbb6Schristos 					ujval = -ujval;
1043f432bbb6Schristos 					sign = '-';
1044f432bbb6Schristos 				}
1045f432bbb6Schristos 			} else {
1046f432bbb6Schristos 				ulval = SARG();
1047f432bbb6Schristos 				if ((long)ulval < 0) {
1048f432bbb6Schristos 					ulval = -ulval;
1049f432bbb6Schristos 					sign = '-';
1050f432bbb6Schristos 				}
1051f432bbb6Schristos 			}
1052f432bbb6Schristos 			base = 10;
1053f432bbb6Schristos 			goto number;
1054f432bbb6Schristos #ifndef NO_FLOATING_POINT
1055f432bbb6Schristos 		case 'a':
1056f432bbb6Schristos 		case 'A':
1057f432bbb6Schristos 			if (ch == 'a') {
1058f432bbb6Schristos 				ox[1] = 'x';
1059f432bbb6Schristos 				xdigs = xdigs_lower;
1060f432bbb6Schristos 				expchar = 'p';
1061f432bbb6Schristos 			} else {
1062f432bbb6Schristos 				ox[1] = 'X';
1063f432bbb6Schristos 				xdigs = xdigs_upper;
1064f432bbb6Schristos 				expchar = 'P';
1065f432bbb6Schristos 			}
1066f432bbb6Schristos 			if (prec >= 0)
1067f432bbb6Schristos 				prec++;
1068c41e4692Schristos #ifdef WIDE_DOUBLE
1069f432bbb6Schristos 			if (flags & LONGDBL) {
1070f432bbb6Schristos 				fparg.ldbl = GETARG(long double);
1071f432bbb6Schristos 				dtoaresult =
1072f432bbb6Schristos 				    __hldtoa(fparg.ldbl, xdigs, prec,
1073f432bbb6Schristos 				        &expt, &signflag, &dtoaend);
1074c41e4692Schristos 			} else
1075c41e4692Schristos #endif
1076c41e4692Schristos 			{
1077f432bbb6Schristos 				fparg.dbl = GETARG(double);
1078f432bbb6Schristos 				dtoaresult =
1079f432bbb6Schristos 				    __hdtoa(fparg.dbl, xdigs, prec,
1080f432bbb6Schristos 				        &expt, &signflag, &dtoaend);
1081f432bbb6Schristos 			}
1082ab625449Schristos 			if (dtoaresult == NULL)
1083ab625449Schristos 				goto oomem;
1084f432bbb6Schristos 
1085c5e820caSchristos 			if (prec < 0) {
1086c5e820caSchristos 				_DIAGASSERT(__type_fit(int,
1087c5e820caSchristos 				    dtoaend - dtoaresult));
1088c5e820caSchristos 				prec = (int)(dtoaend - dtoaresult);
1089c5e820caSchristos 			}
1090f432bbb6Schristos 			if (expt == INT_MAX)
1091f432bbb6Schristos 				ox[1] = '\0';
1092c5e820caSchristos 			_DIAGASSERT(__type_fit(int, dtoaend - dtoaresult));
1093c5e820caSchristos 			ndig = (int)(dtoaend - dtoaresult);
1094f432bbb6Schristos 			if (convbuf != NULL)
1095f432bbb6Schristos 				free(convbuf);
109609314539Schristos #ifndef NARROW
10972561b634Sjoerg 			result = convbuf = __mbsconv(dtoaresult, -1, loc);
109809314539Schristos #else
109909314539Schristos 			/*XXX inefficient*/
110009314539Schristos 			result = convbuf = strdup(dtoaresult);
110109314539Schristos #endif
1102ab625449Schristos 			if (result == NULL)
1103ab625449Schristos 				goto oomem;
1104c975cdc8Schristos 			__freedtoa(dtoaresult);
1105f432bbb6Schristos 			goto fp_common;
1106f432bbb6Schristos 		case 'e':
1107f432bbb6Schristos 		case 'E':
1108f432bbb6Schristos 			expchar = ch;
1109f432bbb6Schristos 			if (prec < 0)	/* account for digit before decpt */
1110f432bbb6Schristos 				prec = DEFPREC + 1;
1111f432bbb6Schristos 			else
1112f432bbb6Schristos 				prec++;
1113f432bbb6Schristos 			goto fp_begin;
1114f432bbb6Schristos 		case 'f':
1115f432bbb6Schristos 		case 'F':
1116f432bbb6Schristos 			expchar = '\0';
1117f432bbb6Schristos 			goto fp_begin;
1118f432bbb6Schristos 		case 'g':
1119f432bbb6Schristos 		case 'G':
1120f432bbb6Schristos 			expchar = ch - ('g' - 'e');
1121f432bbb6Schristos 			if (prec == 0)
1122f432bbb6Schristos 				prec = 1;
1123f432bbb6Schristos fp_begin:
1124f432bbb6Schristos 			if (prec < 0)
1125f432bbb6Schristos 				prec = DEFPREC;
1126c41e4692Schristos #ifdef WIDE_DOUBLE
1127f432bbb6Schristos 			if (flags & LONGDBL) {
1128f432bbb6Schristos 				fparg.ldbl = GETARG(long double);
1129f432bbb6Schristos 				dtoaresult =
1130f432bbb6Schristos 				    __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
1131f432bbb6Schristos 				    &expt, &signflag, &dtoaend);
1132c41e4692Schristos 			} else
1133c41e4692Schristos #endif
1134c41e4692Schristos 			{
1135f432bbb6Schristos 				fparg.dbl = GETARG(double);
1136f432bbb6Schristos 				dtoaresult =
113709314539Schristos 				    __dtoa(fparg.dbl, expchar ? 2 : 3, prec,
1138f432bbb6Schristos 				    &expt, &signflag, &dtoaend);
1139f432bbb6Schristos 				if (expt == 9999)
1140f432bbb6Schristos 					expt = INT_MAX;
1141f432bbb6Schristos 			}
1142ab625449Schristos 			if (dtoaresult == NULL)
1143ab625449Schristos 				goto oomem;
1144c5e820caSchristos 			_DIAGASSERT(__type_fit(int, dtoaend - dtoaresult));
1145c5e820caSchristos 			ndig = (int)(dtoaend - dtoaresult);
114609314539Schristos 			if (convbuf != NULL)
114709314539Schristos 				free(convbuf);
114809314539Schristos #ifndef NARROW
11492561b634Sjoerg 			result = convbuf = __mbsconv(dtoaresult, -1, loc);
115009314539Schristos #else
115109314539Schristos 			/*XXX inefficient*/
115209314539Schristos 			result = convbuf = strdup(dtoaresult);
115309314539Schristos #endif
1154ab625449Schristos 			if (result == NULL)
1155ab625449Schristos 				goto oomem;
1156c975cdc8Schristos 			__freedtoa(dtoaresult);
1157f432bbb6Schristos fp_common:
1158f432bbb6Schristos 			if (signflag)
1159f432bbb6Schristos 				sign = '-';
1160f432bbb6Schristos 			if (expt == INT_MAX) {	/* inf or nan */
11614470fd92Syamt 				if (*result == 'N') {
116209314539Schristos 					result = (ch >= 'a') ? STRCONST("nan") :
116309314539Schristos 					    STRCONST("NAN");
1164f432bbb6Schristos 					sign = '\0';
1165f432bbb6Schristos 				} else
116609314539Schristos 					result = (ch >= 'a') ? STRCONST("inf") :
116709314539Schristos 					    STRCONST("INF");
1168f432bbb6Schristos 				size = 3;
1169b0f3f04aSchristos 				flags &= ~ZEROPAD;
1170f432bbb6Schristos 				break;
1171f432bbb6Schristos 			}
1172f432bbb6Schristos 
1173f432bbb6Schristos 			flags |= FPT;
1174f432bbb6Schristos 			if (ch == 'g' || ch == 'G') {
1175f432bbb6Schristos 				if (expt > -4 && expt <= prec) {
1176f432bbb6Schristos 					/* Make %[gG] smell like %[fF] */
1177f432bbb6Schristos 					expchar = '\0';
1178f432bbb6Schristos 					if (flags & ALT)
1179f432bbb6Schristos 						prec -= expt;
1180f432bbb6Schristos 					else
1181f432bbb6Schristos 						prec = ndig - expt;
1182f432bbb6Schristos 					if (prec < 0)
1183f432bbb6Schristos 						prec = 0;
1184f432bbb6Schristos 				} else {
1185f432bbb6Schristos 					/*
1186f432bbb6Schristos 					 * Make %[gG] smell like %[eE], but
1187f432bbb6Schristos 					 * trim trailing zeroes if no # flag.
1188f432bbb6Schristos 					 */
1189f432bbb6Schristos 					if (!(flags & ALT))
1190f432bbb6Schristos 						prec = ndig;
1191f432bbb6Schristos 				}
1192f432bbb6Schristos 			}
1193f432bbb6Schristos 			if (expchar) {
1194f432bbb6Schristos 				expsize = exponent(expstr, expt - 1, expchar);
1195f432bbb6Schristos 				size = expsize + prec;
1196f432bbb6Schristos 				if (prec > 1 || flags & ALT)
1197f432bbb6Schristos 					++size;
1198f432bbb6Schristos 			} else {
1199f432bbb6Schristos 				/* space for digits before decimal point */
1200f432bbb6Schristos 				if (expt > 0)
1201f432bbb6Schristos 					size = expt;
1202f432bbb6Schristos 				else	/* "0" */
1203f432bbb6Schristos 					size = 1;
1204f432bbb6Schristos 				/* space for decimal pt and following digits */
1205f432bbb6Schristos 				if (prec || flags & ALT)
1206f432bbb6Schristos 					size += prec + 1;
1207f432bbb6Schristos 				if (grouping && expt > 0) {
1208f432bbb6Schristos 					/* space for thousands' grouping */
1209f432bbb6Schristos 					nseps = nrepeats = 0;
1210f432bbb6Schristos 					lead = expt;
1211d9285aa4Schristos 					while ((unsigned char)*grouping
1212d9285aa4Schristos 					    != (unsigned char)CHAR_MAX) {
1213f432bbb6Schristos 						if (lead <= *grouping)
1214f432bbb6Schristos 							break;
1215f432bbb6Schristos 						lead -= *grouping;
1216f432bbb6Schristos 						if (*(grouping+1)) {
1217f432bbb6Schristos 							nseps++;
1218f432bbb6Schristos 							grouping++;
1219f432bbb6Schristos 						} else
1220f432bbb6Schristos 							nrepeats++;
1221f432bbb6Schristos 					}
1222f432bbb6Schristos 					size += nseps + nrepeats;
1223f432bbb6Schristos 				} else
1224f432bbb6Schristos 					lead = expt;
1225f432bbb6Schristos 			}
1226f432bbb6Schristos 			break;
1227f432bbb6Schristos #endif /* !NO_FLOATING_POINT */
1228f432bbb6Schristos 		case 'n':
1229f432bbb6Schristos 			/*
1230f432bbb6Schristos 			 * Assignment-like behavior is specified if the
1231f432bbb6Schristos 			 * value overflows or is otherwise unrepresentable.
1232f432bbb6Schristos 			 * C99 says to use `signed char' for %hhn conversions.
1233f432bbb6Schristos 			 */
1234f432bbb6Schristos 			if (flags & LLONGINT)
123509314539Schristos 				*GETARG(long long *) = ret;
1236f432bbb6Schristos 			else if (flags & SIZET)
1237f432bbb6Schristos 				*GETARG(ssize_t *) = (ssize_t)ret;
1238f432bbb6Schristos 			else if (flags & PTRDIFFT)
1239f432bbb6Schristos 				*GETARG(ptrdiff_t *) = ret;
1240f432bbb6Schristos 			else if (flags & INTMAXT)
1241f432bbb6Schristos 				*GETARG(intmax_t *) = ret;
1242f432bbb6Schristos 			else if (flags & LONGINT)
1243f432bbb6Schristos 				*GETARG(long *) = ret;
1244f432bbb6Schristos 			else if (flags & SHORTINT)
1245f432bbb6Schristos 				*GETARG(short *) = ret;
1246f432bbb6Schristos 			else if (flags & CHARINT)
1247f432bbb6Schristos 				*GETARG(signed char *) = ret;
1248f432bbb6Schristos 			else
1249f432bbb6Schristos 				*GETARG(int *) = ret;
1250f432bbb6Schristos 			continue;	/* no output */
1251f432bbb6Schristos 		case 'O':
1252f432bbb6Schristos 			flags |= LONGINT;
1253f432bbb6Schristos 			/*FALLTHROUGH*/
1254f432bbb6Schristos 		case 'o':
1255f432bbb6Schristos 			if (flags & INTMAX_SIZE)
1256f432bbb6Schristos 				ujval = UJARG();
1257f432bbb6Schristos 			else
1258f432bbb6Schristos 				ulval = UARG();
1259f432bbb6Schristos 			base = 8;
1260f432bbb6Schristos 			goto nosign;
1261f432bbb6Schristos 		case 'p':
1262f432bbb6Schristos 			/*-
1263f432bbb6Schristos 			 * ``The argument shall be a pointer to void.  The
1264f432bbb6Schristos 			 * value of the pointer is converted to a sequence
1265f432bbb6Schristos 			 * of printable characters, in an implementation-
1266f432bbb6Schristos 			 * defined manner.''
1267f432bbb6Schristos 			 *	-- ANSI X3J11
1268f432bbb6Schristos 			 */
1269f432bbb6Schristos 			ujval = (uintmax_t)(uintptr_t)GETARG(void *);
1270f432bbb6Schristos 			base = 16;
1271f432bbb6Schristos 			xdigs = xdigs_lower;
1272f432bbb6Schristos 			flags = flags | INTMAXT;
1273f432bbb6Schristos 			ox[1] = 'x';
1274f432bbb6Schristos 			goto nosign;
1275f432bbb6Schristos 		case 'S':
1276f432bbb6Schristos 			flags |= LONGINT;
1277f432bbb6Schristos 			/*FALLTHROUGH*/
1278f432bbb6Schristos 		case 's':
127909314539Schristos 			if ((flags & LONGINT) != MULTI) {
128009314539Schristos 				if ((result = GETARG(CHAR_T *)) == NULL)
128109314539Schristos 					result = STRCONST("(null)");
1282f432bbb6Schristos 			} else {
128309314539Schristos 				MCHAR_T *mc;
1284f432bbb6Schristos 
1285f432bbb6Schristos 				if (convbuf != NULL)
1286f432bbb6Schristos 					free(convbuf);
128709314539Schristos 				if ((mc = GETARG(MCHAR_T *)) == NULL)
128809314539Schristos 					result = STRCONST("(null)");
1289f432bbb6Schristos 				else {
12902561b634Sjoerg 					convbuf = SCONV(mc, prec, loc);
1291f432bbb6Schristos 					if (convbuf == NULL) {
1292f432bbb6Schristos 						fp->_flags |= __SERR;
1293f432bbb6Schristos 						goto error;
1294f432bbb6Schristos 					}
12954470fd92Syamt 					result = convbuf;
1296f432bbb6Schristos 				}
1297f432bbb6Schristos 			}
1298f432bbb6Schristos 
1299f432bbb6Schristos 			if (prec >= 0) {
1300f432bbb6Schristos 				/*
130109314539Schristos 				 * can't use STRLEN; can only look for the
1302f432bbb6Schristos 				 * NUL in the first `prec' characters, and
130309314539Schristos 				 * STRLEN() will go further.
1304f432bbb6Schristos 				 */
1305*ace5b9b5Schristos 				const CHAR_T *p =
1306*ace5b9b5Schristos 				    MEMCHR(result, 0, (size_t)prec);
1307f432bbb6Schristos 
1308f432bbb6Schristos 				if (p != NULL) {
1309c5e820caSchristos 					_DIAGASSERT(__type_fit(int,
1310c5e820caSchristos 					    p - result));
1311c5e820caSchristos 					size = (int)(p - result);
1312f432bbb6Schristos 					if (size > prec)
1313f432bbb6Schristos 						size = prec;
1314f432bbb6Schristos 				} else
1315f432bbb6Schristos 					size = prec;
1316c5e820caSchristos 			} else {
1317c5e820caSchristos 				size_t rlen = STRLEN(result);
1318c5e820caSchristos 				_DIAGASSERT(__type_fit(int, rlen));
1319c5e820caSchristos 				size = (int)rlen;
1320c5e820caSchristos 			}
1321f432bbb6Schristos 			sign = '\0';
1322f432bbb6Schristos 			break;
1323f432bbb6Schristos 		case 'U':
1324f432bbb6Schristos 			flags |= LONGINT;
1325f432bbb6Schristos 			/*FALLTHROUGH*/
1326f432bbb6Schristos 		case 'u':
1327f432bbb6Schristos 			if (flags & INTMAX_SIZE)
1328f432bbb6Schristos 				ujval = UJARG();
1329f432bbb6Schristos 			else
1330f432bbb6Schristos 				ulval = UARG();
1331f432bbb6Schristos 			base = 10;
1332f432bbb6Schristos 			goto nosign;
1333f432bbb6Schristos 		case 'X':
1334f432bbb6Schristos 			xdigs = xdigs_upper;
1335f432bbb6Schristos 			goto hex;
1336f432bbb6Schristos 		case 'x':
1337f432bbb6Schristos 			xdigs = xdigs_lower;
1338f432bbb6Schristos hex:
1339f432bbb6Schristos 			if (flags & INTMAX_SIZE)
1340f432bbb6Schristos 				ujval = UJARG();
1341f432bbb6Schristos 			else
1342f432bbb6Schristos 				ulval = UARG();
1343f432bbb6Schristos 			base = 16;
1344f432bbb6Schristos 			/* leading 0x/X only if non-zero */
1345f432bbb6Schristos 			if (flags & ALT &&
1346f432bbb6Schristos 			    (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
1347f432bbb6Schristos 				ox[1] = ch;
1348f432bbb6Schristos 
1349f432bbb6Schristos 			flags &= ~GROUPING;
1350f432bbb6Schristos 			/* unsigned conversions */
1351f432bbb6Schristos nosign:			sign = '\0';
1352f432bbb6Schristos 			/*-
1353f432bbb6Schristos 			 * ``... diouXx conversions ... if a precision is
1354f432bbb6Schristos 			 * specified, the 0 flag will be ignored.''
1355f432bbb6Schristos 			 *	-- ANSI X3J11
1356f432bbb6Schristos 			 */
1357f432bbb6Schristos number:			if ((dprec = prec) >= 0)
1358f432bbb6Schristos 				flags &= ~ZEROPAD;
1359f432bbb6Schristos 
1360f432bbb6Schristos 			/*-
1361f432bbb6Schristos 			 * ``The result of converting a zero value with an
1362f432bbb6Schristos 			 * explicit precision of zero is no characters.''
1363f432bbb6Schristos 			 *	-- ANSI X3J11
1364f432bbb6Schristos 			 *
1365f432bbb6Schristos 			 * ``The C Standard is clear enough as is.  The call
1366f432bbb6Schristos 			 * printf("%#.0o", 0) should print 0.''
1367f432bbb6Schristos 			 *	-- Defect Report #151
1368f432bbb6Schristos 			 */
13694470fd92Syamt 			result = cp = buf + BUF;
1370f432bbb6Schristos 			if (flags & INTMAX_SIZE) {
1371f432bbb6Schristos 				if (ujval != 0 || prec != 0 ||
1372f432bbb6Schristos 				    (flags & ALT && base == 8))
13734470fd92Syamt 					result = __ujtoa(ujval, cp, base,
1374f432bbb6Schristos 					    flags & ALT, xdigs,
1375f432bbb6Schristos 					    flags & GROUPING, thousands_sep,
1376f432bbb6Schristos 					    grouping);
1377f432bbb6Schristos 			} else {
1378f432bbb6Schristos 				if (ulval != 0 || prec != 0 ||
1379f432bbb6Schristos 				    (flags & ALT && base == 8))
13804470fd92Syamt 					result = __ultoa(ulval, cp, base,
1381f432bbb6Schristos 					    flags & ALT, xdigs,
1382f432bbb6Schristos 					    flags & GROUPING, thousands_sep,
1383f432bbb6Schristos 					    grouping);
1384f432bbb6Schristos 			}
1385c5e820caSchristos 			_DIAGASSERT(__type_fit(int, buf + BUF - result));
1386c5e820caSchristos 			size = (int)(buf + BUF - result);
1387f432bbb6Schristos 			if (size > BUF)	/* should never happen */
1388f432bbb6Schristos 				abort();
1389f432bbb6Schristos 			break;
1390f432bbb6Schristos 		default:	/* "%?" prints ?, unless ? is NUL */
1391f432bbb6Schristos 			if (ch == '\0')
1392f432bbb6Schristos 				goto done;
1393f432bbb6Schristos 			/* pretend it was %c with argument ch */
13944470fd92Syamt 			*buf = ch;
13954470fd92Syamt 			result = buf;
1396f432bbb6Schristos 			size = 1;
1397f432bbb6Schristos 			sign = '\0';
1398f432bbb6Schristos 			break;
1399f432bbb6Schristos 		}
1400f432bbb6Schristos 
1401f432bbb6Schristos 		/*
14024470fd92Syamt 		 * All reasonable formats wind up here.  At this point, `result'
1403f432bbb6Schristos 		 * points to a string which (if not flags&LADJUST) should be
1404f432bbb6Schristos 		 * padded out to `width' places.  If flags&ZEROPAD, it should
1405f432bbb6Schristos 		 * first be prefixed by any sign or other prefix; otherwise,
1406f432bbb6Schristos 		 * it should be blank padded before the prefix is emitted.
1407f432bbb6Schristos 		 * After any left-hand padding and prefixing, emit zeroes
1408f432bbb6Schristos 		 * required by a decimal [diouxX] precision, then print the
1409f432bbb6Schristos 		 * string proper, then emit zeroes required by any leftover
1410f432bbb6Schristos 		 * floating precision; finally, if LADJUST, pad with blanks.
1411f432bbb6Schristos 		 *
1412f432bbb6Schristos 		 * Compute actual size, so we know how much to pad.
1413f432bbb6Schristos 		 * size excludes decimal prec; realsz includes it.
1414f432bbb6Schristos 		 */
1415f432bbb6Schristos 		realsz = dprec > size ? dprec : size;
1416f432bbb6Schristos 		if (sign)
1417f432bbb6Schristos 			realsz++;
1418f432bbb6Schristos 		if (ox[1])
1419f432bbb6Schristos 			realsz += 2;
1420f432bbb6Schristos 
1421f432bbb6Schristos 		prsize = width > realsz ? width : realsz;
1422f432bbb6Schristos 		if ((unsigned)ret + prsize > INT_MAX) {
142309314539Schristos 			ret = END_OF_FILE;
1424f432bbb6Schristos 			goto error;
1425f432bbb6Schristos 		}
1426f432bbb6Schristos 
1427f432bbb6Schristos 		/* right-adjusting blank padding */
1428f432bbb6Schristos 		if ((flags & (LADJUST|ZEROPAD)) == 0)
1429f432bbb6Schristos 			PAD(width - realsz, blanks);
1430f432bbb6Schristos 
1431f432bbb6Schristos 		/* prefix */
1432f432bbb6Schristos 		if (sign)
1433f432bbb6Schristos 			PRINT(&sign, 1);
1434f432bbb6Schristos 
1435f432bbb6Schristos 		if (ox[1]) {	/* ox[1] is either x, X, or \0 */
1436f432bbb6Schristos 			ox[0] = '0';
1437f432bbb6Schristos 			PRINT(ox, 2);
1438f432bbb6Schristos 		}
1439f432bbb6Schristos 
1440f432bbb6Schristos 		/* right-adjusting zero padding */
1441f432bbb6Schristos 		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1442f432bbb6Schristos 			PAD(width - realsz, zeroes);
1443f432bbb6Schristos 
1444f432bbb6Schristos 		/* leading zeroes from decimal precision */
1445f432bbb6Schristos 		PAD(dprec - size, zeroes);
1446f432bbb6Schristos 
1447f432bbb6Schristos 		/* the string or number proper */
1448f432bbb6Schristos #ifndef NO_FLOATING_POINT
1449f432bbb6Schristos 		if ((flags & FPT) == 0) {
14504470fd92Syamt 			PRINT(result, size);
1451f432bbb6Schristos 		} else {	/* glue together f_p fragments */
1452f432bbb6Schristos 			if (!expchar) {	/* %[fF] or sufficiently short %[gG] */
1453f432bbb6Schristos 				if (expt <= 0) {
1454f432bbb6Schristos 					PRINT(zeroes, 1);
1455f432bbb6Schristos 					if (prec || flags & ALT)
1456f432bbb6Schristos 						PRINT(decimal_point, 1);
1457f432bbb6Schristos 					PAD(-expt, zeroes);
1458f432bbb6Schristos 					/* already handled initial 0's */
1459f432bbb6Schristos 					prec += expt;
1460f432bbb6Schristos 				} else {
14614470fd92Syamt 					PRINTANDPAD(result, convbuf + ndig,
14624470fd92Syamt 					    lead, zeroes);
14634470fd92Syamt 					result += lead;
1464f432bbb6Schristos 					if (grouping) {
1465f432bbb6Schristos 						while (nseps>0 || nrepeats>0) {
1466f432bbb6Schristos 							if (nrepeats > 0)
1467f432bbb6Schristos 								nrepeats--;
1468f432bbb6Schristos 							else {
1469f432bbb6Schristos 								grouping--;
1470f432bbb6Schristos 								nseps--;
1471f432bbb6Schristos 							}
1472f432bbb6Schristos 							PRINT(&thousands_sep,
1473f432bbb6Schristos 							    1);
14744470fd92Syamt 							PRINTANDPAD(result,
1475f432bbb6Schristos 							    convbuf + ndig,
1476f432bbb6Schristos 							    *grouping, zeroes);
14774470fd92Syamt 							result += *grouping;
1478f432bbb6Schristos 						}
14794470fd92Syamt 						if (result > convbuf + ndig)
14804470fd92Syamt 							result = convbuf + ndig;
1481f432bbb6Schristos 					}
1482f432bbb6Schristos 					if (prec || flags & ALT) {
1483f432bbb6Schristos 						buf[0] = *decimal_point;
1484f432bbb6Schristos 						PRINT(buf, 1);
1485f432bbb6Schristos 					}
1486f432bbb6Schristos 				}
14874470fd92Syamt 				PRINTANDPAD(result, convbuf + ndig, prec,
14884470fd92Syamt 				    zeroes);
1489f432bbb6Schristos 			} else {	/* %[eE] or sufficiently long %[gG] */
1490f432bbb6Schristos 				if (prec > 1 || flags & ALT) {
14914470fd92Syamt 					buf[0] = *result++;
1492f432bbb6Schristos 					buf[1] = *decimal_point;
1493f432bbb6Schristos 					PRINT(buf, 2);
14944470fd92Syamt 					PRINT(result, ndig-1);
1495f432bbb6Schristos 					PAD(prec - ndig, zeroes);
1496f432bbb6Schristos 				} else	/* XeYYY */
14974470fd92Syamt 					PRINT(result, 1);
1498f432bbb6Schristos 				PRINT(expstr, expsize);
1499f432bbb6Schristos 			}
1500f432bbb6Schristos 		}
1501f432bbb6Schristos #else
15024470fd92Syamt 		PRINT(result, size);
1503f432bbb6Schristos #endif
1504f432bbb6Schristos 		/* left-adjusting padding (always blank) */
1505f432bbb6Schristos 		if (flags & LADJUST)
1506f432bbb6Schristos 			PAD(width - realsz, blanks);
1507f432bbb6Schristos 
1508f432bbb6Schristos 		/* finally, adjust ret */
1509f432bbb6Schristos 		ret += prsize;
151009314539Schristos 		FLUSH();
1511f432bbb6Schristos 	}
1512f432bbb6Schristos done:
151309314539Schristos 	FLUSH();
1514f432bbb6Schristos error:
1515f432bbb6Schristos 	va_end(orgap);
1516f432bbb6Schristos 	if (convbuf != NULL)
1517f432bbb6Schristos 		free(convbuf);
1518f432bbb6Schristos 	if (__sferror(fp))
151909314539Schristos 		ret = END_OF_FILE;
1520f432bbb6Schristos 	if ((argtable != NULL) && (argtable != statargtable))
1521f432bbb6Schristos 		free (argtable);
1522526d9427Schristos 	return ret;
1523f432bbb6Schristos 	/* NOTREACHED */
1524ab625449Schristos oomem:
1525ab625449Schristos 	errno = ENOMEM;
1526ab625449Schristos 	ret = END_OF_FILE;
1527ab625449Schristos 	goto error;
1528f432bbb6Schristos }
1529f432bbb6Schristos 
1530f432bbb6Schristos /*
1531f432bbb6Schristos  * Find all arguments when a positional parameter is encountered.  Returns a
1532f432bbb6Schristos  * table, indexed by argument number, of pointers to each arguments.  The
1533f432bbb6Schristos  * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
1534f432bbb6Schristos  * It will be replaces with a malloc-ed one if it overflows.
1535f432bbb6Schristos  */
1536ab625449Schristos static int
__find_arguments(const CHAR_T * fmt0,va_list ap,union arg ** argtable)153709314539Schristos __find_arguments(const CHAR_T *fmt0, va_list ap, union arg **argtable)
1538f432bbb6Schristos {
153909314539Schristos 	CHAR_T *fmt;		/* format string */
154009314539Schristos 	int ch;			/* character from fmt */
1541f325545bSchristos 	size_t n, n2;		/* handy index (short term usage) */
154209314539Schristos 	CHAR_T *cp;		/* handy char pointer (short term usage) */
1543f432bbb6Schristos 	int flags;		/* flags as above */
1544f432bbb6Schristos 	enum typeid *typetable; /* table of types */
1545f432bbb6Schristos 	enum typeid stattypetable [STATIC_ARG_TBL_SIZE];
1546f325545bSchristos 	size_t tablesize;	/* current size of type table */
1547f325545bSchristos 	size_t tablemax;	/* largest used index in table */
1548f325545bSchristos 	size_t nextarg;		/* 1-based argument index */
1549f325545bSchristos 	size_t nitems;		/* number of items we picked from the stack */
1550f432bbb6Schristos 
1551f432bbb6Schristos 	/*
1552f432bbb6Schristos 	 * Add an argument type to the table, expanding if necessary.
1553f325545bSchristos 	 * Check for overflow.
1554f432bbb6Schristos 	 */
1555f432bbb6Schristos #define ADDTYPE(type) \
1556ab625449Schristos 	do { \
1557f325545bSchristos 		if (nextarg > SIZE_MAX / sizeof(**argtable)) { \
1558f325545bSchristos 			if (typetable != stattypetable) \
1559f325545bSchristos 				free(typetable); \
1560f325545bSchristos 			return -1; \
1561f325545bSchristos 		} \
1562ab625449Schristos 		if (nextarg >= tablesize) \
1563ab625449Schristos 			if (__grow_type_table(nextarg, &typetable, \
1564ab625449Schristos 			    &tablesize) == -1) \
1565ab625449Schristos 				return -1; \
1566ab625449Schristos 		if (nextarg > tablemax) \
1567ab625449Schristos 			tablemax = nextarg; \
1568ab625449Schristos 		typetable[nextarg++] = type; \
1569f325545bSchristos 		nitems++; \
1570388550b0Srillig 	} while (0)
1571f432bbb6Schristos 
1572f432bbb6Schristos #define	ADDSARG() \
1573ab625449Schristos 	do { \
1574ab625449Schristos 		if (flags & INTMAXT)  \
1575ab625449Schristos 			ADDTYPE(T_INTMAXT); \
1576ab625449Schristos 		else if (flags & SIZET)  \
1577dbf72b0eSroy 			ADDTYPE(T_SSIZET); \
1578ab625449Schristos 		else if (flags & PTRDIFFT) \
1579ab625449Schristos 			ADDTYPE(T_PTRDIFFT); \
1580ab625449Schristos 		else if (flags & LLONGINT) \
1581ab625449Schristos 			ADDTYPE(T_LLONG); \
1582ab625449Schristos 		else if (flags & LONGINT) \
1583ab625449Schristos 			ADDTYPE(T_LONG); \
1584ab625449Schristos 		else \
1585ab625449Schristos 			ADDTYPE(T_INT); \
1586388550b0Srillig 	} while (0)
1587f432bbb6Schristos 
1588f432bbb6Schristos #define	ADDUARG() \
1589ab625449Schristos 	do { \
1590ab625449Schristos 		if (flags & INTMAXT)  \
1591ab625449Schristos 			ADDTYPE(T_UINTMAXT); \
1592ab625449Schristos 		else if (flags & SIZET)  \
1593ab625449Schristos 			ADDTYPE(T_SIZET); \
1594ab625449Schristos 		else if (flags & PTRDIFFT) \
1595ab625449Schristos 			ADDTYPE(T_PTRDIFFT); \
1596ab625449Schristos 		else if (flags & LLONGINT) \
1597ab625449Schristos 			ADDTYPE(T_U_LLONG); \
1598ab625449Schristos 		else if (flags & LONGINT) \
1599ab625449Schristos 			ADDTYPE(T_U_LONG); \
1600ab625449Schristos 		else \
1601ab625449Schristos 			ADDTYPE(T_U_INT); \
1602388550b0Srillig 	} while (0)
1603f432bbb6Schristos 	/*
1604f432bbb6Schristos 	 * Add * arguments to the type array.
1605f432bbb6Schristos 	 */
1606f432bbb6Schristos #define ADDASTER() \
1607f432bbb6Schristos 	n2 = 0; \
1608f432bbb6Schristos 	cp = fmt; \
1609f432bbb6Schristos 	while (is_digit(*cp)) { \
1610f432bbb6Schristos 		n2 = 10 * n2 + to_digit(*cp); \
1611f432bbb6Schristos 		cp++; \
1612f432bbb6Schristos 	} \
1613f432bbb6Schristos 	if (*cp == '$') { \
1614f325545bSchristos 		size_t hold = nextarg; \
1615f432bbb6Schristos 		nextarg = n2; \
1616f432bbb6Schristos 		ADDTYPE(T_INT); \
1617f432bbb6Schristos 		nextarg = hold; \
1618f432bbb6Schristos 		fmt = ++cp; \
1619f432bbb6Schristos 	} else { \
1620f432bbb6Schristos 		ADDTYPE(T_INT); \
1621f432bbb6Schristos 	}
162209314539Schristos 	fmt = (CHAR_T *)__UNCONST(fmt0);
1623f325545bSchristos 	memset(stattypetable, 0, sizeof(stattypetable));
1624f432bbb6Schristos 	typetable = stattypetable;
1625f432bbb6Schristos 	tablesize = STATIC_ARG_TBL_SIZE;
1626f432bbb6Schristos 	tablemax = 0;
1627f432bbb6Schristos 	nextarg = 1;
1628f325545bSchristos 	nitems = 1;
1629f432bbb6Schristos 
1630f432bbb6Schristos 	/*
1631f432bbb6Schristos 	 * Scan the format for conversions (`%' character).
1632f432bbb6Schristos 	 */
1633f432bbb6Schristos 	for (;;) {
1634f432bbb6Schristos 		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
1635f432bbb6Schristos 			/* void */;
1636f432bbb6Schristos 		if (ch == '\0')
1637f432bbb6Schristos 			goto done;
1638f432bbb6Schristos 		fmt++;		/* skip over '%' */
1639f432bbb6Schristos 
1640f432bbb6Schristos 		flags = 0;
1641f432bbb6Schristos 
1642f432bbb6Schristos rflag:		ch = *fmt++;
1643f432bbb6Schristos reswitch:	switch (ch) {
1644f432bbb6Schristos 		case ' ':
1645f432bbb6Schristos 		case '#':
1646f432bbb6Schristos 			goto rflag;
1647f432bbb6Schristos 		case '*':
1648f432bbb6Schristos 			ADDASTER ();
1649f432bbb6Schristos 			goto rflag;
1650f432bbb6Schristos 		case '-':
1651f432bbb6Schristos 		case '+':
1652f432bbb6Schristos 		case '\'':
1653f432bbb6Schristos 			goto rflag;
1654f432bbb6Schristos 		case '.':
1655f432bbb6Schristos 			if ((ch = *fmt++) == '*') {
1656f432bbb6Schristos 				ADDASTER ();
1657f432bbb6Schristos 				goto rflag;
1658f432bbb6Schristos 			}
1659f432bbb6Schristos 			while (is_digit(ch)) {
1660f432bbb6Schristos 				ch = *fmt++;
1661f432bbb6Schristos 			}
1662f432bbb6Schristos 			goto reswitch;
1663f432bbb6Schristos 		case '0':
1664f432bbb6Schristos 			goto rflag;
1665f432bbb6Schristos 		case '1': case '2': case '3': case '4':
1666f432bbb6Schristos 		case '5': case '6': case '7': case '8': case '9':
1667f432bbb6Schristos 			n = 0;
1668f432bbb6Schristos 			do {
1669f432bbb6Schristos 				n = 10 * n + to_digit(ch);
1670f432bbb6Schristos 				ch = *fmt++;
1671f432bbb6Schristos 			} while (is_digit(ch));
1672f432bbb6Schristos 			if (ch == '$') {
1673f432bbb6Schristos 				nextarg = n;
1674f432bbb6Schristos 				goto rflag;
1675f432bbb6Schristos 			}
1676f432bbb6Schristos 			goto reswitch;
1677f432bbb6Schristos #ifndef NO_FLOATING_POINT
1678f432bbb6Schristos 		case 'L':
1679f432bbb6Schristos 			flags |= LONGDBL;
1680f432bbb6Schristos 			goto rflag;
1681f432bbb6Schristos #endif
1682f432bbb6Schristos 		case 'h':
1683f432bbb6Schristos 			if (flags & SHORTINT) {
1684f432bbb6Schristos 				flags &= ~SHORTINT;
1685f432bbb6Schristos 				flags |= CHARINT;
1686f432bbb6Schristos 			} else
1687f432bbb6Schristos 				flags |= SHORTINT;
1688f432bbb6Schristos 			goto rflag;
1689f432bbb6Schristos 		case 'j':
1690f432bbb6Schristos 			flags |= INTMAXT;
1691f432bbb6Schristos 			goto rflag;
1692f432bbb6Schristos 		case 'l':
1693f432bbb6Schristos 			if (flags & LONGINT) {
1694f432bbb6Schristos 				flags &= ~LONGINT;
1695f432bbb6Schristos 				flags |= LLONGINT;
1696f432bbb6Schristos 			} else
1697f432bbb6Schristos 				flags |= LONGINT;
1698f432bbb6Schristos 			goto rflag;
1699f432bbb6Schristos 		case 'q':
1700f432bbb6Schristos 			flags |= LLONGINT;	/* not necessarily */
1701f432bbb6Schristos 			goto rflag;
1702f432bbb6Schristos 		case 't':
1703f432bbb6Schristos 			flags |= PTRDIFFT;
1704f432bbb6Schristos 			goto rflag;
1705f432bbb6Schristos 		case 'z':
1706f432bbb6Schristos 			flags |= SIZET;
1707f432bbb6Schristos 			goto rflag;
1708f432bbb6Schristos 		case 'C':
1709f432bbb6Schristos 			flags |= LONGINT;
1710f432bbb6Schristos 			/*FALLTHROUGH*/
1711f432bbb6Schristos 		case 'c':
1712f432bbb6Schristos 			if (flags & LONGINT)
1713f432bbb6Schristos 				ADDTYPE(T_WINT);
1714f432bbb6Schristos 			else
1715f432bbb6Schristos 				ADDTYPE(T_INT);
1716f432bbb6Schristos 			break;
1717f432bbb6Schristos 		case 'D':
1718f432bbb6Schristos 			flags |= LONGINT;
1719f432bbb6Schristos 			/*FALLTHROUGH*/
1720f432bbb6Schristos 		case 'd':
1721f432bbb6Schristos 		case 'i':
1722f432bbb6Schristos 			ADDSARG();
1723f432bbb6Schristos 			break;
1724f432bbb6Schristos #ifndef NO_FLOATING_POINT
1725f432bbb6Schristos 		case 'a':
1726f432bbb6Schristos 		case 'A':
1727f432bbb6Schristos 		case 'e':
1728f432bbb6Schristos 		case 'E':
1729f432bbb6Schristos 		case 'f':
1730f432bbb6Schristos 		case 'g':
1731f432bbb6Schristos 		case 'G':
1732f432bbb6Schristos 			if (flags & LONGDBL)
1733f432bbb6Schristos 				ADDTYPE(T_LONG_DOUBLE);
1734f432bbb6Schristos 			else
1735f432bbb6Schristos 				ADDTYPE(T_DOUBLE);
1736f432bbb6Schristos 			break;
1737f432bbb6Schristos #endif /* !NO_FLOATING_POINT */
1738f432bbb6Schristos 		case 'n':
1739f432bbb6Schristos 			if (flags & INTMAXT)
1740f432bbb6Schristos 				ADDTYPE(TP_INTMAXT);
1741f432bbb6Schristos 			else if (flags & PTRDIFFT)
1742f432bbb6Schristos 				ADDTYPE(TP_PTRDIFFT);
1743f432bbb6Schristos 			else if (flags & SIZET)
1744f432bbb6Schristos 				ADDTYPE(TP_SIZET);
1745f432bbb6Schristos 			else if (flags & LLONGINT)
1746f432bbb6Schristos 				ADDTYPE(TP_LLONG);
1747f432bbb6Schristos 			else if (flags & LONGINT)
1748f432bbb6Schristos 				ADDTYPE(TP_LONG);
1749f432bbb6Schristos 			else if (flags & SHORTINT)
1750f432bbb6Schristos 				ADDTYPE(TP_SHORT);
1751f432bbb6Schristos 			else if (flags & CHARINT)
1752f432bbb6Schristos 				ADDTYPE(TP_SCHAR);
1753f432bbb6Schristos 			else
1754f432bbb6Schristos 				ADDTYPE(TP_INT);
1755f432bbb6Schristos 			continue;	/* no output */
1756f432bbb6Schristos 		case 'O':
1757f432bbb6Schristos 			flags |= LONGINT;
1758f432bbb6Schristos 			/*FALLTHROUGH*/
1759f432bbb6Schristos 		case 'o':
1760f432bbb6Schristos 			ADDUARG();
1761f432bbb6Schristos 			break;
1762f432bbb6Schristos 		case 'p':
1763f432bbb6Schristos 			ADDTYPE(TP_VOID);
1764f432bbb6Schristos 			break;
1765f432bbb6Schristos 		case 'S':
1766f432bbb6Schristos 			flags |= LONGINT;
1767f432bbb6Schristos 			/*FALLTHROUGH*/
1768f432bbb6Schristos 		case 's':
1769f432bbb6Schristos 			if (flags & LONGINT)
1770f432bbb6Schristos 				ADDTYPE(TP_WCHAR);
1771f432bbb6Schristos 			else
1772f432bbb6Schristos 				ADDTYPE(TP_CHAR);
1773f432bbb6Schristos 			break;
1774f432bbb6Schristos 		case 'U':
1775f432bbb6Schristos 			flags |= LONGINT;
1776f432bbb6Schristos 			/*FALLTHROUGH*/
1777f432bbb6Schristos 		case 'u':
1778f432bbb6Schristos 		case 'X':
1779f432bbb6Schristos 		case 'x':
1780f432bbb6Schristos 			ADDUARG();
1781f432bbb6Schristos 			break;
1782f432bbb6Schristos 		default:	/* "%?" prints ?, unless ? is NUL */
1783f432bbb6Schristos 			if (ch == '\0')
1784f432bbb6Schristos 				goto done;
1785f432bbb6Schristos 			break;
1786f432bbb6Schristos 		}
1787f432bbb6Schristos 	}
1788f432bbb6Schristos done:
1789f432bbb6Schristos 	/*
1790f325545bSchristos 	 * nitems contains the number of arguments we picked from the stack.
1791f325545bSchristos 	 * If tablemax is larger, this means that some positional argument,
1792f325545bSchristos 	 * tried to pick an argument the number of arguments possibly supplied.
1793f325545bSchristos 	 * Since positional arguments are typically used to swap the order of
1794f325545bSchristos 	 * the printf arguments and not to pick random arguments from strange
1795f325545bSchristos 	 * positions in the stack, we assume that if the positional argument
1796f325545bSchristos 	 * is trying to pick beyond the end of arguments, then this is wrong.
1797f325545bSchristos 	 * Alternatively we could find a way to figure out when va_arg() runs
1798f325545bSchristos 	 * out, but how to do that?
1799f325545bSchristos 	 */
1800f325545bSchristos 	if (nitems < tablemax) {
1801f325545bSchristos 		if (typetable != stattypetable)
1802f325545bSchristos 			free(typetable);
1803f325545bSchristos 		return -1;
1804f325545bSchristos 	}
1805f325545bSchristos 	/*
1806f432bbb6Schristos 	 * Build the argument table.
1807f432bbb6Schristos 	 */
1808f432bbb6Schristos 	if (tablemax >= STATIC_ARG_TBL_SIZE) {
1809f325545bSchristos 		*argtable = malloc(sizeof(**argtable) * (tablemax + 1));
1810f325545bSchristos 		if (*argtable == NULL) {
1811f325545bSchristos 			free(typetable);
1812ab625449Schristos 			return -1;
1813f432bbb6Schristos 		}
1814f325545bSchristos 	}
1815f432bbb6Schristos 
1816f432bbb6Schristos 	(*argtable) [0].intarg = 0;
1817f432bbb6Schristos 	for (n = 1; n <= tablemax; n++) {
1818f432bbb6Schristos 		switch (typetable [n]) {
1819f432bbb6Schristos 		    case T_UNUSED: /* whoops! */
1820f432bbb6Schristos 			(*argtable) [n].intarg = va_arg (ap, int);
1821f432bbb6Schristos 			break;
1822f432bbb6Schristos 		    case TP_SCHAR:
1823f432bbb6Schristos 			(*argtable) [n].pschararg = va_arg (ap, signed char *);
1824f432bbb6Schristos 			break;
1825f432bbb6Schristos 		    case TP_SHORT:
1826f432bbb6Schristos 			(*argtable) [n].pshortarg = va_arg (ap, short *);
1827f432bbb6Schristos 			break;
1828f432bbb6Schristos 		    case T_INT:
1829f432bbb6Schristos 			(*argtable) [n].intarg = va_arg (ap, int);
1830f432bbb6Schristos 			break;
1831f432bbb6Schristos 		    case T_U_INT:
1832f432bbb6Schristos 			(*argtable) [n].uintarg = va_arg (ap, unsigned int);
1833f432bbb6Schristos 			break;
1834f432bbb6Schristos 		    case TP_INT:
1835f432bbb6Schristos 			(*argtable) [n].pintarg = va_arg (ap, int *);
1836f432bbb6Schristos 			break;
1837f432bbb6Schristos 		    case T_LONG:
1838f432bbb6Schristos 			(*argtable) [n].longarg = va_arg (ap, long);
1839f432bbb6Schristos 			break;
1840f432bbb6Schristos 		    case T_U_LONG:
1841f432bbb6Schristos 			(*argtable) [n].ulongarg = va_arg (ap, unsigned long);
1842f432bbb6Schristos 			break;
1843f432bbb6Schristos 		    case TP_LONG:
1844f432bbb6Schristos 			(*argtable) [n].plongarg = va_arg (ap, long *);
1845f432bbb6Schristos 			break;
1846f432bbb6Schristos 		    case T_LLONG:
184709314539Schristos 			(*argtable) [n].longlongarg = va_arg (ap, long long);
1848f432bbb6Schristos 			break;
1849f432bbb6Schristos 		    case T_U_LLONG:
185009314539Schristos 			(*argtable) [n].ulonglongarg = va_arg (ap, unsigned long long);
1851f432bbb6Schristos 			break;
1852f432bbb6Schristos 		    case TP_LLONG:
185309314539Schristos 			(*argtable) [n].plonglongarg = va_arg (ap, long long *);
1854f432bbb6Schristos 			break;
1855f432bbb6Schristos 		    case T_PTRDIFFT:
1856f432bbb6Schristos 			(*argtable) [n].ptrdiffarg = va_arg (ap, ptrdiff_t);
1857f432bbb6Schristos 			break;
1858f432bbb6Schristos 		    case TP_PTRDIFFT:
1859f432bbb6Schristos 			(*argtable) [n].pptrdiffarg = va_arg (ap, ptrdiff_t *);
1860f432bbb6Schristos 			break;
1861dbf72b0eSroy 		    case T_SSIZET:
1862dbf72b0eSroy 			(*argtable) [n].ssizearg = va_arg (ap, ssize_t);
1863dbf72b0eSroy 			break;
1864f432bbb6Schristos 		    case T_SIZET:
1865f432bbb6Schristos 			(*argtable) [n].sizearg = va_arg (ap, size_t);
1866f432bbb6Schristos 			break;
1867f432bbb6Schristos 		    case TP_SIZET:
1868f432bbb6Schristos 			(*argtable) [n].psizearg = va_arg (ap, size_t *);
1869f432bbb6Schristos 			break;
1870f432bbb6Schristos 		    case T_INTMAXT:
1871f432bbb6Schristos 			(*argtable) [n].intmaxarg = va_arg (ap, intmax_t);
1872f432bbb6Schristos 			break;
1873f432bbb6Schristos 		    case T_UINTMAXT:
1874f432bbb6Schristos 			(*argtable) [n].uintmaxarg = va_arg (ap, uintmax_t);
1875f432bbb6Schristos 			break;
1876f432bbb6Schristos 		    case TP_INTMAXT:
1877f432bbb6Schristos 			(*argtable) [n].pintmaxarg = va_arg (ap, intmax_t *);
1878f432bbb6Schristos 			break;
1879f432bbb6Schristos 		    case T_DOUBLE:
188009314539Schristos #ifndef NO_FLOATING_POINT
1881f432bbb6Schristos 			(*argtable) [n].doublearg = va_arg (ap, double);
188209314539Schristos #endif
1883f432bbb6Schristos 			break;
1884f432bbb6Schristos 		    case T_LONG_DOUBLE:
188509314539Schristos #ifndef NO_FLOATING_POINT
1886f432bbb6Schristos 			(*argtable) [n].longdoublearg = va_arg (ap, long double);
1887f432bbb6Schristos #endif
188809314539Schristos 			break;
1889f432bbb6Schristos 		    case TP_CHAR:
1890f432bbb6Schristos 			(*argtable) [n].pchararg = va_arg (ap, char *);
1891f432bbb6Schristos 			break;
1892f432bbb6Schristos 		    case TP_VOID:
1893f432bbb6Schristos 			(*argtable) [n].pvoidarg = va_arg (ap, void *);
1894f432bbb6Schristos 			break;
1895f432bbb6Schristos 		    case T_WINT:
1896f432bbb6Schristos 			(*argtable) [n].wintarg = va_arg (ap, wint_t);
1897f432bbb6Schristos 			break;
1898f432bbb6Schristos 		    case TP_WCHAR:
1899f432bbb6Schristos 			(*argtable) [n].pwchararg = va_arg (ap, wchar_t *);
1900f432bbb6Schristos 			break;
1901f432bbb6Schristos 		}
1902f432bbb6Schristos 	}
1903f432bbb6Schristos 
1904f325545bSchristos 	if (typetable != stattypetable)
1905f432bbb6Schristos 		free (typetable);
1906ab625449Schristos 	return 0;
1907f432bbb6Schristos }
1908f432bbb6Schristos 
1909f432bbb6Schristos /*
1910f432bbb6Schristos  * Increase the size of the type table.
1911f432bbb6Schristos  */
1912ab625449Schristos static int
__grow_type_table(size_t nextarg,enum typeid ** typetable,size_t * tablesize)1913f325545bSchristos __grow_type_table (size_t nextarg, enum typeid **typetable, size_t *tablesize)
1914f432bbb6Schristos {
1915f432bbb6Schristos 	enum typeid *const oldtable = *typetable;
1916c5e820caSchristos 	const size_t oldsize = *tablesize;
1917f432bbb6Schristos 	enum typeid *newtable;
1918be2404a7Schristos 	size_t newsize = oldsize * 2;
1919f432bbb6Schristos 
1920f432bbb6Schristos 	if (newsize < nextarg + 1)
1921f432bbb6Schristos 		newsize = nextarg + 1;
1922f432bbb6Schristos 	if (oldsize == STATIC_ARG_TBL_SIZE) {
1923a2c387ceSnia 		newtable = NULL;
19243249d3dcSchristos 		errno = reallocarr(&newtable, newsize, sizeof(*newtable));
19253249d3dcSchristos 		if (errno)
1926ab625449Schristos 			return -1;
1927f325545bSchristos 		memcpy(newtable, oldtable, oldsize * sizeof(*newtable));
1928f432bbb6Schristos 	} else {
1929a2c387ceSnia 		newtable = oldtable;
19303249d3dcSchristos 		errno = reallocarr(&newtable, newsize, sizeof(*newtable));
19313249d3dcSchristos 		if (errno) {
19323249d3dcSchristos 			int serrno = errno;
1933ab625449Schristos 			free(oldtable);
19343249d3dcSchristos 			errno = serrno;
1935ab625449Schristos 			return -1;
1936ab625449Schristos 		}
1937f432bbb6Schristos 	}
1938f325545bSchristos 	memset(&newtable[oldsize], 0, (newsize - oldsize) * sizeof(*newtable));
1939f432bbb6Schristos 
1940f432bbb6Schristos 	*typetable = newtable;
1941f432bbb6Schristos 	*tablesize = newsize;
1942ab625449Schristos 	return 0;
1943f432bbb6Schristos }
1944f432bbb6Schristos 
1945f432bbb6Schristos #ifndef NO_FLOATING_POINT
1946c975cdc8Schristos 
1947f432bbb6Schristos static int
exponent(CHAR_T * p0,int expo,int fmtch)194809314539Schristos exponent(CHAR_T *p0, int expo, int fmtch)
1949f432bbb6Schristos {
195009314539Schristos 	CHAR_T *p, *t;
195109314539Schristos 	CHAR_T expbuf[MAXEXPDIG];
1952f432bbb6Schristos 
1953f432bbb6Schristos 	p = p0;
1954f432bbb6Schristos 	*p++ = fmtch;
1955f432bbb6Schristos 	if (expo < 0) {
1956f432bbb6Schristos 		expo = -expo;
1957f432bbb6Schristos 		*p++ = '-';
1958f432bbb6Schristos 	}
1959f432bbb6Schristos 	else
1960f432bbb6Schristos 		*p++ = '+';
1961f432bbb6Schristos 	t = expbuf + MAXEXPDIG;
1962f432bbb6Schristos 	if (expo > 9) {
1963f432bbb6Schristos 		do {
1964f432bbb6Schristos 			*--t = to_char(expo % 10);
1965f432bbb6Schristos 		} while ((expo /= 10) > 9);
1966f432bbb6Schristos 		*--t = to_char(expo);
1967f432bbb6Schristos 		for (; t < expbuf + MAXEXPDIG; *p++ = *t++);
1968f432bbb6Schristos 	}
1969f432bbb6Schristos 	else {
1970f432bbb6Schristos 		/*
1971f432bbb6Schristos 		 * Exponents for decimal floating point conversions
1972f432bbb6Schristos 		 * (%[eEgG]) must be at least two characters long,
1973f432bbb6Schristos 		 * whereas exponents for hexadecimal conversions can
1974f432bbb6Schristos 		 * be only one character long.
1975f432bbb6Schristos 		 */
1976f432bbb6Schristos 		if (fmtch == 'e' || fmtch == 'E')
1977f432bbb6Schristos 			*p++ = '0';
1978f432bbb6Schristos 		*p++ = to_char(expo);
1979f432bbb6Schristos 	}
1980c5e820caSchristos 	_DIAGASSERT(__type_fit(int, p - p0));
1981c5e820caSchristos 	return (int)(p - p0);
1982f432bbb6Schristos }
1983f432bbb6Schristos #endif /* !NO_FLOATING_POINT */
1984