vfprintf.c 16 KB

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