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="-+ ";
198 char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
203 } else if (fl & MARK_POS) {
205 } else if (fl & PAD_POS) {
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 (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
227 else re=LDBL_MANT_DIG/4-1-p;
230 if (pl && *prefix=='-') y=-y;
231 while (re--) round*=16;
237 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
238 if (estr==ebuf) *--estr='0';
239 *--estr = (e2<0 ? '-' : '+');
240 *--estr = t+('p'-'a');
247 *s++=xdigits[x]|(t&32);
249 if (s-buf==3 && (y||p>0||(fl&ALT_FORM))) *s++='.';
252 if (p<0) p = s-buf-4;
253 l = 1 + p + (p || (fl&ALT_FORM)) + ebuf-estr;
255 pad(f, ' ', w, pl+l, fl);
257 pad(f, '0', w, pl+l, fl^ZERO_PAD);
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);
269 else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
273 y = 1000000000*(y-*z++);
279 for (d=z-1; d>=a; d--) {
280 uint64_t x = ((uint64_t)*d<<sh)+carry;
282 carry = x / 1000000000;
284 if (!z[-1] && z>a) z--;
285 if (carry) *--a = carry;
289 uint32_t carry=0, *z2;
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;
297 if (carry) *z++ = carry;
298 /* Avoid (slow!) computation past requested precision */
299 z2 = ((t|32)=='f' ? r : a) + 2 + p/9;
304 if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
307 /* Perform rounding: j is precision after the radix (possibly neg) */
308 j = p - ((t|32)!='f')*e - ((t|32)=='g');
311 /* We avoid C's broken division of negative numbers */
312 d = r + 1 + (j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP;
315 for (i=10, j++; j<9; i*=10, j++);
317 /* Are there any significant digits past j? */
319 long double round = CONCAT(0x1p,LDBL_MANT_DIG);
321 if (x<i/2) small=0x01p-1;
322 else if (i==i/2 && d+1==z) small=0x10p-1;
324 if (pl && *prefix=='-') round*=-1, small*=-1;
325 /* Decide whether to round by probing round+small */
326 if (round+small != round) {
328 while (*d > 999999999) {
333 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
336 for (; !z[-1] && z>a; z--);
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++);
353 p = MIN(p,MAX(0,9*(z-r-1)-j));
355 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
358 l = 1 + p + (p || (fl&ALT_FORM));
362 estr=fmt_u(e<0 ? -e : e, ebuf);
363 while(ebuf-estr<2) *--estr='0';
364 *--estr = (e<0 ? '-' : '+');
369 pad(f, ' ', w, pl+l, fl);
371 pad(f, '0', w, pl+l, fl^ZERO_PAD);
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';
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';
387 pad(f, '0', p+9, 9, 0);
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';
396 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
398 out(f, s, MIN(buf+9-s, p));
401 pad(f, '0', p+18, 18, 0);
402 out(f, estr, ebuf-estr);
405 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
410 static int getint(char **s) {
412 for (i=0; isdigit(**s); (*s)++)
413 i = 10*i + (**s-'0');
417 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
419 char *a, *z, *s=(char *)fmt;
420 unsigned l10n=0, litpct, fl;
427 char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
434 /* Update output count, end loop when fmt is exhausted */
436 if (l > INT_MAX - cnt) {
443 /* Handle literal text and %% format specifiers */
444 for (a=s; *s && *s!='%'; s++);
445 litpct = strspn(s, "%")/2; /* Optimize %%%% runs */
452 if (isdigit(s[1]) && s[2]=='$') {
461 /* Read modifier flags */
462 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
465 /* Read field width */
467 if (isdigit(s[1]) && s[2]=='$') {
469 nl_type[s[1]-'0'] = INT;
470 w = nl_arg[s[1]-'0'].i;
473 w = f ? va_arg(*ap, int) : 0;
476 if (w<0) fl|=LEFT_ADJ, w=-w;
477 } else if ((w=getint(&s))<0) return -1;
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;
486 p = f ? va_arg(*ap, int) : 0;
489 } else if (*s=='.') {
494 /* Format specifier state machine */
497 if (OOB(*s)) return -1;
499 st=states[st]S(*s++);
503 /* Check validity of argument type (nl/normal) */
505 if (argpos>=0) return -1;
506 else if (!f) continue;
508 if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
509 else if (f) pop_arg(&arg, st, ap);
515 z = buf + sizeof(buf);
520 /* Transform ls,lc -> S,C */
521 if (ps && (t&15)==3) t&=~32;
523 /* - and 0 flags are mutually exclusive */
524 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
529 case BARE: *(int *)arg.p = cnt; break;
530 case LPRE: *(long *)arg.p = cnt; break;
531 case LLPRE: *(long long *)arg.p = cnt; break;
532 case HPRE: *(unsigned short *)arg.p = cnt; break;
533 case HHPRE: *(unsigned char *)arg.p = cnt; break;
534 case ZTPRE: *(size_t *)arg.p = cnt; break;
535 case JPRE: *(uintmax_t *)arg.p = cnt; break;
539 p = MAX(p, 2*sizeof(void*));
543 a = fmt_x(arg.i, z, t&32);
544 if (fl & ALT_FORM) prefix+=(t>>4), pl=2;
548 if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
552 if (arg.i>INTMAX_MAX) {
554 } else if (fl & MARK_POS) {
556 } else if (fl & PAD_POS) {
562 if (!arg.i && !p) continue;
563 if (p>=0) fl &= ~ZERO_PAD;
564 p = MAX(p, z-a + !arg.i);
571 if (1) a = strerror(errno); else
586 for (i=0; *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
589 pad(f, ' ', w, p, fl);
591 for (i=0; *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
593 pad(f, ' ', w, p, fl^LEFT_ADJ);
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);
602 if (p < z-a) p = z-a;
603 if (w < pl+p) w = pl+p;
605 pad(f, ' ', w, pl+p, fl);
607 pad(f, '0', w, pl+p, fl^ZERO_PAD);
608 pad(f, '0', p, z-a, 0);
610 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
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;
625 int vfprintf(FILE *f, const char *fmt, va_list ap)
628 int nl_type[NL_ARGMAX] = {0};
629 union arg nl_arg[NL_ARGMAX];
630 unsigned char internal_buf[80], *saved_buf = 0;
634 if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
639 f->buf = internal_buf;
640 f->buf_size = sizeof internal_buf;
642 ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
645 if (!f->wpos) ret = -1;
648 f->wpos = f->wbase = f->wend = 0;