vfprintf.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517
  1. /* $OpenBSD: vfprintf.c,v 1.64 2013/11/01 19:05:10 guenther Exp $ */
  2. /*-
  3. * Copyright (c) 1990 The Regents of the University of California.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to Berkeley by
  7. * Chris Torek.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. /*
  34. * Actual printf innards.
  35. *
  36. * This code is large and complicated...
  37. */
  38. #include <sys/types.h>
  39. //#include <sys/mman.h>
  40. #include <errno.h>
  41. //#include <langinfo.h>
  42. #include <limits.h>
  43. #include <stdarg.h>
  44. #include <stddef.h>
  45. #include <stdio.h>
  46. #include <stdint.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <unistd.h>
  50. #include <wchar.h>
  51. #include "local.h"
  52. #include "fvwrite.h"
  53. union arg {
  54. int intarg;
  55. unsigned int uintarg;
  56. long longarg;
  57. unsigned long ulongarg;
  58. long long longlongarg;
  59. unsigned long long ulonglongarg;
  60. ptrdiff_t ptrdiffarg;
  61. size_t sizearg;
  62. ssize_t ssizearg;
  63. intmax_t intmaxarg;
  64. uintmax_t uintmaxarg;
  65. void *pvoidarg;
  66. char *pchararg;
  67. signed char *pschararg;
  68. short *pshortarg;
  69. int *pintarg;
  70. long *plongarg;
  71. long long *plonglongarg;
  72. ptrdiff_t *pptrdiffarg;
  73. ssize_t *pssizearg;
  74. intmax_t *pintmaxarg;
  75. #ifdef FLOATING_POINT
  76. double doublearg;
  77. long double longdoublearg;
  78. #endif
  79. #ifdef PRINTF_WIDE_CHAR
  80. wint_t wintarg;
  81. wchar_t *pwchararg;
  82. #endif
  83. };
  84. static int __find_arguments(const char *fmt0, va_list ap, union arg **argtable,
  85. size_t *argtablesiz);
  86. static int __grow_type_table(unsigned char **typetable, int *tablesize);
  87. /*
  88. * Flush out all the vectors defined by the given uio,
  89. * then reset it so that it can be reused.
  90. */
  91. static int
  92. __sprint(FILE *fp, struct __suio *uio)
  93. {
  94. int err;
  95. if (uio->uio_resid == 0) {
  96. uio->uio_iovcnt = 0;
  97. return (0);
  98. }
  99. err = __sfvwrite(fp, uio);
  100. uio->uio_resid = 0;
  101. uio->uio_iovcnt = 0;
  102. return (err);
  103. }
  104. #ifdef PRINTF_WIDE_CHAR
  105. /*
  106. * Convert a wide character string argument for the %ls format to a multibyte
  107. * string representation. If not -1, prec specifies the maximum number of
  108. * bytes to output, and also means that we can't assume that the wide char
  109. * string is null-terminated.
  110. */
  111. static char *
  112. __wcsconv(wchar_t *wcsarg, int prec)
  113. {
  114. mbstate_t mbs;
  115. char buf[MB_LEN_MAX];
  116. wchar_t *p;
  117. char *convbuf;
  118. size_t clen, nbytes;
  119. /* Allocate space for the maximum number of bytes we could output. */
  120. if (prec < 0) {
  121. memset(&mbs, 0, sizeof(mbs));
  122. p = wcsarg;
  123. nbytes = wcsrtombs(NULL, (const wchar_t **)&p, 0, &mbs);
  124. if (nbytes == (size_t)-1) {
  125. errno = EILSEQ;
  126. return (NULL);
  127. }
  128. } else {
  129. /*
  130. * Optimisation: if the output precision is small enough,
  131. * just allocate enough memory for the maximum instead of
  132. * scanning the string.
  133. */
  134. if (prec < 128)
  135. nbytes = prec;
  136. else {
  137. nbytes = 0;
  138. p = wcsarg;
  139. memset(&mbs, 0, sizeof(mbs));
  140. for (;;) {
  141. clen = wcrtomb(buf, *p++, &mbs);
  142. if (clen == 0 || clen == (size_t)-1 ||
  143. nbytes + clen > (size_t)prec)
  144. break;
  145. nbytes += clen;
  146. }
  147. if (clen == (size_t)-1) {
  148. errno = EILSEQ;
  149. return (NULL);
  150. }
  151. }
  152. }
  153. if ((convbuf = (char *)malloc(nbytes + 1)) == NULL)
  154. return (NULL);
  155. /* Fill the output buffer. */
  156. p = wcsarg;
  157. memset(&mbs, 0, sizeof(mbs));
  158. if ((nbytes = wcsrtombs(convbuf, (const wchar_t **)&p,
  159. nbytes, &mbs)) == (size_t)-1) {
  160. free(convbuf);
  161. errno = EILSEQ;
  162. return (NULL);
  163. }
  164. convbuf[nbytes] = '\0';
  165. return (convbuf);
  166. }
  167. #endif
  168. #ifdef FLOATING_POINT
  169. #include <float.h>
  170. //#include <locale.h>
  171. #include <math.h>
  172. #include "floatio.h"
  173. #define DEFPREC 6
  174. extern char *__dtoa(double, int, int, int *, int *, char **);
  175. extern void __freedtoa(char *);
  176. static int exponent(char *, int, int);
  177. #endif /* FLOATING_POINT */
  178. /*
  179. * The size of the buffer we use as scratch space for integer
  180. * conversions, among other things. Technically, we would need the
  181. * most space for base 10 conversions with thousands' grouping
  182. * characters between each pair of digits. 100 bytes is a
  183. * conservative overestimate even for a 128-bit uintmax_t.
  184. */
  185. #define BUF 100
  186. #define STATIC_ARG_TBL_SIZE 8 /* Size of static argument table. */
  187. /*
  188. * Macros for converting digits to letters and vice versa
  189. */
  190. #define to_digit(c) ((c) - '0')
  191. #define is_digit(c) ((unsigned)to_digit(c) <= 9)
  192. #define to_char(n) ((n) + '0')
  193. /*
  194. * Flags used during conversion.
  195. */
  196. #define ALT 0x0001 /* alternate form */
  197. #define LADJUST 0x0004 /* left adjustment */
  198. #define LONGDBL 0x0008 /* long double */
  199. #define LONGINT 0x0010 /* long integer */
  200. #define LLONGINT 0x0020 /* long long integer */
  201. #define SHORTINT 0x0040 /* short integer */
  202. #define ZEROPAD 0x0080 /* zero (as opposed to blank) pad */
  203. #define FPT 0x0100 /* Floating point number */
  204. #define PTRINT 0x0200 /* (unsigned) ptrdiff_t */
  205. #define SIZEINT 0x0400 /* (signed) size_t */
  206. #define CHARINT 0x0800 /* 8 bit integer */
  207. #define MAXINT 0x1000 /* largest integer size (intmax_t) */
  208. int
  209. __vfprintf(FILE *fp, const char *fmt0, __va_list ap)
  210. {
  211. char *fmt; /* format string */
  212. int ch; /* character from fmt */
  213. int n, n2; /* handy integers (short term usage) */
  214. char *cp; /* handy char pointer (short term usage) */
  215. struct __siov *iovp; /* for PRINT macro */
  216. int flags; /* flags as above */
  217. int ret; /* return value accumulator */
  218. int width; /* width from format (%8d), or 0 */
  219. int prec; /* precision from format; <0 for N/A */
  220. char sign; /* sign prefix (' ', '+', '-', or \0) */
  221. wchar_t wc;
  222. mbstate_t ps;
  223. #ifdef FLOATING_POINT
  224. /*
  225. * We can decompose the printed representation of floating
  226. * point numbers into several parts, some of which may be empty:
  227. *
  228. * [+|-| ] [0x|0X] MMM . NNN [e|E|p|P] [+|-] ZZ
  229. * A B ---C--- D E F
  230. *
  231. * A: 'sign' holds this value if present; '\0' otherwise
  232. * B: ox[1] holds the 'x' or 'X'; '\0' if not hexadecimal
  233. * C: cp points to the string MMMNNN. Leading and trailing
  234. * zeros are not in the string and must be added.
  235. * D: expchar holds this character; '\0' if no exponent, e.g. %f
  236. * F: at least two digits for decimal, at least one digit for hex
  237. */
  238. char *decimal_point = ".";
  239. int signflag; /* true if float is negative */
  240. union { /* floating point arguments %[aAeEfFgG] */
  241. double dbl;
  242. long double ldbl;
  243. } fparg;
  244. int expt; /* integer value of exponent */
  245. char expchar; /* exponent character: [eEpP\0] */
  246. char *dtoaend; /* pointer to end of converted digits */
  247. int expsize; /* character count for expstr */
  248. int lead; /* sig figs before decimal or group sep */
  249. int ndig; /* actual number of digits returned by dtoa */
  250. char expstr[MAXEXPDIG+2]; /* buffer for exponent string: e+ZZZ */
  251. char *dtoaresult = NULL;
  252. #endif
  253. uintmax_t _umax; /* integer arguments %[diouxX] */
  254. enum { OCT, DEC, HEX } base; /* base for %[diouxX] conversion */
  255. int dprec; /* a copy of prec if %[diouxX], 0 otherwise */
  256. int realsz; /* field size expanded by dprec */
  257. int size; /* size of converted field or string */
  258. const char *xdigs; /* digits for %[xX] conversion */
  259. #define NIOV 8
  260. struct __suio uio; /* output information: summary */
  261. struct __siov iov[NIOV];/* ... and individual io vectors */
  262. char buf[BUF]; /* buffer with space for digits of uintmax_t */
  263. char ox[2]; /* space for 0x; ox[1] is either x, X, or \0 */
  264. union arg *argtable; /* args, built due to positional arg */
  265. union arg statargtable[STATIC_ARG_TBL_SIZE];
  266. size_t argtablesiz;
  267. int nextarg; /* 1-based argument index */
  268. va_list orgap; /* original argument pointer */
  269. #ifdef PRINTF_WIDE_CHAR
  270. char *convbuf; /* buffer for wide to multi-byte conversion */
  271. #endif
  272. /*
  273. * Choose PADSIZE to trade efficiency vs. size. If larger printf
  274. * fields occur frequently, increase PADSIZE and make the initialisers
  275. * below longer.
  276. */
  277. #define PADSIZE 16 /* pad chunk size */
  278. static char blanks[PADSIZE] =
  279. {' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  280. static char zeroes[PADSIZE] =
  281. {'0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0'};
  282. static const char xdigs_lower[16] = "0123456789abcdef";
  283. static const char xdigs_upper[16] = "0123456789ABCDEF";
  284. /*
  285. * BEWARE, these `goto error' on error, and PAD uses `n'.
  286. */
  287. #define PRINT(ptr, len) do { \
  288. iovp->iov_base = (ptr); \
  289. iovp->iov_len = (len); \
  290. uio.uio_resid += (len); \
  291. iovp++; \
  292. if (++uio.uio_iovcnt >= NIOV) { \
  293. if (__sprint(fp, &uio)) \
  294. goto error; \
  295. iovp = iov; \
  296. } \
  297. } while (0)
  298. #define PAD(howmany, with) do { \
  299. if ((n = (howmany)) > 0) { \
  300. while (n > PADSIZE) { \
  301. PRINT(with, PADSIZE); \
  302. n -= PADSIZE; \
  303. } \
  304. PRINT(with, n); \
  305. } \
  306. } while (0)
  307. #define PRINTANDPAD(p, ep, len, with) do { \
  308. n2 = (ep) - (p); \
  309. if (n2 > (len)) \
  310. n2 = (len); \
  311. if (n2 > 0) \
  312. PRINT((p), n2); \
  313. PAD((len) - (n2 > 0 ? n2 : 0), (with)); \
  314. } while(0)
  315. #define FLUSH() do { \
  316. if (uio.uio_resid && __sprint(fp, &uio)) \
  317. goto error; \
  318. uio.uio_iovcnt = 0; \
  319. iovp = iov; \
  320. } while (0)
  321. /*
  322. * To extend shorts properly, we need both signed and unsigned
  323. * argument extraction methods.
  324. */
  325. #define SARG() \
  326. ((intmax_t)(flags&MAXINT ? GETARG(intmax_t) : \
  327. flags&LLONGINT ? GETARG(long long) : \
  328. flags&LONGINT ? GETARG(long) : \
  329. flags&PTRINT ? GETARG(ptrdiff_t) : \
  330. flags&SIZEINT ? GETARG(ssize_t) : \
  331. flags&SHORTINT ? (short)GETARG(int) : \
  332. flags&CHARINT ? (signed char)GETARG(int) : \
  333. GETARG(int)))
  334. #define UARG() \
  335. ((uintmax_t)(flags&MAXINT ? GETARG(uintmax_t) : \
  336. flags&LLONGINT ? GETARG(unsigned long long) : \
  337. flags&LONGINT ? GETARG(unsigned long) : \
  338. flags&PTRINT ? (uintptr_t)GETARG(ptrdiff_t) : /* XXX */ \
  339. flags&SIZEINT ? GETARG(size_t) : \
  340. flags&SHORTINT ? (unsigned short)GETARG(int) : \
  341. flags&CHARINT ? (unsigned char)GETARG(int) : \
  342. GETARG(unsigned int)))
  343. /*
  344. * Append a digit to a value and check for overflow.
  345. */
  346. #define APPEND_DIGIT(val, dig) do { \
  347. if ((val) > INT_MAX / 10) \
  348. goto overflow; \
  349. (val) *= 10; \
  350. if ((val) > INT_MAX - to_digit((dig))) \
  351. goto overflow; \
  352. (val) += to_digit((dig)); \
  353. } while (0)
  354. /*
  355. * Get * arguments, including the form *nn$. Preserve the nextarg
  356. * that the argument can be gotten once the type is determined.
  357. */
  358. #define GETASTER(val) \
  359. n2 = 0; \
  360. cp = fmt; \
  361. while (is_digit(*cp)) { \
  362. APPEND_DIGIT(n2, *cp); \
  363. cp++; \
  364. } \
  365. if (*cp == '$') { \
  366. int hold = nextarg; \
  367. if (argtable == NULL) { \
  368. argtable = statargtable; \
  369. __find_arguments(fmt0, orgap, &argtable, &argtablesiz); \
  370. } \
  371. nextarg = n2; \
  372. val = GETARG(int); \
  373. nextarg = hold; \
  374. fmt = ++cp; \
  375. } else { \
  376. val = GETARG(int); \
  377. }
  378. /*
  379. * Get the argument indexed by nextarg. If the argument table is
  380. * built, use it to get the argument. If its not, get the next
  381. * argument (and arguments must be gotten sequentially).
  382. */
  383. #define GETARG(type) \
  384. ((argtable != NULL) ? *((type*)(&argtable[nextarg++])) : \
  385. (nextarg++, va_arg(ap, type)))
  386. _SET_ORIENTATION(fp, -1);
  387. /* sorry, fprintf(read_only_file, "") returns EOF, not 0 */
  388. if (cantwrite(fp)) {
  389. errno = EBADF;
  390. return (EOF);
  391. }
  392. /* optimise fprintf(stderr) (and other unbuffered Unix files) */
  393. if ((fp->_flags & (__SNBF|__SWR|__SRW)) == (__SNBF|__SWR) &&
  394. fp->_file >= 0) {
  395. //return (__sbprintf(fp, fmt0, ap));
  396. errno = EBADF;
  397. return (EOF);
  398. }
  399. fmt = (char *)fmt0;
  400. argtable = NULL;
  401. nextarg = 1;
  402. va_copy(orgap, ap);
  403. uio.uio_iov = iovp = iov;
  404. uio.uio_resid = 0;
  405. uio.uio_iovcnt = 0;
  406. ret = 0;
  407. #ifdef PRINTF_WIDE_CHAR
  408. convbuf = NULL;
  409. #endif
  410. memset(&ps, 0, sizeof(ps));
  411. /*
  412. * Scan the format for conversions (`%' character).
  413. */
  414. for (;;) {
  415. cp = fmt;
  416. while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
  417. fmt += n;
  418. if (wc == '%') {
  419. fmt--;
  420. break;
  421. }
  422. }
  423. if (fmt != cp) {
  424. ptrdiff_t m = fmt - cp;
  425. if (m < 0 || m > INT_MAX - ret)
  426. goto overflow;
  427. PRINT(cp, m);
  428. ret += m;
  429. }
  430. if (n <= 0)
  431. goto done;
  432. fmt++; /* skip over '%' */
  433. flags = 0;
  434. dprec = 0;
  435. width = 0;
  436. prec = -1;
  437. sign = '\0';
  438. ox[1] = '\0';
  439. rflag: ch = *fmt++;
  440. reswitch: switch (ch) {
  441. case ' ':
  442. /*
  443. * ``If the space and + flags both appear, the space
  444. * flag will be ignored.''
  445. * -- ANSI X3J11
  446. */
  447. if (!sign)
  448. sign = ' ';
  449. goto rflag;
  450. case '#':
  451. flags |= ALT;
  452. goto rflag;
  453. case '\'':
  454. /* grouping not implemented */
  455. goto rflag;
  456. case '*':
  457. /*
  458. * ``A negative field width argument is taken as a
  459. * - flag followed by a positive field width.''
  460. * -- ANSI X3J11
  461. * They don't exclude field widths read from args.
  462. */
  463. GETASTER(width);
  464. if (width >= 0)
  465. goto rflag;
  466. if (width == INT_MIN)
  467. goto overflow;
  468. width = -width;
  469. /* FALLTHROUGH */
  470. case '-':
  471. flags |= LADJUST;
  472. goto rflag;
  473. case '+':
  474. sign = '+';
  475. goto rflag;
  476. case '.':
  477. if ((ch = *fmt++) == '*') {
  478. GETASTER(n);
  479. prec = n < 0 ? -1 : n;
  480. goto rflag;
  481. }
  482. n = 0;
  483. while (is_digit(ch)) {
  484. APPEND_DIGIT(n, ch);
  485. ch = *fmt++;
  486. }
  487. if (ch == '$') {
  488. nextarg = n;
  489. if (argtable == NULL) {
  490. argtable = statargtable;
  491. __find_arguments(fmt0, orgap,
  492. &argtable, &argtablesiz);
  493. }
  494. goto rflag;
  495. }
  496. prec = n;
  497. goto reswitch;
  498. case '0':
  499. /*
  500. * ``Note that 0 is taken as a flag, not as the
  501. * beginning of a field width.''
  502. * -- ANSI X3J11
  503. */
  504. flags |= ZEROPAD;
  505. goto rflag;
  506. case '1': case '2': case '3': case '4':
  507. case '5': case '6': case '7': case '8': case '9':
  508. n = 0;
  509. do {
  510. APPEND_DIGIT(n, ch);
  511. ch = *fmt++;
  512. } while (is_digit(ch));
  513. if (ch == '$') {
  514. nextarg = n;
  515. if (argtable == NULL) {
  516. argtable = statargtable;
  517. __find_arguments(fmt0, orgap,
  518. &argtable, &argtablesiz);
  519. }
  520. goto rflag;
  521. }
  522. width = n;
  523. goto reswitch;
  524. #ifdef FLOATING_POINT
  525. case 'L':
  526. flags |= LONGDBL;
  527. goto rflag;
  528. #endif
  529. case 'h':
  530. if (*fmt == 'h') {
  531. fmt++;
  532. flags |= CHARINT;
  533. } else {
  534. flags |= SHORTINT;
  535. }
  536. goto rflag;
  537. case 'j':
  538. flags |= MAXINT;
  539. goto rflag;
  540. case 'l':
  541. if (*fmt == 'l') {
  542. fmt++;
  543. flags |= LLONGINT;
  544. } else {
  545. flags |= LONGINT;
  546. }
  547. goto rflag;
  548. case 'q':
  549. flags |= LLONGINT;
  550. goto rflag;
  551. case 't':
  552. flags |= PTRINT;
  553. goto rflag;
  554. case 'z':
  555. flags |= SIZEINT;
  556. goto rflag;
  557. case 'c':
  558. #ifdef PRINTF_WIDE_CHAR
  559. if (flags & LONGINT) {
  560. mbstate_t mbs;
  561. size_t mbseqlen;
  562. memset(&mbs, 0, sizeof(mbs));
  563. mbseqlen = wcrtomb(buf,
  564. (wchar_t)GETARG(wint_t), &mbs);
  565. if (mbseqlen == (size_t)-1) {
  566. fp->_flags |= __SERR;
  567. errno = EILSEQ;
  568. goto error;
  569. }
  570. cp = buf;
  571. size = (int)mbseqlen;
  572. } else {
  573. #endif
  574. *(cp = buf) = GETARG(int);
  575. size = 1;
  576. #ifdef PRINTF_WIDE_CHAR
  577. }
  578. #endif
  579. sign = '\0';
  580. break;
  581. case 'D':
  582. flags |= LONGINT;
  583. /*FALLTHROUGH*/
  584. case 'd':
  585. case 'i':
  586. _umax = SARG();
  587. if ((intmax_t)_umax < 0) {
  588. _umax = -_umax;
  589. sign = '-';
  590. }
  591. base = DEC;
  592. goto number;
  593. #ifdef FLOATING_POINT
  594. case 'a':
  595. case 'A':
  596. if (ch == 'a') {
  597. ox[1] = 'x';
  598. xdigs = xdigs_lower;
  599. expchar = 'p';
  600. } else {
  601. ox[1] = 'X';
  602. xdigs = xdigs_upper;
  603. expchar = 'P';
  604. }
  605. if (prec >= 0)
  606. prec++;
  607. if (dtoaresult)
  608. __freedtoa(dtoaresult);
  609. if (flags & LONGDBL) {
  610. fparg.ldbl = GETARG(long double);
  611. dtoaresult = cp =
  612. __hldtoa(fparg.ldbl, xdigs, prec,
  613. &expt, &signflag, &dtoaend);
  614. if (dtoaresult == NULL) {
  615. errno = ENOMEM;
  616. goto error;
  617. }
  618. } else {
  619. fparg.dbl = GETARG(double);
  620. dtoaresult = cp =
  621. __hdtoa(fparg.dbl, xdigs, prec,
  622. &expt, &signflag, &dtoaend);
  623. if (dtoaresult == NULL) {
  624. errno = ENOMEM;
  625. goto error;
  626. }
  627. }
  628. if (prec < 0)
  629. prec = dtoaend - cp;
  630. if (expt == INT_MAX)
  631. ox[1] = '\0';
  632. goto fp_common;
  633. case 'e':
  634. case 'E':
  635. expchar = ch;
  636. if (prec < 0) /* account for digit before decpt */
  637. prec = DEFPREC + 1;
  638. else
  639. prec++;
  640. goto fp_begin;
  641. case 'f':
  642. case 'F':
  643. expchar = '\0';
  644. goto fp_begin;
  645. case 'g':
  646. case 'G':
  647. expchar = ch - ('g' - 'e');
  648. if (prec == 0)
  649. prec = 1;
  650. fp_begin:
  651. if (prec < 0)
  652. prec = DEFPREC;
  653. if (dtoaresult)
  654. __freedtoa(dtoaresult);
  655. if (flags & LONGDBL) {
  656. fparg.ldbl = GETARG(long double);
  657. dtoaresult = cp =
  658. __ldtoa(&fparg.ldbl, expchar ? 2 : 3, prec,
  659. &expt, &signflag, &dtoaend);
  660. if (dtoaresult == NULL) {
  661. errno = ENOMEM;
  662. goto error;
  663. }
  664. } else {
  665. fparg.dbl = GETARG(double);
  666. dtoaresult = cp =
  667. __dtoa(fparg.dbl, expchar ? 2 : 3, prec,
  668. &expt, &signflag, &dtoaend);
  669. if (dtoaresult == NULL) {
  670. errno = ENOMEM;
  671. goto error;
  672. }
  673. if (expt == 9999)
  674. expt = INT_MAX;
  675. }
  676. fp_common:
  677. if (signflag)
  678. sign = '-';
  679. if (expt == INT_MAX) { /* inf or nan */
  680. if (*cp == 'N') {
  681. cp = (ch >= 'a') ? "nan" : "NAN";
  682. sign = '\0';
  683. } else
  684. cp = (ch >= 'a') ? "inf" : "INF";
  685. size = 3;
  686. flags &= ~ZEROPAD;
  687. break;
  688. }
  689. flags |= FPT;
  690. ndig = dtoaend - cp;
  691. if (ch == 'g' || ch == 'G') {
  692. if (expt > -4 && expt <= prec) {
  693. /* Make %[gG] smell like %[fF] */
  694. expchar = '\0';
  695. if (flags & ALT)
  696. prec -= expt;
  697. else
  698. prec = ndig - expt;
  699. if (prec < 0)
  700. prec = 0;
  701. } else {
  702. /*
  703. * Make %[gG] smell like %[eE], but
  704. * trim trailing zeroes if no # flag.
  705. */
  706. if (!(flags & ALT))
  707. prec = ndig;
  708. }
  709. }
  710. if (expchar) {
  711. expsize = exponent(expstr, expt - 1, expchar);
  712. size = expsize + prec;
  713. if (prec > 1 || flags & ALT)
  714. ++size;
  715. } else {
  716. /* space for digits before decimal point */
  717. if (expt > 0)
  718. size = expt;
  719. else /* "0" */
  720. size = 1;
  721. /* space for decimal pt and following digits */
  722. if (prec || flags & ALT)
  723. size += prec + 1;
  724. lead = expt;
  725. }
  726. break;
  727. #endif /* FLOATING_POINT */
  728. case 'n':
  729. abort();
  730. #if 0
  731. if (flags & LLONGINT)
  732. *GETARG(long long *) = ret;
  733. else if (flags & LONGINT)
  734. *GETARG(long *) = ret;
  735. else if (flags & SHORTINT)
  736. *GETARG(short *) = ret;
  737. else if (flags & CHARINT)
  738. *GETARG(signed char *) = ret;
  739. else if (flags & PTRINT)
  740. *GETARG(ptrdiff_t *) = ret;
  741. else if (flags & SIZEINT)
  742. *GETARG(ssize_t *) = ret;
  743. else if (flags & MAXINT)
  744. *GETARG(intmax_t *) = ret;
  745. else
  746. *GETARG(int *) = ret;
  747. continue; /* no output */
  748. #endif
  749. case 'O':
  750. flags |= LONGINT;
  751. /*FALLTHROUGH*/
  752. case 'o':
  753. _umax = UARG();
  754. base = OCT;
  755. goto nosign;
  756. case 'p':
  757. /*
  758. * ``The argument shall be a pointer to void. The
  759. * value of the pointer is converted to a sequence
  760. * of printable characters, in an implementation-
  761. * defined manner.''
  762. * -- ANSI X3J11
  763. */
  764. /* NOSTRICT */
  765. _umax = (size_t)GETARG(void *);
  766. base = HEX;
  767. xdigs = xdigs_lower;
  768. ox[1] = 'x';
  769. goto nosign;
  770. case 's':
  771. #ifdef PRINTF_WIDE_CHAR
  772. if (flags & LONGINT) {
  773. wchar_t *wcp;
  774. if (convbuf != NULL) {
  775. free(convbuf);
  776. convbuf = NULL;
  777. }
  778. if ((wcp = GETARG(wchar_t *)) == NULL) {
  779. cp = "(null)";
  780. } else {
  781. convbuf = __wcsconv(wcp, prec);
  782. if (convbuf == NULL) {
  783. fp->_flags = __SERR;
  784. goto error;
  785. }
  786. cp = convbuf;
  787. }
  788. } else
  789. #endif /* PRINTF_WIDE_CHAR */
  790. if ((cp = GETARG(char *)) == NULL)
  791. cp = "(null)";
  792. if (prec >= 0) {
  793. /*
  794. * can't use strlen; can only look for the
  795. * NUL in the first `prec' characters, and
  796. * strlen() will go further.
  797. */
  798. char *p = (char *)memchr(cp, 0, prec);
  799. size = p ? (p - cp) : prec;
  800. } else {
  801. size_t len;
  802. if ((len = strlen(cp)) > INT_MAX)
  803. goto overflow;
  804. size = (int)len;
  805. }
  806. sign = '\0';
  807. break;
  808. case 'U':
  809. flags |= LONGINT;
  810. /*FALLTHROUGH*/
  811. case 'u':
  812. _umax = UARG();
  813. base = DEC;
  814. goto nosign;
  815. case 'X':
  816. xdigs = xdigs_upper;
  817. goto hex;
  818. case 'x':
  819. xdigs = xdigs_lower;
  820. hex: _umax = UARG();
  821. base = HEX;
  822. /* leading 0x/X only if non-zero */
  823. if (flags & ALT && _umax != 0)
  824. ox[1] = ch;
  825. /* unsigned conversions */
  826. nosign: sign = '\0';
  827. /*
  828. * ``... diouXx conversions ... if a precision is
  829. * specified, the 0 flag will be ignored.''
  830. * -- ANSI X3J11
  831. */
  832. number: if ((dprec = prec) >= 0)
  833. flags &= ~ZEROPAD;
  834. /*
  835. * ``The result of converting a zero value with an
  836. * explicit precision of zero is no characters.''
  837. * -- ANSI X3J11
  838. */
  839. cp = buf + BUF;
  840. if (_umax != 0 || prec != 0) {
  841. /*
  842. * Unsigned mod is hard, and unsigned mod
  843. * by a constant is easier than that by
  844. * a variable; hence this switch.
  845. */
  846. switch (base) {
  847. case OCT:
  848. do {
  849. *--cp = to_char(_umax & 7);
  850. _umax >>= 3;
  851. } while (_umax);
  852. /* handle octal leading 0 */
  853. if (flags & ALT && *cp != '0')
  854. *--cp = '0';
  855. break;
  856. case DEC:
  857. /* many numbers are 1 digit */
  858. while (_umax >= 10) {
  859. *--cp = to_char(_umax % 10);
  860. _umax /= 10;
  861. }
  862. *--cp = to_char(_umax);
  863. break;
  864. case HEX:
  865. do {
  866. *--cp = xdigs[_umax & 15];
  867. _umax >>= 4;
  868. } while (_umax);
  869. break;
  870. default:
  871. cp = "bug in vfprintf: bad base";
  872. size = strlen(cp);
  873. goto skipsize;
  874. }
  875. }
  876. size = buf + BUF - cp;
  877. if (size > BUF) /* should never happen */
  878. abort();
  879. skipsize:
  880. break;
  881. default: /* "%?" prints ?, unless ? is NUL */
  882. if (ch == '\0')
  883. goto done;
  884. /* pretend it was %c with argument ch */
  885. cp = buf;
  886. *cp = ch;
  887. size = 1;
  888. sign = '\0';
  889. break;
  890. }
  891. /*
  892. * All reasonable formats wind up here. At this point, `cp'
  893. * points to a string which (if not flags&LADJUST) should be
  894. * padded out to `width' places. If flags&ZEROPAD, it should
  895. * first be prefixed by any sign or other prefix; otherwise,
  896. * it should be blank padded before the prefix is emitted.
  897. * After any left-hand padding and prefixing, emit zeroes
  898. * required by a decimal %[diouxX] precision, then print the
  899. * string proper, then emit zeroes required by any leftover
  900. * floating precision; finally, if LADJUST, pad with blanks.
  901. *
  902. * Compute actual size, so we know how much to pad.
  903. * size excludes decimal prec; realsz includes it.
  904. */
  905. realsz = dprec > size ? dprec : size;
  906. if (sign)
  907. realsz++;
  908. if (ox[1])
  909. realsz+= 2;
  910. /* right-adjusting blank padding */
  911. if ((flags & (LADJUST|ZEROPAD)) == 0)
  912. PAD(width - realsz, blanks);
  913. /* prefix */
  914. if (sign)
  915. PRINT(&sign, 1);
  916. if (ox[1]) { /* ox[1] is either x, X, or \0 */
  917. ox[0] = '0';
  918. PRINT(ox, 2);
  919. }
  920. /* right-adjusting zero padding */
  921. if ((flags & (LADJUST|ZEROPAD)) == ZEROPAD)
  922. PAD(width - realsz, zeroes);
  923. /* leading zeroes from decimal precision */
  924. PAD(dprec - size, zeroes);
  925. /* the string or number proper */
  926. #ifdef FLOATING_POINT
  927. if ((flags & FPT) == 0) {
  928. PRINT(cp, size);
  929. } else { /* glue together f_p fragments */
  930. if (!expchar) { /* %[fF] or sufficiently short %[gG] */
  931. if (expt <= 0) {
  932. PRINT(zeroes, 1);
  933. if (prec || flags & ALT)
  934. PRINT(decimal_point, 1);
  935. PAD(-expt, zeroes);
  936. /* already handled initial 0's */
  937. prec += expt;
  938. } else {
  939. PRINTANDPAD(cp, dtoaend, lead, zeroes);
  940. cp += lead;
  941. if (prec || flags & ALT)
  942. PRINT(decimal_point, 1);
  943. }
  944. PRINTANDPAD(cp, dtoaend, prec, zeroes);
  945. } else { /* %[eE] or sufficiently long %[gG] */
  946. if (prec > 1 || flags & ALT) {
  947. buf[0] = *cp++;
  948. buf[1] = *decimal_point;
  949. PRINT(buf, 2);
  950. PRINT(cp, ndig-1);
  951. PAD(prec - ndig, zeroes);
  952. } else { /* XeYYY */
  953. PRINT(cp, 1);
  954. }
  955. PRINT(expstr, expsize);
  956. }
  957. }
  958. #else
  959. PRINT(cp, size);
  960. #endif
  961. /* left-adjusting padding (always blank) */
  962. if (flags & LADJUST)
  963. PAD(width - realsz, blanks);
  964. /* finally, adjust ret */
  965. if (width < realsz)
  966. width = realsz;
  967. if (width > INT_MAX - ret)
  968. goto overflow;
  969. ret += width;
  970. FLUSH(); /* copy out the I/O vectors */
  971. }
  972. done:
  973. FLUSH();
  974. error:
  975. va_end(orgap);
  976. if (__sferror(fp))
  977. ret = -1;
  978. goto finish;
  979. overflow:
  980. errno = ENOMEM;
  981. ret = -1;
  982. finish:
  983. #ifdef PRINTF_WIDE_CHAR
  984. if (convbuf)
  985. free(convbuf);
  986. #endif
  987. #ifdef FLOATING_POINT
  988. if (dtoaresult)
  989. __freedtoa(dtoaresult);
  990. #endif
  991. if (argtable != NULL && argtable != statargtable) {
  992. free(argtable);
  993. argtable = NULL;
  994. }
  995. return (ret);
  996. }
  997. /*
  998. * Type ids for argument type table.
  999. */
  1000. #define T_UNUSED 0
  1001. #define T_SHORT 1
  1002. #define T_U_SHORT 2
  1003. #define TP_SHORT 3
  1004. #define T_INT 4
  1005. #define T_U_INT 5
  1006. #define TP_INT 6
  1007. #define T_LONG 7
  1008. #define T_U_LONG 8
  1009. #define TP_LONG 9
  1010. #define T_LLONG 10
  1011. #define T_U_LLONG 11
  1012. #define TP_LLONG 12
  1013. #define T_DOUBLE 13
  1014. #define T_LONG_DOUBLE 14
  1015. #define TP_CHAR 15
  1016. #define TP_VOID 16
  1017. #define T_PTRINT 17
  1018. #define TP_PTRINT 18
  1019. #define T_SIZEINT 19
  1020. #define T_SSIZEINT 20
  1021. #define TP_SSIZEINT 21
  1022. #define T_MAXINT 22
  1023. #define T_MAXUINT 23
  1024. #define TP_MAXINT 24
  1025. #define T_CHAR 25
  1026. #define T_U_CHAR 26
  1027. #define T_WINT 27
  1028. #define TP_WCHAR 28
  1029. /*
  1030. * Find all arguments when a positional parameter is encountered. Returns a
  1031. * table, indexed by argument number, of pointers to each arguments. The
  1032. * initial argument table should be an array of STATIC_ARG_TBL_SIZE entries.
  1033. * It will be replaced with a mmap-ed one if it overflows (malloc cannot be
  1034. * used since we are attempting to make snprintf thread safe, and alloca is
  1035. * problematic since we have nested functions..)
  1036. */
  1037. static int
  1038. __find_arguments(const char *fmt0, va_list ap, union arg **argtable,
  1039. size_t *argtablesiz)
  1040. {
  1041. char *fmt; /* format string */
  1042. int ch; /* character from fmt */
  1043. int n, n2; /* handy integer (short term usage) */
  1044. char *cp; /* handy char pointer (short term usage) */
  1045. int flags; /* flags as above */
  1046. unsigned char *typetable; /* table of types */
  1047. unsigned char stattypetable[STATIC_ARG_TBL_SIZE];
  1048. int tablesize; /* current size of type table */
  1049. int tablemax; /* largest used index in table */
  1050. int nextarg; /* 1-based argument index */
  1051. int ret = 0; /* return value */
  1052. int rc = 0; /* save return code from __grow_type_table */
  1053. wchar_t wc;
  1054. mbstate_t ps;
  1055. #define check_rc(rc) if (rc == -1) goto overflow
  1056. /*
  1057. * Add an argument type to the table, expanding if necessary.
  1058. */
  1059. #define ADDTYPE(type) \
  1060. ((nextarg >= tablesize) ? \
  1061. rc = __grow_type_table(&typetable, &tablesize) : 0, \
  1062. (nextarg > tablemax) ? tablemax = nextarg : 0, \
  1063. rc == -1 ? 0 : (typetable[nextarg++] = type))
  1064. #define ADDTYPE_CHECK(type) \
  1065. ADDTYPE(type); check_rc(rc)
  1066. #define ADDSARG() \
  1067. ((flags&MAXINT) ? ADDTYPE(T_MAXINT) : \
  1068. ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
  1069. ((flags&SIZEINT) ? ADDTYPE(T_SSIZEINT) : \
  1070. ((flags&LLONGINT) ? ADDTYPE(T_LLONG) : \
  1071. ((flags&LONGINT) ? ADDTYPE(T_LONG) : \
  1072. ((flags&SHORTINT) ? ADDTYPE(T_SHORT) : \
  1073. ((flags&CHARINT) ? ADDTYPE(T_CHAR) : ADDTYPE(T_INT)))))))); \
  1074. check_rc(rc);
  1075. #define ADDUARG() \
  1076. ((flags&MAXINT) ? ADDTYPE(T_MAXUINT) : \
  1077. ((flags&PTRINT) ? ADDTYPE(T_PTRINT) : \
  1078. ((flags&SIZEINT) ? ADDTYPE(T_SIZEINT) : \
  1079. ((flags&LLONGINT) ? ADDTYPE(T_U_LLONG) : \
  1080. ((flags&LONGINT) ? ADDTYPE(T_U_LONG) : \
  1081. ((flags&SHORTINT) ? ADDTYPE(T_U_SHORT) : \
  1082. ((flags&CHARINT) ? ADDTYPE(T_U_CHAR) : ADDTYPE(T_U_INT)))))))); \
  1083. check_rc(rc);
  1084. /*
  1085. * Add * arguments to the type array.
  1086. */
  1087. #define ADDASTER() \
  1088. n2 = 0; \
  1089. cp = fmt; \
  1090. while (is_digit(*cp)) { \
  1091. APPEND_DIGIT(n2, *cp); \
  1092. cp++; \
  1093. } \
  1094. if (*cp == '$') { \
  1095. int hold = nextarg; \
  1096. nextarg = n2; \
  1097. ADDTYPE_CHECK(T_INT); \
  1098. nextarg = hold; \
  1099. fmt = ++cp; \
  1100. } else { \
  1101. ADDTYPE_CHECK(T_INT); \
  1102. }
  1103. fmt = (char *)fmt0;
  1104. typetable = stattypetable;
  1105. tablesize = STATIC_ARG_TBL_SIZE;
  1106. tablemax = 0;
  1107. nextarg = 1;
  1108. memset(typetable, T_UNUSED, STATIC_ARG_TBL_SIZE);
  1109. memset(&ps, 0, sizeof(ps));
  1110. /*
  1111. * Scan the format for conversions (`%' character).
  1112. */
  1113. for (;;) {
  1114. cp = fmt;
  1115. while ((n = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps)) > 0) {
  1116. fmt += n;
  1117. if (wc == '%') {
  1118. fmt--;
  1119. break;
  1120. }
  1121. }
  1122. if (n <= 0)
  1123. goto done;
  1124. fmt++; /* skip over '%' */
  1125. flags = 0;
  1126. rflag: ch = *fmt++;
  1127. reswitch: switch (ch) {
  1128. case ' ':
  1129. case '#':
  1130. case '\'':
  1131. goto rflag;
  1132. case '*':
  1133. ADDASTER();
  1134. goto rflag;
  1135. case '-':
  1136. case '+':
  1137. goto rflag;
  1138. case '.':
  1139. if ((ch = *fmt++) == '*') {
  1140. ADDASTER();
  1141. goto rflag;
  1142. }
  1143. while (is_digit(ch)) {
  1144. ch = *fmt++;
  1145. }
  1146. goto reswitch;
  1147. case '0':
  1148. goto rflag;
  1149. case '1': case '2': case '3': case '4':
  1150. case '5': case '6': case '7': case '8': case '9':
  1151. n = 0;
  1152. do {
  1153. APPEND_DIGIT(n ,ch);
  1154. ch = *fmt++;
  1155. } while (is_digit(ch));
  1156. if (ch == '$') {
  1157. nextarg = n;
  1158. goto rflag;
  1159. }
  1160. goto reswitch;
  1161. #ifdef FLOATING_POINT
  1162. case 'L':
  1163. flags |= LONGDBL;
  1164. goto rflag;
  1165. #endif
  1166. case 'h':
  1167. if (*fmt == 'h') {
  1168. fmt++;
  1169. flags |= CHARINT;
  1170. } else {
  1171. flags |= SHORTINT;
  1172. }
  1173. goto rflag;
  1174. case 'j':
  1175. flags |= MAXINT;
  1176. goto rflag;
  1177. case 'l':
  1178. if (*fmt == 'l') {
  1179. fmt++;
  1180. flags |= LLONGINT;
  1181. } else {
  1182. flags |= LONGINT;
  1183. }
  1184. goto rflag;
  1185. case 'q':
  1186. flags |= LLONGINT;
  1187. goto rflag;
  1188. case 't':
  1189. flags |= PTRINT;
  1190. goto rflag;
  1191. case 'z':
  1192. flags |= SIZEINT;
  1193. goto rflag;
  1194. case 'c':
  1195. #ifdef PRINTF_WIDE_CHAR
  1196. if (flags & LONGINT)
  1197. ADDTYPE_CHECK(T_WINT);
  1198. else
  1199. #endif
  1200. ADDTYPE_CHECK(T_INT);
  1201. break;
  1202. case 'D':
  1203. flags |= LONGINT;
  1204. /*FALLTHROUGH*/
  1205. case 'd':
  1206. case 'i':
  1207. ADDSARG();
  1208. break;
  1209. #ifdef FLOATING_POINT
  1210. case 'a':
  1211. case 'A':
  1212. case 'e':
  1213. case 'E':
  1214. case 'f':
  1215. case 'F':
  1216. case 'g':
  1217. case 'G':
  1218. if (flags & LONGDBL)
  1219. ADDTYPE_CHECK(T_LONG_DOUBLE);
  1220. else
  1221. ADDTYPE_CHECK(T_DOUBLE);
  1222. break;
  1223. #endif /* FLOATING_POINT */
  1224. case 'n':
  1225. if (flags & LLONGINT)
  1226. ADDTYPE_CHECK(TP_LLONG);
  1227. else if (flags & LONGINT)
  1228. ADDTYPE_CHECK(TP_LONG);
  1229. else if (flags & SHORTINT)
  1230. ADDTYPE_CHECK(TP_SHORT);
  1231. else if (flags & PTRINT)
  1232. ADDTYPE_CHECK(TP_PTRINT);
  1233. else if (flags & SIZEINT)
  1234. ADDTYPE_CHECK(TP_SSIZEINT);
  1235. else if (flags & MAXINT)
  1236. ADDTYPE_CHECK(TP_MAXINT);
  1237. else
  1238. ADDTYPE_CHECK(TP_INT);
  1239. continue; /* no output */
  1240. case 'O':
  1241. flags |= LONGINT;
  1242. /*FALLTHROUGH*/
  1243. case 'o':
  1244. ADDUARG();
  1245. break;
  1246. case 'p':
  1247. ADDTYPE_CHECK(TP_VOID);
  1248. break;
  1249. case 's':
  1250. #ifdef PRINTF_WIDE_CHAR
  1251. if (flags & LONGINT)
  1252. ADDTYPE_CHECK(TP_WCHAR);
  1253. else
  1254. #endif
  1255. ADDTYPE_CHECK(TP_CHAR);
  1256. break;
  1257. case 'U':
  1258. flags |= LONGINT;
  1259. /*FALLTHROUGH*/
  1260. case 'u':
  1261. case 'X':
  1262. case 'x':
  1263. ADDUARG();
  1264. break;
  1265. default: /* "%?" prints ?, unless ? is NUL */
  1266. if (ch == '\0')
  1267. goto done;
  1268. break;
  1269. }
  1270. }
  1271. done:
  1272. /*
  1273. * Build the argument table.
  1274. */
  1275. if (tablemax >= STATIC_ARG_TBL_SIZE) {
  1276. *argtablesiz = sizeof(union arg) * (tablemax + 1);
  1277. *argtable = (union arg *)malloc(*argtablesiz);
  1278. if (*argtable == NULL)
  1279. goto overflow; //return (-1);
  1280. }
  1281. #if 0
  1282. /* XXX is this required? */
  1283. (*argtable)[0].intarg = 0;
  1284. #endif
  1285. for (n = 1; n <= tablemax; n++) {
  1286. switch (typetable[n]) {
  1287. case T_UNUSED:
  1288. case T_CHAR:
  1289. case T_U_CHAR:
  1290. case T_SHORT:
  1291. case T_U_SHORT:
  1292. case T_INT:
  1293. (*argtable)[n].intarg = va_arg(ap, int);
  1294. break;
  1295. case TP_SHORT:
  1296. (*argtable)[n].pshortarg = va_arg(ap, short *);
  1297. break;
  1298. case T_U_INT:
  1299. (*argtable)[n].uintarg = va_arg(ap, unsigned int);
  1300. break;
  1301. case TP_INT:
  1302. (*argtable)[n].pintarg = va_arg(ap, int *);
  1303. break;
  1304. case T_LONG:
  1305. (*argtable)[n].longarg = va_arg(ap, long);
  1306. break;
  1307. case T_U_LONG:
  1308. (*argtable)[n].ulongarg = va_arg(ap, unsigned long);
  1309. break;
  1310. case TP_LONG:
  1311. (*argtable)[n].plongarg = va_arg(ap, long *);
  1312. break;
  1313. case T_LLONG:
  1314. (*argtable)[n].longlongarg = va_arg(ap, long long);
  1315. break;
  1316. case T_U_LLONG:
  1317. (*argtable)[n].ulonglongarg = va_arg(ap, unsigned long long);
  1318. break;
  1319. case TP_LLONG:
  1320. (*argtable)[n].plonglongarg = va_arg(ap, long long *);
  1321. break;
  1322. #ifdef FLOATING_POINT
  1323. case T_DOUBLE:
  1324. (*argtable)[n].doublearg = va_arg(ap, double);
  1325. break;
  1326. case T_LONG_DOUBLE:
  1327. (*argtable)[n].longdoublearg = va_arg(ap, long double);
  1328. break;
  1329. #endif
  1330. case TP_CHAR:
  1331. (*argtable)[n].pchararg = va_arg(ap, char *);
  1332. break;
  1333. case TP_VOID:
  1334. (*argtable)[n].pvoidarg = va_arg(ap, void *);
  1335. break;
  1336. case T_PTRINT:
  1337. (*argtable)[n].ptrdiffarg = va_arg(ap, ptrdiff_t);
  1338. break;
  1339. case TP_PTRINT:
  1340. (*argtable)[n].pptrdiffarg = va_arg(ap, ptrdiff_t *);
  1341. break;
  1342. case T_SIZEINT:
  1343. (*argtable)[n].sizearg = va_arg(ap, size_t);
  1344. break;
  1345. case T_SSIZEINT:
  1346. (*argtable)[n].ssizearg = va_arg(ap, ssize_t);
  1347. break;
  1348. case TP_SSIZEINT:
  1349. (*argtable)[n].pssizearg = va_arg(ap, ssize_t *);
  1350. break;
  1351. case T_MAXINT:
  1352. (*argtable)[n].intmaxarg = va_arg(ap, intmax_t);
  1353. break;
  1354. case T_MAXUINT:
  1355. (*argtable)[n].uintmaxarg = va_arg(ap, uintmax_t);
  1356. break;
  1357. case TP_MAXINT:
  1358. (*argtable)[n].pintmaxarg = va_arg(ap, intmax_t *);
  1359. break;
  1360. #ifdef PRINTF_WIDE_CHAR
  1361. case T_WINT:
  1362. (*argtable)[n].wintarg = va_arg(ap, wint_t);
  1363. break;
  1364. case TP_WCHAR:
  1365. (*argtable)[n].pwchararg = va_arg(ap, wchar_t *);
  1366. break;
  1367. #endif
  1368. }
  1369. }
  1370. goto finish;
  1371. overflow:
  1372. errno = ENOMEM;
  1373. ret = -1;
  1374. finish:
  1375. if (typetable != NULL && typetable != stattypetable) {
  1376. free(typetable);
  1377. typetable = NULL;
  1378. }
  1379. return (ret);
  1380. }
  1381. /*
  1382. * Increase the size of the type table.
  1383. */
  1384. static int
  1385. __grow_type_table(unsigned char **typetable, int *tablesize)
  1386. {
  1387. unsigned char *oldtable = *typetable;
  1388. int newsize = *tablesize * 2;
  1389. if (newsize < SE_PAGE_SIZE)
  1390. newsize = SE_PAGE_SIZE;
  1391. if (*tablesize == STATIC_ARG_TBL_SIZE) {
  1392. *typetable = (unsigned char *)malloc(newsize);
  1393. if (*typetable == NULL)
  1394. return (-1);
  1395. memmove(*typetable, oldtable, *tablesize);
  1396. } else {
  1397. unsigned char *new1 = (unsigned char *)malloc(newsize);
  1398. if (new1 == NULL)
  1399. return (-1);
  1400. memmove(new1, *typetable, *tablesize);
  1401. free(*typetable);
  1402. *typetable = new1;
  1403. }
  1404. memset(*typetable + *tablesize, T_UNUSED, (newsize - *tablesize));
  1405. *tablesize = newsize;
  1406. return (0);
  1407. }
  1408. #ifdef FLOATING_POINT
  1409. static int
  1410. exponent(char *p0, int exp, int fmtch)
  1411. {
  1412. char *p, *t;
  1413. char expbuf[MAXEXPDIG];
  1414. p = p0;
  1415. *p++ = fmtch;
  1416. if (exp < 0) {
  1417. exp = -exp;
  1418. *p++ = '-';
  1419. } else
  1420. *p++ = '+';
  1421. t = expbuf + MAXEXPDIG;
  1422. if (exp > 9) {
  1423. do {
  1424. *--t = to_char(exp % 10);
  1425. } while ((exp /= 10) > 9);
  1426. *--t = to_char(exp);
  1427. for (; t < expbuf + MAXEXPDIG; *p++ = *t++)
  1428. /* nothing */;
  1429. } else {
  1430. /*
  1431. * Exponents for decimal floating point conversions
  1432. * (%[eEgG]) must be at least two characters long,
  1433. * whereas exponents for hexadecimal conversions can
  1434. * be only one character long.
  1435. */
  1436. if (fmtch == 'e' || fmtch == 'E')
  1437. *p++ = '0';
  1438. *p++ = to_char(exp);
  1439. }
  1440. return (p - p0);
  1441. }
  1442. #endif /* FLOATING_POINT */