xref: /aosp_15_r20/external/musl/src/stdio/vfprintf.c (revision c9945492fdd68bbe62686c5b452b4dc1be3f8453)
1 #include "stdio_impl.h"
2 #include <errno.h>
3 #include <ctype.h>
4 #include <limits.h>
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <wchar.h>
10 #include <inttypes.h>
11 #include <math.h>
12 #include <float.h>
13 
14 /* Some useful macros */
15 
16 #define MAX(a,b) ((a)>(b) ? (a) : (b))
17 #define MIN(a,b) ((a)<(b) ? (a) : (b))
18 
19 /* Convenient bit representation for modifier flags, which all fall
20  * within 31 codepoints of the space character. */
21 
22 #define ALT_FORM   (1U<<'#'-' ')
23 #define ZERO_PAD   (1U<<'0'-' ')
24 #define LEFT_ADJ   (1U<<'-'-' ')
25 #define PAD_POS    (1U<<' '-' ')
26 #define MARK_POS   (1U<<'+'-' ')
27 #define GROUPED    (1U<<'\''-' ')
28 
29 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
30 
31 /* State machine to accept length modifiers + conversion specifiers.
32  * Result is 0 on failure, or an argument type to pop on success. */
33 
34 enum {
35 	BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
36 	ZTPRE, JPRE,
37 	STOP,
38 	PTR, INT, UINT, ULLONG,
39 	LONG, ULONG,
40 	SHORT, USHORT, CHAR, UCHAR,
41 	LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
42 	DBL, LDBL,
43 	NOARG,
44 	MAXSTATE
45 };
46 
47 #define S(x) [(x)-'A']
48 
49 static const unsigned char states[]['z'-'A'+1] = {
50 	{ /* 0: bare types */
51 		S('d') = INT, S('i') = INT,
52 		S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
53 		S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
54 		S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
55 		S('c') = INT, S('C') = UINT,
56 		S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
57 		S('m') = NOARG,
58 		S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
59 		S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
60 	}, { /* 1: l-prefixed */
61 		S('d') = LONG, S('i') = LONG,
62 		S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
63 		S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
64 		S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
65 		S('c') = UINT, S('s') = PTR, S('n') = PTR,
66 		S('l') = LLPRE,
67 	}, { /* 2: ll-prefixed */
68 		S('d') = LLONG, S('i') = LLONG,
69 		S('o') = ULLONG, S('u') = ULLONG,
70 		S('x') = ULLONG, S('X') = ULLONG,
71 		S('n') = PTR,
72 	}, { /* 3: h-prefixed */
73 		S('d') = SHORT, S('i') = SHORT,
74 		S('o') = USHORT, S('u') = USHORT,
75 		S('x') = USHORT, S('X') = USHORT,
76 		S('n') = PTR,
77 		S('h') = HHPRE,
78 	}, { /* 4: hh-prefixed */
79 		S('d') = CHAR, S('i') = CHAR,
80 		S('o') = UCHAR, S('u') = UCHAR,
81 		S('x') = UCHAR, S('X') = UCHAR,
82 		S('n') = PTR,
83 	}, { /* 5: L-prefixed */
84 		S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
85 		S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
86 		S('n') = PTR,
87 	}, { /* 6: z- or t-prefixed (assumed to be same size) */
88 		S('d') = PDIFF, S('i') = PDIFF,
89 		S('o') = SIZET, S('u') = SIZET,
90 		S('x') = SIZET, S('X') = SIZET,
91 		S('n') = PTR,
92 	}, { /* 7: j-prefixed */
93 		S('d') = IMAX, S('i') = IMAX,
94 		S('o') = UMAX, S('u') = UMAX,
95 		S('x') = UMAX, S('X') = UMAX,
96 		S('n') = PTR,
97 	}
98 };
99 
100 #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
101 
102 union arg
103 {
104 	uintmax_t i;
105 	long double f;
106 	void *p;
107 };
108 
pop_arg(union arg * arg,int type,va_list * ap)109 static void pop_arg(union arg *arg, int type, va_list *ap)
110 {
111 	switch (type) {
112 	       case PTR:	arg->p = va_arg(*ap, void *);
113 	break; case INT:	arg->i = va_arg(*ap, int);
114 	break; case UINT:	arg->i = va_arg(*ap, unsigned int);
115 	break; case LONG:	arg->i = va_arg(*ap, long);
116 	break; case ULONG:	arg->i = va_arg(*ap, unsigned long);
117 	break; case ULLONG:	arg->i = va_arg(*ap, unsigned long long);
118 	break; case SHORT:	arg->i = (short)va_arg(*ap, int);
119 	break; case USHORT:	arg->i = (unsigned short)va_arg(*ap, int);
120 	break; case CHAR:	arg->i = (signed char)va_arg(*ap, int);
121 	break; case UCHAR:	arg->i = (unsigned char)va_arg(*ap, int);
122 	break; case LLONG:	arg->i = va_arg(*ap, long long);
123 	break; case SIZET:	arg->i = va_arg(*ap, size_t);
124 	break; case IMAX:	arg->i = va_arg(*ap, intmax_t);
125 	break; case UMAX:	arg->i = va_arg(*ap, uintmax_t);
126 	break; case PDIFF:	arg->i = va_arg(*ap, ptrdiff_t);
127 	break; case UIPTR:	arg->i = (uintptr_t)va_arg(*ap, void *);
128 	break; case DBL:	arg->f = va_arg(*ap, double);
129 	break; case LDBL:	arg->f = va_arg(*ap, long double);
130 	}
131 }
132 
out(FILE * f,const char * s,size_t l)133 static void out(FILE *f, const char *s, size_t l)
134 {
135 	if (!ferror(f)) __fwritex((void *)s, l, f);
136 }
137 
pad(FILE * f,char c,int w,int l,int fl)138 static void pad(FILE *f, char c, int w, int l, int fl)
139 {
140 	char pad[256];
141 	if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
142 	l = w - l;
143 	memset(pad, c, l>sizeof pad ? sizeof pad : l);
144 	for (; l >= sizeof pad; l -= sizeof pad)
145 		out(f, pad, sizeof pad);
146 	out(f, pad, l);
147 }
148 
149 static const char xdigits[16] = {
150 	"0123456789ABCDEF"
151 };
152 
fmt_x(uintmax_t x,char * s,int lower)153 static char *fmt_x(uintmax_t x, char *s, int lower)
154 {
155 	for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
156 	return s;
157 }
158 
fmt_o(uintmax_t x,char * s)159 static char *fmt_o(uintmax_t x, char *s)
160 {
161 	for (; x; x>>=3) *--s = '0' + (x&7);
162 	return s;
163 }
164 
fmt_u(uintmax_t x,char * s)165 static char *fmt_u(uintmax_t x, char *s)
166 {
167 	unsigned long y;
168 	for (   ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
169 	for (y=x;       y>=10; y/=10) *--s = '0' + y%10;
170 	if (y) *--s = '0' + y;
171 	return s;
172 }
173 
174 /* Do not override this check. The floating point printing code below
175  * depends on the float.h constants being right. If they are wrong, it
176  * may overflow the stack. */
177 #if LDBL_MANT_DIG == 53
178 typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)];
179 #endif
180 
fmt_fp(FILE * f,long double y,int w,int p,int fl,int t)181 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
182 {
183 	uint32_t big[(LDBL_MANT_DIG+28)/29 + 1          // mantissa expansion
184 		+ (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
185 	uint32_t *a, *d, *r, *z;
186 	int e2=0, e, i, j, l;
187 	char buf[9+LDBL_MANT_DIG/4], *s;
188 	const char *prefix="-0X+0X 0X-0x+0x 0x";
189 	int pl;
190 	char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
191 
192 	pl=1;
193 	if (signbit(y)) {
194 		y=-y;
195 	} else if (fl & MARK_POS) {
196 		prefix+=3;
197 	} else if (fl & PAD_POS) {
198 		prefix+=6;
199 	} else prefix++, pl=0;
200 
201 	if (!isfinite(y)) {
202 		char *s = (t&32)?"inf":"INF";
203 		if (y!=y) s=(t&32)?"nan":"NAN";
204 		pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
205 		out(f, prefix, pl);
206 		out(f, s, 3);
207 		pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
208 		return MAX(w, 3+pl);
209 	}
210 
211 	y = frexpl(y, &e2) * 2;
212 	if (y) e2--;
213 
214 	if ((t|32)=='a') {
215 		if (t&32) prefix += 9;
216 		pl += 2;
217 
218 		if (p>=0 && p<(LDBL_MANT_DIG-1+3)/4) {
219 			double round = scalbn(1, LDBL_MANT_DIG-1-(p*4));
220 			if (*prefix=='-') {
221 				y=-y;
222 				y-=round;
223 				y+=round;
224 				y=-y;
225 			} else {
226 				y+=round;
227 				y-=round;
228 			}
229 		}
230 
231 		estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
232 		if (estr==ebuf) *--estr='0';
233 		*--estr = (e2<0 ? '-' : '+');
234 		*--estr = t+('p'-'a');
235 
236 		s=buf;
237 		do {
238 			int x=y;
239 			*s++=xdigits[x]|(t&32);
240 			y=16*(y-x);
241 			if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
242 		} while (y);
243 
244 		if (p > INT_MAX-2-(ebuf-estr)-pl)
245 			return -1;
246 		if (p && s-buf-2 < p)
247 			l = (p+2) + (ebuf-estr);
248 		else
249 			l = (s-buf) + (ebuf-estr);
250 
251 		pad(f, ' ', w, pl+l, fl);
252 		out(f, prefix, pl);
253 		pad(f, '0', w, pl+l, fl^ZERO_PAD);
254 		out(f, buf, s-buf);
255 		pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
256 		out(f, estr, ebuf-estr);
257 		pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
258 		return MAX(w, pl+l);
259 	}
260 	if (p<0) p=6;
261 
262 	if (y) y *= 0x1p28, e2-=28;
263 
264 	if (e2<0) a=r=z=big;
265 	else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
266 
267 	do {
268 		*z = y;
269 		y = 1000000000*(y-*z++);
270 	} while (y);
271 
272 	while (e2>0) {
273 		uint32_t carry=0;
274 		int sh=MIN(29,e2);
275 		for (d=z-1; d>=a; d--) {
276 			uint64_t x = ((uint64_t)*d<<sh)+carry;
277 			*d = x % 1000000000;
278 			carry = x / 1000000000;
279 		}
280 		if (carry) *--a = carry;
281 		while (z>a && !z[-1]) z--;
282 		e2-=sh;
283 	}
284 	while (e2<0) {
285 		uint32_t carry=0, *b;
286 		int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3U+8)/9;
287 		for (d=a; d<z; d++) {
288 			uint32_t rm = *d & (1<<sh)-1;
289 			*d = (*d>>sh) + carry;
290 			carry = (1000000000>>sh) * rm;
291 		}
292 		if (!*a) a++;
293 		if (carry) *z++ = carry;
294 		/* Avoid (slow!) computation past requested precision */
295 		b = (t|32)=='f' ? r : a;
296 		if (z-b > need) z = b+need;
297 		e2+=sh;
298 	}
299 
300 	if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
301 	else e=0;
302 
303 	/* Perform rounding: j is precision after the radix (possibly neg) */
304 	j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
305 	if (j < 9*(z-r-1)) {
306 		uint32_t x;
307 		/* We avoid C's broken division of negative numbers */
308 		d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
309 		j += 9*LDBL_MAX_EXP;
310 		j %= 9;
311 		for (i=10, j++; j<9; i*=10, j++);
312 		x = *d % i;
313 		/* Are there any significant digits past j? */
314 		if (x || d+1!=z) {
315 			long double round = 2/LDBL_EPSILON;
316 			long double small;
317 			if ((*d/i & 1) || (i==1000000000 && d>a && (d[-1]&1)))
318 				round += 2;
319 			if (x<i/2) small=0x0.8p0;
320 			else if (x==i/2 && d+1==z) small=0x1.0p0;
321 			else small=0x1.8p0;
322 			if (pl && *prefix=='-') round*=-1, small*=-1;
323 			*d -= x;
324 			/* Decide whether to round by probing round+small */
325 			if (round+small != round) {
326 				*d = *d + i;
327 				while (*d > 999999999) {
328 					*d--=0;
329 					if (d<a) *--a=0;
330 					(*d)++;
331 				}
332 				for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
333 			}
334 		}
335 		if (z>d+1) z=d+1;
336 	}
337 	for (; z>a && !z[-1]; z--);
338 
339 	if ((t|32)=='g') {
340 		if (!p) p++;
341 		if (p>e && e>=-4) {
342 			t--;
343 			p-=e+1;
344 		} else {
345 			t-=2;
346 			p--;
347 		}
348 		if (!(fl&ALT_FORM)) {
349 			/* Count trailing zeros in last place */
350 			if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
351 			else j=9;
352 			if ((t|32)=='f')
353 				p = MIN(p,MAX(0,9*(z-r-1)-j));
354 			else
355 				p = MIN(p,MAX(0,9*(z-r-1)+e-j));
356 		}
357 	}
358 	if (p > INT_MAX-1-(p || (fl&ALT_FORM)))
359 		return -1;
360 	l = 1 + p + (p || (fl&ALT_FORM));
361 	if ((t|32)=='f') {
362 		if (e > INT_MAX-l) return -1;
363 		if (e>0) l+=e;
364 	} else {
365 		estr=fmt_u(e<0 ? -e : e, ebuf);
366 		while(ebuf-estr<2) *--estr='0';
367 		*--estr = (e<0 ? '-' : '+');
368 		*--estr = t;
369 		if (ebuf-estr > INT_MAX-l) return -1;
370 		l += ebuf-estr;
371 	}
372 
373 	if (l > INT_MAX-pl) return -1;
374 	pad(f, ' ', w, pl+l, fl);
375 	out(f, prefix, pl);
376 	pad(f, '0', w, pl+l, fl^ZERO_PAD);
377 
378 	if ((t|32)=='f') {
379 		if (a>r) a=r;
380 		for (d=a; d<=r; d++) {
381 			char *s = fmt_u(*d, buf+9);
382 			if (d!=a) while (s>buf) *--s='0';
383 			else if (s==buf+9) *--s='0';
384 			out(f, s, buf+9-s);
385 		}
386 		if (p || (fl&ALT_FORM)) out(f, ".", 1);
387 		for (; d<z && p>0; d++, p-=9) {
388 			char *s = fmt_u(*d, buf+9);
389 			while (s>buf) *--s='0';
390 			out(f, s, MIN(9,p));
391 		}
392 		pad(f, '0', p+9, 9, 0);
393 	} else {
394 		if (z<=a) z=a+1;
395 		for (d=a; d<z && p>=0; d++) {
396 			char *s = fmt_u(*d, buf+9);
397 			if (s==buf+9) *--s='0';
398 			if (d!=a) while (s>buf) *--s='0';
399 			else {
400 				out(f, s++, 1);
401 				if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
402 			}
403 			out(f, s, MIN(buf+9-s, p));
404 			p -= buf+9-s;
405 		}
406 		pad(f, '0', p+18, 18, 0);
407 		out(f, estr, ebuf-estr);
408 	}
409 
410 	pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
411 
412 	return MAX(w, pl+l);
413 }
414 
getint(char ** s)415 static int getint(char **s) {
416 	int i;
417 	for (i=0; isdigit(**s); (*s)++) {
418 		if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
419 		else i = 10*i + (**s-'0');
420 	}
421 	return i;
422 }
423 
printf_core(FILE * f,const char * fmt,va_list * ap,union arg * nl_arg,int * nl_type)424 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
425 {
426 	char *a, *z, *s=(char *)fmt;
427 	unsigned l10n=0, fl;
428 	int w, p, xp;
429 	union arg arg;
430 	int argpos;
431 	unsigned st, ps;
432 	int cnt=0, l=0;
433 	size_t i;
434 	char buf[sizeof(uintmax_t)*3];
435 	const char *prefix;
436 	int t, pl;
437 	wchar_t wc[2], *ws;
438 	char mb[4];
439 
440 	for (;;) {
441 		/* This error is only specified for snprintf, but since it's
442 		 * unspecified for other forms, do the same. Stop immediately
443 		 * on overflow; otherwise %n could produce wrong results. */
444 		if (l > INT_MAX - cnt) goto overflow;
445 
446 		/* Update output count, end loop when fmt is exhausted */
447 		cnt += l;
448 		if (!*s) break;
449 
450 		/* Handle literal text and %% format specifiers */
451 		for (a=s; *s && *s!='%'; s++);
452 		for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
453 		if (z-a > INT_MAX-cnt) goto overflow;
454 		l = z-a;
455 		if (f) out(f, a, l);
456 		if (l) continue;
457 
458 		if (isdigit(s[1]) && s[2]=='$') {
459 			l10n=1;
460 			argpos = s[1]-'0';
461 			s+=3;
462 		} else {
463 			argpos = -1;
464 			s++;
465 		}
466 
467 		/* Read modifier flags */
468 		for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
469 			fl |= 1U<<*s-' ';
470 
471 		/* Read field width */
472 		if (*s=='*') {
473 			if (isdigit(s[1]) && s[2]=='$') {
474 				l10n=1;
475 				if (!f) nl_type[s[1]-'0'] = INT, w = 0;
476 				else w = nl_arg[s[1]-'0'].i;
477 				s+=3;
478 			} else if (!l10n) {
479 				w = f ? va_arg(*ap, int) : 0;
480 				s++;
481 			} else goto inval;
482 			if (w<0) fl|=LEFT_ADJ, w=-w;
483 		} else if ((w=getint(&s))<0) goto overflow;
484 
485 		/* Read precision */
486 		if (*s=='.' && s[1]=='*') {
487 			if (isdigit(s[2]) && s[3]=='$') {
488 				if (!f) nl_type[s[2]-'0'] = INT, p = 0;
489 				else p = nl_arg[s[2]-'0'].i;
490 				s+=4;
491 			} else if (!l10n) {
492 				p = f ? va_arg(*ap, int) : 0;
493 				s+=2;
494 			} else goto inval;
495 			xp = (p>=0);
496 		} else if (*s=='.') {
497 			s++;
498 			p = getint(&s);
499 			xp = 1;
500 		} else {
501 			p = -1;
502 			xp = 0;
503 		}
504 
505 		/* Format specifier state machine */
506 		st=0;
507 		do {
508 			if (OOB(*s)) goto inval;
509 			ps=st;
510 			st=states[st]S(*s++);
511 		} while (st-1<STOP);
512 		if (!st) goto inval;
513 
514 		/* Check validity of argument type (nl/normal) */
515 		if (st==NOARG) {
516 			if (argpos>=0) goto inval;
517 		} else {
518 			if (argpos>=0) {
519 				if (!f) nl_type[argpos]=st;
520 				else arg=nl_arg[argpos];
521 			} else if (f) pop_arg(&arg, st, ap);
522 			else return 0;
523 		}
524 
525 		if (!f) continue;
526 
527 		/* Do not process any new directives once in error state. */
528 		if (ferror(f)) return -1;
529 
530 		z = buf + sizeof(buf);
531 		prefix = "-+   0X0x";
532 		pl = 0;
533 		t = s[-1];
534 
535 		/* Transform ls,lc -> S,C */
536 		if (ps && (t&15)==3) t&=~32;
537 
538 		/* - and 0 flags are mutually exclusive */
539 		if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
540 
541 		switch(t) {
542 		case 'n':
543 			switch(ps) {
544 			case BARE: *(int *)arg.p = cnt; break;
545 			case LPRE: *(long *)arg.p = cnt; break;
546 			case LLPRE: *(long long *)arg.p = cnt; break;
547 			case HPRE: *(unsigned short *)arg.p = cnt; break;
548 			case HHPRE: *(unsigned char *)arg.p = cnt; break;
549 			case ZTPRE: *(size_t *)arg.p = cnt; break;
550 			case JPRE: *(uintmax_t *)arg.p = cnt; break;
551 			}
552 			continue;
553 		case 'p':
554 			p = MAX(p, 2*sizeof(void*));
555 			t = 'x';
556 			fl |= ALT_FORM;
557 		case 'x': case 'X':
558 			a = fmt_x(arg.i, z, t&32);
559 			if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
560 			if (0) {
561 		case 'o':
562 			a = fmt_o(arg.i, z);
563 			if ((fl&ALT_FORM) && p<z-a+1) p=z-a+1;
564 			} if (0) {
565 		case 'd': case 'i':
566 			pl=1;
567 			if (arg.i>INTMAX_MAX) {
568 				arg.i=-arg.i;
569 			} else if (fl & MARK_POS) {
570 				prefix++;
571 			} else if (fl & PAD_POS) {
572 				prefix+=2;
573 			} else pl=0;
574 		case 'u':
575 			a = fmt_u(arg.i, z);
576 			}
577 			if (xp && p<0) goto overflow;
578 			if (xp) fl &= ~ZERO_PAD;
579 			if (!arg.i && !p) {
580 				a=z;
581 				break;
582 			}
583 			p = MAX(p, z-a + !arg.i);
584 			break;
585 		narrow_c:
586 		case 'c':
587 			*(a=z-(p=1))=arg.i;
588 			fl &= ~ZERO_PAD;
589 			break;
590 		case 'm':
591 			if (1) a = strerror(errno); else
592 		case 's':
593 			a = arg.p ? arg.p : "(null)";
594 			z = a + strnlen(a, p<0 ? INT_MAX : p);
595 			if (p<0 && *z) goto overflow;
596 			p = z-a;
597 			fl &= ~ZERO_PAD;
598 			break;
599 		case 'C':
600 			if (!arg.i) goto narrow_c;
601 			wc[0] = arg.i;
602 			wc[1] = 0;
603 			arg.p = wc;
604 			p = -1;
605 		case 'S':
606 			ws = arg.p;
607 			for (i=l=0; i<p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=p-i; i+=l);
608 			if (l<0) return -1;
609 			if (i > INT_MAX) goto overflow;
610 			p = i;
611 			pad(f, ' ', w, p, fl);
612 			ws = arg.p;
613 			for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
614 				out(f, mb, l);
615 			pad(f, ' ', w, p, fl^LEFT_ADJ);
616 			l = w>p ? w : p;
617 			continue;
618 		case 'e': case 'f': case 'g': case 'a':
619 		case 'E': case 'F': case 'G': case 'A':
620 			if (xp && p<0) goto overflow;
621 			l = fmt_fp(f, arg.f, w, p, fl, t);
622 			if (l<0) goto overflow;
623 			continue;
624 		}
625 
626 		if (p < z-a) p = z-a;
627 		if (p > INT_MAX-pl) goto overflow;
628 		if (w < pl+p) w = pl+p;
629 		if (w > INT_MAX-cnt) goto overflow;
630 
631 		pad(f, ' ', w, pl+p, fl);
632 		out(f, prefix, pl);
633 		pad(f, '0', w, pl+p, fl^ZERO_PAD);
634 		pad(f, '0', p, z-a, 0);
635 		out(f, a, z-a);
636 		pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
637 
638 		l = w;
639 	}
640 
641 	if (f) return cnt;
642 	if (!l10n) return 0;
643 
644 	for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
645 		pop_arg(nl_arg+i, nl_type[i], ap);
646 	for (; i<=NL_ARGMAX && !nl_type[i]; i++);
647 	if (i<=NL_ARGMAX) goto inval;
648 	return 1;
649 
650 inval:
651 	errno = EINVAL;
652 	return -1;
653 overflow:
654 	errno = EOVERFLOW;
655 	return -1;
656 }
657 
vfprintf(FILE * restrict f,const char * restrict fmt,va_list ap)658 int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
659 {
660 	va_list ap2;
661 	int nl_type[NL_ARGMAX+1] = {0};
662 	union arg nl_arg[NL_ARGMAX+1];
663 	unsigned char internal_buf[80], *saved_buf = 0;
664 	int olderr;
665 	int ret;
666 
667 	/* the copy allows passing va_list* even if va_list is an array */
668 	va_copy(ap2, ap);
669 	if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
670 		va_end(ap2);
671 		return -1;
672 	}
673 
674 	FLOCK(f);
675 	olderr = f->flags & F_ERR;
676 	f->flags &= ~F_ERR;
677 	if (!f->buf_size) {
678 		saved_buf = f->buf;
679 		f->buf = internal_buf;
680 		f->buf_size = sizeof internal_buf;
681 		f->wpos = f->wbase = f->wend = 0;
682 	}
683 	if (!f->wend && __towrite(f)) ret = -1;
684 	else ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
685 	if (saved_buf) {
686 		f->write(f, 0, 0);
687 		if (!f->wpos) ret = -1;
688 		f->buf = saved_buf;
689 		f->buf_size = 0;
690 		f->wpos = f->wbase = f->wend = 0;
691 	}
692 	if (ferror(f)) ret = -1;
693 	f->flags |= olderr;
694 	FUNLOCK(f);
695 	va_end(ap2);
696 	return ret;
697 }
698