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