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