initial check-in, version 0.5.0
[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(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="-+ ";
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++;
205         } else if (fl & PAD_POS) {
206                 prefix+=2;
207         } else 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 (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
227                 else re=LDBL_MANT_DIG/4-1-p;
228
229                 if (re) {
230                         if (pl && *prefix=='-') y=-y;
231                         while (re--) round*=16;
232                         y+=round;
233                         y-=round;
234                         if (y<0) y=-y;
235                 }
236
237                 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
238                 if (estr==ebuf) *--estr='0';
239                 *--estr = (e2<0 ? '-' : '+');
240                 *--estr = t+('p'-'a');
241
242                 s=buf;
243                 *s++='0';
244                 *s++=t+('x'-'a');
245                 do {
246                         int x=y;
247                         *s++=xdigits[x]|(t&32);
248                         y=16*(y-x);
249                         if (s-buf==3 && (y||p>0||(fl&ALT_FORM))) *s++='.';
250                 } while (y);
251
252                 if (p<0) p = s-buf-4;
253                 l = 1 + p + (p || (fl&ALT_FORM)) + ebuf-estr;
254
255                 pad(f, ' ', w, pl+l, fl);
256                 out(f, prefix, pl);
257                 pad(f, '0', w, pl+l, fl^ZERO_PAD);
258                 out(f, buf, s-buf);
259                 pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
260                 out(f, estr, ebuf-estr);
261                 pad(f, '0', w, pl+l, fl^LEFT_ADJ);
262                 return MAX(w, pl+l);
263         }
264         if (p<0) p=6;
265
266         y *= 0x1p28; e2-=28;
267
268         if (e2<0) a=r=z=big;
269         else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
270
271         do {
272                 *z = y;
273                 y = 1000000000*(y-*z++);
274         } while (y);
275
276         while (e2>0) {
277                 uint32_t carry=0;
278                 int sh=MIN(29,e2);
279                 for (d=z-1; d>=a; d--) {
280                         uint64_t x = ((uint64_t)*d<<sh)+carry;
281                         *d = x % 1000000000;
282                         carry = x / 1000000000;
283                 }
284                 if (!z[-1] && z>a) z--;
285                 if (carry) *--a = carry;
286                 e2-=sh;
287         }
288         while (e2<0) {
289                 uint32_t carry=0, *z2;
290                 int sh=MIN(9,-e2);
291                 for (d=a; d<z; d++) {
292                         uint32_t rm = *d & (1<<sh)-1;
293                         *d = (*d>>sh) + carry;
294                         carry = (1000000000>>sh) * rm;
295                 }
296                 if (!*a) a++;
297                 if (carry) *z++ = carry;
298                 /* Avoid (slow!) computation past requested precision */
299                 z2 = ((t|32)=='f' ? r : a) + 2 + p/9;
300                 z = MIN(z, z2);
301                 e2+=sh;
302         }
303
304         if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
305         else e=0;
306
307         /* Perform rounding: j is precision after the radix (possibly neg) */
308         j = p - ((t|32)!='f')*e - ((t|32)=='g');
309         if (j < 9*(z-r-1)) {
310                 uint32_t x;
311                 /* We avoid C's broken division of negative numbers */
312                 d = r + 1 + (j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP;
313                 j += 9*LDBL_MAX_EXP;
314                 j %= 9;
315                 for (i=10, j++; j<9; i*=10, j++);
316                 x = *d % i;
317                 /* Are there any significant digits past j? */
318                 if (x || d+1!=z) {
319                         long double round = CONCAT(0x1p,LDBL_MANT_DIG);
320                         long double small;
321                         if (x<i/2) small=0x01p-1;
322                         else if (i==i/2 && d+1==z) small=0x10p-1;
323                         else small=0x11p-1;
324                         if (pl && *prefix=='-') round*=-1, small*=-1;
325                         /* Decide whether to round by probing round+small */
326                         if (round+small != round) {
327                                 *d = *d - x + i;
328                                 while (*d > 999999999) {
329                                         *d--=0;
330                                         (*d)++;
331                                 }
332                                 if (d<a) a=d;
333                                 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
334                         }
335                 }
336                 for (; !z[-1] && z>a; z--);
337         }
338         
339         if ((t|32)=='g') {
340                 if (!p) p++;
341                 if (p>e && e>=-4) {
342                         t--;
343                         p-=e+1;
344                 } else {
345                         t-=2;
346                         p--;
347                 }
348                 if (!(fl&ALT_FORM)) {
349                         /* Count trailing zeros in last place */
350                         if (z>a) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
351                         else j=9;
352                         if ((t|32)=='f')
353                                 p = MIN(p,MAX(0,9*(z-r-1)-j));
354                         else
355                                 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
356                 }
357         }
358         l = 1 + p + (p || (fl&ALT_FORM));
359         if ((t|32)=='f') {
360                 if (e>0) l+=e;
361         } else {
362                 estr=fmt_u(e<0 ? -e : e, ebuf);
363                 while(ebuf-estr<2) *--estr='0';
364                 *--estr = (e<0 ? '-' : '+');
365                 *--estr = t;
366                 l += ebuf-estr;
367         }
368
369         pad(f, ' ', w, pl+l, fl);
370         out(f, prefix, pl);
371         pad(f, '0', w, pl+l, fl^ZERO_PAD);
372
373         if ((t|32)=='f') {
374                 if (a>r) a=r;
375                 for (d=a; d<=r; d++) {
376                         char *s = fmt_u(*d, buf+9);
377                         if (d!=a) while (s>buf) *--s='0';
378                         else if (s==buf+9) *--s='0';
379                         out(f, s, buf+9-s);
380                 }
381                 if (p || (fl&ALT_FORM)) out(f, ".", 1);
382                 for (; d<z && p>0; d++, p-=9) {
383                         char *s = fmt_u(*d, buf+9);
384                         while (s>buf) *--s='0';
385                         out(f, s, MIN(9,p));
386                 }
387                 pad(f, '0', p+9, 9, 0);
388         } else {
389                 if (z<=a) z=a+1;
390                 for (d=a; d<z && p>=0; d++) {
391                         char *s = fmt_u(*d, buf+9);
392                         if (s==buf+9) *--s='0';
393                         if (d!=a) while (s>buf) *--s='0';
394                         else {
395                                 out(f, s++, 1);
396                                 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
397                         }
398                         out(f, s, MIN(buf+9-s, p));
399                         p -= buf+9-s;
400                 }
401                 pad(f, '0', p+18, 18, 0);
402                 out(f, estr, ebuf-estr);
403         }
404
405         pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
406
407         return MAX(w, pl+l);
408 }
409
410 static int getint(char **s) {
411         int i;
412         for (i=0; isdigit(**s); (*s)++)
413                 i = 10*i + (**s-'0');
414         return i;
415 }
416
417 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
418 {
419         char *a, *z, *s=(char *)fmt;
420         unsigned l10n=0, litpct, fl;
421         int w, p;
422         union arg arg;
423         int argpos;
424         unsigned st, ps;
425         int cnt=0, l=0;
426         int i;
427         char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
428         const char *prefix;
429         int t, pl;
430         wchar_t wc[2], *ws;
431         char mb[4];
432
433         for (;;) {
434                 /* Update output count, end loop when fmt is exhausted */
435                 if (cnt >= 0) {
436                         if (l > INT_MAX - cnt) {
437                                 if (!ferror(f)) errno = EOVERFLOW;
438                                 cnt = -1;
439                         } else cnt += l;
440                 }
441                 if (!*s) break;
442
443                 /* Handle literal text and %% format specifiers */
444                 for (a=s; *s && *s!='%'; s++);
445                 litpct = strspn(s, "%")/2; /* Optimize %%%% runs */
446                 z = s+litpct;
447                 s += 2*litpct;
448                 l = z-a;
449                 if (f) out(f, a, l);
450                 if (l) continue;
451
452                 if (isdigit(s[1]) && s[2]=='$') {
453                         l10n=1;
454                         argpos = s[1]-'0';
455                         s+=3;
456                 } else {
457                         argpos = -1;
458                         s++;
459                 }
460
461                 /* Read modifier flags */
462                 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
463                         fl |= 1U<<*s-' ';
464
465                 /* Read field width */
466                 if (*s=='*') {
467                         if (isdigit(s[1]) && s[2]=='$') {
468                                 l10n=1;
469                                 nl_type[s[1]-'0'] = INT;
470                                 w = nl_arg[s[1]-'0'].i;
471                                 s+=3;
472                         } else if (!l10n) {
473                                 w = f ? va_arg(*ap, int) : 0;
474                                 s++;
475                         } else return -1;
476                         if (w<0) fl|=LEFT_ADJ, w=-w;
477                 } else if ((w=getint(&s))<0) return -1;
478
479                 /* Read precision */
480                 if (*s=='.' && s[1]=='*') {
481                         if (isdigit(s[2]) && s[3]=='$') {
482                                 nl_type[s[2]-'0'] = INT;
483                                 p = nl_arg[s[2]-'0'].i;
484                                 s+=4;
485                         } else if (!l10n) {
486                                 p = f ? va_arg(*ap, int) : 0;
487                                 s+=2;
488                         } else return -1;
489                 } else if (*s=='.') {
490                         s++;
491                         p = getint(&s);
492                 } else p = -1;
493
494                 /* Format specifier state machine */
495                 st=0;
496                 do {
497                         if (OOB(*s)) return -1;
498                         ps=st;
499                         st=states[st]S(*s++);
500                 } while (st-1<STOP);
501                 if (!st) return -1;
502
503                 /* Check validity of argument type (nl/normal) */
504                 if (st==NOARG) {
505                         if (argpos>=0) return -1;
506                         else if (!f) continue;
507                 } else {
508                         if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
509                         else if (f) pop_arg(&arg, st, ap);
510                         else return 0;
511                 }
512
513                 if (!f) continue;
514
515                 z = buf + sizeof(buf);
516                 prefix = "-+   0X0x";
517                 pl = 0;
518                 t = s[-1];
519
520                 /* Transform ls,lc -> S,C */
521                 if (ps && (t&15)==3) t&=~32;
522
523                 /* - and 0 flags are mutually exclusive */
524                 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
525
526                 switch(t) {
527                 case 'n':
528                         switch(ps) {
529                         case BARE: *(int *)arg.p = l;
530                         case LPRE: *(long *)arg.p = l;
531                         case LLPRE: *(long long *)arg.p = l;
532                         case HPRE: *(unsigned short *)arg.p = l;
533                         case HHPRE: *(unsigned char *)arg.p = l;
534                         case ZTPRE: *(size_t *)arg.p = l;
535                         case JPRE: *(uintmax_t *)arg.p = l;
536                         }
537                         continue;
538                 case 'p':
539                         p = MAX(p, 2*sizeof(void*));
540                         t = 'x';
541                         fl |= ALT_FORM;
542                 case 'x': case 'X':
543                         a = fmt_x(arg.i, z, t&32);
544                         if (fl & ALT_FORM) prefix+=(t>>4), pl=2;
545                         if (0) {
546                 case 'o':
547                         a = fmt_o(arg.i, z);
548                         if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
549                         } if (0) {
550                 case 'd': case 'i':
551                         pl=1;
552                         if (arg.i>INTMAX_MAX) {
553                                 arg.i=-arg.i;
554                         } else if (fl & MARK_POS) {
555                                 prefix++;
556                         } else if (fl & PAD_POS) {
557                                 prefix+=2;
558                         } else pl=0;
559                 case 'u':
560                         a = fmt_u(arg.i, z);
561                         }
562                         if (!arg.i && !p) continue;
563                         if (p>=0) fl &= ~ZERO_PAD;
564                         p = MAX(p, z-a + !arg.i);
565                         break;
566                 case 'c':
567                         *(a=z-(p=1))=arg.i;
568                         fl &= ~ZERO_PAD;
569                         break;
570                 case 'm':
571                         if (1) a = strerror(errno); else
572                 case 's':
573                         a = arg.p;
574                         z = memchr(a, 0, p);
575                         if (!z) z=a+p;
576                         else p=z-a;
577                         fl &= ~ZERO_PAD;
578                         break;
579                 case 'C':
580                         wc[0] = arg.i;
581                         wc[1] = 0;
582                         arg.p = wc;
583                         p = -1;
584                 case 'S':
585                         ws = arg.p;
586                         for (i=0; *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
587                         if (l<0) return -1;
588                         p = i;
589                         pad(f, ' ', w, p, fl);
590                         ws = arg.p;
591                         for (i=0; *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
592                                 out(f, mb, l);
593                         pad(f, ' ', w, p, fl^LEFT_ADJ);
594                         l = w>p ? w : p;
595                         continue;
596                 case 'e': case 'f': case 'g': case 'a':
597                 case 'E': case 'F': case 'G': case 'A':
598                         l = fmt_fp(f, arg.f, w, p, fl, t);
599                         continue;
600                 }
601
602                 if (p < z-a) p = z-a;
603                 if (w < pl+p) w = pl+p;
604
605                 pad(f, ' ', w, pl+p, fl);
606                 out(f, prefix, pl);
607                 pad(f, '0', w, pl+p, fl^ZERO_PAD);
608                 pad(f, '0', p, z-a, 0);
609                 out(f, a, z-a);
610                 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
611
612                 l = w;
613         }
614
615         if (f) return cnt;
616         if (!l10n) return 0;
617
618         for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
619                 pop_arg(nl_arg+i, nl_type[i], ap);
620         for (; i<=NL_ARGMAX && !nl_type[i]; i++);
621         if (i<=NL_ARGMAX) return -1;
622         return 1;
623 }
624
625 int vfprintf(FILE *f, const char *fmt, va_list ap)
626 {
627         va_list ap2;
628         int nl_type[NL_ARGMAX] = {0};
629         union arg nl_arg[NL_ARGMAX];
630         int ret;
631
632         va_copy(ap2, ap);
633         if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
634
635         FLOCK(f);
636         ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
637         FUNLOCK(f);
638         va_end(ap2);
639         return ret;
640 }