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