1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
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 * Copyright (c) 2011 The FreeBSD Foundation
11 *
12 * Portions of this software were developed by David Chisnall
13 * under sponsorship from the FreeBSD Foundation.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 */
39
40/*
41 * Actual wprintf innards.
42 *
43 * Avoid making gratuitous changes to this source file; it should be kept
44 * as close as possible to vfprintf.c for ease of maintenance.
45 */
46
47#include "namespace.h"
48#include <sys/types.h>
49
50#include <ctype.h>
51#include <errno.h>
52#include <limits.h>
53#include <locale.h>
54#include <stdarg.h>
55#include <stddef.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <wchar.h>
61#include <wctype.h>
62#include "un-namespace.h"
63
64#include "libc_private.h"
65#include "local.h"
66#include "fvwrite.h"
67#include "printflocal.h"
68#include "xlocale_private.h"
69
70static int	__sprint(FILE *, struct __suio *, locale_t);
71static int	__sbprintf(FILE *, locale_t, const wchar_t *, va_list) __noinline;
72static wint_t	__xfputwc(wchar_t, FILE *, locale_t);
73static wchar_t	*__mbsconv(char *, int);
74
75#define	CHAR	wchar_t
76#include "printfcommon.h"
77
78struct grouping_state {
79	wchar_t thousands_sep;	/* locale-specific thousands separator */
80	const char *grouping;	/* locale-specific numeric grouping rules */
81	int lead;		/* sig figs before decimal or group sep */
82	int nseps;		/* number of group separators with ' */
83	int nrepeats;		/* number of repeats of the last group */
84};
85
86static const mbstate_t initial_mbs;
87
88static inline wchar_t
89get_decpt(locale_t locale)
90{
91	mbstate_t mbs;
92	wchar_t decpt;
93	int nconv;
94
95	mbs = initial_mbs;
96	nconv = mbrtowc(&decpt, localeconv_l(locale)->decimal_point, MB_CUR_MAX, &mbs);
97	if (nconv == (size_t)-1 || nconv == (size_t)-2)
98		decpt = '.';    /* failsafe */
99	return (decpt);
100}
101
102static inline wchar_t
103get_thousep(locale_t locale)
104{
105	mbstate_t mbs;
106	wchar_t thousep;
107	int nconv;
108
109	mbs = initial_mbs;
110	nconv = mbrtowc(&thousep, localeconv_l(locale)->thousands_sep,
111	    MB_CUR_MAX, &mbs);
112	if (nconv == (size_t)-1 || nconv == (size_t)-2)
113		thousep = '\0';    /* failsafe */
114	return (thousep);
115}
116
117/*
118 * Initialize the thousands' grouping state in preparation to print a
119 * number with ndigits digits. This routine returns the total number
120 * of wide characters that will be printed.
121 */
122static int
123grouping_init(struct grouping_state *gs, int ndigits, locale_t locale)
124{
125
126	gs->grouping = localeconv_l(locale)->grouping;
127	gs->thousands_sep = get_thousep(locale);
128
129	gs->nseps = gs->nrepeats = 0;
130	gs->lead = ndigits;
131	while (*gs->grouping != CHAR_MAX) {
132		if (gs->lead <= *gs->grouping)
133			break;
134		gs->lead -= *gs->grouping;
135		if (*(gs->grouping+1)) {
136			gs->nseps++;
137			gs->grouping++;
138		} else
139			gs->nrepeats++;
140	}
141	return (gs->nseps + gs->nrepeats);
142}
143
144/*
145 * Print a number with thousands' separators.
146 */
147static int
148grouping_print(struct grouping_state *gs, struct io_state *iop,
149	       const CHAR *cp, const CHAR *ep, locale_t locale)
150{
151	const CHAR *cp0 = cp;
152
153	if (io_printandpad(iop, cp, ep, gs->lead, zeroes, locale))
154		return (-1);
155	cp += gs->lead;
156	while (gs->nseps > 0 || gs->nrepeats > 0) {
157		if (gs->nrepeats > 0)
158			gs->nrepeats--;
159		else {
160			gs->grouping--;
161			gs->nseps--;
162		}
163		if (io_print(iop, &gs->thousands_sep, 1, locale))
164			return (-1);
165		if (io_printandpad(iop, cp, ep, *gs->grouping, zeroes, locale))
166			return (-1);
167		cp += *gs->grouping;
168	}
169	if (cp > ep)
170		cp = ep;
171	return (cp - cp0);
172}
173
174
175/*
176 * Flush out all the vectors defined by the given uio,
177 * then reset it so that it can be reused.
178 *
179 * XXX The fact that we do this a character at a time and convert to a
180 * multibyte character sequence even if the destination is a wide
181 * string eclipses the benefits of buffering.
182 */
183static int
184__sprint(FILE *fp, struct __suio *uio, locale_t locale)
185{
186	struct __siov *iov;
187	wchar_t *p;
188	int i, len;
189
190	iov = uio->uio_iov;
191	for (; uio->uio_resid != 0; uio->uio_resid -= len, iov++) {
192		p = (wchar_t *)iov->iov_base;
193		len = iov->iov_len;
194		for (i = 0; i < len; i++) {
195			if (__xfputwc(p[i], fp, locale) == WEOF)
196				return (-1);
197		}
198	}
199	uio->uio_iovcnt = 0;
200	return (0);
201}
202
203/*
204 * Helper function for `fprintf to unbuffered unix file': creates a
205 * temporary buffer.  We only work on write-only files; this avoids
206 * worries about ungetc buffers and so forth.
207 */
208static int
209__sbprintf(FILE *fp, locale_t locale, const wchar_t *fmt, va_list ap)
210{
211	int ret;
212	FILE fake;
213	unsigned char buf[BUFSIZ];
214
215	/* XXX This is probably not needed. */
216	if (prepwrite(fp) != 0)
217		return (EOF);
218
219	/* copy the important variables */
220	fake._flags = fp->_flags & ~__SNBF;
221	fake._file = fp->_file;
222	fake._cookie = fp->_cookie;
223	fake._write = fp->_write;
224	fake._orientation = fp->_orientation;
225	fake._mbstate = fp->_mbstate;
226
227	/* set up the buffer */
228	fake._bf._base = fake._p = buf;
229	fake._bf._size = fake._w = sizeof(buf);
230	fake._lbfsize = 0;	/* not actually used, but Just In Case */
231
232	/* do the work, then copy any error status */
233	ret = __vfwprintf(&fake, locale, fmt, ap);
234	if (ret >= 0 && __fflush(&fake))
235		ret = WEOF;
236	if (fake._flags & __SERR)
237		fp->_flags |= __SERR;
238	return (ret);
239}
240
241/*
242 * Like __fputwc, but handles fake string (__SSTR) files properly.
243 * File must already be locked.
244 */
245static wint_t
246__xfputwc(wchar_t wc, FILE *fp, locale_t locale)
247{
248	mbstate_t mbs;
249	char buf[MB_LEN_MAX];
250	struct __suio uio;
251	struct __siov iov;
252	size_t len;
253
254	if ((fp->_flags & __SSTR) == 0)
255		return (__fputwc(wc, fp, locale));
256
257	mbs = initial_mbs;
258	if ((len = wcrtomb(buf, wc, &mbs)) == (size_t)-1) {
259		fp->_flags |= __SERR;
260		return (WEOF);
261	}
262	uio.uio_iov = &iov;
263	uio.uio_resid = len;
264	uio.uio_iovcnt = 1;
265	iov.iov_base = buf;
266	iov.iov_len = len;
267	return (__sfvwrite(fp, &uio) != EOF ? (wint_t)wc : WEOF);
268}
269
270/*
271 * Convert a multibyte character string argument for the %s format to a wide
272 * string representation. ``prec'' specifies the maximum number of bytes
273 * to output. If ``prec'' is greater than or equal to zero, we can't assume
274 * that the multibyte char. string ends in a null character.
275 */
276static wchar_t *
277__mbsconv(char *mbsarg, int prec)
278{
279	mbstate_t mbs;
280	wchar_t *convbuf, *wcp;
281	const char *p;
282	size_t insize, nchars, nconv;
283
284	if (mbsarg == NULL)
285		return (NULL);
286
287	/*
288	 * Supplied argument is a multibyte string; convert it to wide
289	 * characters first.
290	 */
291	if (prec >= 0) {
292		/*
293		 * String is not guaranteed to be NUL-terminated. Find the
294		 * number of characters to print.
295		 */
296		p = mbsarg;
297		insize = nchars = nconv = 0;
298		mbs = initial_mbs;
299		while (nchars != (size_t)prec) {
300			nconv = mbrlen(p, MB_CUR_MAX, &mbs);
301			if (nconv == 0 || nconv == (size_t)-1 ||
302			    nconv == (size_t)-2)
303				break;
304			p += nconv;
305			nchars++;
306			insize += nconv;
307		}
308		if (nconv == (size_t)-1 || nconv == (size_t)-2)
309			return (NULL);
310	} else {
311		insize = strlen(mbsarg);
312		nconv = 0;
313	}
314
315	/*
316	 * Allocate buffer for the result and perform the conversion,
317	 * converting at most `size' bytes of the input multibyte string to
318	 * wide characters for printing.
319	 */
320	convbuf = malloc((insize + 1) * sizeof(*convbuf));
321	if (convbuf == NULL)
322		return (NULL);
323	wcp = convbuf;
324	p = mbsarg;
325	mbs = initial_mbs;
326	while (insize != 0) {
327		nconv = mbrtowc(wcp, p, insize, &mbs);
328		if (nconv == 0 || nconv == (size_t)-1 || nconv == (size_t)-2)
329			break;
330		wcp++;
331		p += nconv;
332		insize -= nconv;
333	}
334	if (nconv == (size_t)-1 || nconv == (size_t)-2) {
335		free(convbuf);
336		return (NULL);
337	}
338	*wcp = L'\0';
339
340	return (convbuf);
341}
342
343/*
344 * MT-safe version
345 */
346int
347vfwprintf_l(FILE * __restrict fp, locale_t locale,
348		const wchar_t * __restrict fmt0, va_list ap)
349
350{
351	int ret;
352	FIX_LOCALE(locale);
353	FLOCKFILE_CANCELSAFE(fp);
354	/* optimise fprintf(stderr) (and other unbuffered Unix files) */
355	if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
356	    fp->_file >= 0)
357		ret = __sbprintf(fp, locale, fmt0, ap);
358	else
359		ret = __vfwprintf(fp, locale, fmt0, ap);
360	FUNLOCKFILE_CANCELSAFE();
361	return (ret);
362}
363int
364vfwprintf(FILE * __restrict fp, const wchar_t * __restrict fmt0, va_list ap)
365{
366	return vfwprintf_l(fp, __get_locale(), fmt0, ap);
367}
368
369/*
370 * The size of the buffer we use as scratch space for integer
371 * conversions, among other things.  We need enough space to
372 * write a uintmax_t in octal (plus one byte).
373 */
374#if UINTMAX_MAX <= UINT64_MAX
375#define	BUF	32
376#else
377#error "BUF must be large enough to format a uintmax_t"
378#endif
379
380/*
381 * Non-MT-safe version
382 */
383int
384__vfwprintf(FILE *fp, locale_t locale, const wchar_t *fmt0, va_list ap)
385{
386	wchar_t *fmt;		/* format string */
387	wchar_t ch;		/* character from fmt */
388	int n, n2;		/* handy integer (short term usage) */
389	wchar_t *cp;		/* handy char pointer (short term usage) */
390	int flags;		/* flags as above */
391	int ret;		/* return value accumulator */
392	int width;		/* width from format (%8d), or 0 */
393	int prec;		/* precision from format; <0 for N/A */
394	wchar_t sign;		/* sign prefix (' ', '+', '-', or \0) */
395	struct grouping_state gs; /* thousands' grouping info */
396#ifndef NO_FLOATING_POINT
397	/*
398	 * We can decompose the printed representation of floating
399	 * point numbers into several parts, some of which may be empty:
400	 *
401	 * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
402	 *    A       B     ---C---      D       E   F
403	 *
404	 * A:	'sign' holds this value if present; '\0' otherwise
405	 * B:	ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
406	 * C:	cp points to the string MMMNNN.  Leading and trailing
407	 *	zeros are not in the string and must be added.
408	 * D:	expchar holds this character; '\0' if no exponent, e.g. %f
409	 * F:	at least two digits for decimal, at least one digit for hex
410	 */
411	wchar_t decimal_point;	/* locale specific decimal point */
412	int signflag;		/* true if float is negative */
413	union {			/* floating point arguments %[aAeEfFgG] */
414		double dbl;
415		long double ldbl;
416	} fparg;
417	int expt;		/* integer value of exponent */
418	char expchar;		/* exponent character: [eEpP\0] */
419	char *dtoaend;		/* pointer to end of converted digits */
420	int expsize;		/* character count for expstr */
421	int ndig;		/* actual number of digits returned by dtoa */
422	wchar_t expstr[MAXEXPDIG+2];	/* buffer for exponent string: e+ZZZ */
423	char *dtoaresult;	/* buffer allocated by dtoa */
424#endif
425	u_long	ulval;		/* integer arguments %[diouxX] */
426	uintmax_t ujval;	/* %j, %ll, %q, %t, %z integers */
427	int base;		/* base for [diouxX] conversion */
428	int dprec;		/* a copy of prec if [diouxX], 0 otherwise */
429	int realsz;		/* field size expanded by dprec, sign, etc */
430	int size;		/* size of converted field or string */
431	int prsize;             /* max size of printed field */
432	const char *xdigs;	/* digits for [xX] conversion */
433	struct io_state io;	/* I/O buffering state */
434	wchar_t buf[BUF];	/* buffer with space for digits of uintmax_t */
435	wchar_t ox[2];		/* space for 0x hex-prefix */
436	union arg *argtable;	/* args, built due to positional arg */
437	union arg statargtable [STATIC_ARG_TBL_SIZE];
438	int nextarg;		/* 1-based argument index */
439	va_list orgap;		/* original argument pointer */
440	wchar_t *convbuf;	/* multibyte to wide conversion result */
441	int savserr;
442
443	static const char xdigs_lower[16] = "0123456789abcdef";
444	static const char xdigs_upper[16] = "0123456789ABCDEF";
445
446	/* BEWARE, these `goto error' on error. */
447#define	PRINT(ptr, len)	do {			\
448	if (io_print(&io, (ptr), (len), locale))	\
449		goto error; \
450} while (0)
451#define	PAD(howmany, with) { \
452	if (io_pad(&io, (howmany), (with), locale)) \
453		goto error; \
454}
455#define	PRINTANDPAD(p, ep, len, with) {	\
456	if (io_printandpad(&io, (p), (ep), (len), (with), locale)) \
457		goto error; \
458}
459#define	FLUSH() { \
460	if (io_flush(&io, locale)) \
461		goto error; \
462}
463
464	/*
465	 * Get the argument indexed by nextarg.   If the argument table is
466	 * built, use it to get the argument.  If its not, get the next
467	 * argument (and arguments must be gotten sequentially).
468	 */
469#define GETARG(type) \
470	((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
471	    (nextarg++, va_arg(ap, type)))
472
473	/*
474	 * To extend shorts properly, we need both signed and unsigned
475	 * argument extraction methods.
476	 */
477#define	SARG() \
478	(flags&LONGINT ? GETARG(long) : \
479	    flags&SHORTINT ? (long)(short)GETARG(int) : \
480	    flags&CHARINT ? (long)(signed char)GETARG(int) : \
481	    (long)GETARG(int))
482#define	UARG() \
483	(flags&LONGINT ? GETARG(u_long) : \
484	    flags&SHORTINT ? (u_long)(u_short)GETARG(int) : \
485	    flags&CHARINT ? (u_long)(u_char)GETARG(int) : \
486	    (u_long)GETARG(u_int))
487#define	INTMAX_SIZE	(INTMAXT|SIZET|PTRDIFFT|LLONGINT)
488#define SJARG() \
489	(flags&INTMAXT ? GETARG(intmax_t) : \
490	    flags&SIZET ? (intmax_t)GETARG(ssize_t) : \
491	    flags&PTRDIFFT ? (intmax_t)GETARG(ptrdiff_t) : \
492	    (intmax_t)GETARG(long long))
493#define	UJARG() \
494	(flags&INTMAXT ? GETARG(uintmax_t) : \
495	    flags&SIZET ? (uintmax_t)GETARG(size_t) : \
496	    flags&PTRDIFFT ? (uintmax_t)GETARG(ptrdiff_t) : \
497	    (uintmax_t)GETARG(unsigned long long))
498
499	/*
500	 * Get * arguments, including the form *nn$.  Preserve the nextarg
501	 * that the argument can be gotten once the type is determined.
502	 */
503#define GETASTER(val) \
504	n2 = 0; \
505	cp = fmt; \
506	while (is_digit(*cp)) { \
507		n2 = 10 * n2 + to_digit(*cp); \
508		cp++; \
509	} \
510	if (*cp == '$') { \
511		int hold = nextarg; \
512		if (argtable == NULL) { \
513			argtable = statargtable; \
514			if (__find_warguments (fmt0, orgap, &argtable)) { \
515				ret = EOF; \
516				goto error; \
517			} \
518		} \
519		nextarg = n2; \
520		val = GETARG (int); \
521		nextarg = hold; \
522		fmt = ++cp; \
523	} else { \
524		val = GETARG (int); \
525	}
526
527
528	/* sorry, fwprintf(read_only_file, L"") returns WEOF, not 0 */
529	if (prepwrite(fp) != 0) {
530		errno = EBADF;
531		return (EOF);
532	}
533
534	savserr = fp->_flags & __SERR;
535	fp->_flags &= ~__SERR;
536
537	convbuf = NULL;
538	fmt = (wchar_t *)fmt0;
539	argtable = NULL;
540	nextarg = 1;
541	va_copy(orgap, ap);
542	io_init(&io, fp);
543	ret = 0;
544#ifndef NO_FLOATING_POINT
545	decimal_point = get_decpt(locale);
546#endif
547
548	/*
549	 * Scan the format for conversions (`%' character).
550	 */
551	for (;;) {
552		for (cp = fmt; (ch = *fmt) != '\0' && ch != '%'; fmt++)
553			/* void */;
554		if ((n = fmt - cp) != 0) {
555			if ((unsigned)ret + n > INT_MAX) {
556				ret = EOF;
557				errno = EOVERFLOW;
558				goto error;
559			}
560			PRINT(cp, n);
561			ret += n;
562		}
563		if (ch == '\0')
564			goto done;
565		fmt++;		/* skip over '%' */
566
567		flags = 0;
568		dprec = 0;
569		width = 0;
570		prec = -1;
571		gs.grouping = NULL;
572		sign = '\0';
573		ox[1] = '\0';
574
575rflag:		ch = *fmt++;
576reswitch:	switch (ch) {
577		case ' ':
578			/*-
579			 * ``If the space and + flags both appear, the space
580			 * flag will be ignored.''
581			 *	-- ANSI X3J11
582			 */
583			if (!sign)
584				sign = ' ';
585			goto rflag;
586		case '#':
587			flags |= ALT;
588			goto rflag;
589		case '*':
590			/*-
591			 * ``A negative field width argument is taken as a
592			 * - flag followed by a positive field width.''
593			 *	-- ANSI X3J11
594			 * They don't exclude field widths read from args.
595			 */
596			GETASTER (width);
597			if (width >= 0)
598				goto rflag;
599			width = -width;
600			/* FALLTHROUGH */
601		case '-':
602			flags |= LADJUST;
603			goto rflag;
604		case '+':
605			sign = '+';
606			goto rflag;
607		case '\'':
608			flags |= GROUPING;
609			goto rflag;
610		case '.':
611			if ((ch = *fmt++) == '*') {
612				GETASTER (prec);
613				goto rflag;
614			}
615			prec = 0;
616			while (is_digit(ch)) {
617				prec = 10 * prec + to_digit(ch);
618				ch = *fmt++;
619			}
620			goto reswitch;
621		case '0':
622			/*-
623			 * ``Note that 0 is taken as a flag, not as the
624			 * beginning of a field width.''
625			 *	-- ANSI X3J11
626			 */
627			flags |= ZEROPAD;
628			goto rflag;
629		case '1': case '2': case '3': case '4':
630		case '5': case '6': case '7': case '8': case '9':
631			n = 0;
632			do {
633				n = 10 * n + to_digit(ch);
634				ch = *fmt++;
635			} while (is_digit(ch));
636			if (ch == '$') {
637				nextarg = n;
638				if (argtable == NULL) {
639					argtable = statargtable;
640					if (__find_warguments (fmt0, orgap,
641							       &argtable)) {
642						ret = EOF;
643						goto error;
644					}
645				}
646				goto rflag;
647			}
648			width = n;
649			goto reswitch;
650#ifndef NO_FLOATING_POINT
651		case 'L':
652			flags |= LONGDBL;
653			goto rflag;
654#endif
655		case 'h':
656			if (flags & SHORTINT) {
657				flags &= ~SHORTINT;
658				flags |= CHARINT;
659			} else
660				flags |= SHORTINT;
661			goto rflag;
662		case 'j':
663			flags |= INTMAXT;
664			goto rflag;
665		case 'l':
666			if (flags & LONGINT) {
667				flags &= ~LONGINT;
668				flags |= LLONGINT;
669			} else
670				flags |= LONGINT;
671			goto rflag;
672		case 'q':
673			flags |= LLONGINT;	/* not necessarily */
674			goto rflag;
675		case 't':
676			flags |= PTRDIFFT;
677			goto rflag;
678		case 'w':
679			/*
680			 * Fixed-width integer types.  On all platforms we
681			 * support, int8_t is equivalent to char, int16_t
682			 * is equivalent to short, int32_t is equivalent
683			 * to int, int64_t is equivalent to long long int.
684			 * Furthermore, int_fast8_t, int_fast16_t and
685			 * int_fast32_t are equivalent to int, and
686			 * int_fast64_t is equivalent to long long int.
687			 */
688			flags &= ~(CHARINT|SHORTINT|LONGINT|LLONGINT|INTMAXT);
689			if (fmt[0] == 'f') {
690				flags |= FASTINT;
691				fmt++;
692			} else {
693				flags &= ~FASTINT;
694			}
695			if (fmt[0] == '8') {
696				if (!(flags & FASTINT))
697					flags |= CHARINT;
698				else
699					/* no flag set = 32 */ ;
700				fmt += 1;
701			} else if (fmt[0] == '1' && fmt[1] == '6') {
702				if (!(flags & FASTINT))
703					flags |= SHORTINT;
704				else
705					/* no flag set = 32 */ ;
706				fmt += 2;
707			} else if (fmt[0] == '3' && fmt[1] == '2') {
708				/* no flag set = 32 */ ;
709				fmt += 2;
710			} else if (fmt[0] == '6' && fmt[1] == '4') {
711				flags |= LLONGINT;
712				fmt += 2;
713			} else {
714				if (flags & FASTINT) {
715					flags &= ~FASTINT;
716					fmt--;
717				}
718				goto invalid;
719			}
720			goto rflag;
721		case 'z':
722			flags |= SIZET;
723			goto rflag;
724		case 'B':
725		case 'b':
726			if (flags & INTMAX_SIZE)
727				ujval = UJARG();
728			else
729				ulval = UARG();
730			base = 2;
731			/* leading 0b/B only if non-zero */
732			if (flags & ALT &&
733			    (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
734				ox[1] = ch;
735			goto nosign;
736			break;
737		case 'C':
738			flags |= LONGINT;
739			/*FALLTHROUGH*/
740		case 'c':
741			if (flags & LONGINT)
742				*(cp = buf) = (wchar_t)GETARG(wint_t);
743			else
744				*(cp = buf) = (wchar_t)btowc(GETARG(int));
745			size = 1;
746			sign = '\0';
747			break;
748		case 'D':
749			flags |= LONGINT;
750			/*FALLTHROUGH*/
751		case 'd':
752		case 'i':
753			if (flags & INTMAX_SIZE) {
754				ujval = SJARG();
755				if ((intmax_t)ujval < 0) {
756					ujval = -ujval;
757					sign = '-';
758				}
759			} else {
760				ulval = SARG();
761				if ((long)ulval < 0) {
762					ulval = -ulval;
763					sign = '-';
764				}
765			}
766			base = 10;
767			goto number;
768#ifndef NO_FLOATING_POINT
769		case 'a':
770		case 'A':
771			if (ch == 'a') {
772				ox[1] = 'x';
773				xdigs = xdigs_lower;
774				expchar = 'p';
775			} else {
776				ox[1] = 'X';
777				xdigs = xdigs_upper;
778				expchar = 'P';
779			}
780			if (prec >= 0)
781				prec++;
782			if (flags & LONGDBL) {
783				fparg.ldbl = GETARG(long double);
784				dtoaresult =
785				    __hldtoa(fparg.ldbl, xdigs, prec,
786				        &expt, &signflag, &dtoaend);
787			} else {
788				fparg.dbl = GETARG(double);
789				dtoaresult =
790				    __hdtoa(fparg.dbl, xdigs, prec,
791				        &expt, &signflag, &dtoaend);
792			}
793			if (prec < 0)
794				prec = dtoaend - dtoaresult;
795			if (expt == INT_MAX)
796				ox[1] = '\0';
797			if (convbuf != NULL)
798				free(convbuf);
799			ndig = dtoaend - dtoaresult;
800			cp = convbuf = __mbsconv(dtoaresult, -1);
801			freedtoa(dtoaresult);
802			goto fp_common;
803		case 'e':
804		case 'E':
805			expchar = ch;
806			if (prec < 0)	/* account for digit before decpt */
807				prec = DEFPREC + 1;
808			else
809				prec++;
810			goto fp_begin;
811		case 'f':
812		case 'F':
813			expchar = '\0';
814			goto fp_begin;
815		case 'g':
816		case 'G':
817			expchar = ch - ('g' - 'e');
818			if (prec == 0)
819				prec = 1;
820fp_begin:
821			if (prec < 0)
822				prec = DEFPREC;
823			if (convbuf != NULL)
824				free(convbuf);
825			if (flags & LONGDBL) {
826				fparg.ldbl = GETARG(long double);
827				dtoaresult =
828				    __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
829				    &expt, &signflag, &dtoaend);
830			} else {
831				fparg.dbl = GETARG(double);
832				dtoaresult =
833				    dtoa(fparg.dbl, expchar ? 2 : 3, prec,
834				    &expt, &signflag, &dtoaend);
835				if (expt == 9999)
836					expt = INT_MAX;
837			}
838			ndig = dtoaend - dtoaresult;
839			cp = convbuf = __mbsconv(dtoaresult, -1);
840			freedtoa(dtoaresult);
841fp_common:
842			if (signflag)
843				sign = '-';
844			if (expt == INT_MAX) {	/* inf or nan */
845				if (*cp == 'N') {
846					cp = (ch >= 'a') ? L"nan" : L"NAN";
847					sign = '\0';
848				} else
849					cp = (ch >= 'a') ? L"inf" : L"INF";
850				size = 3;
851				flags &= ~ZEROPAD;
852				break;
853			}
854			flags |= FPT;
855			if (ch == 'g' || ch == 'G') {
856				if (expt > -4 && expt <= prec) {
857					/* Make %[gG] smell like %[fF] */
858					expchar = '\0';
859					if (flags & ALT)
860						prec -= expt;
861					else
862						prec = ndig - expt;
863					if (prec < 0)
864						prec = 0;
865				} else {
866					/*
867					 * Make %[gG] smell like %[eE], but
868					 * trim trailing zeroes if no # flag.
869					 */
870					if (!(flags & ALT))
871						prec = ndig;
872				}
873			}
874			if (expchar) {
875				expsize = exponent(expstr, expt - 1, expchar);
876				size = expsize + prec;
877				if (prec > 1 || flags & ALT)
878					++size;
879			} else {
880				/* space for digits before decimal point */
881				if (expt > 0)
882					size = expt;
883				else	/* "0" */
884					size = 1;
885				/* space for decimal pt and following digits */
886				if (prec || flags & ALT)
887					size += prec + 1;
888				if ((flags & GROUPING) && expt > 0)
889					size += grouping_init(&gs, expt, locale);
890			}
891			break;
892#endif /* !NO_FLOATING_POINT */
893		case 'n':
894			/*
895			 * Assignment-like behavior is specified if the
896			 * value overflows or is otherwise unrepresentable.
897			 * C99 says to use `signed char' for %hhn conversions.
898			 */
899			if (flags & LLONGINT)
900				*GETARG(long long *) = ret;
901			else if (flags & SIZET)
902				*GETARG(ssize_t *) = (ssize_t)ret;
903			else if (flags & PTRDIFFT)
904				*GETARG(ptrdiff_t *) = ret;
905			else if (flags & INTMAXT)
906				*GETARG(intmax_t *) = ret;
907			else if (flags & LONGINT)
908				*GETARG(long *) = ret;
909			else if (flags & SHORTINT)
910				*GETARG(short *) = ret;
911			else if (flags & CHARINT)
912				*GETARG(signed char *) = ret;
913			else
914				*GETARG(int *) = ret;
915			continue;	/* no output */
916		case 'O':
917			flags |= LONGINT;
918			/*FALLTHROUGH*/
919		case 'o':
920			if (flags & INTMAX_SIZE)
921				ujval = UJARG();
922			else
923				ulval = UARG();
924			base = 8;
925			goto nosign;
926		case 'p':
927			/*-
928			 * ``The argument shall be a pointer to void.  The
929			 * value of the pointer is converted to a sequence
930			 * of printable characters, in an implementation-
931			 * defined manner.''
932			 *	-- ANSI X3J11
933			 */
934			ujval = (uintmax_t)(uintptr_t)GETARG(void *);
935			base = 16;
936			xdigs = xdigs_lower;
937			flags = flags | INTMAXT;
938			ox[1] = 'x';
939			goto nosign;
940		case 'S':
941			flags |= LONGINT;
942			/*FALLTHROUGH*/
943		case 's':
944			if (flags & LONGINT) {
945				if ((cp = GETARG(wchar_t *)) == NULL)
946					cp = L"(null)";
947			} else {
948				char *mbp;
949
950				if (convbuf != NULL)
951					free(convbuf);
952				if ((mbp = GETARG(char *)) == NULL)
953					cp = L"(null)";
954				else {
955					convbuf = __mbsconv(mbp, prec);
956					if (convbuf == NULL) {
957						fp->_flags |= __SERR;
958						goto error;
959					}
960					cp = convbuf;
961				}
962			}
963			size = (prec >= 0) ? wcsnlen(cp, prec) : wcslen(cp);
964			sign = '\0';
965			break;
966		case 'U':
967			flags |= LONGINT;
968			/*FALLTHROUGH*/
969		case 'u':
970			if (flags & INTMAX_SIZE)
971				ujval = UJARG();
972			else
973				ulval = UARG();
974			base = 10;
975			goto nosign;
976		case 'X':
977			xdigs = xdigs_upper;
978			goto hex;
979		case 'x':
980			xdigs = xdigs_lower;
981hex:
982			if (flags & INTMAX_SIZE)
983				ujval = UJARG();
984			else
985				ulval = UARG();
986			base = 16;
987			/* leading 0x/X only if non-zero */
988			if (flags & ALT &&
989			    (flags & INTMAX_SIZE ? ujval != 0 : ulval != 0))
990				ox[1] = ch;
991
992			flags &= ~GROUPING;
993			/* unsigned conversions */
994nosign:			sign = '\0';
995			/*-
996			 * ``... diouXx conversions ... if a precision is
997			 * specified, the 0 flag will be ignored.''
998			 *	-- ANSI X3J11
999			 */
1000number:			if ((dprec = prec) >= 0)
1001				flags &= ~ZEROPAD;
1002
1003			/*-
1004			 * ``The result of converting a zero value with an
1005			 * explicit precision of zero is no characters.''
1006			 *	-- ANSI X3J11
1007			 *
1008			 * ``The C Standard is clear enough as is.  The call
1009			 * printf("%#.0o", 0) should print 0.''
1010			 *	-- Defect Report #151
1011			 */
1012			cp = buf + BUF;
1013			if (flags & INTMAX_SIZE) {
1014				if (ujval != 0 || prec != 0 ||
1015				    (flags & ALT && base == 8))
1016					cp = __ujtoa(ujval, cp, base,
1017					    flags & ALT, xdigs);
1018			} else {
1019				if (ulval != 0 || prec != 0 ||
1020				    (flags & ALT && base == 8))
1021					cp = __ultoa(ulval, cp, base,
1022					    flags & ALT, xdigs);
1023			}
1024			size = buf + BUF - cp;
1025			if (size > BUF)	/* should never happen */
1026				abort();
1027			if ((flags & GROUPING) && size != 0)
1028				size += grouping_init(&gs, size, locale);
1029			break;
1030		default:	/* "%?" prints ?, unless ? is NUL */
1031			if (ch == '\0')
1032				goto done;
1033invalid:
1034			/* pretend it was %c with argument ch */
1035			cp = buf;
1036			*cp = ch;
1037			size = 1;
1038			sign = '\0';
1039			break;
1040		}
1041
1042		/*
1043		 * All reasonable formats wind up here.  At this point, `cp'
1044		 * points to a string which (if not flags&LADJUST) should be
1045		 * padded out to `width' places.  If flags&ZEROPAD, it should
1046		 * first be prefixed by any sign or other prefix; otherwise,
1047		 * it should be blank padded before the prefix is emitted.
1048		 * After any left-hand padding and prefixing, emit zeroes
1049		 * required by a decimal [diouxX] precision, then print the
1050		 * string proper, then emit zeroes required by any leftover
1051		 * floating precision; finally, if LADJUST, pad with blanks.
1052		 *
1053		 * Compute actual size, so we know how much to pad.
1054		 * size excludes decimal prec; realsz includes it.
1055		 */
1056		realsz = dprec > size ? dprec : size;
1057		if (sign)
1058			realsz++;
1059		if (ox[1])
1060			realsz += 2;
1061
1062		prsize = width > realsz ? width : realsz;
1063		if ((unsigned)ret + prsize > INT_MAX) {
1064			ret = EOF;
1065			errno = EOVERFLOW;
1066			goto error;
1067		}
1068
1069		/* right-adjusting blank padding */
1070		if ((flags & (LADJUST|ZEROPAD)) == 0)
1071			PAD(width - realsz, blanks);
1072
1073		/* prefix */
1074		if (sign)
1075			PRINT(&sign, 1);
1076
1077		if (ox[1]) {	/* ox[1] is either x, X, or \0 */
1078			ox[0] = '0';
1079			PRINT(ox, 2);
1080		}
1081
1082		/* right-adjusting zero padding */
1083		if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
1084			PAD(width - realsz, zeroes);
1085
1086		/* the string or number proper */
1087#ifndef NO_FLOATING_POINT
1088		if ((flags & FPT) == 0) {
1089#endif
1090			/* leading zeroes from decimal precision */
1091			PAD(dprec - size, zeroes);
1092			if (gs.grouping) {
1093				if (grouping_print(&gs, &io, cp, buf+BUF, locale) < 0)
1094					goto error;
1095			} else {
1096				PRINT(cp, size);
1097			}
1098#ifndef NO_FLOATING_POINT
1099		} else {	/* glue together f_p fragments */
1100			if (!expchar) {	/* %[fF] or sufficiently short %[gG] */
1101				if (expt <= 0) {
1102					PRINT(zeroes, 1);
1103					if (prec || flags & ALT)
1104						PRINT(&decimal_point, 1);
1105					PAD(-expt, zeroes);
1106					/* already handled initial 0's */
1107					prec += expt;
1108				} else {
1109					if (gs.grouping) {
1110						n = grouping_print(&gs, &io,
1111						    cp, convbuf + ndig, locale);
1112						if (n < 0)
1113							goto error;
1114						cp += n;
1115					} else {
1116						PRINTANDPAD(cp, convbuf + ndig,
1117						    expt, zeroes);
1118						cp += expt;
1119					}
1120					if (prec || flags & ALT)
1121						PRINT(&decimal_point, 1);
1122				}
1123				PRINTANDPAD(cp, convbuf + ndig, prec, zeroes);
1124			} else {	/* %[eE] or sufficiently long %[gG] */
1125				if (prec > 1 || flags & ALT) {
1126					buf[0] = *cp++;
1127					buf[1] = decimal_point;
1128					PRINT(buf, 2);
1129					PRINT(cp, ndig-1);
1130					PAD(prec - ndig, zeroes);
1131				} else	/* XeYYY */
1132					PRINT(cp, 1);
1133				PRINT(expstr, expsize);
1134			}
1135		}
1136#endif
1137		/* left-adjusting padding (always blank) */
1138		if (flags & LADJUST)
1139			PAD(width - realsz, blanks);
1140
1141		/* finally, adjust ret */
1142		ret += prsize;
1143
1144		FLUSH();	/* copy out the I/O vectors */
1145	}
1146done:
1147	FLUSH();
1148error:
1149	va_end(orgap);
1150	if (convbuf != NULL)
1151		free(convbuf);
1152	if (__sferror(fp))
1153		ret = EOF;
1154	else
1155		fp->_flags |= savserr;
1156	if ((argtable != NULL) && (argtable != statargtable))
1157		free (argtable);
1158	return (ret);
1159	/* NOTREACHED */
1160}
1161