vfprintf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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>=10; y/=10) *--s = '0' + y%10;
  152. if (y) *--s = '0' + y;
  153. return s;
  154. }
  155. /* Do not override this check. The floating point printing code below
  156. * depends on the float.h constants being right. If they are wrong, it
  157. * may overflow the stack. */
  158. #if LDBL_MANT_DIG == 53
  159. typedef char compiler_defines_long_double_incorrectly[9-(int)sizeof(long double)];
  160. #endif
  161. static int fmt_fp(FILE *f, long double y, int w, int p, int fl, int t)
  162. {
  163. uint32_t big[(LDBL_MANT_DIG+28)/29 + 1 // mantissa expansion
  164. + (LDBL_MAX_EXP+LDBL_MANT_DIG+28+8)/9]; // exponent expansion
  165. uint32_t *a, *d, *r, *z;
  166. int e2=0, e, i, j, l;
  167. char buf[9+LDBL_MANT_DIG/4], *s;
  168. const char *prefix="-0X+0X 0X-0x+0x 0x";
  169. int pl;
  170. char ebuf0[3*sizeof(int)], *ebuf=&ebuf0[3*sizeof(int)], *estr;
  171. pl=1;
  172. if (signbit(y)) {
  173. y=-y;
  174. } else if (fl & MARK_POS) {
  175. prefix+=3;
  176. } else if (fl & PAD_POS) {
  177. prefix+=6;
  178. } else prefix++, pl=0;
  179. if (!isfinite(y)) {
  180. char *s = (t&32)?"inf":"INF";
  181. if (y!=y) s=(t&32)?"nan":"NAN";
  182. pad(f, ' ', w, 3+pl, fl&~ZERO_PAD);
  183. out(f, prefix, pl);
  184. out(f, s, 3);
  185. pad(f, ' ', w, 3+pl, fl^LEFT_ADJ);
  186. return MAX(w, 3+pl);
  187. }
  188. y = frexpl(y, &e2) * 2;
  189. if (y) e2--;
  190. if ((t|32)=='a') {
  191. if (t&32) prefix += 9;
  192. pl += 2;
  193. if (p>=0 && p<(LDBL_MANT_DIG-1+3)/4) {
  194. double round = scalbn(1, LDBL_MANT_DIG-1-(p*4));
  195. if (*prefix=='-') {
  196. y=-y;
  197. y-=round;
  198. y+=round;
  199. y=-y;
  200. } else {
  201. y+=round;
  202. y-=round;
  203. }
  204. }
  205. estr=fmt_u(e2<0 ? -e2 : e2, ebuf);
  206. if (estr==ebuf) *--estr='0';
  207. *--estr = (e2<0 ? '-' : '+');
  208. *--estr = t+('p'-'a');
  209. s=buf;
  210. do {
  211. int x=y;
  212. *s++=xdigits[x]|(t&32);
  213. y=16*(y-x);
  214. if (s-buf==1 && (y||p>0||(fl&ALT_FORM))) *s++='.';
  215. } while (y);
  216. if (p > INT_MAX-2-(ebuf-estr)-pl)
  217. return -1;
  218. if (p && s-buf-2 < p)
  219. l = (p+2) + (ebuf-estr);
  220. else
  221. l = (s-buf) + (ebuf-estr);
  222. pad(f, ' ', w, pl+l, fl);
  223. out(f, prefix, pl);
  224. pad(f, '0', w, pl+l, fl^ZERO_PAD);
  225. out(f, buf, s-buf);
  226. pad(f, '0', l-(ebuf-estr)-(s-buf), 0, 0);
  227. out(f, estr, ebuf-estr);
  228. pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
  229. return MAX(w, pl+l);
  230. }
  231. if (p<0) p=6;
  232. if (y) y *= 0x1p28, e2-=28;
  233. if (e2<0) a=r=z=big;
  234. else a=r=z=big+sizeof(big)/sizeof(*big) - LDBL_MANT_DIG - 1;
  235. do {
  236. *z = y;
  237. y = 1000000000*(y-*z++);
  238. } while (y);
  239. while (e2>0) {
  240. uint32_t carry=0;
  241. int sh=MIN(29,e2);
  242. for (d=z-1; d>=a; d--) {
  243. uint64_t x = ((uint64_t)*d<<sh)+carry;
  244. *d = x % 1000000000;
  245. carry = x / 1000000000;
  246. }
  247. if (carry) *--a = carry;
  248. while (z>a && !z[-1]) z--;
  249. e2-=sh;
  250. }
  251. while (e2<0) {
  252. uint32_t carry=0, *b;
  253. int sh=MIN(9,-e2), need=1+(p+LDBL_MANT_DIG/3U+8)/9;
  254. for (d=a; d<z; d++) {
  255. uint32_t rm = *d & (1<<sh)-1;
  256. *d = (*d>>sh) + carry;
  257. carry = (1000000000>>sh) * rm;
  258. }
  259. if (!*a) a++;
  260. if (carry) *z++ = carry;
  261. /* Avoid (slow!) computation past requested precision */
  262. b = (t|32)=='f' ? r : a;
  263. if (z-b > need) z = b+need;
  264. e2+=sh;
  265. }
  266. if (a<z) for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
  267. else e=0;
  268. /* Perform rounding: j is precision after the radix (possibly neg) */
  269. j = p - ((t|32)!='f')*e - ((t|32)=='g' && p);
  270. if (j < 9*(z-r-1)) {
  271. uint32_t x;
  272. /* We avoid C's broken division of negative numbers */
  273. d = r + 1 + ((j+9*LDBL_MAX_EXP)/9 - LDBL_MAX_EXP);
  274. j += 9*LDBL_MAX_EXP;
  275. j %= 9;
  276. for (i=10, j++; j<9; i*=10, j++);
  277. x = *d % i;
  278. /* Are there any significant digits past j? */
  279. if (x || d+1!=z) {
  280. long double round = 2/LDBL_EPSILON;
  281. long double small;
  282. if ((*d/i & 1) || (i==1000000000 && d>a && (d[-1]&1)))
  283. round += 2;
  284. if (x<i/2) small=0x0.8p0;
  285. else if (x==i/2 && d+1==z) small=0x1.0p0;
  286. else small=0x1.8p0;
  287. if (pl && *prefix=='-') round*=-1, small*=-1;
  288. *d -= x;
  289. /* Decide whether to round by probing round+small */
  290. if (round+small != round) {
  291. *d = *d + i;
  292. while (*d > 999999999) {
  293. *d--=0;
  294. if (d<a) *--a=0;
  295. (*d)++;
  296. }
  297. for (i=10, e=9*(r-a); *a>=i; i*=10, e++);
  298. }
  299. }
  300. if (z>d+1) z=d+1;
  301. }
  302. for (; z>a && !z[-1]; z--);
  303. if ((t|32)=='g') {
  304. if (!p) p++;
  305. if (p>e && e>=-4) {
  306. t--;
  307. p-=e+1;
  308. } else {
  309. t-=2;
  310. p--;
  311. }
  312. if (!(fl&ALT_FORM)) {
  313. /* Count trailing zeros in last place */
  314. if (z>a && z[-1]) for (i=10, j=0; z[-1]%i==0; i*=10, j++);
  315. else j=9;
  316. if ((t|32)=='f')
  317. p = MIN(p,MAX(0,9*(z-r-1)-j));
  318. else
  319. p = MIN(p,MAX(0,9*(z-r-1)+e-j));
  320. }
  321. }
  322. if (p > INT_MAX-1-(p || (fl&ALT_FORM)))
  323. return -1;
  324. l = 1 + p + (p || (fl&ALT_FORM));
  325. if ((t|32)=='f') {
  326. if (e > INT_MAX-l) return -1;
  327. if (e>0) l+=e;
  328. } else {
  329. estr=fmt_u(e<0 ? -e : e, ebuf);
  330. while(ebuf-estr<2) *--estr='0';
  331. *--estr = (e<0 ? '-' : '+');
  332. *--estr = t;
  333. if (ebuf-estr > INT_MAX-l) return -1;
  334. l += ebuf-estr;
  335. }
  336. if (l > INT_MAX-pl) return -1;
  337. pad(f, ' ', w, pl+l, fl);
  338. out(f, prefix, pl);
  339. pad(f, '0', w, pl+l, fl^ZERO_PAD);
  340. if ((t|32)=='f') {
  341. if (a>r) a=r;
  342. for (d=a; d<=r; d++) {
  343. char *s = fmt_u(*d, buf+9);
  344. if (d!=a) while (s>buf) *--s='0';
  345. else if (s==buf+9) *--s='0';
  346. out(f, s, buf+9-s);
  347. }
  348. if (p || (fl&ALT_FORM)) out(f, ".", 1);
  349. for (; d<z && p>0; d++, p-=9) {
  350. char *s = fmt_u(*d, buf+9);
  351. while (s>buf) *--s='0';
  352. out(f, s, MIN(9,p));
  353. }
  354. pad(f, '0', p+9, 9, 0);
  355. } else {
  356. if (z<=a) z=a+1;
  357. for (d=a; d<z && p>=0; d++) {
  358. char *s = fmt_u(*d, buf+9);
  359. if (s==buf+9) *--s='0';
  360. if (d!=a) while (s>buf) *--s='0';
  361. else {
  362. out(f, s++, 1);
  363. if (p>0||(fl&ALT_FORM)) out(f, ".", 1);
  364. }
  365. out(f, s, MIN(buf+9-s, p));
  366. p -= buf+9-s;
  367. }
  368. pad(f, '0', p+18, 18, 0);
  369. out(f, estr, ebuf-estr);
  370. }
  371. pad(f, ' ', w, pl+l, fl^LEFT_ADJ);
  372. return MAX(w, pl+l);
  373. }
  374. static int getint(char **s) {
  375. int i;
  376. for (i=0; isdigit(**s); (*s)++) {
  377. if (i > INT_MAX/10U || **s-'0' > INT_MAX-10*i) i = -1;
  378. else i = 10*i + (**s-'0');
  379. }
  380. return i;
  381. }
  382. static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, int *nl_type)
  383. {
  384. char *a, *z, *s=(char *)fmt;
  385. unsigned l10n=0, fl;
  386. int w, p, xp;
  387. union arg arg;
  388. int argpos;
  389. unsigned st, ps;
  390. int cnt=0, l=0;
  391. size_t i;
  392. char buf[sizeof(uintmax_t)*3];
  393. const char *prefix;
  394. int t, pl;
  395. wchar_t wc[2], *ws;
  396. char mb[4];
  397. for (;;) {
  398. /* This error is only specified for snprintf, but since it's
  399. * unspecified for other forms, do the same. Stop immediately
  400. * on overflow; otherwise %n could produce wrong results. */
  401. if (l > INT_MAX - cnt) goto overflow;
  402. /* Update output count, end loop when fmt is exhausted */
  403. cnt += l;
  404. if (!*s) break;
  405. /* Handle literal text and %% format specifiers */
  406. for (a=s; *s && *s!='%'; s++);
  407. for (z=s; s[0]=='%' && s[1]=='%'; z++, s+=2);
  408. if (z-a > INT_MAX-cnt) goto overflow;
  409. l = z-a;
  410. if (f) out(f, a, l);
  411. if (l) continue;
  412. if (isdigit(s[1]) && s[2]=='$') {
  413. l10n=1;
  414. argpos = s[1]-'0';
  415. s+=3;
  416. } else {
  417. argpos = -1;
  418. s++;
  419. }
  420. /* Read modifier flags */
  421. for (fl=0; (unsigned)*s-' '<32 && (FLAGMASK&(1U<<*s-' ')); s++)
  422. fl |= 1U<<*s-' ';
  423. /* Read field width */
  424. if (*s=='*') {
  425. if (isdigit(s[1]) && s[2]=='$') {
  426. l10n=1;
  427. if (!f) nl_type[s[1]-'0'] = INT, w = 0;
  428. else w = nl_arg[s[1]-'0'].i;
  429. s+=3;
  430. } else if (!l10n) {
  431. w = f ? va_arg(*ap, int) : 0;
  432. s++;
  433. } else goto inval;
  434. if (w<0) fl|=LEFT_ADJ, w=-w;
  435. } else if ((w=getint(&s))<0) goto overflow;
  436. /* Read precision */
  437. if (*s=='.' && s[1]=='*') {
  438. if (isdigit(s[2]) && s[3]=='$') {
  439. if (!f) nl_type[s[2]-'0'] = INT, p = 0;
  440. else p = nl_arg[s[2]-'0'].i;
  441. s+=4;
  442. } else if (!l10n) {
  443. p = f ? va_arg(*ap, int) : 0;
  444. s+=2;
  445. } else goto inval;
  446. xp = (p>=0);
  447. } else if (*s=='.') {
  448. s++;
  449. p = getint(&s);
  450. xp = 1;
  451. } else {
  452. p = -1;
  453. xp = 0;
  454. }
  455. /* Format specifier state machine */
  456. st=0;
  457. do {
  458. if (OOB(*s)) goto inval;
  459. ps=st;
  460. st=states[st]S(*s++);
  461. } while (st-1<STOP);
  462. if (!st) goto inval;
  463. /* Check validity of argument type (nl/normal) */
  464. if (st==NOARG) {
  465. if (argpos>=0) goto inval;
  466. } else {
  467. if (argpos>=0) {
  468. if (!f) nl_type[argpos]=st;
  469. else arg=nl_arg[argpos];
  470. } else if (f) pop_arg(&arg, st, ap);
  471. else return 0;
  472. }
  473. if (!f) continue;
  474. /* Do not process any new directives once in error state. */
  475. if (ferror(f)) return -1;
  476. z = buf + sizeof(buf);
  477. prefix = "-+ 0X0x";
  478. pl = 0;
  479. t = s[-1];
  480. /* Transform ls,lc -> S,C */
  481. if (ps && (t&15)==3) t&=~32;
  482. /* - and 0 flags are mutually exclusive */
  483. if (fl & LEFT_ADJ) fl &= ~ZERO_PAD;
  484. switch(t) {
  485. case 'n':
  486. switch(ps) {
  487. case BARE: *(int *)arg.p = cnt; break;
  488. case LPRE: *(long *)arg.p = cnt; break;
  489. case LLPRE: *(long long *)arg.p = cnt; break;
  490. case HPRE: *(unsigned short *)arg.p = cnt; break;
  491. case HHPRE: *(unsigned char *)arg.p = cnt; break;
  492. case ZTPRE: *(size_t *)arg.p = cnt; break;
  493. case JPRE: *(uintmax_t *)arg.p = cnt; break;
  494. }
  495. continue;
  496. case 'p':
  497. p = MAX(p, 2*sizeof(void*));
  498. t = 'x';
  499. fl |= ALT_FORM;
  500. case 'x': case 'X':
  501. a = fmt_x(arg.i, z, t&32);
  502. if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2;
  503. if (0) {
  504. case 'o':
  505. a = fmt_o(arg.i, z);
  506. if ((fl&ALT_FORM) && p<z-a+1) p=z-a+1;
  507. } if (0) {
  508. case 'd': case 'i':
  509. pl=1;
  510. if (arg.i>INTMAX_MAX) {
  511. arg.i=-arg.i;
  512. } else if (fl & MARK_POS) {
  513. prefix++;
  514. } else if (fl & PAD_POS) {
  515. prefix+=2;
  516. } else pl=0;
  517. case 'u':
  518. a = fmt_u(arg.i, z);
  519. }
  520. if (xp && p<0) goto overflow;
  521. if (xp) fl &= ~ZERO_PAD;
  522. if (!arg.i && !p) {
  523. a=z;
  524. break;
  525. }
  526. p = MAX(p, z-a + !arg.i);
  527. break;
  528. narrow_c:
  529. case 'c':
  530. *(a=z-(p=1))=arg.i;
  531. fl &= ~ZERO_PAD;
  532. break;
  533. case 'm':
  534. if (1) a = strerror(errno); else
  535. case 's':
  536. a = arg.p ? arg.p : "(null)";
  537. z = a + strnlen(a, p<0 ? INT_MAX : p);
  538. if (p<0 && *z) goto overflow;
  539. p = z-a;
  540. fl &= ~ZERO_PAD;
  541. break;
  542. case 'C':
  543. if (!arg.i) goto narrow_c;
  544. wc[0] = arg.i;
  545. wc[1] = 0;
  546. arg.p = wc;
  547. p = -1;
  548. case 'S':
  549. ws = arg.p;
  550. for (i=l=0; i<p && *ws && (l=wctomb(mb, *ws++))>=0 && l<=p-i; i+=l);
  551. if (l<0) return -1;
  552. if (i > INT_MAX) goto overflow;
  553. p = i;
  554. pad(f, ' ', w, p, fl);
  555. ws = arg.p;
  556. for (i=0; i<0U+p && *ws && i+(l=wctomb(mb, *ws++))<=p; i+=l)
  557. out(f, mb, l);
  558. pad(f, ' ', w, p, fl^LEFT_ADJ);
  559. l = w>p ? w : p;
  560. continue;
  561. case 'e': case 'f': case 'g': case 'a':
  562. case 'E': case 'F': case 'G': case 'A':
  563. if (xp && p<0) goto overflow;
  564. l = fmt_fp(f, arg.f, w, p, fl, t);
  565. if (l<0) goto overflow;
  566. continue;
  567. }
  568. if (p < z-a) p = z-a;
  569. if (p > INT_MAX-pl) goto overflow;
  570. if (w < pl+p) w = pl+p;
  571. if (w > INT_MAX-cnt) goto overflow;
  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) goto inval;
  586. return 1;
  587. inval:
  588. errno = EINVAL;
  589. return -1;
  590. overflow:
  591. errno = EOVERFLOW;
  592. return -1;
  593. }
  594. int vfprintf(FILE *restrict f, const char *restrict fmt, va_list ap)
  595. {
  596. va_list ap2;
  597. int nl_type[NL_ARGMAX+1] = {0};
  598. union arg nl_arg[NL_ARGMAX+1];
  599. unsigned char internal_buf[80], *saved_buf = 0;
  600. int olderr;
  601. int ret;
  602. /* the copy allows passing va_list* even if va_list is an array */
  603. va_copy(ap2, ap);
  604. if (printf_core(0, fmt, &ap2, nl_arg, nl_type) < 0) {
  605. va_end(ap2);
  606. return -1;
  607. }
  608. FLOCK(f);
  609. olderr = f->flags & F_ERR;
  610. f->flags &= ~F_ERR;
  611. if (!f->buf_size) {
  612. saved_buf = f->buf;
  613. f->buf = internal_buf;
  614. f->buf_size = sizeof internal_buf;
  615. f->wpos = f->wbase = f->wend = 0;
  616. }
  617. if (!f->wend && __towrite(f)) ret = -1;
  618. else ret = printf_core(f, fmt, &ap2, nl_arg, nl_type);
  619. if (saved_buf) {
  620. f->write(f, 0, 0);
  621. if (!f->wpos) ret = -1;
  622. f->buf = saved_buf;
  623. f->buf_size = 0;
  624. f->wpos = f->wbase = f->wend = 0;
  625. }
  626. if (ferror(f)) ret = -1;
  627. f->flags |= olderr;
  628. FUNLOCK(f);
  629. va_end(ap2);
  630. return ret;
  631. }