printf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  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. *
  63. * Added asprintf.
  64. *
  65. * Hopefully fixed 0-pad prefixing bug.
  66. */
  67. /*****************************************************************************/
  68. /* OPTIONS */
  69. /*****************************************************************************/
  70. /* The optional support for long longs and doubles comes in two forms.
  71. *
  72. * 1) Normal (or partial for doubles) output support. Set to 1 to turn on.
  73. * Adds about 54 byes and about 217 bytes for long longss to the base size
  74. * of 1298. (Bizarre: both turned on is smaller than WANT_LONG_LONG only.)
  75. */
  76. #define WANT_LONG_LONG 0
  77. #define WANT_DOUBLE 0
  78. /* 2) An error message is inserted into the stream, an arg of the
  79. * appropriate size is removed from the arglist, and processing
  80. * continues. This is adds less code and may be useful in some
  81. * cases. Set to 1 to turn on. Adds about 31 bytes for doubles
  82. * and about 54 bytes for long longs to the base size of 1298.
  83. */
  84. #define WANT_LONG_LONG_ERROR 0
  85. #define WANT_DOUBLE_ERROR 0
  86. /*
  87. * Set to support GNU extension of %m to print string corresponding to errno.
  88. *
  89. * Warning: This adds about 50 bytes (i386) to the code but it also pulls in
  90. * strerror and the corresponding string table which together are about 3.8k.
  91. */
  92. #define WANT_GNU_ERRNO 0
  93. /*
  94. * Use fputc instead of macro putc. Slower but saves about 36 bytes.
  95. * WARNING! This may cause problems the the *s*printf functions!
  96. * Don't enable at this time. Manuel
  97. */
  98. #define WANT_FPUTC 0
  99. /**************************************************************************/
  100. #include <sys/types.h>
  101. #include <fcntl.h>
  102. #include <string.h>
  103. #include <stdlib.h>
  104. #if WANT_GNU_ERRNO
  105. #include <errno.h>
  106. #endif
  107. #ifdef __STDC__
  108. #include <stdarg.h>
  109. #define va_strt va_start
  110. #else
  111. #include <varargs.h>
  112. #define va_strt(p,i) va_start(p)
  113. #endif
  114. #include "stdio.h"
  115. #if WANT_FPUTC
  116. #undef putc
  117. #define putc(c,s) fputc(c,s)
  118. #endif
  119. extern int vfnprintf(FILE * op, size_t max_size,
  120. register __const char *fmt, register va_list ap);
  121. #ifdef L_printf
  122. int printf(const char *fmt, ...)
  123. {
  124. va_list ptr;
  125. int rv;
  126. va_strt(ptr, fmt);
  127. rv = vfnprintf(stdout, -1, fmt, ptr);
  128. va_end(ptr);
  129. return rv;
  130. }
  131. #endif
  132. #ifdef L_asprintf
  133. int asprintf(char **app, const char *fmt, ...)
  134. {
  135. va_list ptr;
  136. int rv, i;
  137. char *p; /* unitialized warning is ok here */
  138. /*
  139. * First iteration - find out size of buffer required and allocate it.
  140. * Second iteration - actually produce output.
  141. */
  142. rv = 0;
  143. for (i=0 ; i<2 ; i++) {
  144. va_strt(ptr, fmt);
  145. rv = vsnprintf(p, rv, fmt, ptr);
  146. va_end(ptr);
  147. if (i==0) { /* first time through so */
  148. p = malloc(++rv); /* allocate the buffer */
  149. *app = p;
  150. if (!p) {
  151. return -1;
  152. }
  153. }
  154. }
  155. return rv;
  156. }
  157. #endif
  158. #ifdef L_sprintf
  159. int sprintf(char *sp, const char *fmt, ...)
  160. {
  161. va_list ptr;
  162. int rv;
  163. va_strt(ptr, fmt);
  164. rv = vsnprintf(sp, -1, fmt, ptr);
  165. va_end(ptr);
  166. return rv;
  167. }
  168. #endif
  169. #ifdef L_snprintf
  170. int snprintf(char *sp, size_t size, const char *fmt, ...)
  171. {
  172. va_list ptr;
  173. int rv;
  174. va_strt(ptr, fmt);
  175. rv = vsnprintf(sp, size, fmt, ptr);
  176. va_end(ptr);
  177. return rv;
  178. }
  179. #endif
  180. #ifdef L_fprintf
  181. int fprintf(FILE * fp, const char *fmt, ...)
  182. {
  183. va_list ptr;
  184. int rv;
  185. va_strt(ptr, fmt);
  186. rv = vfnprintf(fp, -1, fmt, ptr);
  187. va_end(ptr);
  188. return rv;
  189. }
  190. #endif
  191. #ifdef L_vprintf
  192. int vprintf(const char *fmt, va_list ap)
  193. {
  194. return vfprintf(stdout, 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. lval = (sizeof(int) == sizeof(long)); /* long value flaged */
  291. tmp[1] = 0; /* set things up for %c -- better done here */
  292. /* init flags */
  293. for (p =(char *) spec ; *p ; p++) {
  294. flag[p-spec] = '\0';
  295. }
  296. flag[FLAG_0_PAD] = ' ';
  297. /* process optional flags */
  298. for (p = (char *)spec ; *p ; p++) {
  299. if (*fmt == *p) {
  300. flag[p-spec] = *fmt++;
  301. p = (char *)spec; /* restart scan */
  302. }
  303. }
  304. if (!flag[FLAG_PLUS]) {
  305. flag[FLAG_PLUS] = flag[FLAG_SPACE];
  306. }
  307. /* process optional width and precision */
  308. do {
  309. if (*fmt == '.') {
  310. ++fmt;
  311. dpoint = 1;
  312. }
  313. if (*fmt == '*') { /* parameter width value */
  314. ++fmt;
  315. i = va_arg(ap, int);
  316. } else {
  317. for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
  318. i = (i * 10) + (*fmt - '0');
  319. }
  320. }
  321. if (dpoint) {
  322. preci = i;
  323. if (i<0) {
  324. preci = 0;
  325. }
  326. } else {
  327. width = i;
  328. if (i<0) {
  329. width = -i;
  330. flag[FLAG_MINUS_LJUSTIFY] = 1;
  331. }
  332. }
  333. } while ((*fmt == '.') && !dpoint );
  334. /* process optional qualifier */
  335. for (p = (char *) qual ; *p ; p++) {
  336. if (*p == *fmt) {
  337. lval = p - qual;
  338. ++fmt;
  339. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  340. if ((*p == 'l') && (*fmt == *p)) {
  341. ++lval;
  342. ++fmt;
  343. }
  344. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  345. }
  346. }
  347. #if WANT_GNU_ERRNO
  348. if (*fmt == 'm') {
  349. flag[FLAG_PLUS] = '\0';
  350. flag[FLAG_0_PAD] = ' ';
  351. p = strerror(errno);
  352. goto print;
  353. }
  354. #endif
  355. /* process format specifier */
  356. for (p = (char *) u_spec ; *p ; p++) {
  357. if (*fmt != *p) continue;
  358. if (p-u_spec < 1) { /* print a % */
  359. goto charout;
  360. }
  361. if (p-u_spec < 2) { /* store output count in int ptr */
  362. *(va_arg(ap, int *)) = cnt;
  363. goto nextfmt;
  364. }
  365. if (p-u_spec < 8) { /* unsigned conversion */
  366. radix = u_radix[p-u_spec-2];
  367. upcase = ((int)'x') - *p;
  368. if (*p == 'p') {
  369. lval = (sizeof(char *) == sizeof(long));
  370. upcase = 0;
  371. }
  372. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  373. if (lval >= 2) {
  374. #if WANT_LONG_LONG
  375. p = __ulltostr(tmp + sizeof(tmp) - 1,
  376. va_arg(ap, unsigned long long),
  377. radix, upcase);
  378. #else
  379. (void) va_arg(ap, unsigned long long); /* cary on */
  380. p = (char *) ll_err;
  381. #endif /* WANT_LONG_LONG */
  382. } else {
  383. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  384. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  385. ((lval)
  386. ? va_arg(ap, unsigned long)
  387. : va_arg(ap, unsigned int)),
  388. radix, upcase);
  389. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  390. }
  391. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  392. flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
  393. if (flag[FLAG_HASH]) {
  394. switch (radix) {
  395. case 16:
  396. flag[FLAG_PLUS] = '0';
  397. *--p = 'x';
  398. if (*fmt == 'X') {
  399. *p = 'X';
  400. }
  401. break;
  402. case 8:
  403. if (*p != '0') { /* if not zero */
  404. *--p = '0'; /* add leadding zero */
  405. }
  406. }
  407. }
  408. } else if (p-u_spec < 10) { /* signed conversion */
  409. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  410. if (lval >= 2) {
  411. #if WANT_LONG_LONG
  412. p = __lltostr(tmp + sizeof(tmp) - 1,
  413. va_arg(ap, long long), 10, 0);
  414. #else
  415. (void) va_arg(ap, long long); /* carry on */
  416. p = (char *) ll_err;
  417. #endif /* WANT_LONG_LONG */
  418. } else {
  419. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  420. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  421. ((lval)
  422. ? va_arg(ap, long)
  423. : va_arg(ap, int)), 10, 0);
  424. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  425. }
  426. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  427. } else if (p-u_spec < 12) { /* character or string */
  428. flag[FLAG_PLUS] = '\0';
  429. flag[FLAG_0_PAD] = ' ';
  430. if (*p == 'c') { /* character */
  431. p = tmp;
  432. *p = va_arg(ap, int);
  433. } else { /* string */
  434. p = va_arg(ap, char *);
  435. }
  436. #if WANT_DOUBLE || WANT_DOUBLE_ERROR
  437. } else if (p-u_spec < 27) { /* floating point */
  438. #endif /* WANT_DOUBLE || WANT_DOUBLE_ERROR */
  439. #if WANT_DOUBLE
  440. p = __dtostr(tmp + sizeof(tmp) - 1, va_arg(ap, double));
  441. #elif WANT_DOUBLE_ERROR
  442. (void) va_arg(ap,double); /* carry on */
  443. p = (char *) dbl_err;
  444. #endif /* WANT_DOUBLE */
  445. }
  446. #if WANT_GNU_ERRNO
  447. print:
  448. #endif
  449. { /* this used to be printfield */
  450. int len;
  451. /* cheaper than strlen call */
  452. for ( len = 0 ; p[len] ; len++ ) { }
  453. if ((*p == '-')
  454. #if WANT_GNU_ERRNO
  455. && (*fmt != 'm')
  456. #endif
  457. && (*fmt != 's')) {
  458. flag[FLAG_PLUS] = *p++;
  459. --len;
  460. }
  461. if (flag[FLAG_PLUS]) {
  462. ++len;
  463. ++preci;
  464. if (flag[FLAG_PLUS] == '0') { /* base 16 */
  465. ++preci; /* account for x or X */
  466. }
  467. }
  468. if (preci >= 0) {
  469. if ((*fmt == 's')
  470. #if WANT_GNU_ERRNO
  471. || (*fmt == 'm')
  472. #endif
  473. ) {
  474. len = preci;
  475. }
  476. preci -= len;
  477. if (preci < 0) {
  478. preci = 0;
  479. }
  480. width -= preci;
  481. }
  482. width -= len;
  483. if (width < 0) {
  484. width = 0;
  485. }
  486. if (preci < 0) {
  487. preci = 0;
  488. if (flag[FLAG_PLUS]
  489. && !flag[FLAG_MINUS_LJUSTIFY]
  490. && (flag[FLAG_0_PAD] == '0')) {
  491. preci = width;
  492. width = 0;
  493. }
  494. }
  495. while (width + len + preci) {
  496. unsigned char ch;
  497. /* right padding || left padding */
  498. if ((!len && !preci)
  499. || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
  500. ch = flag[FLAG_0_PAD];
  501. --width;
  502. } else if (flag[FLAG_PLUS]) {
  503. ch = flag[FLAG_PLUS]; /* sign */
  504. if (flag[FLAG_PLUS]=='0') { /* base 16 case */
  505. flag[FLAG_PLUS] = *p++; /* get the x|X */
  506. } else {
  507. flag[FLAG_PLUS] = '\0';
  508. }
  509. --len;
  510. } else {
  511. flag[FLAG_0_PAD]=' ';
  512. if (preci) {
  513. ch = '0';
  514. --preci;
  515. } else {
  516. ch = *p++; /* main field */
  517. --len;
  518. }
  519. }
  520. if (++cnt < max_size) {
  521. putc(ch, op);
  522. }
  523. if ((ch == '\n') && (buffer_mode == _IOLBF)) {
  524. fflush(op);
  525. }
  526. }
  527. }
  528. goto nextfmt;
  529. }
  530. fmt = fmt0; /* this was an illegal format */
  531. }
  532. charout:
  533. if (++cnt < max_size) {
  534. putc(*fmt, op); /* normal char out */
  535. }
  536. if ((*fmt == '\n') && (buffer_mode == _IOLBF)) {
  537. fflush(op);
  538. }
  539. nextfmt:
  540. ++fmt;
  541. }
  542. op->mode |= buffer_mode;
  543. if (buffer_mode == _IONBF) {
  544. fflush(op);
  545. }
  546. if (buffer_mode == _IOLBF) {
  547. op->bufwrite = op->bufstart;
  548. }
  549. if (ferror(op)) {
  550. cnt = -1;
  551. }
  552. return (cnt);
  553. }
  554. #endif
  555. #ifdef L_vfprintf
  556. int vfprintf(FILE * op, register __const char *fmt, register va_list ap)
  557. {
  558. return (vfnprintf(op, -1, fmt, ap));
  559. }
  560. #endif