1570af302Sopenharmony_ci#include "stdio_impl.h"
2570af302Sopenharmony_ci#include <errno.h>
3570af302Sopenharmony_ci#include <ctype.h>
4570af302Sopenharmony_ci#include <limits.h>
5570af302Sopenharmony_ci#include <string.h>
6570af302Sopenharmony_ci#include <stdarg.h>
7570af302Sopenharmony_ci#include <stddef.h>
8570af302Sopenharmony_ci#include <stdlib.h>
9570af302Sopenharmony_ci#include <wchar.h>
10570af302Sopenharmony_ci#include <inttypes.h>
11570af302Sopenharmony_ci
12570af302Sopenharmony_ci/* Convenient bit representation for modifier flags, which all fall
13570af302Sopenharmony_ci * within 31 codepoints of the space character. */
14570af302Sopenharmony_ci
15570af302Sopenharmony_ci#define ALT_FORM   (1U<<'#'-' ')
16570af302Sopenharmony_ci#define ZERO_PAD   (1U<<'0'-' ')
17570af302Sopenharmony_ci#define LEFT_ADJ   (1U<<'-'-' ')
18570af302Sopenharmony_ci#define PAD_POS    (1U<<' '-' ')
19570af302Sopenharmony_ci#define MARK_POS   (1U<<'+'-' ')
20570af302Sopenharmony_ci#define GROUPED    (1U<<'\''-' ')
21570af302Sopenharmony_ci
22570af302Sopenharmony_ci#define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
23570af302Sopenharmony_ci
24570af302Sopenharmony_ci/* State machine to accept length modifiers + conversion specifiers.
25570af302Sopenharmony_ci * Result is 0 on failure, or an argument type to pop on success. */
26570af302Sopenharmony_ci
27570af302Sopenharmony_cienum {
28570af302Sopenharmony_ci	BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
29570af302Sopenharmony_ci	ZTPRE, JPRE,
30570af302Sopenharmony_ci	STOP,
31570af302Sopenharmony_ci	PTR, INT, UINT, ULLONG,
32570af302Sopenharmony_ci	LONG, ULONG,
33570af302Sopenharmony_ci	SHORT, USHORT, CHAR, UCHAR,
34570af302Sopenharmony_ci	LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
35570af302Sopenharmony_ci	DBL, LDBL,
36570af302Sopenharmony_ci	NOARG,
37570af302Sopenharmony_ci	MAXSTATE
38570af302Sopenharmony_ci};
39570af302Sopenharmony_ci
40570af302Sopenharmony_ci#define S(x) [(x)-'A']
41570af302Sopenharmony_ci
42570af302Sopenharmony_cistatic const unsigned char states[]['z'-'A'+1] = {
43570af302Sopenharmony_ci	{ /* 0: bare types */
44570af302Sopenharmony_ci		S('d') = INT, S('i') = INT,
45570af302Sopenharmony_ci		S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
46570af302Sopenharmony_ci		S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
47570af302Sopenharmony_ci		S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
48570af302Sopenharmony_ci		S('c') = INT, S('C') = UINT,
49570af302Sopenharmony_ci		S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
50570af302Sopenharmony_ci		S('m') = NOARG,
51570af302Sopenharmony_ci		S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
52570af302Sopenharmony_ci		S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
53570af302Sopenharmony_ci	}, { /* 1: l-prefixed */
54570af302Sopenharmony_ci		S('d') = LONG, S('i') = LONG,
55570af302Sopenharmony_ci		S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
56570af302Sopenharmony_ci		S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
57570af302Sopenharmony_ci		S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
58570af302Sopenharmony_ci		S('c') = UINT, S('s') = PTR, S('n') = PTR,
59570af302Sopenharmony_ci		S('l') = LLPRE,
60570af302Sopenharmony_ci	}, { /* 2: ll-prefixed */
61570af302Sopenharmony_ci		S('d') = LLONG, S('i') = LLONG,
62570af302Sopenharmony_ci		S('o') = ULLONG, S('u') = ULLONG,
63570af302Sopenharmony_ci		S('x') = ULLONG, S('X') = ULLONG,
64570af302Sopenharmony_ci		S('n') = PTR,
65570af302Sopenharmony_ci	}, { /* 3: h-prefixed */
66570af302Sopenharmony_ci		S('d') = SHORT, S('i') = SHORT,
67570af302Sopenharmony_ci		S('o') = USHORT, S('u') = USHORT,
68570af302Sopenharmony_ci		S('x') = USHORT, S('X') = USHORT,
69570af302Sopenharmony_ci		S('n') = PTR,
70570af302Sopenharmony_ci		S('h') = HHPRE,
71570af302Sopenharmony_ci	}, { /* 4: hh-prefixed */
72570af302Sopenharmony_ci		S('d') = CHAR, S('i') = CHAR,
73570af302Sopenharmony_ci		S('o') = UCHAR, S('u') = UCHAR,
74570af302Sopenharmony_ci		S('x') = UCHAR, S('X') = UCHAR,
75570af302Sopenharmony_ci		S('n') = PTR,
76570af302Sopenharmony_ci	}, { /* 5: L-prefixed */
77570af302Sopenharmony_ci		S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
78570af302Sopenharmony_ci		S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
79570af302Sopenharmony_ci		S('n') = PTR,
80570af302Sopenharmony_ci	}, { /* 6: z- or t-prefixed (assumed to be same size) */
81570af302Sopenharmony_ci		S('d') = PDIFF, S('i') = PDIFF,
82570af302Sopenharmony_ci		S('o') = SIZET, S('u') = SIZET,
83570af302Sopenharmony_ci		S('x') = SIZET, S('X') = SIZET,
84570af302Sopenharmony_ci		S('n') = PTR,
85570af302Sopenharmony_ci	}, { /* 7: j-prefixed */
86570af302Sopenharmony_ci		S('d') = IMAX, S('i') = IMAX,
87570af302Sopenharmony_ci		S('o') = UMAX, S('u') = UMAX,
88570af302Sopenharmony_ci		S('x') = UMAX, S('X') = UMAX,
89570af302Sopenharmony_ci		S('n') = PTR,
90570af302Sopenharmony_ci	}
91570af302Sopenharmony_ci};
92570af302Sopenharmony_ci
93570af302Sopenharmony_ci#define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
94570af302Sopenharmony_ci
95570af302Sopenharmony_ciunion arg
96570af302Sopenharmony_ci{
97570af302Sopenharmony_ci	uintmax_t i;
98570af302Sopenharmony_ci	long double f;
99570af302Sopenharmony_ci	void *p;
100570af302Sopenharmony_ci};
101570af302Sopenharmony_ci
102570af302Sopenharmony_cistatic void pop_arg(union arg *arg, int type, va_list *ap)
103570af302Sopenharmony_ci{
104570af302Sopenharmony_ci	switch (type) {
105570af302Sopenharmony_ci	       case PTR:	arg->p = va_arg(*ap, void *);
106570af302Sopenharmony_ci	break; case INT:	arg->i = va_arg(*ap, int);
107570af302Sopenharmony_ci	break; case UINT:	arg->i = va_arg(*ap, unsigned int);
108570af302Sopenharmony_ci	break; case LONG:	arg->i = va_arg(*ap, long);
109570af302Sopenharmony_ci	break; case ULONG:	arg->i = va_arg(*ap, unsigned long);
110570af302Sopenharmony_ci	break; case ULLONG:	arg->i = va_arg(*ap, unsigned long long);
111570af302Sopenharmony_ci	break; case SHORT:	arg->i = (short)va_arg(*ap, int);
112570af302Sopenharmony_ci	break; case USHORT:	arg->i = (unsigned short)va_arg(*ap, int);
113570af302Sopenharmony_ci	break; case CHAR:	arg->i = (signed char)va_arg(*ap, int);
114570af302Sopenharmony_ci	break; case UCHAR:	arg->i = (unsigned char)va_arg(*ap, int);
115570af302Sopenharmony_ci	break; case LLONG:	arg->i = va_arg(*ap, long long);
116570af302Sopenharmony_ci	break; case SIZET:	arg->i = va_arg(*ap, size_t);
117570af302Sopenharmony_ci	break; case IMAX:	arg->i = va_arg(*ap, intmax_t);
118570af302Sopenharmony_ci	break; case UMAX:	arg->i = va_arg(*ap, uintmax_t);
119570af302Sopenharmony_ci	break; case PDIFF:	arg->i = va_arg(*ap, ptrdiff_t);
120570af302Sopenharmony_ci	break; case UIPTR:	arg->i = (uintptr_t)va_arg(*ap, void *);
121570af302Sopenharmony_ci	break; case DBL:	arg->f = va_arg(*ap, double);
122570af302Sopenharmony_ci	break; case LDBL:	arg->f = va_arg(*ap, long double);
123570af302Sopenharmony_ci	}
124570af302Sopenharmony_ci}
125570af302Sopenharmony_ci
126570af302Sopenharmony_cistatic void out(FILE *f, const wchar_t *s, size_t l)
127570af302Sopenharmony_ci{
128570af302Sopenharmony_ci	while (l-- && !ferror(f)) fputwc(*s++, f);
129570af302Sopenharmony_ci}
130570af302Sopenharmony_ci
131570af302Sopenharmony_cistatic void pad(FILE *f, int n, int fl)
132570af302Sopenharmony_ci{
133570af302Sopenharmony_ci	if ((fl & LEFT_ADJ) || !n || ferror(f)) return;
134570af302Sopenharmony_ci	fprintf(f, "%*s", n, "");
135570af302Sopenharmony_ci}
136570af302Sopenharmony_ci
137570af302Sopenharmony_cistatic int getint(wchar_t **s) {
138570af302Sopenharmony_ci	int i;
139570af302Sopenharmony_ci	for (i=0; iswdigit(**s); (*s)++) {
140570af302Sopenharmony_ci		if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
141570af302Sopenharmony_ci		else i = 10*i + (**s-'0');
142570af302Sopenharmony_ci	}
143570af302Sopenharmony_ci	return i;
144570af302Sopenharmony_ci}
145570af302Sopenharmony_ci
146570af302Sopenharmony_cistatic const char sizeprefix['y'-'a'] = {
147570af302Sopenharmony_ci['a'-'a']='L', ['e'-'a']='L', ['f'-'a']='L', ['g'-'a']='L',
148570af302Sopenharmony_ci['d'-'a']='j', ['i'-'a']='j', ['o'-'a']='j', ['u'-'a']='j', ['x'-'a']='j',
149570af302Sopenharmony_ci['p'-'a']='j'
150570af302Sopenharmony_ci};
151570af302Sopenharmony_ci
152570af302Sopenharmony_cistatic int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
153570af302Sopenharmony_ci{
154570af302Sopenharmony_ci	wchar_t *a, *z, *s=(wchar_t *)fmt;
155570af302Sopenharmony_ci	unsigned l10n=0, fl;
156570af302Sopenharmony_ci	int w, p, xp;
157570af302Sopenharmony_ci	union arg arg;
158570af302Sopenharmony_ci	int argpos;
159570af302Sopenharmony_ci	unsigned st, ps;
160570af302Sopenharmony_ci	int cnt=0, l=0;
161570af302Sopenharmony_ci	int i;
162570af302Sopenharmony_ci	int t;
163570af302Sopenharmony_ci	char *bs;
164570af302Sopenharmony_ci	char charfmt[16];
165570af302Sopenharmony_ci	wchar_t wc;
166570af302Sopenharmony_ci
167570af302Sopenharmony_ci	for (;;) {
168570af302Sopenharmony_ci		/* This error is only specified for snprintf, but since it's
169570af302Sopenharmony_ci		 * unspecified for other forms, do the same. Stop immediately
170570af302Sopenharmony_ci		 * on overflow; otherwise %n could produce wrong results. */
171570af302Sopenharmony_ci		if (l > INT_MAX - cnt) goto overflow;
172570af302Sopenharmony_ci
173570af302Sopenharmony_ci		/* Update output count, end loop when fmt is exhausted */
174570af302Sopenharmony_ci		cnt += l;
175570af302Sopenharmony_ci		if (!*s) break;
176570af302Sopenharmony_ci
177570af302Sopenharmony_ci		/* Handle literal text and %% format specifiers */
178570af302Sopenharmony_ci		for (a=s; *s && *s!='%'; s++);
179570af302Sopenharmony_ci		for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
180570af302Sopenharmony_ci		if (z-a > INT_MAX-cnt) goto overflow;
181570af302Sopenharmony_ci		l = z-a;
182570af302Sopenharmony_ci		if (f) out(f, a, l);
183570af302Sopenharmony_ci		if (l) continue;
184570af302Sopenharmony_ci
185570af302Sopenharmony_ci		if (iswdigit(s[1]) && s[2]=='$') {
186570af302Sopenharmony_ci			l10n=1;
187570af302Sopenharmony_ci			argpos = s[1]-'0';
188570af302Sopenharmony_ci			s+=3;
189570af302Sopenharmony_ci		} else {
190570af302Sopenharmony_ci			argpos = -1;
191570af302Sopenharmony_ci			s++;
192570af302Sopenharmony_ci		}
193570af302Sopenharmony_ci
194570af302Sopenharmony_ci		/* Read modifier flags */
195570af302Sopenharmony_ci		for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
196570af302Sopenharmony_ci			fl |= 1U<<*s-' ';
197570af302Sopenharmony_ci
198570af302Sopenharmony_ci		/* Read field width */
199570af302Sopenharmony_ci		if (*s=='*') {
200570af302Sopenharmony_ci			if (iswdigit(s[1]) && s[2]=='$') {
201570af302Sopenharmony_ci				l10n=1;
202570af302Sopenharmony_ci				nl_type[s[1]-'0'] = INT;
203570af302Sopenharmony_ci				w = nl_arg[s[1]-'0'].i;
204570af302Sopenharmony_ci				s+=3;
205570af302Sopenharmony_ci			} else if (!l10n) {
206570af302Sopenharmony_ci				w = f ? va_arg(*ap, int) : 0;
207570af302Sopenharmony_ci				s++;
208570af302Sopenharmony_ci			} else goto inval;
209570af302Sopenharmony_ci			if (w<0) fl|=LEFT_ADJ, w=-w;
210570af302Sopenharmony_ci		} else if ((w=getint(&s))<0) goto overflow;
211570af302Sopenharmony_ci
212570af302Sopenharmony_ci		/* Read precision */
213570af302Sopenharmony_ci		if (*s=='.' && s[1]=='*') {
214570af302Sopenharmony_ci			if (isdigit(s[2]) && s[3]=='$') {
215570af302Sopenharmony_ci				nl_type[s[2]-'0'] = INT;
216570af302Sopenharmony_ci				p = nl_arg[s[2]-'0'].i;
217570af302Sopenharmony_ci				s+=4;
218570af302Sopenharmony_ci			} else if (!l10n) {
219570af302Sopenharmony_ci				p = f ? va_arg(*ap, int) : 0;
220570af302Sopenharmony_ci				s+=2;
221570af302Sopenharmony_ci			} else goto inval;
222570af302Sopenharmony_ci			xp = (p>=0);
223570af302Sopenharmony_ci		} else if (*s=='.') {
224570af302Sopenharmony_ci			s++;
225570af302Sopenharmony_ci			p = getint(&s);
226570af302Sopenharmony_ci			xp = 1;
227570af302Sopenharmony_ci		} else {
228570af302Sopenharmony_ci			p = -1;
229570af302Sopenharmony_ci			xp = 0;
230570af302Sopenharmony_ci		}
231570af302Sopenharmony_ci
232570af302Sopenharmony_ci		/* Format specifier state machine */
233570af302Sopenharmony_ci		st=0;
234570af302Sopenharmony_ci		do {
235570af302Sopenharmony_ci			if (OOB(*s)) goto inval;
236570af302Sopenharmony_ci			ps=st;
237570af302Sopenharmony_ci			st=states[st]S(*s++);
238570af302Sopenharmony_ci		} while (st-1<STOP);
239570af302Sopenharmony_ci		if (!st) goto inval;
240570af302Sopenharmony_ci
241570af302Sopenharmony_ci		/* Check validity of argument type (nl/normal) */
242570af302Sopenharmony_ci		if (st==NOARG) {
243570af302Sopenharmony_ci			if (argpos>=0) goto inval;
244570af302Sopenharmony_ci		} else {
245570af302Sopenharmony_ci			if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
246570af302Sopenharmony_ci			else if (f) pop_arg(&arg, st, ap);
247570af302Sopenharmony_ci			else return 0;
248570af302Sopenharmony_ci		}
249570af302Sopenharmony_ci
250570af302Sopenharmony_ci		if (!f) continue;
251570af302Sopenharmony_ci
252570af302Sopenharmony_ci		/* Do not process any new directives once in error state. */
253570af302Sopenharmony_ci		if (ferror(f)) return -1;
254570af302Sopenharmony_ci
255570af302Sopenharmony_ci		t = s[-1];
256570af302Sopenharmony_ci		if (ps && (t&15)==3) t&=~32;
257570af302Sopenharmony_ci
258570af302Sopenharmony_ci		switch (t) {
259570af302Sopenharmony_ci		case 'n':
260570af302Sopenharmony_ci			switch(ps) {
261570af302Sopenharmony_ci			case BARE: *(int *)arg.p = cnt; break;
262570af302Sopenharmony_ci			case LPRE: *(long *)arg.p = cnt; break;
263570af302Sopenharmony_ci			case LLPRE: *(long long *)arg.p = cnt; break;
264570af302Sopenharmony_ci			case HPRE: *(unsigned short *)arg.p = cnt; break;
265570af302Sopenharmony_ci			case HHPRE: *(unsigned char *)arg.p = cnt; break;
266570af302Sopenharmony_ci			case ZTPRE: *(size_t *)arg.p = cnt; break;
267570af302Sopenharmony_ci			case JPRE: *(uintmax_t *)arg.p = cnt; break;
268570af302Sopenharmony_ci			}
269570af302Sopenharmony_ci			continue;
270570af302Sopenharmony_ci		case 'c':
271570af302Sopenharmony_ci		case 'C':
272570af302Sopenharmony_ci			if (w<1) w=1;
273570af302Sopenharmony_ci			pad(f, w-1, fl);
274570af302Sopenharmony_ci			out(f, &(wchar_t){t=='C' ? arg.i : btowc(arg.i)}, 1);
275570af302Sopenharmony_ci			pad(f, w-1, fl^LEFT_ADJ);
276570af302Sopenharmony_ci			l = w;
277570af302Sopenharmony_ci			continue;
278570af302Sopenharmony_ci		case 'S':
279570af302Sopenharmony_ci			a = arg.p;
280570af302Sopenharmony_ci			z = a + wcsnlen(a, p<0 ? INT_MAX : p);
281570af302Sopenharmony_ci			if (p<0 && *z) goto overflow;
282570af302Sopenharmony_ci			p = z-a;
283570af302Sopenharmony_ci			if (w<p) w=p;
284570af302Sopenharmony_ci			pad(f, w-p, fl);
285570af302Sopenharmony_ci			out(f, a, p);
286570af302Sopenharmony_ci			pad(f, w-p, fl^LEFT_ADJ);
287570af302Sopenharmony_ci			l=w;
288570af302Sopenharmony_ci			continue;
289570af302Sopenharmony_ci		case 'm':
290570af302Sopenharmony_ci			arg.p = strerror(errno);
291570af302Sopenharmony_ci		case 's':
292570af302Sopenharmony_ci			if (!arg.p) arg.p = "(null)";
293570af302Sopenharmony_ci			bs = arg.p;
294570af302Sopenharmony_ci			for (i=l=0; l<(p<0?INT_MAX:p) && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
295570af302Sopenharmony_ci			if (i<0) return -1;
296570af302Sopenharmony_ci			if (p<0 && *bs) goto overflow;
297570af302Sopenharmony_ci			p=l;
298570af302Sopenharmony_ci			if (w<p) w=p;
299570af302Sopenharmony_ci			pad(f, w-p, fl);
300570af302Sopenharmony_ci			bs = arg.p;
301570af302Sopenharmony_ci			while (l--) {
302570af302Sopenharmony_ci				i=mbtowc(&wc, bs, MB_LEN_MAX);
303570af302Sopenharmony_ci				bs+=i;
304570af302Sopenharmony_ci				out(f, &wc, 1);
305570af302Sopenharmony_ci			}
306570af302Sopenharmony_ci			pad(f, w-p, fl^LEFT_ADJ);
307570af302Sopenharmony_ci			l=w;
308570af302Sopenharmony_ci			continue;
309570af302Sopenharmony_ci		}
310570af302Sopenharmony_ci
311570af302Sopenharmony_ci		if (xp && p<0) goto overflow;
312570af302Sopenharmony_ci		snprintf(charfmt, sizeof charfmt, "%%%s%s%s%s%s*.*%c%c",
313570af302Sopenharmony_ci			"#"+!(fl & ALT_FORM),
314570af302Sopenharmony_ci			"+"+!(fl & MARK_POS),
315570af302Sopenharmony_ci			"-"+!(fl & LEFT_ADJ),
316570af302Sopenharmony_ci			" "+!(fl & PAD_POS),
317570af302Sopenharmony_ci			"0"+!(fl & ZERO_PAD),
318570af302Sopenharmony_ci			sizeprefix[(t|32)-'a'], t);
319570af302Sopenharmony_ci
320570af302Sopenharmony_ci		switch (t|32) {
321570af302Sopenharmony_ci		case 'a': case 'e': case 'f': case 'g':
322570af302Sopenharmony_ci			l = fprintf(f, charfmt, w, p, arg.f);
323570af302Sopenharmony_ci			break;
324570af302Sopenharmony_ci		case 'd': case 'i': case 'o': case 'u': case 'x': case 'p':
325570af302Sopenharmony_ci			l = fprintf(f, charfmt, w, p, arg.i);
326570af302Sopenharmony_ci			break;
327570af302Sopenharmony_ci		}
328570af302Sopenharmony_ci	}
329570af302Sopenharmony_ci
330570af302Sopenharmony_ci	if (f) return cnt;
331570af302Sopenharmony_ci	if (!l10n) return 0;
332570af302Sopenharmony_ci
333570af302Sopenharmony_ci	for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
334570af302Sopenharmony_ci		pop_arg(nl_arg+i, nl_type[i], ap);
335570af302Sopenharmony_ci	for (; i<=NL_ARGMAX && !nl_type[i]; i++);
336570af302Sopenharmony_ci	if (i<=NL_ARGMAX) return -1;
337570af302Sopenharmony_ci	return 1;
338570af302Sopenharmony_ci
339570af302Sopenharmony_ciinval:
340570af302Sopenharmony_ci	errno = EINVAL;
341570af302Sopenharmony_ci	return -1;
342570af302Sopenharmony_cioverflow:
343570af302Sopenharmony_ci	errno = EOVERFLOW;
344570af302Sopenharmony_ci	return -1;
345570af302Sopenharmony_ci}
346570af302Sopenharmony_ci
347570af302Sopenharmony_ciint vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
348570af302Sopenharmony_ci{
349570af302Sopenharmony_ci	va_list ap2;
350570af302Sopenharmony_ci	int nl_type[NL_ARGMAX+1] = {0};
351570af302Sopenharmony_ci	union arg nl_arg[NL_ARGMAX+1];
352570af302Sopenharmony_ci	int olderr;
353570af302Sopenharmony_ci	int ret;
354570af302Sopenharmony_ci
355570af302Sopenharmony_ci	/* the copy allows passing va_list* even if va_list is an array */
356570af302Sopenharmony_ci	va_copy(ap2, ap);
357570af302Sopenharmony_ci	if (wprintf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
358570af302Sopenharmony_ci		va_end(ap2);
359570af302Sopenharmony_ci		return -1;
360570af302Sopenharmony_ci	}
361570af302Sopenharmony_ci
362570af302Sopenharmony_ci	FLOCK(f);
363570af302Sopenharmony_ci	fwide(f, 1);
364570af302Sopenharmony_ci	olderr = f->flags & F_ERR;
365570af302Sopenharmony_ci	f->flags &= ~F_ERR;
366570af302Sopenharmony_ci	ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
367570af302Sopenharmony_ci	if (ferror(f)) ret = -1;
368570af302Sopenharmony_ci	f->flags |= olderr;
369570af302Sopenharmony_ci	FUNLOCK(f);
370570af302Sopenharmony_ci	va_end(ap2);
371570af302Sopenharmony_ci	return ret;
372570af302Sopenharmony_ci}
373