old_vfprintf.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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 2001
  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. * Fixed bug parsing flags -- did not restart scan.
  65. * Added function asprintf.
  66. * Fixed 0-pad prefixing bug.
  67. * Converted sizeof(int) == sizeof(long) tests to compile time vs run time.
  68. * This saves 112 bytes of code on i386.
  69. * Fixed precision bug -- when negative set to default.
  70. * Added function fnprintf to support __dtostr.
  71. * Added floating point support for doubles. Yeah!
  72. *
  73. *
  74. * May 2001 Fixes from Johan Adolfsson (johan.adolfsson@axis.com)
  75. * 1) printf("%c",0) returned 0 instead of 1.
  76. * 2) unrolled loop in asprintf to reduce size and remove compile warning.
  77. *
  78. *
  79. * June 2001
  80. * 1) fix %p so that "0x" is prepended to outputed hex val
  81. * 2) fix %p so that "(nil)" is output for (void *)0 to match glibc
  82. *
  83. * Sep 5, 2003
  84. * Convert to new floating point conversion routine.
  85. *
  86. */
  87. /*****************************************************************************/
  88. /* OPTIONS */
  89. /*****************************************************************************/
  90. /* The optional support for long longs and doubles comes in two forms.
  91. *
  92. * 1) Normal (or partial for doubles) output support. Set to 1 to turn on.
  93. * Adds about 130 bytes for doubles, about 220 bytes for long longs,
  94. * and about 275 for both to the base code size of 1163 on i386.
  95. */
  96. /* These are now set in uClibc_config.h based on Config. */
  97. /*
  98. #define __UCLIBC_HAS_FLOATS__ 1
  99. */
  100. /* 2) An error message is inserted into the stream, an arg of the
  101. * appropriate size is removed from the arglist, and processing
  102. * continues. This is adds less code and may be useful in some
  103. * cases. Set to 1 to turn on. Adds about 50 bytes for doubles,
  104. * about 140 bytes for long longs, and about 175 bytes for both
  105. * to the base code size of 1163 on i386.
  106. */
  107. #define WANT_FLOAT_ERROR 0
  108. /*
  109. * Set to support GNU extension of %m to print string corresponding to errno.
  110. *
  111. * Warning: This adds about 50 bytes (i386) to the code but it also pulls in
  112. * strerror and the corresponding string table which together are about 3.8k.
  113. */
  114. /* Now controlled by uClibc_stdio.h and set below. */
  115. /* #define WANT_GNU_ERRNO 0 */
  116. /**************************************************************************/
  117. #define _GNU_SOURCE /* for strnlen */
  118. #define _STDIO_UTILITY
  119. #include <stdio.h>
  120. #include <stdarg.h>
  121. #include <limits.h>
  122. #include <string.h>
  123. #include <errno.h>
  124. #include <ctype.h>
  125. #define __PRINTF_INFO_NO_BITFIELD
  126. #include <printf.h>
  127. #ifdef __STDIO_THREADSAFE
  128. #include <pthread.h>
  129. #endif /* __STDIO_THREADSAFE */
  130. /* #undef __UCLIBC_HAS_FLOATS__ */
  131. /* #undef WANT_FLOAT_ERROR */
  132. /* #define WANT_FLOAT_ERROR 1 */
  133. /* #define __isdigit(c) (((unsigned int)(c - '0')) < 10) */
  134. #ifdef __STDIO_PRINTF_M_SUPPORT
  135. #define WANT_GNU_ERRNO 1
  136. #else
  137. #define WANT_GNU_ERRNO 0
  138. #endif
  139. #if defined(__UCLIBC_HAS_FLOATS__)
  140. #include <float.h>
  141. #include <bits/uClibc_fpmax.h>
  142. typedef void (__fp_outfunc_t)(FILE *fp, intptr_t type, intptr_t len,
  143. intptr_t buf);
  144. extern size_t _fpmaxtostr(FILE * fp, __fpmax_t x, struct printf_info *info,
  145. __fp_outfunc_t fp_outfunc);
  146. #define FMT_TYPE char
  147. #define OUTNSTR _outnstr
  148. #define _outnstr(stream, string, len) _stdio_fwrite(string, len, stream)
  149. static void _charpad(FILE * __restrict stream, int padchar, size_t numpad)
  150. {
  151. /* TODO -- Use a buffer to cut down on function calls... */
  152. FMT_TYPE pad[1];
  153. *pad = padchar;
  154. while (numpad) {
  155. OUTNSTR(stream, pad, 1);
  156. --numpad;
  157. }
  158. }
  159. static void _fp_out_narrow(FILE *fp, intptr_t type, intptr_t len, intptr_t buf)
  160. {
  161. if (type & 0x80) { /* Some type of padding needed. */
  162. int buflen = strlen((const char *) buf);
  163. if ((len -= buflen) > 0) {
  164. _charpad(fp, (type & 0x7f), len);
  165. }
  166. len = buflen;
  167. }
  168. OUTNSTR(fp, (const char *) buf, len);
  169. }
  170. #endif
  171. enum {
  172. FLAG_PLUS = 0,
  173. FLAG_MINUS_LJUSTIFY,
  174. FLAG_HASH,
  175. FLAG_0_PAD,
  176. FLAG_SPACE,
  177. };
  178. /* layout 01234 */
  179. static const char spec[] = "+-#0 ";
  180. static const char qual[] = "hlLq";
  181. #if !defined(__UCLIBC_HAS_FLOATS__) && WANT_FLOAT_ERROR
  182. static const char dbl_err[] = "<DOUBLE>";
  183. #endif
  184. #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
  185. /* layout 012345678901234567 */
  186. static const char u_spec[] = "%nbopxXudicsfgGeEaA";
  187. #else
  188. /* layout 0123456789012 */
  189. static const char u_spec[] = "%nbopxXudics";
  190. #endif
  191. /* WARNING: u_spec and u_radix need to stay in agreement!!! */
  192. /* u_radix[i] <-> u_spec[i+2] for unsigned entries only */
  193. static const char u_radix[] = "\x02\x08\x10\x10\x10\x0a";
  194. int vfprintf(FILE * __restrict op, register const char * __restrict fmt,
  195. va_list ap)
  196. {
  197. int i, cnt, lval, len;
  198. register char *p;
  199. const char *fmt0;
  200. int preci, width;
  201. #define upcase i
  202. int radix, dpoint /*, upcase*/;
  203. char tmp[65]; /* TODO - determing needed size from headers */
  204. char flag[sizeof(spec)];
  205. __STDIO_THREADLOCK(op);
  206. cnt = 0;
  207. while (*fmt) {
  208. if (*fmt == '%') {
  209. fmt0 = fmt; /* save our position in case of bad format */
  210. ++fmt;
  211. width = -1; /* min field width */
  212. preci = -5; /* max string width or mininum digits */
  213. radix = 10; /* number base */
  214. dpoint = 0; /* found decimal point */
  215. #if INT_MAX != LONG_MAX
  216. lval = 0; /* sizeof(int) != sizeof(long) */
  217. #else
  218. lval = 1; /* sizeof(int) == sizeof(long) */
  219. #endif
  220. /* init flags */
  221. for (p =(char *) spec ; *p ; p++) {
  222. flag[p-spec] = '\0';
  223. }
  224. flag[FLAG_0_PAD] = ' ';
  225. /* process optional flags */
  226. for (p = (char *)spec ; *p ; ) {
  227. if (*fmt == *p) {
  228. flag[p-spec] = *fmt++;
  229. p = (char *)spec; /* restart scan */
  230. } else {
  231. p++;
  232. }
  233. }
  234. if (!flag[FLAG_PLUS]) {
  235. flag[FLAG_PLUS] = flag[FLAG_SPACE];
  236. }
  237. /* process optional width and precision */
  238. do {
  239. if (*fmt == '.') {
  240. ++fmt;
  241. dpoint = 1;
  242. }
  243. if (*fmt == '*') { /* parameter width value */
  244. ++fmt;
  245. i = va_arg(ap, int);
  246. } else {
  247. for ( i = 0 ; (*fmt >= '0') && (*fmt <= '9') ; ++fmt ) {
  248. i = (i * 10) + (*fmt - '0');
  249. }
  250. }
  251. if (dpoint) {
  252. preci = i;
  253. if (i<0) {
  254. preci = -5;
  255. }
  256. } else {
  257. width = i;
  258. if (i<0) {
  259. width = -i;
  260. flag[FLAG_MINUS_LJUSTIFY] = 1;
  261. }
  262. }
  263. } while ((*fmt == '.') && !dpoint );
  264. /* process optional qualifier */
  265. for (p = (char *) qual ; *p ; p++) {
  266. if (*p == *fmt) {
  267. lval = p - qual;
  268. ++fmt; /* TODO - hh */
  269. if ((*p == 'l') && (*fmt == *p)) {
  270. ++lval;
  271. ++fmt;
  272. }
  273. }
  274. }
  275. #if WANT_GNU_ERRNO
  276. if (*fmt == 'm') {
  277. flag[FLAG_PLUS] = '\0';
  278. flag[FLAG_0_PAD] = ' ';
  279. p = _glibc_strerror_r(errno, tmp, sizeof(tmp));
  280. goto print;
  281. }
  282. #endif
  283. /* process format specifier */
  284. for (p = (char *) u_spec ; *p ; p++) {
  285. if (*fmt != *p) continue;
  286. if (p-u_spec < 1) { /* print a % */
  287. goto charout;
  288. }
  289. if (p-u_spec < 2) { /* store output count in int ptr */
  290. *(va_arg(ap, int *)) = cnt;
  291. goto nextfmt;
  292. }
  293. if (p-u_spec < 8) { /* unsigned conversion */
  294. radix = u_radix[p-u_spec-2];
  295. upcase = ((*p == 'x') ? __UIM_LOWER : __UIM_UPPER);
  296. if (*p == 'p') {
  297. lval = (sizeof(char *) == sizeof(long));
  298. upcase = __UIM_LOWER;
  299. flag[FLAG_HASH] = 'p';
  300. }
  301. p = _uintmaxtostr((tmp + sizeof(tmp) - 1),
  302. ((lval>1) /* TODO -- longlong/long/int/short/char */
  303. ? va_arg(ap, uintmax_t)
  304. : (uintmax_t)
  305. va_arg(ap, unsigned long)),
  306. radix, upcase);
  307. flag[FLAG_PLUS] = '\0'; /* meaningless for unsigned */
  308. if (*p != '0') { /* non-zero */
  309. if (flag[FLAG_HASH]) {
  310. if (radix == 8) {
  311. *--p = '0'; /* add leadding zero */
  312. } else if (radix != 10) { /* either 2 or 16 */
  313. flag[FLAG_PLUS] = '0';
  314. *--p = 'b';
  315. if (radix == 16) {
  316. *p = 'x';
  317. if (*fmt == 'X') {
  318. *p = 'X';
  319. }
  320. }
  321. }
  322. }
  323. } else if (flag[FLAG_HASH] == 'p') { /* null pointer */
  324. p = "(nil)";
  325. }
  326. } else if (p-u_spec < 10) { /* signed conversion */
  327. p = _uintmaxtostr((tmp + sizeof(tmp) - 1),
  328. ((lval>1) /* TODO -- longlong/long/int/short/char */
  329. ? va_arg(ap, uintmax_t)
  330. : (uintmax_t) ((intmax_t) /* sign-extend! */
  331. va_arg(ap, long))),
  332. -radix, upcase);
  333. } else if (p-u_spec < 12) { /* character or string */
  334. flag[FLAG_PLUS] = '\0';
  335. flag[FLAG_0_PAD] = ' ';
  336. if (*p == 'c') { /* character */
  337. p = tmp;
  338. *p = va_arg(ap, int);
  339. /* This takes care of the "%c",0 case */
  340. len = 1;
  341. goto print_len_set;
  342. } else { /* string */
  343. p = va_arg(ap, char *);
  344. if (!p) {
  345. p = "(null)";
  346. preci = 6;
  347. } else {
  348. if (preci < 0) {
  349. preci = INT_MAX;
  350. }
  351. }
  352. len = strnlen(p, preci);
  353. goto print_len_set;
  354. }
  355. #if defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR
  356. } else if (p-u_spec < 27) { /* floating point */
  357. #endif /* defined(__UCLIBC_HAS_FLOATS__) || WANT_FLOAT_ERROR */
  358. #if defined(__UCLIBC_HAS_FLOATS__)
  359. struct printf_info info;
  360. if (preci < 0) {
  361. preci = 6;
  362. }
  363. info.width = width;
  364. info.prec = preci;
  365. info.spec = *fmt;
  366. info.pad = flag[FLAG_0_PAD];
  367. info._flags = 0;
  368. if (flag[FLAG_PLUS] == '+') {
  369. PRINT_INFO_SET_FLAG(&info,showsign);
  370. } else if (flag[FLAG_PLUS] == ' ') {
  371. PRINT_INFO_SET_FLAG(&info,space);
  372. }
  373. if (flag[FLAG_HASH]) {
  374. PRINT_INFO_SET_FLAG(&info,alt);
  375. }
  376. if (flag[FLAG_MINUS_LJUSTIFY]) {
  377. PRINT_INFO_SET_FLAG(&info,left);
  378. }
  379. cnt += _fpmaxtostr(op,
  380. (__fpmax_t)
  381. ((lval > 1)
  382. ? va_arg(ap, long double)
  383. : (long double) va_arg(ap, double)),
  384. &info, _fp_out_narrow);
  385. goto nextfmt;
  386. #elif WANT_FLOAT_ERROR
  387. (void) ((lval > 1) ? va_arg(ap, long double)
  388. : va_arg(ap, double)); /* carry on */
  389. p = (char *) dbl_err;
  390. #endif /* defined(__UCLIBC_HAS_FLOATS__) */
  391. }
  392. #if WANT_GNU_ERRNO
  393. print:
  394. #endif
  395. { /* this used to be printfield */
  396. /* cheaper than strlen call */
  397. /* for ( len = 0 ; p[len] ; len++ ) { } */
  398. len = strnlen(p, SIZE_MAX);
  399. print_len_set:
  400. if ((*p == '-')
  401. #if WANT_GNU_ERRNO
  402. && (*fmt != 'm')
  403. #endif
  404. && (*fmt != 's')) {
  405. flag[FLAG_PLUS] = *p++;
  406. --len;
  407. }
  408. if (flag[FLAG_PLUS]) {
  409. ++len;
  410. ++preci;
  411. if (flag[FLAG_PLUS] == '0') { /* base 16 */
  412. ++preci; /* account for x or X */
  413. }
  414. }
  415. if (preci >= 0) {
  416. if ((*fmt == 's')
  417. #if WANT_GNU_ERRNO
  418. || (*fmt == 'm')
  419. #endif
  420. ) {
  421. if (len > preci) {
  422. len = preci;
  423. } else {
  424. preci = len;
  425. }
  426. }
  427. preci -= len;
  428. if (preci < 0) {
  429. preci = 0;
  430. }
  431. width -= preci;
  432. }
  433. width -= len;
  434. if (width < 0) {
  435. width = 0;
  436. }
  437. if (preci < 0) {
  438. preci = 0;
  439. if (!flag[FLAG_MINUS_LJUSTIFY]
  440. /* && flag[FLAG_PLUS] */
  441. && (flag[FLAG_0_PAD] == '0')) {
  442. preci = width;
  443. width = 0;
  444. }
  445. }
  446. while (width + len + preci) {
  447. unsigned char ch;
  448. /* right padding || left padding */
  449. if ((!len && !preci)
  450. || (width && !flag[FLAG_MINUS_LJUSTIFY])) {
  451. ch = ' ';
  452. --width;
  453. } else if (flag[FLAG_PLUS]) {
  454. ch = flag[FLAG_PLUS]; /* sign */
  455. if (flag[FLAG_PLUS]=='0') { /* base 16 case */
  456. flag[FLAG_PLUS] = *p++; /* get the x|X */
  457. } else {
  458. flag[FLAG_PLUS] = '\0';
  459. }
  460. --len;
  461. } else if (preci) {
  462. ch = '0';
  463. --preci;
  464. } else {
  465. ch = *p++; /* main field */
  466. --len;
  467. }
  468. ++cnt;
  469. putc(ch, op);
  470. }
  471. }
  472. goto nextfmt;
  473. }
  474. fmt = fmt0; /* this was an illegal format */
  475. }
  476. charout:
  477. ++cnt;
  478. putc(*fmt, op); /* normal char out */
  479. nextfmt:
  480. ++fmt;
  481. }
  482. i = (__FERROR(op)) ? -1 : cnt;
  483. __STDIO_THREADUNLOCK(op);
  484. return i;
  485. }