printf.c 14 KB

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