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