1 #include "stdio_impl.h"
12 /* Some useful macros */
14 #define MAX(a,b) ((a)>(b) ? (a) : (b))
15 #define MIN(a,b) ((a)<(b) ? (a) : (b))
16 #define CONCAT2(x,y) x ## y
17 #define CONCAT(x,y) CONCAT2(x,y)
19 /* Convenient bit representation for modifier flags, which all fall
20 * within 31 codepoints of the space character. */
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<<'\''-' ')
29 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
31 #if UINT_MAX == ULONG_MAX
35 #if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
39 /* State machine to accept length modifiers + conversion specifiers.
40 * Result is 0 on failure, or an argument type to pop on success. */
43 BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
46 PTR, INT, UINT, ULLONG,
53 SHORT, USHORT, CHAR, UCHAR,
55 LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
69 #define S(x) [(x)-'A']
71 static const unsigned char states[]['z'-'A'+1] = {
73 S('d') = INT, S('i') = INT,
74 S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
75 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
76 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
77 S('c') = CHAR, S('C') = INT,
78 S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
80 S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
81 S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
82 }, { /* 1: l-prefixed */
83 S('d') = LONG, S('i') = LONG,
84 S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
85 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
86 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
87 S('c') = INT, S('s') = PTR, S('n') = PTR,
89 }, { /* 2: ll-prefixed */
90 S('d') = LLONG, S('i') = LLONG,
91 S('o') = ULLONG, S('u') = ULLONG,
92 S('x') = ULLONG, S('X') = ULLONG,
94 }, { /* 3: h-prefixed */
95 S('d') = SHORT, S('i') = SHORT,
96 S('o') = USHORT, S('u') = USHORT,
97 S('x') = USHORT, S('X') = USHORT,
100 }, { /* 4: hh-prefixed */
101 S('d') = CHAR, S('i') = CHAR,
102 S('o') = UCHAR, S('u') = UCHAR,
103 S('x') = UCHAR, S('X') = UCHAR,
105 }, { /* 5: L-prefixed */
106 S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
107 S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
109 }, { /* 6: z- or t-prefixed (assumed to be same size) */
110 S('d') = PDIFF, S('i') = PDIFF,
111 S('o') = SIZET, S('u') = SIZET,
112 S('x') = SIZET, S('X') = SIZET,
114 }, { /* 7: j-prefixed */
115 S('d') = IMAX, S('i') = IMAX,
116 S('o') = UMAX, S('u') = UMAX,
117 S('x') = UMAX, S('X') = UMAX,
122 #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
131 static void pop_arg(union arg *arg, int type, va_list *ap)
133 /* Give the compiler a hint for optimizing the switch. */
134 if ((unsigned)type > MAXSTATE) return;
136 case PTR: arg->p = va_arg(*ap, void *);
137 break; case INT: arg->i = va_arg(*ap, int);
138 break; case UINT: arg->i = va_arg(*ap, unsigned int);
140 break; case LONG: arg->i = va_arg(*ap, long);
141 break; case ULONG: arg->i = va_arg(*ap, unsigned long);
143 break; case ULLONG: arg->i = va_arg(*ap, unsigned long long);
144 break; case SHORT: arg->i = (short)va_arg(*ap, int);
145 break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int);
146 break; case CHAR: arg->i = (signed char)va_arg(*ap, int);
147 break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int);
149 break; case LLONG: arg->i = va_arg(*ap, long long);
150 break; case SIZET: arg->i = va_arg(*ap, size_t);
151 break; case IMAX: arg->i = va_arg(*ap, intmax_t);
152 break; case UMAX: arg->i = va_arg(*ap, uintmax_t);
153 break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t);
154 break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *);
156 break; case DBL: arg->f = va_arg(*ap, double);
157 break; case LDBL: arg->f = va_arg(*ap, long double);
161 static void out(FILE *f, const char *s, size_t l)
163 __fwritex((void *)s, l, f);
166 static void pad(FILE *f, char c, int w, int l, int fl)
169 if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
171 memset(pad, c, l>sizeof pad ? sizeof pad : l);
172 for (; l >= sizeof pad; l -= sizeof pad)
173 out(f, pad, sizeof pad);
177 static const char xdigits[16] = {
181 static char *fmt_x(uintmax_t x, char *s, int lower)
183 for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
187 static char *fmt_o(uintmax_t x, char *s)
189 for (; x; x>>=3) *--s = '0' + (x&7);
193 static char *fmt_u(uintmax_t x, char *s)
196 for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
197 for (y=x; y; y/=10) *--s = '0' + y%10;
201 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
203 uint32_t big[(LDBL_MAX_EXP+LDBL_MANT_DIG)/9+1];
204 uint32_t *a, *d, *r, *z;
205 int e2=0, e, i, j, l;
206 char buf[9+LDBL_MANT_DIG/4], *s;
207 const char *prefix="-0X+0X 0X-0x+0x 0x";
209 char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
214 } else if (fl & MARK_POS) {
216 } else if (fl & PAD_POS) {
218 } else prefix++, pl=0;
221 char *s = (t&32)?"inf":"INF";
222 if (y!=y) s=(t&32)?"nan":"NAN", pl=0;
223 pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
226 pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
230 y = frexpl(y, &e2) * 2;
234 long double round = 8.0;
237 if (t&32) prefix += 9;
240 if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
241 else re=LDBL_MANT_DIG/4-1-p;
244 while (re--) round*=16;
256 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
257 if (estr==ebuf) *--estr='0';
258 *--estr = (e2<0 ? '-' : '+');
259 *--estr = t+('p'-'a');
264 *s++=xdigits[x]|(t&32);
266 if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
269 if (p && s-buf-2 < p)
270 l = (p+2) + (ebuf-estr);
272 l = (s-buf) + (ebuf-estr);
274 pad(f, ' ', w, pl+l, fl);
276 pad(f, '0', w, pl+l, fl^ZERO_PAD);
278 pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
279 out(f, estr, ebuf-estr);
280 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
285 if (y) y *= 0x1p28, e2-=28;
288 else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
292 y = 1000000000*(y-*z++);
298 for (d=z-1; d>=a; d--) {
299 uint64_t x = ((uint64_t)*d<<sh)+carry;
301 carry = x / 1000000000;
303 if (!z[-1] && z>a) z--;
304 if (carry) *--a = carry;
308 uint32_t carry=0, *b;
310 for (d=a; d<z; d++) {
311 uint32_t rm = *d & (1<<sh)-1;
312 *d = (*d>>sh) + carry;
313 carry = (1000000000>>sh) * rm;
316 if (carry) *z++ = carry;
317 /* Avoid (slow!) computation past requested precision */
318 b = (t|32)=='f' ? r : a;
319 if (z-b > 2+p/9) z = b+2+p/9;
323 if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
326 /* Perform rounding: j is precision after the radix (possibly neg) */
327 j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
330 /* We avoid C's broken division of negative numbers */
331 d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
334 for (i=10, j++; j<9; i*=10, j++);
336 /* Are there any significant digits past j? */
338 long double round = CONCAT(0x1p,LDBL_MANT_DIG);
340 if (*d/i & 1) round += 2;
341 if (x<i/2) small=0x0.8p0;
342 else if (x==i/2 && d+1==z) small=0x1.0p0;
344 if (pl && *prefix=='-') round*=-1, small*=-1;
346 /* Decide whether to round by probing round+small */
347 if (round+small != round) {
349 while (*d > 999999999) {
354 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
358 for (; !z[-1] && z>a; z--);
370 if (!(fl&ALT_FORM)) {
371 /* Count trailing zeros in last place */
372 if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
375 p = MIN(p,MAX(0,9*(z-r-1)-j));
377 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
380 l = 1 + p + (p || (fl&ALT_FORM));
384 estr=fmt_u(e<0 ? -e : e, ebuf);
385 while(ebuf-estr<2) *--estr='0';
386 *--estr = (e<0 ? '-' : '+');
391 pad(f, ' ', w, pl+l, fl);
393 pad(f, '0', w, pl+l, fl^ZERO_PAD);
397 for (d=a; d<=r; d++) {
398 char *s = fmt_u(*d, buf+9);
399 if (d!=a) while (s>buf) *--s='0';
400 else if (s==buf+9) *--s='0';
403 if (p || (fl&ALT_FORM)) out(f, ".", 1);
404 for (; d<z && p>0; d++, p-=9) {
405 char *s = fmt_u(*d, buf+9);
406 while (s>buf) *--s='0';
409 pad(f, '0', p+9, 9, 0);
412 for (d=a; d<z && p>=0; d++) {
413 char *s = fmt_u(*d, buf+9);
414 if (s==buf+9) *--s='0';
415 if (d!=a) while (s>buf) *--s='0';
418 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
420 out(f, s, MIN(buf+9-s, p));
423 pad(f, '0', p+18, 18, 0);
424 out(f, estr, ebuf-estr);
427 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
432 static int getint(char **s) {
434 for (i=0; isdigit(**s); (*s)++)
435 i = 10*i + (**s-'0');
439 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
441 char *a, *z, *s=(char *)fmt;
449 char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
456 /* Update output count, end loop when fmt is exhausted */
458 if (l > INT_MAX - cnt) {
465 /* Handle literal text and %% format specifiers */
466 for (a=s; *s && *s!='%'; s++);
467 for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
472 if (isdigit(s[1]) && s[2]=='$') {
481 /* Read modifier flags */
482 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
485 /* Read field width */
487 if (isdigit(s[1]) && s[2]=='$') {
489 nl_type[s[1]-'0'] = INT;
490 w = nl_arg[s[1]-'0'].i;
493 w = f ? va_arg(*ap, int) : 0;
496 if (w<0) fl|=LEFT_ADJ, w=-w;
497 } else if ((w=getint(&s))<0) return -1;
500 if (*s=='.' && s[1]=='*') {
501 if (isdigit(s[2]) && s[3]=='$') {
502 nl_type[s[2]-'0'] = INT;
503 p = nl_arg[s[2]-'0'].i;
506 p = f ? va_arg(*ap, int) : 0;
509 } else if (*s=='.') {
514 /* Format specifier state machine */
517 if (OOB(*s)) return -1;
519 st=states[st]S(*s++);
523 /* Check validity of argument type (nl/normal) */
525 if (argpos>=0) return -1;
526 else if (!f) continue;
528 if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
529 else if (f) pop_arg(&arg, st, ap);
535 z = buf + sizeof(buf);
540 /* Transform ls,lc -> S,C */
541 if (ps && (t&15)==3) t&=~32;
543 /* - and 0 flags are mutually exclusive */
544 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
549 case BARE: *(int *)arg.p = cnt; break;
550 case LPRE: *(long *)arg.p = cnt; break;
551 case LLPRE: *(long long *)arg.p = cnt; break;
552 case HPRE: *(unsigned short *)arg.p = cnt; break;
553 case HHPRE: *(unsigned char *)arg.p = cnt; break;
554 case ZTPRE: *(size_t *)arg.p = cnt; break;
555 case JPRE: *(uintmax_t *)arg.p = cnt; break;
559 p = MAX(p, 2*sizeof(void*));
563 a = fmt_x(arg.i, z, t&32);
564 if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
568 if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
572 if (arg.i>INTMAX_MAX) {
574 } else if (fl & MARK_POS) {
576 } else if (fl & PAD_POS) {
582 if (p>=0) fl &= ~ZERO_PAD;
587 p = MAX(p, z-a + !arg.i);
594 if (1) a = strerror(errno); else
596 a = arg.p ? arg.p : "(null)";
609 for (i=l=0; i<0U+p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
612 pad(f, ' ', w, p, fl);
614 for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
616 pad(f, ' ', w, p, fl^LEFT_ADJ);
619 case 'e': case 'f': case 'g': case 'a':
620 case 'E': case 'F': case 'G': case 'A':
621 l = fmt_fp(f, arg.f, w, p, fl, t);
625 if (p < z-a) p = z-a;
626 if (w < pl+p) w = pl+p;
628 pad(f, ' ', w, pl+p, fl);
630 pad(f, '0', w, pl+p, fl^ZERO_PAD);
631 pad(f, '0', p, z-a, 0);
633 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
641 for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
642 pop_arg(nl_arg+i, nl_type[i], ap);
643 for (; i<=NL_ARGMAX && !nl_type[i]; i++);
644 if (i<=NL_ARGMAX) return -1;
648 int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
651 int nl_type[NL_ARGMAX+1] = {0};
652 union arg nl_arg[NL_ARGMAX+1];
653 unsigned char internal_buf[80], *saved_buf = 0;
657 if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
662 f->wpos = f->wbase = f->buf = internal_buf;
663 f->buf_size = sizeof internal_buf;
664 f->wend = internal_buf + sizeof internal_buf;
666 ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
669 if (!f->wpos) ret = -1;
672 f->wpos = f->wbase = f->wend = 0;