1 #include "stdio_impl.h"
3 /* Some useful macros */
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)
10 /* Convenient bit representation for modifier flags, which all fall
11 * within 31 codepoints of the space character. */
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<<'\''-' ')
20 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
22 #if UINT_MAX == ULONG_MAX
26 #if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
30 /* State machine to accept length modifiers + conversion specifiers.
31 * Result is 0 on failure, or an argument type to pop on success. */
34 BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
37 PTR, INT, UINT, ULLONG,
44 SHORT, USHORT, CHAR, UCHAR,
46 LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
60 #define S(x) [(x)-'A']
62 static const unsigned char states[]['z'-'A'+1] = {
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,
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,
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,
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,
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,
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,
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,
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,
111 #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
120 static void pop_arg(union arg *arg, int type, va_list *ap)
122 /* Give the compiler a hint for optimizing the switch. */
123 if ((unsigned)type > MAXSTATE) return;
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);
129 break; case LONG: arg->i = va_arg(*ap, long);
130 break; case ULONG: arg->i = va_arg(*ap, unsigned long);
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);
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 *);
145 break; case DBL: arg->f = va_arg(*ap, double);
146 break; case LDBL: arg->f = va_arg(*ap, long double);
150 static void out(FILE *f, const char *s, size_t l)
152 __fwritex((void *)s, l, f);
155 static void pad(FILE *f, char c, int w, int l, int fl)
158 if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
160 memset(pad, c, l>sizeof pad ? sizeof pad : l);
161 for (; l >= sizeof pad; l -= sizeof pad)
162 out(f, pad, sizeof pad);
166 static const char xdigits[16] = {
170 static char *fmt_x(uintmax_t x, char *s, int lower)
172 for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
176 static char *fmt_o(uintmax_t x, char *s)
178 for (; x; x>>=3) *--s = '0' + (x&7);
182 static char *fmt_u(uintmax_t x, char *s)
185 for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
186 for (y=x; y; y/=10) *--s = '0' + y%10;
190 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
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";
198 char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
203 } else if (fl & MARK_POS) {
205 } else if (fl & PAD_POS) {
207 } else prefix++, pl=0;
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);
215 pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
219 y = frexpl(y, &e2) * 2;
223 long double round = 8.0;
226 if (t&32) prefix += 9;
229 if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
230 else re=LDBL_MANT_DIG/4-1-p;
233 while (re--) round*=16;
245 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
246 if (estr==ebuf) *--estr='0';
247 *--estr = (e2<0 ? '-' : '+');
248 *--estr = t+('p'-'a');
253 *s++=xdigits[x]|(t&32);
255 if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
258 if (p && s-buf-2 < p)
259 l = (p+2) + (ebuf-estr);
261 l = (s-buf) + (ebuf-estr);
263 pad(f, ' ', w, pl+l, fl);
265 pad(f, '0', w, pl+l, fl^ZERO_PAD);
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);
274 if (y) y *= 0x1p28, e2-=28;
277 else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
281 y = 1000000000*(y-*z++);
287 for (d=z-1; d>=a; d--) {
288 uint64_t x = ((uint64_t)*d<<sh)+carry;
290 carry = x / 1000000000;
292 if (!z[-1] && z>a) z--;
293 if (carry) *--a = carry;
297 uint32_t carry=0, *z2;
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;
305 if (carry) *z++ = carry;
306 /* Avoid (slow!) computation past requested precision */
307 z2 = ((t|32)=='f' ? r : a) + 2 + p/9;
312 if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
315 /* Perform rounding: j is precision after the radix (possibly neg) */
316 j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
319 /* We avoid C's broken division of negative numbers */
320 d = r + 1 + (j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP;
323 for (i=10, j++; j<9; i*=10, j++);
325 /* Are there any significant digits past j? */
327 long double round = CONCAT(0x1p,LDBL_MANT_DIG);
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;
333 if (pl && *prefix=='-') round*=-1, small*=-1;
335 /* Decide whether to round by probing round+small */
336 if (round+small != round) {
338 while (*d > 999999999) {
343 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
347 for (; !z[-1] && z>a; z--);
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++);
364 p = MIN(p,MAX(0,9*(z-r-1)-j));
366 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
369 l = 1 + p + (p || (fl&ALT_FORM));
373 estr=fmt_u(e<0 ? -e : e, ebuf);
374 while(ebuf-estr<2) *--estr='0';
375 *--estr = (e<0 ? '-' : '+');
380 pad(f, ' ', w, pl+l, fl);
382 pad(f, '0', w, pl+l, fl^ZERO_PAD);
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';
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';
398 pad(f, '0', p+9, 9, 0);
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';
407 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
409 out(f, s, MIN(buf+9-s, p));
412 pad(f, '0', p+18, 18, 0);
413 out(f, estr, ebuf-estr);
416 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
421 static int getint(char **s) {
423 for (i=0; isdigit(**s); (*s)++)
424 i = 10*i + (**s-'0');
428 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
430 char *a, *z, *s=(char *)fmt;
431 unsigned l10n=0, litpct, fl;
438 char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
445 /* Update output count, end loop when fmt is exhausted */
447 if (l > INT_MAX - cnt) {
454 /* Handle literal text and %% format specifiers */
455 for (a=s; *s && *s!='%'; s++);
456 litpct = strspn(s, "%")/2; /* Optimize %%%% runs */
463 if (isdigit(s[1]) && s[2]=='$') {
472 /* Read modifier flags */
473 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
476 /* Read field width */
478 if (isdigit(s[1]) && s[2]=='$') {
480 nl_type[s[1]-'0'] = INT;
481 w = nl_arg[s[1]-'0'].i;
484 w = f ? va_arg(*ap, int) : 0;
487 if (w<0) fl|=LEFT_ADJ, w=-w;
488 } else if ((w=getint(&s))<0) return -1;
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;
497 p = f ? va_arg(*ap, int) : 0;
500 } else if (*s=='.') {
505 /* Format specifier state machine */
508 if (OOB(*s)) return -1;
510 st=states[st]S(*s++);
514 /* Check validity of argument type (nl/normal) */
516 if (argpos>=0) return -1;
517 else if (!f) continue;
519 if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
520 else if (f) pop_arg(&arg, st, ap);
526 z = buf + sizeof(buf);
531 /* Transform ls,lc -> S,C */
532 if (ps && (t&15)==3) t&=~32;
534 /* - and 0 flags are mutually exclusive */
535 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
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;
550 p = MAX(p, 2*sizeof(void*));
554 a = fmt_x(arg.i, z, t&32);
555 if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
559 if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
563 if (arg.i>INTMAX_MAX) {
565 } else if (fl & MARK_POS) {
567 } else if (fl & PAD_POS) {
577 if (p>=0) fl &= ~ZERO_PAD;
578 p = MAX(p, z-a + !arg.i);
585 if (1) a = strerror(errno); else
600 for (i=0; *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
603 pad(f, ' ', w, p, fl);
605 for (i=0; *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
607 pad(f, ' ', w, p, fl^LEFT_ADJ);
610 case 'e': case 'f': case 'g': case 'a':
611 case 'E': case 'F': case 'G': case 'A':
612 l = fmt_fp(f, arg.f, w, p, fl, t);
616 if (p < z-a) p = z-a;
617 if (w < pl+p) w = pl+p;
619 pad(f, ' ', w, pl+p, fl);
621 pad(f, '0', w, pl+p, fl^ZERO_PAD);
622 pad(f, '0', p, z-a, 0);
624 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
632 for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
633 pop_arg(nl_arg+i, nl_type[i], ap);
634 for (; i<=NL_ARGMAX && !nl_type[i]; i++);
635 if (i<=NL_ARGMAX) return -1;
639 int vfprintf(FILE *f, const char *fmt, va_list ap)
642 int nl_type[NL_ARGMAX+1] = {0};
643 union arg nl_arg[NL_ARGMAX+1];
644 unsigned char internal_buf[80], *saved_buf = 0;
648 if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
653 f->buf = internal_buf;
654 f->buf_size = sizeof internal_buf;
656 ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
659 if (!f->wpos) ret = -1;
662 f->wpos = f->wbase = f->wend = 0;