vfprintf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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="-0X+0X 0X-0x+0x 0x";
  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+=3;
  183. } else if (fl & PAD_POS) {
  184. prefix+=6;
  185. } else prefix++, 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 (t&32) prefix += 9;
  201. pl += 2;
  202. if (p<0 || p>=LDBL_MANT_DIG/4-1) re=0;
  203. else re=LDBL_MANT_DIG/4-1-p;
  204. if (re) {
  205. while (re--) round*=16;
  206. if (*prefix=='-') {
  207. y=-y;
  208. y-=round;
  209. y+=round;
  210. y=-y;
  211. } else {
  212. y+=round;
  213. y-=round;
  214. }
  215. }
  216. estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
  217. if (estr==ebuf) *--estr='0';
  218. *--estr = (e2<0 ? '-' : '+');
  219. *--estr = t+('p'-'a');
  220. s=buf;
  221. do {
  222. int x=y;
  223. *s++=xdigits[x]|(t&32);
  224. y=16*(y-x);
  225. if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
  226. } while (y);
  227. if (p && s-buf-2 < p)
  228. l = (p+2) + (ebuf-estr);
  229. else
  230. l = (s-buf) + (ebuf-estr);
  231. pad(f, ' ', w, pl+l, fl);
  232. out(f, prefix, pl);
  233. pad(f, '0', w, pl+l, fl^ZERO_PAD);
  234. out(f, buf, s-buf);
  235. pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
  236. out(f, estr, ebuf-estr);
  237. pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
  238. return MAX(w, pl+l);
  239. }
  240. if (p<0) p=6;
  241. if (y) y *= 0x1p28, e2-=28;
  242. if (e2<0) a=r=z=big;
  243. else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
  244. do {
  245. *z = y;
  246. y = 1000000000*(y-*z++);
  247. } while (y);
  248. while (e2>0) {
  249. uint32_t carry=0;
  250. int sh=MIN(29,e2);
  251. for (d=z-1; d>=a; d--) {
  252. uint64_t x = ((uint64_t)*d<<sh)+carry;
  253. *d = x % 1000000000;
  254. carry = x / 1000000000;
  255. }
  256. if (!z[-1] && z>a) z--;
  257. if (carry) *--a = carry;
  258. e2-=sh;
  259. }
  260. while (e2<0) {
  261. uint32_t carry=0, *z2;
  262. int sh=MIN(9,-e2);
  263. for (d=a; d<z; d++) {
  264. uint32_t rm = *d & (1<<sh)-1;
  265. *d = (*d>>sh) + carry;
  266. carry = (1000000000>>sh) * rm;
  267. }
  268. if (!*a) a++;
  269. if (carry) *z++ = carry;
  270. /* Avoid (slow!) computation past requested precision */
  271. z2 = ((t|32)=='f' ? r : a) + 2 + p/9;
  272. z = MIN(z, z2);
  273. e2+=sh;
  274. }
  275. if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
  276. else e=0;
  277. /* Perform rounding: j is precision after the radix (possibly neg) */
  278. j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
  279. if (j < 9*(z-r-1)) {
  280. uint32_t x;
  281. /* We avoid C's broken division of negative numbers */
  282. d = r + 1 + (j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP;
  283. j += 9*LDBL_MAX_EXP;
  284. j %= 9;
  285. for (i=10, j++; j<9; i*=10, j++);
  286. x = *d % i;
  287. /* Are there any significant digits past j? */
  288. if (x || d+1!=z) {
  289. long double round = CONCAT(0x1p,LDBL_MANT_DIG);
  290. long double small;
  291. if (*d/i & 1) round += 2;
  292. if (x<i/2) small=0x0.8p0;
  293. else if (x==i/2 && d+1==z) small=0x1.0p0;
  294. else small=0x1.8p0;
  295. if (pl && *prefix=='-') round*=-1, small*=-1;
  296. *d -= x;
  297. /* Decide whether to round by probing round+small */
  298. if (round+small != round) {
  299. *d = *d + i;
  300. while (*d > 999999999) {
  301. *d--=0;
  302. (*d)++;
  303. }
  304. if (d<a) a=d;
  305. for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
  306. }
  307. }
  308. if (z>d+1) z=d+1;
  309. for (; !z[-1] && z>a; z--);
  310. }
  311. if ((t|32)=='g') {
  312. if (!p) p++;
  313. if (p>e && e>=-4) {
  314. t--;
  315. p-=e+1;
  316. } else {
  317. t-=2;
  318. p--;
  319. }
  320. if (!(fl&ALT_FORM)) {
  321. /* Count trailing zeros in last place */
  322. if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
  323. else j=9;
  324. if ((t|32)=='f')
  325. p = MIN(p,MAX(0,9*(z-r-1)-j));
  326. else
  327. p = MIN(p,MAX(0,9*(z-r-1)+e-j));
  328. }
  329. }
  330. l = 1 + p + (p || (fl&ALT_FORM));
  331. if ((t|32)=='f') {
  332. if (e>0) l+=e;
  333. } else {
  334. estr=fmt_u(e<0 ? -e : e, ebuf);
  335. while(ebuf-estr<2) *--estr='0';
  336. *--estr = (e<0 ? '-' : '+');
  337. *--estr = t;
  338. l += ebuf-estr;
  339. }
  340. pad(f, ' ', w, pl+l, fl);
  341. out(f, prefix, pl);
  342. pad(f, '0', w, pl+l, fl^ZERO_PAD);
  343. if ((t|32)=='f') {
  344. if (a>r) a=r;
  345. for (d=a; d<=r; d++) {
  346. char *s = fmt_u(*d, buf+9);
  347. if (d!=a) while (s>buf) *--s='0';
  348. else if (s==buf+9) *--s='0';
  349. out(f, s, buf+9-s);
  350. }
  351. if (p || (fl&ALT_FORM)) out(f, ".", 1);
  352. for (; d<z && p>0; d++, p-=9) {
  353. char *s = fmt_u(*d, buf+9);
  354. while (s>buf) *--s='0';
  355. out(f, s, MIN(9,p));
  356. }
  357. pad(f, '0', p+9, 9, 0);
  358. } else {
  359. if (z<=a) z=a+1;
  360. for (d=a; d<z && p>=0; d++) {
  361. char *s = fmt_u(*d, buf+9);
  362. if (s==buf+9) *--s='0';
  363. if (d!=a) while (s>buf) *--s='0';
  364. else {
  365. out(f, s++, 1);
  366. if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
  367. }
  368. out(f, s, MIN(buf+9-s, p));
  369. p -= buf+9-s;
  370. }
  371. pad(f, '0', p+18, 18, 0);
  372. out(f, estr, ebuf-estr);
  373. }
  374. pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
  375. return MAX(w, pl+l);
  376. }
  377. static int getint(char **s) {
  378. int i;
  379. for (i=0; isdigit(**s); (*s)++)
  380. i = 10*i + (**s-'0');
  381. return i;
  382. }
  383. static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
  384. {
  385. char *a, *z, *s=(char *)fmt;
  386. unsigned l10n=0, litpct, fl;
  387. int w, p;
  388. union arg arg;
  389. int argpos;
  390. unsigned st, ps;
  391. int cnt=0, l=0;
  392. int i;
  393. char buf[sizeof(uintmax_t)*3+3+LDBL_MANT_DIG/4];
  394. const char *prefix;
  395. int t, pl;
  396. wchar_t wc[2], *ws;
  397. char mb[4];
  398. for (;;) {
  399. /* Update output count, end loop when fmt is exhausted */
  400. if (cnt >= 0) {
  401. if (l > INT_MAX - cnt) {
  402. errno = EOVERFLOW;
  403. cnt = -1;
  404. } else cnt += l;
  405. }
  406. if (!*s) break;
  407. /* Handle literal text and %% format specifiers */
  408. for (a=s; *s && *s!='%'; s++);
  409. litpct = strspn(s, "%")/2; /* Optimize %%%% runs */
  410. z = s+litpct;
  411. s += 2*litpct;
  412. l = z-a;
  413. if (f) out(f, a, l);
  414. if (l) continue;
  415. if (isdigit(s[1]) && s[2]=='$') {
  416. l10n=1;
  417. argpos = s[1]-'0';
  418. s+=3;
  419. } else {
  420. argpos = -1;
  421. s++;
  422. }
  423. /* Read modifier flags */
  424. for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
  425. fl |= 1U<<*s-' ';
  426. /* Read field width */
  427. if (*s=='*') {
  428. if (isdigit(s[1]) && s[2]=='$') {
  429. l10n=1;
  430. nl_type[s[1]-'0'] = INT;
  431. w = nl_arg[s[1]-'0'].i;
  432. s+=3;
  433. } else if (!l10n) {
  434. w = f ? va_arg(*ap, int) : 0;
  435. s++;
  436. } else return -1;
  437. if (w<0) fl|=LEFT_ADJ, w=-w;
  438. } else if ((w=getint(&s))<0) return -1;
  439. /* Read precision */
  440. if (*s=='.' && s[1]=='*') {
  441. if (isdigit(s[2]) && s[3]=='$') {
  442. nl_type[s[2]-'0'] = INT;
  443. p = nl_arg[s[2]-'0'].i;
  444. s+=4;
  445. } else if (!l10n) {
  446. p = f ? va_arg(*ap, int) : 0;
  447. s+=2;
  448. } else return -1;
  449. } else if (*s=='.') {
  450. s++;
  451. p = getint(&s);
  452. } else p = -1;
  453. /* Format specifier state machine */
  454. st=0;
  455. do {
  456. if (OOB(*s)) return -1;
  457. ps=st;
  458. st=states[st]S(*s++);
  459. } while (st-1<STOP);
  460. if (!st) return -1;
  461. /* Check validity of argument type (nl/normal) */
  462. if (st==NOARG) {
  463. if (argpos>=0) return -1;
  464. else if (!f) continue;
  465. } else {
  466. if (argpos>=0) nl_type[argpos]=st, arg=nl_arg[argpos];
  467. else if (f) pop_arg(&arg, st, ap);
  468. else return 0;
  469. }
  470. if (!f) continue;
  471. z = buf + sizeof(buf);
  472. prefix = "-+ 0X0x";
  473. pl = 0;
  474. t = s[-1];
  475. /* Transform ls,lc -> S,C */
  476. if (ps && (t&15)==3) t&=~32;
  477. /* - and 0 flags are mutually exclusive */
  478. if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
  479. switch(t) {
  480. case 'n':
  481. switch(ps) {
  482. case BARE: *(int *)arg.p = cnt; break;
  483. case LPRE: *(long *)arg.p = cnt; break;
  484. case LLPRE: *(long long *)arg.p = cnt; break;
  485. case HPRE: *(unsigned short *)arg.p = cnt; break;
  486. case HHPRE: *(unsigned char *)arg.p = cnt; break;
  487. case ZTPRE: *(size_t *)arg.p = cnt; break;
  488. case JPRE: *(uintmax_t *)arg.p = cnt; break;
  489. }
  490. continue;
  491. case 'p':
  492. p = MAX(p, 2*sizeof(void*));
  493. t = 'x';
  494. fl |= ALT_FORM;
  495. case 'x': case 'X':
  496. a = fmt_x(arg.i, z, t&32);
  497. if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
  498. if (0) {
  499. case 'o':
  500. a = fmt_o(arg.i, z);
  501. if ((fl&ALT_FORM) && arg.i) prefix+=5, pl=1;
  502. } if (0) {
  503. case 'd': case 'i':
  504. pl=1;
  505. if (arg.i>INTMAX_MAX) {
  506. arg.i=-arg.i;
  507. } else if (fl & MARK_POS) {
  508. prefix++;
  509. } else if (fl & PAD_POS) {
  510. prefix+=2;
  511. } else pl=0;
  512. case 'u':
  513. a = fmt_u(arg.i, z);
  514. }
  515. if (!arg.i && !p) {
  516. a=z;
  517. break;
  518. }
  519. if (p>=0) fl &= ~ZERO_PAD;
  520. p = MAX(p, z-a + !arg.i);
  521. break;
  522. case 'c':
  523. *(a=z-(p=1))=arg.i;
  524. fl &= ~ZERO_PAD;
  525. break;
  526. case 'm':
  527. if (1) a = strerror(errno); else
  528. case 's':
  529. a = arg.p;
  530. z = memchr(a, 0, p);
  531. if (!z) z=a+p;
  532. else p=z-a;
  533. fl &= ~ZERO_PAD;
  534. break;
  535. case 'C':
  536. wc[0] = arg.i;
  537. wc[1] = 0;
  538. arg.p = wc;
  539. p = -1;
  540. case 'S':
  541. ws = arg.p;
  542. for (i=0; *ws && (l=wctomb(mb, *ws++))>=0 && l<=0U+p-i; i+=l);
  543. if (l<0) return -1;
  544. p = i;
  545. pad(f, ' ', w, p, fl);
  546. ws = arg.p;
  547. for (i=0; *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
  548. out(f, mb, l);
  549. pad(f, ' ', w, p, fl^LEFT_ADJ);
  550. l = w>p ? w : p;
  551. continue;
  552. case 'e': case 'f': case 'g': case 'a':
  553. case 'E': case 'F': case 'G': case 'A':
  554. l = fmt_fp(f, arg.f, w, p, fl, t);
  555. continue;
  556. }
  557. if (p < z-a) p = z-a;
  558. if (w < pl+p) w = pl+p;
  559. pad(f, ' ', w, pl+p, fl);
  560. out(f, prefix, pl);
  561. pad(f, '0', w, pl+p, fl^ZERO_PAD);
  562. pad(f, '0', p, z-a, 0);
  563. out(f, a, z-a);
  564. pad(f, ' ', w, pl+p, fl^LEFT_ADJ);
  565. l = w;
  566. }
  567. if (f) return cnt;
  568. if (!l10n) return 0;
  569. for (i=1; i<=NL_ARGMAX && nl_type[i]; i++)
  570. pop_arg(nl_arg+i, nl_type[i], ap);
  571. for (; i<=NL_ARGMAX && !nl_type[i]; i++);
  572. if (i<=NL_ARGMAX) return -1;
  573. return 1;
  574. }
  575. int vfprintf(FILE *f, const char *fmt, va_list ap)
  576. {
  577. va_list ap2;
  578. int nl_type[NL_ARGMAX+1] = {0};
  579. union arg nl_arg[NL_ARGMAX+1];
  580. unsigned char internal_buf[80], *saved_buf = 0;
  581. int ret;
  582. va_copy(ap2, ap);
  583. if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) return -1;
  584. FLOCK(f);
  585. if (!f->buf_size) {
  586. saved_buf = f->buf;
  587. f->buf = internal_buf;
  588. f->buf_size = sizeof internal_buf;
  589. }
  590. ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
  591. if (saved_buf) {
  592. f->write(f, 0, 0);
  593. if (!f->wpos) ret = -1;
  594. f->buf = saved_buf;
  595. f->buf_size = 0;
  596. f->wpos = f->wbase = f->wend = 0;
  597. }
  598. FUNLOCK(f);
  599. va_end(ap2);
  600. return ret;
  601. }