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))
17 /* Convenient bit representation for modifier flags, which all fall
18 * within 31 codepoints of the space character. */
20 #define ALT_FORM (1U<<'#'-' ')
21 #define ZERO_PAD (1U<<'0'-' ')
22 #define LEFT_ADJ (1U<<'-'-' ')
23 #define PAD_POS (1U<<' '-' ')
24 #define MARK_POS (1U<<'+'-' ')
25 #define GROUPED (1U<<'\''-' ')
27 #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
29 #if UINT_MAX == ULONG_MAX
33 #if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
37 /* State machine to accept length modifiers + conversion specifiers.
38 * Result is 0 on failure, or an argument type to pop on success. */
41 BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
44 PTR, INT, UINT, ULLONG,
51 SHORT, USHORT, CHAR, UCHAR,
53 LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
67 #define S(x) [(x)-'A']
69 static const unsigned char states[]['z'-'A'+1] = {
71 S('d') = INT, S('i') = INT,
72 S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
73 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
74 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
75 S('c') = CHAR, S('C') = INT,
76 S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
78 S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
79 S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
80 }, { /* 1: l-prefixed */
81 S('d') = LONG, S('i') = LONG,
82 S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
83 S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
84 S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
85 S('c') = INT, S('s') = PTR, S('n') = PTR,
87 }, { /* 2: ll-prefixed */
88 S('d') = LLONG, S('i') = LLONG,
89 S('o') = ULLONG, S('u') = ULLONG,
90 S('x') = ULLONG, S('X') = ULLONG,
92 }, { /* 3: h-prefixed */
93 S('d') = SHORT, S('i') = SHORT,
94 S('o') = USHORT, S('u') = USHORT,
95 S('x') = USHORT, S('X') = USHORT,
98 }, { /* 4: hh-prefixed */
99 S('d') = CHAR, S('i') = CHAR,
100 S('o') = UCHAR, S('u') = UCHAR,
101 S('x') = UCHAR, S('X') = UCHAR,
103 }, { /* 5: L-prefixed */
104 S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
105 S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
107 }, { /* 6: z- or t-prefixed (assumed to be same size) */
108 S('d') = PDIFF, S('i') = PDIFF,
109 S('o') = SIZET, S('u') = SIZET,
110 S('x') = SIZET, S('X') = SIZET,
112 }, { /* 7: j-prefixed */
113 S('d') = IMAX, S('i') = IMAX,
114 S('o') = UMAX, S('u') = UMAX,
115 S('x') = UMAX, S('X') = UMAX,
120 #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
129 static void pop_arg(union arg *arg, int type, va_list *ap)
131 /* Give the compiler a hint for optimizing the switch. */
132 if ((unsigned)type > MAXSTATE) return;
134 case PTR: arg->p = va_arg(*ap, void *);
135 break; case INT: arg->i = va_arg(*ap, int);
136 break; case UINT: arg->i = va_arg(*ap, unsigned int);
138 break; case LONG: arg->i = va_arg(*ap, long);
139 break; case ULONG: arg->i = va_arg(*ap, unsigned long);
141 break; case ULLONG: arg->i = va_arg(*ap, unsigned long long);
142 break; case SHORT: arg->i = (short)va_arg(*ap, int);
143 break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int);
144 break; case CHAR: arg->i = (signed char)va_arg(*ap, int);
145 break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int);
147 break; case LLONG: arg->i = va_arg(*ap, long long);
148 break; case SIZET: arg->i = va_arg(*ap, size_t);
149 break; case IMAX: arg->i = va_arg(*ap, intmax_t);
150 break; case UMAX: arg->i = va_arg(*ap, uintmax_t);
151 break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t);
152 break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *);
154 break; case DBL: arg->f = va_arg(*ap, double);
155 break; case LDBL: arg->f = va_arg(*ap, long double);
159 static void out(FILE *f, const char *s, size_t l)
161 __fwritex((void *)s, l, f);
164 static void pad(FILE *f, char c, int w, int l, int fl)
167 if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
169 memset(pad, c, l>sizeof pad ? sizeof pad : l);
170 for (; l >= sizeof pad; l -= sizeof pad)
171 out(f, pad, sizeof pad);
175 static const char xdigits[16] = {
179 static char *fmt_x(uintmax_t x, char *s, int lower)
181 for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
185 static char *fmt_o(uintmax_t x, char *s)
187 for (; x; x>>=3) *--s = '0' + (x&7);
191 static char *fmt_u(uintmax_t x, char *s)
194 for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
195 for (y=x; y; y/=10) *--s = '0' + y%10;
199 /* Do not override this check. The floating point printing code below
200 * depends on the float.h constants being right. If they are wrong, it
201 * may overflow the stack. */
202 #if LDBL_MANT_DIG == 53
203 typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)];
206 static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
208 uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion
209 + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
210 uint32_t *a, *d, *r, *z;
211 int e2=0, e, i, j, l;
212 char buf[9+LDBL_MANT_DIG/4], *s;
213 const char *prefix="-0X+0X 0X-0x+0x 0x";
215 char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
220 } else if (fl & MARK_POS) {
222 } else if (fl & PAD_POS) {
224 } else prefix++, pl=0;
227 char *s = (t&32)?"inf":"INF";
228 if (y!=y) s=(t&32)?"nan":"NAN", pl=0;
229 pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
232 pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
236 y = frexpl(y, &e2) * 2;
240 long double round = 8.0;
243 if (t&32) prefix += 9;
246 if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
247 else re=LDBL_MANT_DIG/4-1-p;
250 while (re--) round*=16;
262 estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
263 if (estr==ebuf) *--estr='0';
264 *--estr = (e2<0 ? '-' : '+');
265 *--estr = t+('p'-'a');
270 *s++=xdigits[x]|(t&32);
272 if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
275 if (p && s-buf-2 < p)
276 l = (p+2) + (ebuf-estr);
278 l = (s-buf) + (ebuf-estr);
280 pad(f, ' ', w, pl+l, fl);
282 pad(f, '0', w, pl+l, fl^ZERO_PAD);
284 pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
285 out(f, estr, ebuf-estr);
286 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
291 if (y) y *= 0x1p28, e2-=28;
294 else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
298 y = 1000000000*(y-*z++);
304 for (d=z-1; d>=a; d--) {
305 uint64_t x = ((uint64_t)*d<<sh)+carry;
307 carry = x / 1000000000;
309 if (carry) *--a = carry;
310 while (z>a && !z[-1]) z--;
314 uint32_t carry=0, *b;
315 int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3+8)/9;
316 for (d=a; d<z; d++) {
317 uint32_t rm = *d & (1<<sh)-1;
318 *d = (*d>>sh) + carry;
319 carry = (1000000000>>sh) * rm;
322 if (carry) *z++ = carry;
323 /* Avoid (slow!) computation past requested precision */
324 b = (t|32)=='f' ? r : a;
325 if (z-b > need) z = b+need;
329 if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
332 /* Perform rounding: j is precision after the radix (possibly neg) */
333 j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
336 /* We avoid C's broken division of negative numbers */
337 d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
340 for (i=10, j++; j<9; i*=10, j++);
342 /* Are there any significant digits past j? */
344 long double round = 2/LDBL_EPSILON;
346 if (*d/i & 1) round += 2;
347 if (x<i/2) small=0x0.8p0;
348 else if (x==i/2 && d+1==z) small=0x1.0p0;
350 if (pl && *prefix=='-') round*=-1, small*=-1;
352 /* Decide whether to round by probing round+small */
353 if (round+small != round) {
355 while (*d > 999999999) {
360 for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
365 for (; z>a && !z[-1]; z--);
376 if (!(fl&ALT_FORM)) {
377 /* Count trailing zeros in last place */
378 if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
381 p = MIN(p,MAX(0,9*(z-r-1)-j));
383 p = MIN(p,MAX(0,9*(z-r-1)+e-j));
386 l = 1 + p + (p || (fl&ALT_FORM));
390 estr=fmt_u(e<0 ? -e : e, ebuf);
391 while(ebuf-estr<2) *--estr='0';
392 *--estr = (e<0 ? '-' : '+');
397 pad(f, ' ', w, pl+l, fl);
399 pad(f, '0', w, pl+l, fl^ZERO_PAD);
403 for (d=a; d<=r; d++) {
404 char *s = fmt_u(*d, buf+9);
405 if (d!=a) while (s>buf) *--s='0';
406 else if (s==buf+9) *--s='0';
409 if (p || (fl&ALT_FORM)) out(f, ".", 1);
410 for (; d<z && p>0; d++, p-=9) {
411 char *s = fmt_u(*d, buf+9);
412 while (s>buf) *--s='0';
415 pad(f, '0', p+9, 9, 0);
418 for (d=a; d<z && p>=0; d++) {
419 char *s = fmt_u(*d, buf+9);
420 if (s==buf+9) *--s='0';
421 if (d!=a) while (s>buf) *--s='0';
424 if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
426 out(f, s, MIN(buf+9-s, p));
429 pad(f, '0', p+18, 18, 0);
430 out(f, estr, ebuf-estr);
433 pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
438 static int getint(char **s) {
440 for (i=0; isdigit(**s); (*s)++)
441 i = 10*i + (**s-'0');
445 static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
447 char *a, *z, *s=(char *)fmt;
455 char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
462 /* Update output count, end loop when fmt is exhausted */
464 if (l > INT_MAX - cnt) {
471 /* Handle literal text and %% format specifiers */
472 for (a=s; *s && *s!='%'; s++);
473 for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
478 if (isdigit(s[1]) && s[2]=='$') {
487 /* Read modifier flags */
488 for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
491 /* Read field width */
493 if (isdigit(s[1]) && s[2]=='$') {
495 nl_type[s[1]-'0'] = INT;
496 w = nl_arg[s[1]-'0'].i;
499 w = f ? va_arg(*ap, int) : 0;
502 if (w<0) fl|=LEFT_ADJ, w=-w;
503 } else if ((w=getint(&s))<0) return -1;
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;
512 p = f ? va_arg(*ap, int) : 0;
515 } else if (*s=='.') {
520 /* Format specifier state machine */
523 if (OOB(*s)) return -1;
525 st=states[st]S(*s++);
529 /* Check validity of argument type (nl/normal) */
531 if (argpos>=0) return -1;
533 if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
534 else if (f) pop_arg(&arg, st, ap);
540 z = buf + sizeof(buf);
545 /* Transform ls,lc -> S,C */
546 if (ps && (t&15)==3) t&=~32;
548 /* - and 0 flags are mutually exclusive */
549 if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
554 case BARE: *(int *)arg.p = cnt; break;
555 case LPRE: *(long *)arg.p = cnt; break;
556 case LLPRE: *(long long *)arg.p = cnt; break;
557 case HPRE: *(unsigned short *)arg.p = cnt; break;
558 case HHPRE: *(unsigned char *)arg.p = cnt; break;
559 case ZTPRE: *(size_t *)arg.p = cnt; break;
560 case JPRE: *(uintmax_t *)arg.p = cnt; break;
564 p = MAX(p, 2*sizeof(void*));
568 a = fmt_x(arg.i, z, t&32);
569 if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
573 if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
577 if (arg.i>INTMAX_MAX) {
579 } else if (fl & MARK_POS) {
581 } else if (fl & PAD_POS) {
587 if (p>=0) fl &= ~ZERO_PAD;
592 p = MAX(p, z-a + !arg.i);
599 if (1) a = strerror(errno); else
601 a = arg.p ? arg.p : "(null)";
614 for (i=l=0; i<0U+p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
617 pad(f, ' ', w, p, fl);
619 for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
621 pad(f, ' ', w, p, fl^LEFT_ADJ);
624 case 'e': case 'f': case 'g': case 'a':
625 case 'E': case 'F': case 'G': case 'A':
626 l = fmt_fp(f, arg.f, w, p, fl, t);
630 if (p < z-a) p = z-a;
631 if (w < pl+p) w = pl+p;
633 pad(f, ' ', w, pl+p, fl);
635 pad(f, '0', w, pl+p, fl^ZERO_PAD);
636 pad(f, '0', p, z-a, 0);
638 pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
646 for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
647 pop_arg(nl_arg+i, nl_type[i], ap);
648 for (; i<=NL_ARGMAX && !nl_type[i]; i++);
649 if (i<=NL_ARGMAX) return -1;
653 int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
656 int nl_type[NL_ARGMAX+1] = {0};
657 union arg nl_arg[NL_ARGMAX+1];
658 unsigned char internal_buf[80], *saved_buf = 0;
661 /* the copy allows passing va_list* even if va_list is an array */
663 if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
671 f->wpos = f->wbase = f->buf = internal_buf;
672 f->buf_size = sizeof internal_buf;
673 f->wend = internal_buf + sizeof internal_buf;
675 ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
678 if (!f->wpos) ret = -1;
681 f->wpos = f->wbase = f->wend = 0;