printf.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * This file based on printf.c from 'Dlibs' on the atari ST (RdeBath)
  3. *
  4. *
  5. * Dale Schumacher 399 Beacon Ave.
  6. * (alias: Dalnefre') St. Paul, MN 55104
  7. * dal@syntel.UUCP United States of America
  8. * "It's not reality that's important, but how you perceive things."
  9. */
  10. /* Altered to use stdarg, made the core function vfnprintf.
  11. * Hooked into the stdio package using 'inside information'
  12. * Altered sizeof() assumptions, now assumes all integers except chars
  13. * will be either
  14. * sizeof(xxx) == sizeof(long) or sizeof(xxx) == sizeof(short)
  15. *
  16. * -RDB
  17. */
  18. /*
  19. * Manuel Novoa III Dec 2000
  20. *
  21. * The previous vfnprintf routine was almost completely rewritten with the
  22. * goal of fixing some shortcomings and reducing object size.
  23. *
  24. * The summary of changes:
  25. *
  26. * Converted print conversion specification parsing from one big switch
  27. * to a method using string tables. This new method verifies that the
  28. * conversion flags, field width, precision, qualifier, and specifier
  29. * appear in the correct order. Many questionable specifications were
  30. * accepted by the previous code. This new method also resulted in a
  31. * substantial reduction in object size of about 330 bytes (20%) from
  32. * the old version (1627 bytes) on i386, even with the following
  33. * improvements.
  34. *
  35. * Implemented %n specifier as required by the standards.
  36. * Implemented proper handling of precision for int types.
  37. * Implemented # for hex and pointer, fixed error for octal rep of 0.
  38. * Implemented return of -1 on stream error.
  39. *
  40. * Added optional support for the GNU extension %m which prints the string
  41. * corresponding the errno.
  42. *
  43. * Added optional support for long long ints and unsigned long long ints
  44. * using the conversion qualifiers "ll", "L", or "q" (like glibc).
  45. *
  46. * Added optional support for doubles in a very limited form. None of
  47. * the formating options are obeyed. The string returned by __dtostr
  48. * is printed directly.
  49. *
  50. * Converted to use my (un)signed long (long) to string routines, which are
  51. * smaller than the previous functions and don't require static buffers.
  52. *
  53. * Other Modifications:
  54. * Modified sprintf, snprintf, vsprintf, vsnprintf to share on fake-file.
  55. */
  56. /*
  57. * Manuel Novoa III Jan 2000
  58. *
  59. * Removed fake file from *s*printf functions because of possible problems
  60. * if called recursively. Instead, have sprintf, snprintf, and vsprintf
  61. * call vsnprintf which allocates a fake file on the stack.
  62. * Removed WANT_FPUTC option. Always use standard putc macro to avoid
  63. * problems with the fake file used by the *s*printf functions.
  64. * Added asprintf.
  65. * Fixed 0-pad prefixing bug.
  66. * Converted sizeof(int) == sizeof(long) tests to compile time vs run time.
  67. * This saves 112 bytes of code on i386.
  68. */
  69. /*****************************************************************************/
  70. /* OPTIONS */
  71. /*****************************************************************************/
  72. /* The optional support for long longs and doubles comes in two forms.
  73. *
  74. * 1) Normal (or partial for doubles) output support. Set to 1 to turn on.
  75. * Adds about 70 bytes for doubles, about 220 bytes for long longs,
  76. * and about 275 for both to the base code size of 1163 on i386.
  77. */
  78. #define WANT_LONG_LONG 0
  79. #define WANT_DOUBLE 0
  80. /* 2) An error message is inserted into the stream, an arg of the
  81. * appropriate size is removed from the arglist, and processing
  82. * continues. This is adds less code and may be useful in some
  83. * cases. Set to 1 to turn on. Adds about 50 bytes for doubles,
  84. * about 140 bytes for long longs, and about 175 bytes for both
  85. * to the base code size of 1163 on i386.
  86. */
  87. #define WANT_LONG_LONG_ERROR 0
  88. #define WANT_DOUBLE_ERROR 0
  89. /*
  90. * Set to support GNU extension of %m to print string corresponding to errno.
  91. *
  92. * Warning: This adds about 50 bytes (i386) to the code but it also pulls in
  93. * strerror and the corresponding string table which together are about 3.8k.
  94. */
  95. #define WANT_GNU_ERRNO 0
  96. /**************************************************************************/
  97. #include <sys/types.h>
  98. #include <fcntl.h>
  99. #include <string.h>
  100. #include <stdlib.h>
  101. #include <limits.h>
  102. #if WANT_GNU_ERRNO
  103. #include <errno.h>
  104. #endif
  105. #ifdef __STDC__
  106. #include <stdarg.h>
  107. #define va_strt va_start
  108. #else
  109. #include <varargs.h>
  110. #define va_strt(p,i) va_start(p)
  111. #endif
  112. #include "stdio.h"
  113. extern int vfnprintf(FILE * op, size_t max_size,
  114. register __const char *fmt, register va_list ap);
  115. #ifdef L_printf
  116. int printf(const char *fmt, ...)
  117. {
  118. va_list ptr;
  119. int rv;
  120. va_strt(ptr, fmt);
  121. rv = vfnprintf(stdout, -1, fmt, ptr);
  122. va_end(ptr);
  123. return rv;
  124. }
  125. #endif
  126. #ifdef L_asprintf
  127. int asprintf(char **app, const char *fmt, ...)
  128. {
  129. va_list ptr;
  130. int rv, i;
  131. char *p; /* unitialized warning is ok here */
  132. /*
  133. * First iteration - find out size of buffer required and allocate it.
  134. * Second iteration - actually produce output.
  135. */
  136. rv = 0;
  137. for (i=0 ; i<2 ; i++) {
  138. va_strt(ptr, fmt);
  139. rv = vsnprintf(p, rv, fmt, ptr);
  140. va_end(ptr);
  141. if (i==0) { /* first time through so */
  142. p = malloc(++rv); /* allocate the buffer */
  143. *app = p;
  144. if (!p) {
  145. return -1;
  146. }
  147. }
  148. }
  149. return rv;
  150. }
  151. #endif
  152. #ifdef L_sprintf
  153. int sprintf(char *sp, const char *fmt, ...)
  154. {
  155. va_list ptr;
  156. int rv;
  157. va_strt(ptr, fmt);
  158. rv = vsnprintf(sp, -1, fmt, ptr);
  159. va_end(ptr);
  160. return rv;
  161. }
  162. #endif
  163. #ifdef L_snprintf
  164. int snprintf(char *sp, size_t size, const char *fmt, ...)
  165. {
  166. va_list ptr;
  167. int rv;
  168. va_strt(ptr, fmt);
  169. rv = vsnprintf(sp, size, fmt, ptr);
  170. va_end(ptr);
  171. return rv;
  172. }
  173. #endif
  174. #ifdef L_fprintf
  175. int fprintf(FILE * fp, const char *fmt, ...)
  176. {
  177. va_list ptr;
  178. int rv;
  179. va_strt(ptr, fmt);
  180. rv = vfnprintf(fp, -1, fmt, ptr);
  181. va_end(ptr);
  182. return rv;
  183. }
  184. #endif
  185. #ifdef L_vprintf
  186. int vprintf(const char *fmt, va_list ap)
  187. {
  188. return vfprintf(stdout, fmt, ap);
  189. }
  190. #endif
  191. #ifdef L_vfprintf
  192. int vfprintf(FILE * op, register __const char *fmt, register va_list ap)
  193. {
  194. return (vfnprintf(op, -1, fmt, ap));
  195. }
  196. #endif
  197. #ifdef L_vsprintf
  198. int vsprintf(char *sp, __const char *fmt, va_list ap)
  199. {
  200. return vsnprintf(sp, -1, fmt, ap);
  201. }
  202. #endif
  203. #ifdef L_vsnprintf
  204. int vsnprintf(char *sp, size_t size, __const char *fmt, va_list ap)
  205. {
  206. int rv;
  207. #if 0
  208. FILE f = {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1,
  209. _IOFBF | __MODE_WRITE};
  210. #else
  211. /* As we're only using the putc macro in vfnprintf, we don't need to
  212. initialize all FILE fields. */
  213. FILE f;
  214. f.bufwrite = (char *) (unsigned) -1;
  215. f.bufpos = sp;
  216. f.mode = _IOFBF | __MODE_WRITE;
  217. #endif
  218. rv = vfnprintf(&f, size, fmt, ap);
  219. if (size) {
  220. *(f.bufpos) = 0;
  221. }
  222. return rv;
  223. }
  224. #endif
  225. #ifdef L_vfnprintf
  226. extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase);
  227. extern char *__ltostr(char *buf, long val, int base, int uppercase);
  228. extern char *__ulltostr(char *buf, unsigned long long uval, int base, int uppercase);
  229. extern char *__lltostr(char *buf, long long val, int base, int uppercase);
  230. extern char *__dtostr(char *buf, double x);
  231. enum {
  232. FLAG_PLUS = 0,
  233. FLAG_MINUS_LJUSTIFY,
  234. FLAG_HASH,
  235. FLAG_0_PAD,
  236. FLAG_SPACE,
  237. };
  238. /* layout 01234 */
  239. static const char spec[] = "+-#0 ";
  240. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  241. static const char qual[] = "hlLq";
  242. #else
  243. static const char qual[] = "hl";
  244. #endif
  245. #if !WANT_LONG_LONG && WANT_LONG_LONG_ERROR
  246. static const char ll_err[] = "<LONG-LONG>";
  247. #endif
  248. #if !WANT_DOUBLE && WANT_DOUBLE_ERROR
  249. static const char dbl_err[] = "<DOUBLE>";
  250. #endif
  251. #if WANT_DOUBLE || WANT_DOUBLE_ERROR
  252. /* layout 012345678901234567 */
  253. static const char u_spec[] = "%nbopxXudicsfgGeEaA";
  254. #else
  255. /* layout 0123456789012 */
  256. static const char u_spec[] = "%nbopxXudics0";
  257. #endif
  258. /* WARNING: u_spec and u_radix need to stay in agreement!!! */
  259. /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */
  260. static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
  261. int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap)
  262. {
  263. int i, cnt = 0, lval;
  264. char *p;
  265. const char *fmt0;
  266. int buffer_mode;
  267. int preci, width;
  268. #define upcase i
  269. int radix, dpoint /*, upcase*/;
  270. #if WANT_LONG_LONG
  271. char tmp[65];
  272. #else
  273. char tmp[33];
  274. #endif
  275. char flag[sizeof(spec)];
  276. /* This speeds things up a bit for unbuffered */
  277. buffer_mode = (op->mode & __MODE_BUF);
  278. op->mode &= (~__MODE_BUF);
  279. while (*fmt) {
  280. if (*fmt == '%') {
  281. fmt0 = fmt; /* save our position in case of bad format */
  282. ++fmt;
  283. if (buffer_mode == _IONBF) {
  284. fflush(op);
  285. }
  286. width = -1; /* min field width */
  287. preci = -5; /* max string width or mininum digits */
  288. radix = 10; /* number base */
  289. dpoint = 0; /* found decimal point */
  290. #if INT_MAX != LONG_MAX
  291. lval = 0; /* sizeof(int) != sizeof(long) */
  292. #else
  293. lval = 1; /* sizeof(int) == sizeof(long) */
  294. #endif
  295. tmp[1] = 0; /* set things up for %c -- better done here */
  296. /* init flags */
  297. for (p =(char *) spec ; *p ; p++) {
  298. flag[p-spec] = '\0';
  299. }
  300. flag[FLAG_0_PAD] = ' ';
  301. /* process optional flags */
  302. for (p = (char *)spec ; *p ; ) {
  303. if (*fmt == *p) {
  304. flag[p-spec] = *fmt++;
  305. p = (char *)spec; /* restart scan */
  306. } else {
  307. p++;
  308. }
  309. }
  310. if (!flag[FLAG_PLUS]) {
  311. flag[FLAG_PLUS] = flag[FLAG_SPACE];
  312. }
  313. /* process optional width and precision */
  314. do {
  315. if (*fmt == '.') {
  316. ++fmt;
  317. dpoint = 1;
  318. }
  319. if (*fmt == '*') { /* parameter width value */
  320. ++fmt;
  321. i = va_arg(ap, int);
  322. } else {
  323. for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
  324. i = (i * 10) + (*fmt - '0');
  325. }
  326. }
  327. if (dpoint) {
  328. preci = i;
  329. if (i<0) {
  330. preci = 0;
  331. }
  332. } else {
  333. width = i;
  334. if (i<0) {
  335. width = -i;
  336. flag[FLAG_MINUS_LJUSTIFY] = 1;
  337. }
  338. }
  339. } while ((*fmt == '.') && !dpoint );
  340. /* process optional qualifier */
  341. for (p = (char *) qual ; *p ; p++) {
  342. if (*p == *fmt) {
  343. lval = p - qual;
  344. ++fmt;
  345. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  346. if ((*p == 'l') && (*fmt == *p)) {
  347. ++lval;
  348. ++fmt;
  349. }
  350. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  351. }
  352. }
  353. #if WANT_GNU_ERRNO
  354. if (*fmt == 'm') {
  355. flag[FLAG_PLUS] = '\0';
  356. flag[FLAG_0_PAD] = ' ';
  357. p = strerror(errno);
  358. goto print;
  359. }
  360. #endif
  361. /* process format specifier */
  362. for (p = (char *) u_spec ; *p ; p++) {
  363. if (*fmt != *p) continue;
  364. if (p-u_spec < 1) { /* print a % */
  365. goto charout;
  366. }
  367. if (p-u_spec < 2) { /* store output count in int ptr */
  368. *(va_arg(ap, int *)) = cnt;
  369. goto nextfmt;
  370. }
  371. if (p-u_spec < 8) { /* unsigned conversion */
  372. radix = u_radix[p-u_spec-2];
  373. upcase = ((int)'x') - *p;
  374. if (*p == 'p') {
  375. lval = (sizeof(char *) == sizeof(long));
  376. upcase = 0;
  377. }
  378. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  379. if (lval >= 2) {
  380. #if WANT_LONG_LONG
  381. p = __ulltostr(tmp + sizeof(tmp) - 1,
  382. va_arg(ap, unsigned long long),
  383. radix, upcase);
  384. #else
  385. (void) va_arg(ap, unsigned long long); /* cary on */
  386. p = (char *) ll_err;
  387. #endif /* WANT_LONG_LONG */
  388. } else {
  389. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  390. #if UINT_MAX != ULONG_MAX
  391. /* sizeof(unsigned int) != sizeof(unsigned long) */
  392. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  393. ((lval)
  394. ? va_arg(ap, unsigned long)
  395. : va_arg(ap, unsigned int)),
  396. radix, upcase);
  397. #else
  398. /* sizeof(unsigned int) == sizeof(unsigned long) */
  399. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  400. va_arg(ap, unsigned long),
  401. radix, upcase);
  402. #endif
  403. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  404. }
  405. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  406. flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
  407. if (flag[FLAG_HASH] && (*p != '0')) { /* non-zero */
  408. if (radix == 8) {
  409. *--p = '0'; /* add leadding zero */
  410. } else { /* either 2 or 16 */
  411. flag[FLAG_PLUS] = '0';
  412. *--p = 'b';
  413. if (radix == 16) {
  414. *p = 'x';
  415. if (*fmt == 'X') {
  416. *p = 'X';
  417. }
  418. }
  419. }
  420. }
  421. } else if (p-u_spec < 10) { /* signed conversion */
  422. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  423. if (lval >= 2) {
  424. #if WANT_LONG_LONG
  425. p = __lltostr(tmp + sizeof(tmp) - 1,
  426. va_arg(ap, long long), 10, 0);
  427. #else
  428. (void) va_arg(ap, long long); /* carry on */
  429. p = (char *) ll_err;
  430. #endif /* WANT_LONG_LONG */
  431. } else {
  432. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  433. #if INT_MAX != LONG_MAX
  434. /* sizeof(int) != sizeof(long) */
  435. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  436. ((lval)
  437. ? va_arg(ap, long)
  438. : va_arg(ap, int)), 10, 0);
  439. #else
  440. /* sizeof(int) == sizeof(long) */
  441. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  442. va_arg(ap, long), 10, 0);
  443. #endif
  444. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  445. }
  446. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  447. } else if (p-u_spec < 12) { /* character or string */
  448. flag[FLAG_PLUS] = '\0';
  449. flag[FLAG_0_PAD] = ' ';
  450. if (*p == 'c') { /* character */
  451. p = tmp;
  452. *p = va_arg(ap, int);
  453. } else { /* string */
  454. p = va_arg(ap, char *);
  455. }
  456. #if WANT_DOUBLE || WANT_DOUBLE_ERROR
  457. } else if (p-u_spec < 27) { /* floating point */
  458. #endif /* WANT_DOUBLE || WANT_DOUBLE_ERROR */
  459. #if WANT_DOUBLE
  460. p = __dtostr(tmp + sizeof(tmp) - 1, va_arg(ap, double));
  461. #elif WANT_DOUBLE_ERROR
  462. (void) va_arg(ap,double); /* carry on */
  463. p = (char *) dbl_err;
  464. #endif /* WANT_DOUBLE */
  465. }
  466. #if WANT_GNU_ERRNO
  467. print:
  468. #endif
  469. { /* this used to be printfield */
  470. int len;
  471. /* cheaper than strlen call */
  472. for ( len = 0 ; p[len] ; len++ ) { }
  473. if ((*p == '-')
  474. #if WANT_GNU_ERRNO
  475. && (*fmt != 'm')
  476. #endif
  477. && (*fmt != 's')) {
  478. flag[FLAG_PLUS] = *p++;
  479. --len;
  480. }
  481. if (flag[FLAG_PLUS]) {
  482. ++len;
  483. ++preci;
  484. if (flag[FLAG_PLUS] == '0') { /* base 16 */
  485. ++preci; /* account for x or X */
  486. }
  487. }
  488. if (preci >= 0) {
  489. if ((*fmt == 's')
  490. #if WANT_GNU_ERRNO
  491. || (*fmt == 'm')
  492. #endif
  493. ) {
  494. len = preci;
  495. }
  496. preci -= len;
  497. if (preci < 0) {
  498. preci = 0;
  499. }
  500. width -= preci;
  501. }
  502. width -= len;
  503. if (width < 0) {
  504. width = 0;
  505. }
  506. if (preci < 0) {
  507. preci = 0;
  508. if (!flag[FLAG_MINUS_LJUSTIFY]
  509. /* && flag[FLAG_PLUS] */
  510. && (flag[FLAG_0_PAD] == '0')) {
  511. preci = width;
  512. width = 0;
  513. }
  514. }
  515. while (width + len + preci) {
  516. unsigned char ch;
  517. /* right padding || left padding */
  518. if ((!len && !preci)
  519. || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
  520. ch = ' ';
  521. --width;
  522. } else if (flag[FLAG_PLUS]) {
  523. ch = flag[FLAG_PLUS]; /* sign */
  524. if (flag[FLAG_PLUS]=='0') { /* base 16 case */
  525. flag[FLAG_PLUS] = *p++; /* get the x|X */
  526. } else {
  527. flag[FLAG_PLUS] = '\0';
  528. }
  529. --len;
  530. } else if (preci) {
  531. ch = '0';
  532. --preci;
  533. } else {
  534. ch = *p++; /* main field */
  535. --len;
  536. }
  537. if (++cnt < max_size) {
  538. putc(ch, op);
  539. }
  540. if ((ch == '\n') && (buffer_mode == _IOLBF)) {
  541. fflush(op);
  542. }
  543. }
  544. }
  545. goto nextfmt;
  546. }
  547. fmt = fmt0; /* this was an illegal format */
  548. }
  549. charout:
  550. if (++cnt < max_size) {
  551. putc(*fmt, op); /* normal char out */
  552. }
  553. if ((*fmt == '\n') && (buffer_mode == _IOLBF)) {
  554. fflush(op);
  555. }
  556. nextfmt:
  557. ++fmt;
  558. }
  559. op->mode |= buffer_mode;
  560. if (buffer_mode == _IONBF) {
  561. fflush(op);
  562. }
  563. if (buffer_mode == _IOLBF) {
  564. op->bufwrite = op->bufstart;
  565. }
  566. if (ferror(op)) {
  567. cnt = -1;
  568. }
  569. return (cnt);
  570. }
  571. #endif