printf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 vfprintf.
  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 vfprintf 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. */
  54. /*****************************************************************************/
  55. /* OPTIONS */
  56. /*****************************************************************************/
  57. /* The optional support for long longs and doubles comes in two forms.
  58. *
  59. * 1) Normal (or partial for doubles) output support. Set to 1 to turn on.
  60. * Adds about 54 byes and about 217 bytes for long longss to the base size
  61. * of 1298. (Bizarre: both turned on is smaller than WANT_LONG_LONG only.)
  62. */
  63. #define WANT_LONG_LONG 0
  64. #define WANT_DOUBLE 0
  65. /* 2) An error message is inserted into the stream, an arg of the
  66. * appropriate size is removed from the arglist, and processing
  67. * continues. This is adds less code and may be useful in some
  68. * cases. Set to 1 to turn on. Adds about 31 bytes for doubles
  69. * and about 54 bytes for long longs to the base size of 1298.
  70. */
  71. #define WANT_LONG_LONG_ERROR 0
  72. #define WANT_DOUBLE_ERROR 0
  73. /*
  74. * Set to support GNU extension of %m to print string corresponding to errno.
  75. *
  76. * Warning: This adds about 50 bytes (i386) to the code but it also pulls in
  77. * strerror and the corresponding string table which together are about 3.8k.
  78. */
  79. #define WANT_GNU_ERRNO 0
  80. /*
  81. * Use fputc instead of macro putc. Slower but saves about 36 bytes.
  82. */
  83. #define WANT_FPUTC 0
  84. /**************************************************************************/
  85. #include <sys/types.h>
  86. #include <fcntl.h>
  87. #include <string.h>
  88. #include <stdlib.h>
  89. #if WANT_GNU_ERRNO
  90. #include <errno.h>
  91. #endif
  92. #ifdef __STDC__
  93. #include <stdarg.h>
  94. #define va_strt va_start
  95. #else
  96. #include <varargs.h>
  97. #define va_strt(p,i) va_start(p)
  98. #endif
  99. #include "stdio.h"
  100. #if WANT_FPUTC
  101. #undef putc
  102. #define putc(c,s) fputc(c,s)
  103. #endif
  104. extern int vfnprintf(FILE * op, size_t max_size,
  105. register __const char *fmt, register va_list ap);
  106. #ifdef L_printf
  107. int printf(const char *fmt, ...)
  108. {
  109. va_list ptr;
  110. int rv;
  111. va_strt(ptr, fmt);
  112. rv = vfnprintf(stdout, -1, fmt, ptr);
  113. va_end(ptr);
  114. return rv;
  115. }
  116. #endif
  117. #ifdef L_sprintf
  118. int sprintf(char *sp, const char *fmt, ...)
  119. {
  120. static FILE string[1] = {
  121. {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1,
  122. _IOFBF | __MODE_WRITE}
  123. };
  124. va_list ptr;
  125. int rv;
  126. va_strt(ptr, fmt);
  127. string->bufpos = sp;
  128. rv = vfnprintf(string, -1, fmt, ptr);
  129. va_end(ptr);
  130. *(string->bufpos) = 0;
  131. return rv;
  132. }
  133. #endif
  134. #ifdef L_snprintf
  135. int snprintf(char *sp, size_t size, const char *fmt, ...)
  136. {
  137. static FILE string[1] = {
  138. {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1,
  139. _IOFBF | __MODE_WRITE}
  140. };
  141. va_list ptr;
  142. int rv;
  143. va_strt(ptr, fmt);
  144. string->bufpos = sp;
  145. rv = vfnprintf(string, size, fmt, ptr);
  146. va_end(ptr);
  147. *(string->bufpos) = 0;
  148. return rv;
  149. }
  150. #endif
  151. #ifdef L_fprintf
  152. int fprintf(FILE * fp, const char *fmt, ...)
  153. {
  154. va_list ptr;
  155. int rv;
  156. va_strt(ptr, fmt);
  157. rv = vfnprintf(fp, -1, fmt, ptr);
  158. va_end(ptr);
  159. return rv;
  160. }
  161. #endif
  162. #ifdef L_vprintf
  163. int vprintf(const char *fmt, va_list ap)
  164. {
  165. return vfprintf(stdout, fmt, ap);
  166. }
  167. #endif
  168. #ifdef L_vsprintf
  169. int vsprintf(char *sp, __const char *fmt, va_list ap)
  170. {
  171. static FILE string[1] = {
  172. {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1,
  173. _IOFBF | __MODE_WRITE}
  174. };
  175. int rv;
  176. string->bufpos = sp;
  177. rv = vfnprintf(string, -1, fmt, ap);
  178. *(string->bufpos) = 0;
  179. return rv;
  180. }
  181. #endif
  182. #ifdef L_vsprintf
  183. int vsnprintf(char *sp, size_t size, __const char *fmt, va_list ap)
  184. {
  185. static FILE string[1] = {
  186. {0, 0, (char *) (unsigned) -1, 0, (char *) (unsigned) -1, -1,
  187. _IOFBF | __MODE_WRITE}
  188. };
  189. int rv;
  190. string->bufpos = sp;
  191. rv = vfnprintf(string, size, fmt, ap);
  192. *(string->bufpos) = 0;
  193. return rv;
  194. }
  195. #endif
  196. #ifdef L_vfprintf
  197. extern char *__ultostr(char *buf, unsigned long uval, int base, int uppercase);
  198. extern char *__ltostr(char *buf, long val, int base, int uppercase);
  199. extern char *__ulltostr(char *buf, unsigned long long uval, int base, int uppercase);
  200. extern char *__lltostr(char *buf, long long val, int base, int uppercase);
  201. extern char *__dtostr(char *buf, double x);
  202. enum {
  203. FLAG_PLUS = 0,
  204. FLAG_MINUS_LJUSTIFY,
  205. FLAG_HASH,
  206. FLAG_0_PAD,
  207. FLAG_SPACE,
  208. };
  209. /* layout 01234 */
  210. static const char spec[] = "+-#0 ";
  211. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  212. static const char qual[] = "hlLq";
  213. #else
  214. static const char qual[] = "hl";
  215. #endif
  216. #if !WANT_LONG_LONG && WANT_LONG_LONG_ERROR
  217. static const char ll_err[] = "<LONG-LONG>";
  218. #endif
  219. #if !WANT_DOUBLE && WANT_DOUBLE_ERROR
  220. static const char dbl_err[] = "<DOUBLE>";
  221. #endif
  222. #if WANT_DOUBLE || WANT_DOUBLE_ERROR
  223. /* layout 012345678901234567 */
  224. static const char u_spec[] = "%nbopxXudicsfgGeEaA";
  225. #else
  226. /* layout 0123456789012 */
  227. static const char u_spec[] = "%nbopxXudics0";
  228. #endif
  229. /* WARNING: u_spec and u_radix need to stay in agreement!!! */
  230. /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */
  231. static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
  232. int vfnprintf(FILE * op, size_t max_size, const char *fmt, va_list ap)
  233. {
  234. int i, cnt = 0, lval;
  235. char *p;
  236. const char *fmt0;
  237. int buffer_mode;
  238. int preci, width;
  239. #define upcase i
  240. int radix, dpoint /*, upcase*/;
  241. #if WANT_LONG_LONG
  242. char tmp[65];
  243. #else
  244. char tmp[33];
  245. #endif
  246. char flag[sizeof(spec)];
  247. /* This speeds things up a bit for unbuffered */
  248. buffer_mode = (op->mode & __MODE_BUF);
  249. op->mode &= (~__MODE_BUF);
  250. while (*fmt) {
  251. if (*fmt == '%') {
  252. fmt0 = fmt; /* save our position in case of bad format */
  253. ++fmt;
  254. if (buffer_mode == _IONBF) {
  255. fflush(op);
  256. }
  257. width = -1; /* min field width */
  258. preci = -5; /* max string width or mininum digits */
  259. radix = 10; /* number base */
  260. dpoint = 0; /* found decimal point */
  261. lval = (sizeof(int) == sizeof(long)); /* long value flaged */
  262. tmp[1] = 0; /* set things up for %c -- better done here */
  263. /* init flags */
  264. for (p =(char *) spec ; *p ; p++) {
  265. flag[p-spec] = '\0';
  266. }
  267. flag[FLAG_0_PAD] = ' ';
  268. /* process optional flags */
  269. for (p = (char *)spec ; *p ; p++) {
  270. if (*fmt == *p) {
  271. flag[p-spec] = *fmt++;
  272. p = (char *)spec; /* restart scan */
  273. }
  274. }
  275. if (!flag[FLAG_PLUS]) {
  276. flag[FLAG_PLUS] = flag[FLAG_SPACE];
  277. }
  278. /* process optional width and precision */
  279. do {
  280. if (*fmt == '.') {
  281. ++fmt;
  282. dpoint = 1;
  283. }
  284. if (*fmt == '*') { /* parameter width value */
  285. ++fmt;
  286. i = va_arg(ap, int);
  287. } else {
  288. for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
  289. i = (i * 10) + (*fmt - '0');
  290. }
  291. }
  292. if (dpoint) {
  293. preci = i;
  294. if (i<0) {
  295. preci = 0;
  296. }
  297. } else {
  298. width = i;
  299. if (i<0) {
  300. width = -i;
  301. flag[FLAG_MINUS_LJUSTIFY] = 1;
  302. }
  303. }
  304. } while ((*fmt == '.') && !dpoint );
  305. /* process optional qualifier */
  306. for (p = (char *) qual ; *p ; p++) {
  307. if (*p == *fmt) {
  308. lval = p - qual;
  309. ++fmt;
  310. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  311. if ((*p == 'l') && (*fmt == *p)) {
  312. ++lval;
  313. ++fmt;
  314. }
  315. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  316. }
  317. }
  318. #if WANT_GNU_ERRNO
  319. if (*fmt == 'm') {
  320. flag[FLAG_PLUS] = '\0';
  321. flag[FLAG_0_PAD] = ' ';
  322. p = strerror(errno);
  323. goto print;
  324. }
  325. #endif
  326. /* process format specifier */
  327. for (p = (char *) u_spec ; *p ; p++) {
  328. if (*fmt != *p) continue;
  329. if (p-u_spec < 1) { /* print a % */
  330. goto charout;
  331. }
  332. if (p-u_spec < 2) { /* store output count in int ptr */
  333. *(va_arg(ap, int *)) = cnt;
  334. goto nextfmt;
  335. }
  336. if (p-u_spec < 8) { /* unsigned conversion */
  337. radix = u_radix[p-u_spec-2];
  338. upcase = ((int)'x') - *p;
  339. if (*p == 'p') {
  340. lval = (sizeof(char *) == sizeof(long));
  341. upcase = 0;
  342. }
  343. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  344. if (lval >= 2) {
  345. #if WANT_LONG_LONG
  346. p = __ulltostr(tmp + sizeof(tmp) - 1,
  347. va_arg(ap, unsigned long long),
  348. radix, upcase);
  349. #else
  350. (void) va_arg(ap, unsigned long long); /* cary on */
  351. p = (char *) ll_err;
  352. #endif /* WANT_LONG_LONG */
  353. } else {
  354. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  355. p = __ultostr(tmp + sizeof(tmp) - 1, (unsigned long)
  356. ((lval)
  357. ? va_arg(ap, unsigned long)
  358. : va_arg(ap, unsigned short)),
  359. radix, upcase);
  360. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  361. }
  362. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  363. flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
  364. if (flag[FLAG_HASH]) {
  365. switch (radix) {
  366. case 16:
  367. flag[FLAG_PLUS] = '0';
  368. *--p = 'x';
  369. if (*fmt == 'X') {
  370. *p = 'X';
  371. }
  372. break;
  373. case 8:
  374. if (*p != '0') { /* if not zero */
  375. *--p = '0'; /* add leadding zero */
  376. }
  377. }
  378. }
  379. } else if (p-u_spec < 10) { /* signed conversion */
  380. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  381. if (lval >= 2) {
  382. #if WANT_LONG_LONG
  383. p = __lltostr(tmp + sizeof(tmp) - 1,
  384. va_arg(ap, long long), 10, 0);
  385. #else
  386. (void) va_arg(ap, long long); /* carry on */
  387. p = (char *) ll_err;
  388. #endif /* WANT_LONG_LONG */
  389. } else {
  390. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  391. p = __ltostr(tmp + sizeof(tmp) - 1, (long)
  392. ((lval)
  393. ? va_arg(ap, long)
  394. : va_arg(ap, short)), 10, 0);
  395. #if WANT_LONG_LONG || WANT_LONG_LONG_ERROR
  396. }
  397. #endif /* WANT_LONG_LONG || WANT_LONG_LONG_ERROR */
  398. } else if (p-u_spec < 12) { /* character or string */
  399. flag[FLAG_PLUS] = '\0';
  400. flag[FLAG_0_PAD] = ' ';
  401. if (*p == 'c') { /* character */
  402. p = tmp;
  403. *p = va_arg(ap, int);
  404. } else { /* string */
  405. p = va_arg(ap, char *);
  406. }
  407. #if WANT_DOUBLE || WANT_DOUBLE_ERROR
  408. } else if (p-u_spec < 27) { /* floating point */
  409. #endif /* WANT_DOUBLE || WANT_DOUBLE_ERROR */
  410. #if WANT_DOUBLE
  411. p = __dtostr(tmp + sizeof(tmp) - 1, va_arg(ap, double));
  412. #elif WANT_DOUBLE_ERROR
  413. (void) va_arg(ap,double); /* carry on */
  414. p = (char *) dbl_err;
  415. #endif /* WANT_DOUBLE */
  416. }
  417. #if WANT_GNU_ERRNO
  418. print:
  419. #endif
  420. { /* this used to be printfield */
  421. int len;
  422. /* cheaper than strlen call */
  423. for ( len = 0 ; p[len] ; len++ ) { }
  424. if ((*p == '-')
  425. #if WANT_GNU_ERRNO
  426. && (*fmt != 'm')
  427. #endif
  428. && (*fmt != 's')) {
  429. flag[FLAG_PLUS] = *p++;
  430. --len;
  431. }
  432. if (flag[FLAG_PLUS]) {
  433. ++len;
  434. ++preci;
  435. if (flag[FLAG_PLUS] == '0') { /* base 16 */
  436. ++preci; /* account for x or X */
  437. }
  438. }
  439. if (preci >= 0) {
  440. if ((*fmt == 's')
  441. #if WANT_GNU_ERRNO
  442. || (*fmt == 'm')
  443. #endif
  444. ) {
  445. len = preci;
  446. }
  447. preci -= len;
  448. if (preci < 0) {
  449. preci = 0;
  450. }
  451. width -= preci;
  452. }
  453. width -= len;
  454. if (width < 0) {
  455. width = 0;
  456. }
  457. if (preci < 0) {
  458. preci = 0;
  459. if (flag[FLAG_PLUS]
  460. && !flag[FLAG_MINUS_LJUSTIFY]
  461. && (flag[FLAG_0_PAD] == '0')) {
  462. preci = width;
  463. width = 0;
  464. }
  465. }
  466. while (width + len + preci) {
  467. unsigned char ch;
  468. /* right padding || left padding */
  469. if ((!len && !preci)
  470. || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
  471. ch = ' ';
  472. --width;
  473. } else if (flag[FLAG_PLUS]) {
  474. ch = flag[FLAG_PLUS]; /* sign */
  475. if (flag[FLAG_PLUS]=='0') { /* base 16 case */
  476. flag[FLAG_PLUS] = *p++; /* get the x|X */
  477. } else {
  478. flag[FLAG_PLUS] = '\0';
  479. }
  480. --len;
  481. } else if (preci) {
  482. ch = '0';
  483. --preci;
  484. } else {
  485. ch = *p++; /* main field */
  486. --len;
  487. }
  488. if (++cnt < max_size) {
  489. putc(ch, op);
  490. }
  491. if ((ch == '\n') && (buffer_mode == _IOLBF)) {
  492. fflush(op);
  493. }
  494. }
  495. }
  496. goto nextfmt;
  497. }
  498. fmt = fmt0; /* this was an illegal format */
  499. }
  500. charout:
  501. if (++cnt < max_size) {
  502. putc(*fmt, op); /* normal char out */
  503. }
  504. if ((*fmt == '\n') && (buffer_mode == _IOLBF)) {
  505. fflush(op);
  506. }
  507. nextfmt:
  508. ++fmt;
  509. }
  510. op->mode |= buffer_mode;
  511. if (buffer_mode == _IONBF) {
  512. fflush(op);
  513. }
  514. if (buffer_mode == _IOLBF) {
  515. op->bufwrite = op->bufstart;
  516. }
  517. if (ferror(op)) {
  518. cnt = -1;
  519. }
  520. return (cnt);
  521. }
  522. int vfprintf(FILE * op, register __const char *fmt, register va_list ap)
  523. {
  524. return (vfnprintf(op, -1, fmt, ap));
  525. }
  526. #endif