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