f13fbe10576d157684ecfc9d4e8bcf448010cd6d
[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 (*d/i & 1) round += 2;
330                         if (x<i/2) small=0x0.8p0;
331                         else if (x==i/2 && d+1==z) small=0x1.0p0;
332                         else small=0x1.8p0;
333                         if (pl && *prefix=='-') round*=-1, small*=-1;
334                         *d -= x;
335                         /* Decide whether to round by probing round+small */
336                         if (round+small != round) {
337                                 *d = *d + i;
338                                 while (*d > 999999999) {
339                                         *d--=0;
340                                         (*d)++;
341                                 }
342                                 if (d<a) a=d;
343                                 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
344                         }
345                 }
346                 if (z>d+1) z=d+1;
347                 for (; !z[-1] && z>a; z--);
348         }
349         
350         if ((t|32)=='g') {
351                 if (!p) p++;
352                 if (p>e && e>=-4) {
353                         t--;
354                         p-=e+1;
355                 } else {
356                         t-=2;
357                         p--;
358                 }
359                 if (!(fl&ALT_FORM)) {
360                         /* Count trailing zeros in last place */
361                         if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
362                         else j=9;
363                         if ((t|32)=='f')
364                                 p = MIN(p,MAX(0,9*(z-r-1)-j));
365                         else
366                                 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
367                 }
368         }
369         l = 1 + p + (p || (fl&ALT_FORM));
370         if ((t|32)=='f') {
371                 if (e>0) l+=e;
372         } else {
373                 estr=fmt_u(e<0 ? -e : e, ebuf);
374                 while(ebuf-estr<2) *--estr='0';
375                 *--estr = (e<0 ? '-' : '+');
376                 *--estr = t;
377                 l += ebuf-estr;
378         }
379
380         pad(f, ' ', w, pl+l, fl);
381         out(f, prefix, pl);
382         pad(f, '0', w, pl+l, fl^ZERO_PAD);
383
384         if ((t|32)=='f') {
385                 if (a>r) a=r;
386                 for (d=a; d<=r; d++) {
387                         char *s = fmt_u(*d, buf+9);
388                         if (d!=a) while (s>buf) *--s='0';
389                         else if (s==buf+9) *--s='0';
390                         out(f, s, buf+9-s);
391                 }
392                 if (p || (fl&ALT_FORM)) out(f, ".", 1);
393                 for (; d<z && p>0; d++, p-=9) {
394                         char *s = fmt_u(*d, buf+9);
395                         while (s>buf) *--s='0';
396                         out(f, s, MIN(9,p));
397                 }
398                 pad(f, '0', p+9, 9, 0);
399         } else {
400                 if (z<=a) z=a+1;
401                 for (d=a; d<z && p>=0; d++) {
402                         char *s = fmt_u(*d, buf+9);
403                         if (s==buf+9) *--s='0';
404                         if (d!=a) while (s>buf) *--s='0';
405                         else {
406                                 out(f, s++, 1);
407                                 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
408                         }
409                         out(f, s, MIN(buf+9-s, p));
410                         p -= buf+9-s;
411                 }
412                 pad(f, '0', p+18, 18, 0);
413                 out(f, estr, ebuf-estr);
414         }
415
416         pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
417
418         return MAX(w, pl+l);
419 }
420
421 static int getint(char **s) {
422         int i;
423         for (i=0; isdigit(**s); (*s)++)
424                 i = 10*i + (**s-'0');
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, litpct, fl;
432         int w, p;
433         union arg arg;
434         int argpos;
435         unsigned st, ps;
436         int cnt=0, l=0;
437         int 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                 /* Update output count, end loop when fmt is exhausted */
446                 if (cnt >= 0) {
447                         if (l > INT_MAX - cnt) {
448                                 errno = EOVERFLOW;
449                                 cnt = -1;
450                         } else cnt += l;
451                 }
452                 if (!*s) break;
453
454                 /* Handle literal text and %% format specifiers */
455                 for (a=s; *s && *s!='%'; s++);
456                 litpct = strspn(s, "%")/2; /* Optimize %%%% runs */
457                 z = s+litpct;
458                 s += 2*litpct;
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 return -1;
487                         if (w<0) fl|=LEFT_ADJ, w=-w;
488                 } else if ((w=getint(&s))<0) return -1;
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 return -1;
500                 } else if (*s=='.') {
501                         s++;
502                         p = getint(&s);
503                 } else p = -1;
504
505                 /* Format specifier state machine */
506                 st=0;
507                 do {
508                         if (OOB(*s)) return -1;
509                         ps=st;
510                         st=states[st]S(*s++);
511                 } while (st-1<STOP);
512                 if (!st) return -1;
513
514                 /* Check validity of argument type (nl/normal) */
515                 if (st==NOARG) {
516                         if (argpos>=0) return -1;
517                         else if (!f) continue;
518                 } else {
519                         if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
520                         else if (f) pop_arg(&arg, st, ap);
521                         else return 0;
522                 }
523
524                 if (!f) continue;
525
526                 z = buf + sizeof(buf);
527                 prefix = "-+   0X0x";
528                 pl = 0;
529                 t = s[-1];
530
531                 /* Transform ls,lc -> S,C */
532                 if (ps && (t&15)==3) t&=~32;
533
534                 /* - and 0 flags are mutually exclusive */
535                 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
536
537                 switch(t) {
538                 case 'n':
539                         switch(ps) {
540                         case BARE: *(int *)arg.p = cnt; break;
541                         case LPRE: *(long *)arg.p = cnt; break;
542                         case LLPRE: *(long long *)arg.p = cnt; break;
543                         case HPRE: *(unsigned short *)arg.p = cnt; break;
544                         case HHPRE: *(unsigned char *)arg.p = cnt; break;
545                         case ZTPRE: *(size_t *)arg.p = cnt; break;
546                         case JPRE: *(uintmax_t *)arg.p = cnt; break;
547                         }
548                         continue;
549                 case 'p':
550                         p = MAX(p, 2*sizeof(void*));
551                         t = 'x';
552                         fl |= ALT_FORM;
553                 case 'x': case 'X':
554                         a = fmt_x(arg.i, z, t&32);
555                         if (fl & ALT_FORM) prefix+=(t>>4), pl=2;
556                         if (0) {
557                 case 'o':
558                         a = fmt_o(arg.i, z);
559                         if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
560                         } if (0) {
561                 case 'd': case 'i':
562                         pl=1;
563                         if (arg.i>INTMAX_MAX) {
564                                 arg.i=-arg.i;
565                         } else if (fl & MARK_POS) {
566                                 prefix++;
567                         } else if (fl & PAD_POS) {
568                                 prefix+=2;
569                         } else pl=0;
570                 case 'u':
571                         a = fmt_u(arg.i, z);
572                         }
573                         if (!arg.i && !p) continue;
574                         if (p>=0) fl &= ~ZERO_PAD;
575                         p = MAX(p, z-a + !arg.i);
576                         break;
577                 case 'c':
578                         *(a=z-(p=1))=arg.i;
579                         fl &= ~ZERO_PAD;
580                         break;
581                 case 'm':
582                         if (1) a = strerror(errno); else
583                 case 's':
584                         a = arg.p;
585                         z = memchr(a, 0, p);
586                         if (!z) z=a+p;
587                         else p=z-a;
588                         fl &= ~ZERO_PAD;
589                         break;
590                 case 'C':
591                         wc[0] = arg.i;
592                         wc[1] = 0;
593                         arg.p = wc;
594                         p = -1;
595                 case 'S':
596                         ws = arg.p;
597                         for (i=0; *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
598                         if (l<0) return -1;
599                         p = i;
600                         pad(f, ' ', w, p, fl);
601                         ws = arg.p;
602                         for (i=0; *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
603                                 out(f, mb, l);
604                         pad(f, ' ', w, p, fl^LEFT_ADJ);
605                         l = w>p ? w : p;
606                         continue;
607                 case 'e': case 'f': case 'g': case 'a':
608                 case 'E': case 'F': case 'G': case 'A':
609                         l = fmt_fp(f, arg.f, w, p, fl, t);
610                         continue;
611                 }
612
613                 if (p < z-a) p = z-a;
614                 if (w < pl+p) w = pl+p;
615
616                 pad(f, ' ', w, pl+p, fl);
617                 out(f, prefix, pl);
618                 pad(f, '0', w, pl+p, fl^ZERO_PAD);
619                 pad(f, '0', p, z-a, 0);
620                 out(f, a, z-a);
621                 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
622
623                 l = w;
624         }
625
626         if (f) return cnt;
627         if (!l10n) return 0;
628
629         for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
630                 pop_arg(nl_arg+i, nl_type[i], ap);
631         for (; i<=NL_ARGMAX && !nl_type[i]; i++);
632         if (i<=NL_ARGMAX) return -1;
633         return 1;
634 }
635
636 int vfprintf(FILE *f, const char *fmt, va_list ap)
637 {
638         va_list ap2;
639         int nl_type[NL_ARGMAX+1] = {0};
640         union arg nl_arg[NL_ARGMAX+1];
641         unsigned char internal_buf[80], *saved_buf = 0;
642         int ret;
643
644         va_copy(ap2, ap);
645         if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
646
647         FLOCK(f);
648         if (!f->buf_size) {
649                 saved_buf = f->buf;
650                 f->buf = internal_buf;
651                 f->buf_size = sizeof internal_buf;
652         }
653         ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
654         if (saved_buf) {
655                 f->write(f, 0, 0);
656                 if (!f->wpos) ret = -1;
657                 f->buf = saved_buf;
658                 f->buf_size = 0;
659                 f->wpos = f->wbase = f->wend = 0;
660         }
661         FUNLOCK(f);
662         va_end(ap2);
663         return ret;
664 }