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