vfprintf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. #include "stdio_impl.h"
  2. /* Some useful macros */
  3. #define MAX(a,b) ((a)>(b) ? (a) : (b))
  4. #define MIN(a,b) ((a)<(b) ? (a) : (b))
  5. #define CONCAT2(x,y) x ## y
  6. #define CONCAT(x,y) CONCAT2(x,y)
  7. /* Convenient bit representation for modifier flags, which all fall
  8. * within 31 codepoints of the space character. */
  9. #define ALT_FORM (1U<<'#'-' ')
  10. #define ZERO_PAD (1U<<'0'-' ')
  11. #define LEFT_ADJ (1U<<'-'-' ')
  12. #define PAD_POS (1U<<' '-' ')
  13. #define MARK_POS (1U<<'+'-' ')
  14. #define GROUPED (1U<<'\''-' ')
  15. #define FLAGMASK (ALT_FORM|ZERO_PAD|LEFT_ADJ|PAD_POS|MARK_POS|GROUPED)
  16. #if UINT_MAX == ULONG_MAX
  17. #define LONG_IS_INT
  18. #endif
  19. #if SIZE_MAX != ULONG_MAX || UINTMAX_MAX != ULLONG_MAX
  20. #define ODD_TYPES
  21. #endif
  22. /* State machine to accept length modifiers + conversion specifiers.
  23. * Result is 0 on failure, or an argument type to pop on success. */
  24. enum {
  25. BARE, LPRE, LLPRE, HPRE, HHPRE, BIGLPRE,
  26. ZTPRE, JPRE,
  27. STOP,
  28. PTR, INT, UINT, ULLONG,
  29. #ifndef LONG_IS_INT
  30. LONG, ULONG,
  31. #else
  32. #define LONG INT
  33. #define ULONG UINT
  34. #endif
  35. SHORT, USHORT, CHAR, UCHAR,
  36. #ifdef ODD_TYPES
  37. LLONG, SIZET, IMAX, UMAX, PDIFF, UIPTR,
  38. #else
  39. #define LLONG ULLONG
  40. #define SIZET ULONG
  41. #define IMAX LLONG
  42. #define UMAX ULLONG
  43. #define PDIFF LONG
  44. #define UIPTR ULONG
  45. #endif
  46. DBL, LDBL,
  47. NOARG,
  48. MAXSTATE
  49. };
  50. #define S(x) [(x)-'A']
  51. static const unsigned char states[]['z'-'A'+1] = {
  52. { /* 0: bare types */
  53. S('d') = INT, S('i') = INT,
  54. S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT,
  55. S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL,
  56. S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL,
  57. S('c') = CHAR, S('C') = INT,
  58. S('s') = PTR, S('S') = PTR, S('p') = UIPTR, S('n') = PTR,
  59. S('m') = NOARG,
  60. S('l') = LPRE, S('h') = HPRE, S('L') = BIGLPRE,
  61. S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE,
  62. }, { /* 1: l-prefixed */
  63. S('d') = LONG, S('i') = LONG,
  64. S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG,
  65. S('c') = INT, S('s') = PTR, S('n') = PTR,
  66. S('l') = LLPRE,
  67. }, { /* 2: ll-prefixed */
  68. S('d') = LLONG, S('i') = LLONG,
  69. S('o') = ULLONG, S('u') = ULLONG,
  70. S('x') = ULLONG, S('X') = ULLONG,
  71. S('n') = PTR,
  72. }, { /* 3: h-prefixed */
  73. S('d') = SHORT, S('i') = SHORT,
  74. S('o') = USHORT, S('u') = USHORT,
  75. S('x') = USHORT, S('X') = USHORT,
  76. S('n') = PTR,
  77. S('h') = HHPRE,
  78. }, { /* 4: hh-prefixed */
  79. S('d') = CHAR, S('i') = CHAR,
  80. S('o') = UCHAR, S('u') = UCHAR,
  81. S('x') = UCHAR, S('X') = UCHAR,
  82. S('n') = PTR,
  83. }, { /* 5: L-prefixed */
  84. S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL,
  85. S('E') = LDBL, S('F') = LDBL, S('G') = LDBL, S('A') = LDBL,
  86. S('n') = PTR,
  87. }, { /* 6: z- or t-prefixed (assumed to be same size) */
  88. S('d') = PDIFF, S('i') = PDIFF,
  89. S('o') = SIZET, S('u') = SIZET,
  90. S('x') = SIZET, S('X') = SIZET,
  91. S('n') = PTR,
  92. }, { /* 7: j-prefixed */
  93. S('d') = IMAX, S('i') = IMAX,
  94. S('o') = UMAX, S('u') = UMAX,
  95. S('x') = UMAX, S('X') = UMAX,
  96. S('n') = PTR,
  97. }
  98. };
  99. #define OOB(x) ((unsigned)(x)-'A' > 'z'-'A')
  100. union arg
  101. {
  102. uintmax_t i;
  103. long double f;
  104. void *p;
  105. };
  106. static void pop_arg(union arg *arg, int type, va_list *ap)
  107. {
  108. /* Give the compiler a hint for optimizing the switch. */
  109. if ((unsigned)type > MAXSTATE) return;
  110. switch (type) {
  111. case PTR: arg->p = va_arg(*ap, void *);
  112. break; case INT: arg->i = va_arg(*ap, int);
  113. break; case UINT: arg->i = va_arg(*ap, unsigned int);
  114. #ifndef LONG_IS_INT
  115. break; case LONG: arg->i = va_arg(*ap, long);
  116. break; case ULONG: arg->i = va_arg(*ap, unsigned long);
  117. #endif
  118. break; case ULLONG: arg->i = va_arg(*ap, unsigned long long);
  119. break; case SHORT: arg->i = (short)va_arg(*ap, int);
  120. break; case USHORT: arg->i = (unsigned short)va_arg(*ap, int);
  121. break; case CHAR: arg->i = (signed char)va_arg(*ap, int);
  122. break; case UCHAR: arg->i = (unsigned char)va_arg(*ap, int);
  123. #ifdef ODD_TYPES
  124. break; case LLONG: arg->i = va_arg(*ap, long long);
  125. break; case SIZET: arg->i = va_arg(*ap, size_t);
  126. break; case IMAX: arg->i = va_arg(*ap, intmax_t);
  127. break; case UMAX: arg->i = va_arg(*ap, uintmax_t);
  128. break; case PDIFF: arg->i = va_arg(*ap, ptrdiff_t);
  129. break; case UIPTR: arg->i = (uintptr_t)va_arg(*ap, void *);
  130. #endif
  131. break; case DBL: arg->f = va_arg(*ap, double);
  132. break; case LDBL: arg->f = va_arg(*ap, long double);
  133. }
  134. }
  135. static void out(FILE *f, const char *s, size_t l)
  136. {
  137. __fwritex((void *)s, l, f);
  138. }
  139. static void pad(FILE *f, char c, int w, int l, int fl)
  140. {
  141. char pad[256];
  142. if (fl & (LEFT_ADJ | ZERO_PAD) || l >= w) return;
  143. l = w - l;
  144. memset(pad, c, l>sizeof pad ? sizeof pad : l);
  145. for (; l >= sizeof pad; l -= sizeof pad)
  146. out(f, pad, sizeof pad);
  147. out(f, pad, l);
  148. }
  149. static const char xdigits[16] = {
  150. "0123456789ABCDEF"
  151. };
  152. static char *fmt_x(uintmax_t x, char *s, int lower)
  153. {
  154. for (; x; x>>=4) *--s = xdigits[(x&15)]|lower;
  155. return s;
  156. }
  157. static char *fmt_o(uintmax_t x, char *s)
  158. {
  159. for (; x; x>>=3) *--s = '0' + (x&7);
  160. return s;
  161. }
  162. static char *fmt_u(uintmax_t x, char *s)
  163. {
  164. unsigned long y;
  165. for ( ; x>ULONG_MAX; x/=10) *--s = '0' + x%10;
  166. for (y=x; y; y/=10) *--s = '0' + y%10;
  167. return s;
  168. }
  169. static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
  170. {
  171. uint32_t big[(LDBL_MAX_EXP+LDBL_MANT_DIG)/9+1];
  172. uint32_t *a, *d, *r, *z;
  173. int e2=0, e, i, j, l;
  174. char buf[9+LDBL_MANT_DIG/4], *s;
  175. const char *prefix="-+ ";
  176. int pl;
  177. char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
  178. pl=1;
  179. if (y<0 || 1/y<0) {
  180. y=-y;
  181. } else if (fl & MARK_POS) {
  182. prefix++;
  183. } else if (fl & PAD_POS) {
  184. prefix+=2;
  185. } else pl=0;
  186. if (!isfinite(y)) {
  187. char *s = (t&32)?"inf":"INF";
  188. if (y!=y) s=(t&32)?"nan":"NAN", pl=0;
  189. pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
  190. out(f, prefix, pl);
  191. out(f, s, 3);
  192. pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
  193. return MAX(w, 3+pl);
  194. }
  195. y = frexpl(y, &e2) * 2;
  196. if (y) e2--;
  197. if ((t|32)=='a') {
  198. long double round = 8.0;
  199. int re;
  200. if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
  201. else re=LDBL_MANT_DIG/4-1-p;
  202. if (re) {
  203. if (pl && *prefix=='-') y=-y;
  204. while (re--) round*=16;
  205. y+=round;
  206. y-=round;
  207. if (y<0) y=-y;
  208. }
  209. estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
  210. if (estr==ebuf) *--estr='0';
  211. *--estr = (e2<0 ? '-' : '+');
  212. *--estr = t+('p'-'a');
  213. s=buf;
  214. *s++='0';
  215. *s++=t+('x'-'a');
  216. do {
  217. int x=y;
  218. *s++=xdigits[x]|(t&32);
  219. y=16*(y-x);
  220. if (s-buf==3 && (y||p>0||(fl&ALT_FORM))) *s++='.';
  221. } while (y);
  222. if (p<0) p = s-buf-4;
  223. l = 1 + p + (p || (fl&ALT_FORM)) + ebuf-estr;
  224. pad(f, ' ', w, pl+l, fl);
  225. out(f, prefix, pl);
  226. pad(f, '0', w, pl+l, fl^ZERO_PAD);
  227. out(f, buf, s-buf);
  228. pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
  229. out(f, estr, ebuf-estr);
  230. pad(f, '0', w, pl+l, fl^LEFT_ADJ);
  231. return MAX(w, pl+l);
  232. }
  233. if (p<0) p=6;
  234. y *= 0x1p28; e2-=28;
  235. if (e2<0) a=r=z=big;
  236. else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
  237. do {
  238. *z = y;
  239. y = 1000000000*(y-*z++);
  240. } while (y);
  241. while (e2>0) {
  242. uint32_t carry=0;
  243. int sh=MIN(29,e2);
  244. for (d=z-1; d>=a; d--) {
  245. uint64_t x = ((uint64_t)*d<<sh)+carry;
  246. *d = x % 1000000000;
  247. carry = x / 1000000000;
  248. }
  249. if (!z[-1] && z>a) z--;
  250. if (carry) *--a = carry;
  251. e2-=sh;
  252. }
  253. while (e2<0) {
  254. uint32_t carry=0, *z2;
  255. int sh=MIN(9,-e2);
  256. for (d=a; d<z; d++) {
  257. uint32_t rm = *d & (1<<sh)-1;
  258. *d = (*d>>sh) + carry;
  259. carry = (1000000000>>sh) * rm;
  260. }
  261. if (!*a) a++;
  262. if (carry) *z++ = carry;
  263. /* Avoid (slow!) computation past requested precision */
  264. z2 = ((t|32)=='f' ? r : a) + 2 + p/9;
  265. z = MIN(z, z2);
  266. e2+=sh;
  267. }
  268. if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
  269. else e=0;
  270. /* Perform rounding: j is precision after the radix (possibly neg) */
  271. j = p - ((t|32)!='f')*e - ((t|32)=='g');
  272. if (j < 9*(z-r-1)) {
  273. uint32_t x;
  274. /* We avoid C's broken division of negative numbers */
  275. d = r + 1 + (j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP;
  276. j += 9*LDBL_MAX_EXP;
  277. j %= 9;
  278. for (i=10, j++; j<9; i*=10, j++);
  279. x = *d % i;
  280. /* Are there any significant digits past j? */
  281. if (x || d+1!=z) {
  282. long double round = CONCAT(0x1p,LDBL_MANT_DIG);
  283. long double small;
  284. if (x<i/2) small=0x01p-1;
  285. else if (i==i/2 && d+1==z) small=0x10p-1;
  286. else small=0x11p-1;
  287. if (pl && *prefix=='-') round*=-1, small*=-1;
  288. /* Decide whether to round by probing round+small */
  289. if (round+small != round) {
  290. *d = *d - x + i;
  291. while (*d > 999999999) {
  292. *d--=0;
  293. (*d)++;
  294. }
  295. if (d<a) a=d;
  296. for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
  297. }
  298. }
  299. for (; !z[-1] && z>a; z--);
  300. }
  301. if ((t|32)=='g') {
  302. if (!p) p++;
  303. if (p>e && e>=-4) {
  304. t--;
  305. p-=e+1;
  306. } else {
  307. t-=2;
  308. p--;
  309. }
  310. if (!(fl&ALT_FORM)) {
  311. /* Count trailing zeros in last place */
  312. if (z>a) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
  313. else j=9;
  314. if ((t|32)=='f')
  315. p = MIN(p,MAX(0,9*(z-r-1)-j));
  316. else
  317. p = MIN(p,MAX(0,9*(z-r-1)+e-j));
  318. }
  319. }
  320. l = 1 + p + (p || (fl&ALT_FORM));
  321. if ((t|32)=='f') {
  322. if (e>0) l+=e;
  323. } else {
  324. estr=fmt_u(e<0 ? -e : e, ebuf);
  325. while(ebuf-estr<2) *--estr='0';
  326. *--estr = (e<0 ? '-' : '+');
  327. *--estr = t;
  328. l += ebuf-estr;
  329. }
  330. pad(f, ' ', w, pl+l, fl);
  331. out(f, prefix, pl);
  332. pad(f, '0', w, pl+l, fl^ZERO_PAD);
  333. if ((t|32)=='f') {
  334. if (a>r) a=r;
  335. for (d=a; d<=r; d++) {
  336. char *s = fmt_u(*d, buf+9);
  337. if (d!=a) while (s>buf) *--s='0';
  338. else if (s==buf+9) *--s='0';
  339. out(f, s, buf+9-s);
  340. }
  341. if (p || (fl&ALT_FORM)) out(f, ".", 1);
  342. for (; d<z && p>0; d++, p-=9) {
  343. char *s = fmt_u(*d, buf+9);
  344. while (s>buf) *--s='0';
  345. out(f, s, MIN(9,p));
  346. }
  347. pad(f, '0', p+9, 9, 0);
  348. } else {
  349. if (z<=a) z=a+1;
  350. for (d=a; d<z && p>=0; d++) {
  351. char *s = fmt_u(*d, buf+9);
  352. if (s==buf+9) *--s='0';
  353. if (d!=a) while (s>buf) *--s='0';
  354. else {
  355. out(f, s++, 1);
  356. if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
  357. }
  358. out(f, s, MIN(buf+9-s, p));
  359. p -= buf+9-s;
  360. }
  361. pad(f, '0', p+18, 18, 0);
  362. out(f, estr, ebuf-estr);
  363. }
  364. pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
  365. return MAX(w, pl+l);
  366. }
  367. static int getint(char **s) {
  368. int i;
  369. for (i=0; isdigit(**s); (*s)++)
  370. i = 10*i + (**s-'0');
  371. return i;
  372. }
  373. static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
  374. {
  375. char *a, *z, *s=(char *)fmt;
  376. unsigned l10n=0, litpct, fl;
  377. int w, p;
  378. union arg arg;
  379. int argpos;
  380. unsigned st, ps;
  381. int cnt=0, l=0;
  382. int i;
  383. char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
  384. const char *prefix;
  385. int t, pl;
  386. wchar_t wc[2], *ws;
  387. char mb[4];
  388. for (;;) {
  389. /* Update output count, end loop when fmt is exhausted */
  390. if (cnt >= 0) {
  391. if (l > INT_MAX - cnt) {
  392. errno = EOVERFLOW;
  393. cnt = -1;
  394. } else cnt += l;
  395. }
  396. if (!*s) break;
  397. /* Handle literal text and %% format specifiers */
  398. for (a=s; *s && *s!='%'; s++);
  399. litpct = strspn(s, "%")/2; /* Optimize %%%% runs */
  400. z = s+litpct;
  401. s += 2*litpct;
  402. l = z-a;
  403. if (f) out(f, a, l);
  404. if (l) continue;
  405. if (isdigit(s[1]) && s[2]=='$') {
  406. l10n=1;
  407. argpos = s[1]-'0';
  408. s+=3;
  409. } else {
  410. argpos = -1;
  411. s++;
  412. }
  413. /* Read modifier flags */
  414. for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
  415. fl |= 1U<<*s-' ';
  416. /* Read field width */
  417. if (*s=='*') {
  418. if (isdigit(s[1]) && s[2]=='$') {
  419. l10n=1;
  420. nl_type[s[1]-'0'] = INT;
  421. w = nl_arg[s[1]-'0'].i;
  422. s+=3;
  423. } else if (!l10n) {
  424. w = f ? va_arg(*ap, int) : 0;
  425. s++;
  426. } else return -1;
  427. if (w<0) fl|=LEFT_ADJ, w=-w;
  428. } else if ((w=getint(&s))<0) return -1;
  429. /* Read precision */
  430. if (*s=='.' && s[1]=='*') {
  431. if (isdigit(s[2]) && s[3]=='$') {
  432. nl_type[s[2]-'0'] = INT;
  433. p = nl_arg[s[2]-'0'].i;
  434. s+=4;
  435. } else if (!l10n) {
  436. p = f ? va_arg(*ap, int) : 0;
  437. s+=2;
  438. } else return -1;
  439. } else if (*s=='.') {
  440. s++;
  441. p = getint(&s);
  442. } else p = -1;
  443. /* Format specifier state machine */
  444. st=0;
  445. do {
  446. if (OOB(*s)) return -1;
  447. ps=st;
  448. st=states[st]S(*s++);
  449. } while (st-1<STOP);
  450. if (!st) return -1;
  451. /* Check validity of argument type (nl/normal) */
  452. if (st==NOARG) {
  453. if (argpos>=0) return -1;
  454. else if (!f) continue;
  455. } else {
  456. if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
  457. else if (f) pop_arg(&arg, st, ap);
  458. else return 0;
  459. }
  460. if (!f) continue;
  461. z = buf + sizeof(buf);
  462. prefix = "-+ 0X0x";
  463. pl = 0;
  464. t = s[-1];
  465. /* Transform ls,lc -> S,C */
  466. if (ps && (t&15)==3) t&=~32;
  467. /* - and 0 flags are mutually exclusive */
  468. if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
  469. switch(t) {
  470. case 'n':
  471. switch(ps) {
  472. case BARE: *(int *)arg.p = cnt; break;
  473. case LPRE: *(long *)arg.p = cnt; break;
  474. case LLPRE: *(long long *)arg.p = cnt; break;
  475. case HPRE: *(unsigned short *)arg.p = cnt; break;
  476. case HHPRE: *(unsigned char *)arg.p = cnt; break;
  477. case ZTPRE: *(size_t *)arg.p = cnt; break;
  478. case JPRE: *(uintmax_t *)arg.p = cnt; break;
  479. }
  480. continue;
  481. case 'p':
  482. p = MAX(p, 2*sizeof(void*));
  483. t = 'x';
  484. fl |= ALT_FORM;
  485. case 'x': case 'X':
  486. a = fmt_x(arg.i, z, t&32);
  487. if (fl & ALT_FORM) prefix+=(t>>4), pl=2;
  488. if (0) {
  489. case 'o':
  490. a = fmt_o(arg.i, z);
  491. if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
  492. } if (0) {
  493. case 'd': case 'i':
  494. pl=1;
  495. if (arg.i>INTMAX_MAX) {
  496. arg.i=-arg.i;
  497. } else if (fl & MARK_POS) {
  498. prefix++;
  499. } else if (fl & PAD_POS) {
  500. prefix+=2;
  501. } else pl=0;
  502. case 'u':
  503. a = fmt_u(arg.i, z);
  504. }
  505. if (!arg.i && !p) continue;
  506. if (p>=0) fl &= ~ZERO_PAD;
  507. p = MAX(p, z-a + !arg.i);
  508. break;
  509. case 'c':
  510. *(a=z-(p=1))=arg.i;
  511. fl &= ~ZERO_PAD;
  512. break;
  513. case 'm':
  514. if (1) a = strerror(errno); else
  515. case 's':
  516. a = arg.p;
  517. z = memchr(a, 0, p);
  518. if (!z) z=a+p;
  519. else p=z-a;
  520. fl &= ~ZERO_PAD;
  521. break;
  522. case 'C':
  523. wc[0] = arg.i;
  524. wc[1] = 0;
  525. arg.p = wc;
  526. p = -1;
  527. case 'S':
  528. ws = arg.p;
  529. for (i=0; *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
  530. if (l<0) return -1;
  531. p = i;
  532. pad(f, ' ', w, p, fl);
  533. ws = arg.p;
  534. for (i=0; *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
  535. out(f, mb, l);
  536. pad(f, ' ', w, p, fl^LEFT_ADJ);
  537. l = w>p ? w : p;
  538. continue;
  539. case 'e': case 'f': case 'g': case 'a':
  540. case 'E': case 'F': case 'G': case 'A':
  541. l = fmt_fp(f, arg.f, w, p, fl, t);
  542. continue;
  543. }
  544. if (p < z-a) p = z-a;
  545. if (w < pl+p) w = pl+p;
  546. pad(f, ' ', w, pl+p, fl);
  547. out(f, prefix, pl);
  548. pad(f, '0', w, pl+p, fl^ZERO_PAD);
  549. pad(f, '0', p, z-a, 0);
  550. out(f, a, z-a);
  551. pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
  552. l = w;
  553. }
  554. if (f) return cnt;
  555. if (!l10n) return 0;
  556. for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
  557. pop_arg(nl_arg+i, nl_type[i], ap);
  558. for (; i<=NL_ARGMAX && !nl_type[i]; i++);
  559. if (i<=NL_ARGMAX) return -1;
  560. return 1;
  561. }
  562. int vfprintf(FILE *f, const char *fmt, va_list ap)
  563. {
  564. va_list ap2;
  565. int nl_type[NL_ARGMAX] = {0};
  566. union arg nl_arg[NL_ARGMAX];
  567. unsigned char internal_buf[80], *saved_buf = 0;
  568. int ret;
  569. va_copy(ap2, ap);
  570. if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
  571. FLOCK(f);
  572. if (!f->buf_size) {
  573. saved_buf = f->buf;
  574. f->buf = internal_buf;
  575. f->buf_size = sizeof internal_buf;
  576. }
  577. ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
  578. if (saved_buf) {
  579. f->write(f, 0, 0);
  580. if (!f->wpos) ret = -1;
  581. f->buf = saved_buf;
  582. f->buf_size = 0;
  583. f->wpos = f->wbase = f->wend = 0;
  584. }
  585. FUNLOCK(f);
  586. va_end(ap2);
  587. return ret;
  588. }