vfprintf.c 16 KB

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