fix printf precision specifier for hex floats on non-ld80 archs
[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                         round *= 1<<(LDBL_MANT_DIG%4);
224                         while (re--) round*=16;
225                         if (*prefix=='-') {
226                                 y=-y;
227                                 y-=round;
228                                 y+=round;
229                                 y=-y;
230                         } else {
231                                 y+=round;
232                                 y-=round;
233                         }
234                 }
235
236                 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
237                 if (estr==ebuf) *--estr='0';
238                 *--estr = (e2<0 ? '-' : '+');
239                 *--estr = t+('p'-'a');
240
241                 s=buf;
242                 do {
243                         int x=y;
244                         *s++=xdigits[x]|(t&32);
245                         y=16*(y-x);
246                         if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
247                 } while (y);
248
249                 if (p > INT_MAX-2-(ebuf-estr)-pl)
250                         return -1;
251                 if (p && s-buf-2 < p)
252                         l = (p+2) + (ebuf-estr);
253                 else
254                         l = (s-buf) + (ebuf-estr);
255
256                 pad(f, ' ', w, pl+l, fl);
257                 out(f, prefix, pl);
258                 pad(f, '0', w, pl+l, fl^ZERO_PAD);
259                 out(f, buf, s-buf);
260                 pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
261                 out(f, estr, ebuf-estr);
262                 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
263                 return MAX(w, pl+l);
264         }
265         if (p<0) p=6;
266
267         if (y) y *= 0x1p28, e2-=28;
268
269         if (e2<0) a=r=z=big;
270         else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
271
272         do {
273                 *z = y;
274                 y = 1000000000*(y-*z++);
275         } while (y);
276
277         while (e2>0) {
278                 uint32_t carry=0;
279                 int sh=MIN(29,e2);
280                 for (d=z-1; d>=a; d--) {
281                         uint64_t x = ((uint64_t)*d<<sh)+carry;
282                         *d = x % 1000000000;
283                         carry = x / 1000000000;
284                 }
285                 if (carry) *--a = carry;
286                 while (z>a && !z[-1]) z--;
287                 e2-=sh;
288         }
289         while (e2<0) {
290                 uint32_t carry=0, *b;
291                 int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3U+8)/9;
292                 for (d=a; d<z; d++) {
293                         uint32_t rm = *d & (1<<sh)-1;
294                         *d = (*d>>sh) + carry;
295                         carry = (1000000000>>sh) * rm;
296                 }
297                 if (!*a) a++;
298                 if (carry) *z++ = carry;
299                 /* Avoid (slow!) computation past requested precision */
300                 b = (t|32)=='f' ? r : a;
301                 if (z-b > need) z = b+need;
302                 e2+=sh;
303         }
304
305         if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
306         else e=0;
307
308         /* Perform rounding: j is precision after the radix (possibly neg) */
309         j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
310         if (j < 9*(z-r-1)) {
311                 uint32_t x;
312                 /* We avoid C's broken division of negative numbers */
313                 d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
314                 j += 9*LDBL_MAX_EXP;
315                 j %= 9;
316                 for (i=10, j++; j<9; i*=10, j++);
317                 x = *d % i;
318                 /* Are there any significant digits past j? */
319                 if (x || d+1!=z) {
320                         long double round = 2/LDBL_EPSILON;
321                         long double small;
322                         if ((*d/i & 1) || (i==1000000000 && d>a && (d[-1]&1)))
323                                 round += 2;
324                         if (x<i/2) small=0x0.8p0;
325                         else if (x==i/2 && d+1==z) small=0x1.0p0;
326                         else small=0x1.8p0;
327                         if (pl && *prefix=='-') round*=-1, small*=-1;
328                         *d -= x;
329                         /* Decide whether to round by probing round+small */
330                         if (round+small != round) {
331                                 *d = *d + i;
332                                 while (*d > 999999999) {
333                                         *d--=0;
334                                         if (d<a) *--a=0;
335                                         (*d)++;
336                                 }
337                                 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
338                         }
339                 }
340                 if (z>d+1) z=d+1;
341         }
342         for (; z>a && !z[-1]; z--);
343         
344         if ((t|32)=='g') {
345                 if (!p) p++;
346                 if (p>e && e>=-4) {
347                         t--;
348                         p-=e+1;
349                 } else {
350                         t-=2;
351                         p--;
352                 }
353                 if (!(fl&ALT_FORM)) {
354                         /* Count trailing zeros in last place */
355                         if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
356                         else j=9;
357                         if ((t|32)=='f')
358                                 p = MIN(p,MAX(0,9*(z-r-1)-j));
359                         else
360                                 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
361                 }
362         }
363         if (p > INT_MAX-1-(p || (fl&ALT_FORM)))
364                 return -1;
365         l = 1 + p + (p || (fl&ALT_FORM));
366         if ((t|32)=='f') {
367                 if (e > INT_MAX-l) return -1;
368                 if (e>0) l+=e;
369         } else {
370                 estr=fmt_u(e<0 ? -e : e, ebuf);
371                 while(ebuf-estr<2) *--estr='0';
372                 *--estr = (e<0 ? '-' : '+');
373                 *--estr = t;
374                 if (ebuf-estr > INT_MAX-l) return -1;
375                 l += ebuf-estr;
376         }
377
378         if (l > INT_MAX-pl) return -1;
379         pad(f, ' ', w, pl+l, fl);
380         out(f, prefix, pl);
381         pad(f, '0', w, pl+l, fl^ZERO_PAD);
382
383         if ((t|32)=='f') {
384                 if (a>r) a=r;
385                 for (d=a; d<=r; d++) {
386                         char *s = fmt_u(*d, buf+9);
387                         if (d!=a) while (s>buf) *--s='0';
388                         else if (s==buf+9) *--s='0';
389                         out(f, s, buf+9-s);
390                 }
391                 if (p || (fl&ALT_FORM)) out(f, ".", 1);
392                 for (; d<z && p>0; d++, p-=9) {
393                         char *s = fmt_u(*d, buf+9);
394                         while (s>buf) *--s='0';
395                         out(f, s, MIN(9,p));
396                 }
397                 pad(f, '0', p+9, 9, 0);
398         } else {
399                 if (z<=a) z=a+1;
400                 for (d=a; d<z && p>=0; d++) {
401                         char *s = fmt_u(*d, buf+9);
402                         if (s==buf+9) *--s='0';
403                         if (d!=a) while (s>buf) *--s='0';
404                         else {
405                                 out(f, s++, 1);
406                                 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
407                         }
408                         out(f, s, MIN(buf+9-s, p));
409                         p -= buf+9-s;
410                 }
411                 pad(f, '0', p+18, 18, 0);
412                 out(f, estr, ebuf-estr);
413         }
414
415         pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
416
417         return MAX(w, pl+l);
418 }
419
420 static int getint(char **s) {
421         int i;
422         for (i=0; isdigit(**s); (*s)++) {
423                 if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
424                 else i = 10*i + (**s-'0');
425         }
426         return i;
427 }
428
429 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
430 {
431         char *a, *z, *s=(char *)fmt;
432         unsigned l10n=0, fl;
433         int w, p, xp;
434         union arg arg;
435         int argpos;
436         unsigned st, ps;
437         int cnt=0, l=0;
438         size_t i;
439         char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
440         const char *prefix;
441         int t, pl;
442         wchar_t wc[2], *ws;
443         char mb[4];
444
445         for (;;) {
446                 /* This error is only specified for snprintf, but since it's
447                  * unspecified for other forms, do the same. Stop immediately
448                  * on overflow; otherwise %n could produce wrong results. */
449                 if (l > INT_MAX - cnt) goto overflow;
450
451                 /* Update output count, end loop when fmt is exhausted */
452                 cnt += l;
453                 if (!*s) break;
454
455                 /* Handle literal text and %% format specifiers */
456                 for (a=s; *s && *s!='%'; s++);
457                 for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
458                 if (z-a > INT_MAX-cnt) goto overflow;
459                 l = z-a;
460                 if (f) out(f, a, l);
461                 if (l) continue;
462
463                 if (isdigit(s[1]) && s[2]=='$') {
464                         l10n=1;
465                         argpos = s[1]-'0';
466                         s+=3;
467                 } else {
468                         argpos = -1;
469                         s++;
470                 }
471
472                 /* Read modifier flags */
473                 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
474                         fl |= 1U<<*s-' ';
475
476                 /* Read field width */
477                 if (*s=='*') {
478                         if (isdigit(s[1]) && s[2]=='$') {
479                                 l10n=1;
480                                 nl_type[s[1]-'0'] = INT;
481                                 w = nl_arg[s[1]-'0'].i;
482                                 s+=3;
483                         } else if (!l10n) {
484                                 w = f ? va_arg(*ap, int) : 0;
485                                 s++;
486                         } else goto inval;
487                         if (w<0) fl|=LEFT_ADJ, w=-w;
488                 } else if ((w=getint(&s))<0) goto overflow;
489
490                 /* Read precision */
491                 if (*s=='.' && s[1]=='*') {
492                         if (isdigit(s[2]) && s[3]=='$') {
493                                 nl_type[s[2]-'0'] = INT;
494                                 p = nl_arg[s[2]-'0'].i;
495                                 s+=4;
496                         } else if (!l10n) {
497                                 p = f ? va_arg(*ap, int) : 0;
498                                 s+=2;
499                         } else goto inval;
500                         xp = (p>=0);
501                 } else if (*s=='.') {
502                         s++;
503                         p = getint(&s);
504                         xp = 1;
505                 } else {
506                         p = -1;
507                         xp = 0;
508                 }
509
510                 /* Format specifier state machine */
511                 st=0;
512                 do {
513                         if (OOB(*s)) goto inval;
514                         ps=st;
515                         st=states[st]S(*s++);
516                 } while (st-1<STOP);
517                 if (!st) goto inval;
518
519                 /* Check validity of argument type (nl/normal) */
520                 if (st==NOARG) {
521                         if (argpos>=0) goto inval;
522                 } else {
523                         if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
524                         else if (f) pop_arg(&arg, st, ap);
525                         else return 0;
526                 }
527
528                 if (!f) continue;
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                 case 'c':
586                         *(a=z-(p=1))=arg.i;
587                         fl &= ~ZERO_PAD;
588                         break;
589                 case 'm':
590                         if (1) a = strerror(errno); else
591                 case 's':
592                         a = arg.p ? arg.p : "(null)";
593                         z = a + strnlen(a, p<0 ? INT_MAX : p);
594                         if (p<0 && *z) goto overflow;
595                         p = z-a;
596                         fl &= ~ZERO_PAD;
597                         break;
598                 case 'C':
599                         wc[0] = arg.i;
600                         wc[1] = 0;
601                         arg.p = wc;
602                         p = -1;
603                 case 'S':
604                         ws = arg.p;
605                         for (i=l=0; i<p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=p-i; i+=l);
606                         if (l<0) return -1;
607                         if (i > INT_MAX) goto overflow;
608                         p = i;
609                         pad(f, ' ', w, p, fl);
610                         ws = arg.p;
611                         for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
612                                 out(f, mb, l);
613                         pad(f, ' ', w, p, fl^LEFT_ADJ);
614                         l = w>p ? w : p;
615                         continue;
616                 case 'e': case 'f': case 'g': case 'a':
617                 case 'E': case 'F': case 'G': case 'A':
618                         if (xp && p<0) goto overflow;
619                         l = fmt_fp(f, arg.f, w, p, fl, t);
620                         if (l<0) goto overflow;
621                         continue;
622                 }
623
624                 if (p < z-a) p = z-a;
625                 if (p > INT_MAX-pl) goto overflow;
626                 if (w < pl+p) w = pl+p;
627                 if (w > INT_MAX-cnt) goto overflow;
628
629                 pad(f, ' ', w, pl+p, fl);
630                 out(f, prefix, pl);
631                 pad(f, '0', w, pl+p, fl^ZERO_PAD);
632                 pad(f, '0', p, z-a, 0);
633                 out(f, a, z-a);
634                 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
635
636                 l = w;
637         }
638
639         if (f) return cnt;
640         if (!l10n) return 0;
641
642         for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
643                 pop_arg(nl_arg+i, nl_type[i], ap);
644         for (; i<=NL_ARGMAX && !nl_type[i]; i++);
645         if (i<=NL_ARGMAX) goto inval;
646         return 1;
647
648 inval:
649         errno = EINVAL;
650         return -1;
651 overflow:
652         errno = EOVERFLOW;
653         return -1;
654 }
655
656 int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
657 {
658         va_list ap2;
659         int nl_type[NL_ARGMAX+1] = {0};
660         union arg nl_arg[NL_ARGMAX+1];
661         unsigned char internal_buf[80], *saved_buf = 0;
662         int olderr;
663         int ret;
664
665         /* the copy allows passing va_list* even if va_list is an array */
666         va_copy(ap2, ap);
667         if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
668                 va_end(ap2);
669                 return -1;
670         }
671
672         FLOCK(f);
673         olderr = f->flags & F_ERR;
674         if (f->mode < 1) f->flags &= ~F_ERR;
675         if (!f->buf_size) {
676                 saved_buf = f->buf;
677                 f->wpos = f->wbase = f->buf = internal_buf;
678                 f->buf_size = sizeof internal_buf;
679                 f->wend = internal_buf + sizeof internal_buf;
680         }
681         ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
682         if (saved_buf) {
683                 f->write(f, 0, 0);
684                 if (!f->wpos) ret = -1;
685                 f->buf = saved_buf;
686                 f->buf_size = 0;
687                 f->wpos = f->wbase = f->wend = 0;
688         }
689         if (f->flags & F_ERR) ret = -1;
690         f->flags |= olderr;
691         FUNLOCK(f);
692         va_end(ap2);
693         return ret;
694 }