printf.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /* vi: set sw=4 ts=4: */
  2. /* printf - format and print data
  3. Copyright 1999 Dave Cinege
  4. Portions copyright (C) 1990-1996 Free Software Foundation, Inc.
  5. Licensed under GPLv2 or later, see file LICENSE in this source tree.
  6. */
  7. /* Usage: printf format [argument...]
  8. A front end to the printf function that lets it be used from the shell.
  9. Backslash escapes:
  10. \" = double quote
  11. \\ = backslash
  12. \a = alert (bell)
  13. \b = backspace
  14. \c = produce no further output
  15. \f = form feed
  16. \n = new line
  17. \r = carriage return
  18. \t = horizontal tab
  19. \v = vertical tab
  20. \0ooo = octal number (ooo is 0 to 3 digits)
  21. \xhhh = hexadecimal number (hhh is 1 to 3 digits)
  22. Additional directive:
  23. %b = print an argument string, interpreting backslash escapes
  24. The 'format' argument is re-used as many times as necessary
  25. to convert all of the given arguments.
  26. David MacKenzie <djm@gnu.ai.mit.edu>
  27. */
  28. /* 19990508 Busy Boxed! Dave Cinege */
  29. //usage:#define printf_trivial_usage
  30. //usage: "FORMAT [ARG]..."
  31. //usage:#define printf_full_usage "\n\n"
  32. //usage: "Format and print ARG(s) according to FORMAT (a-la C printf)"
  33. //usage:
  34. //usage:#define printf_example_usage
  35. //usage: "$ printf \"Val=%d\\n\" 5\n"
  36. //usage: "Val=5\n"
  37. #include "libbb.h"
  38. /* A note on bad input: neither bash 3.2 nor coreutils 6.10 stop on it.
  39. * They report it:
  40. * bash: printf: XXX: invalid number
  41. * printf: XXX: expected a numeric value
  42. * bash: printf: 123XXX: invalid number
  43. * printf: 123XXX: value not completely converted
  44. * but then they use 0 (or partially converted numeric prefix) as a value
  45. * and continue. They exit with 1 in this case.
  46. * Both accept insane field width/precision (e.g. %9999999999.9999999999d).
  47. * Both print error message and assume 0 if %*.*f width/precision is "bad"
  48. * (but negative numbers are not "bad").
  49. * Both accept negative numbers for %u specifier.
  50. *
  51. * We try to be compatible.
  52. */
  53. typedef void FAST_FUNC (*converter)(const char *arg, void *result);
  54. static int multiconvert(const char *arg, void *result, converter convert)
  55. {
  56. if (*arg == '"' || *arg == '\'') {
  57. arg = utoa((unsigned char)arg[1]);
  58. }
  59. errno = 0;
  60. convert(arg, result);
  61. if (errno) {
  62. bb_error_msg("invalid number '%s'", arg);
  63. return 1;
  64. }
  65. return 0;
  66. }
  67. static void FAST_FUNC conv_strtoull(const char *arg, void *result)
  68. {
  69. *(unsigned long long*)result = bb_strtoull(arg, NULL, 0);
  70. /* both coreutils 6.10 and bash 3.2:
  71. * $ printf '%x\n' -2
  72. * fffffffffffffffe
  73. * Mimic that:
  74. */
  75. if (errno) {
  76. *(unsigned long long*)result = bb_strtoll(arg, NULL, 0);
  77. }
  78. }
  79. static void FAST_FUNC conv_strtoll(const char *arg, void *result)
  80. {
  81. *(long long*)result = bb_strtoll(arg, NULL, 0);
  82. }
  83. static void FAST_FUNC conv_strtod(const char *arg, void *result)
  84. {
  85. char *end;
  86. /* Well, this one allows leading whitespace... so what? */
  87. /* What I like much less is that "-" accepted too! :( */
  88. *(double*)result = strtod(arg, &end);
  89. if (end[0]) {
  90. errno = ERANGE;
  91. *(double*)result = 0;
  92. }
  93. }
  94. /* Callers should check errno to detect errors */
  95. static unsigned long long my_xstrtoull(const char *arg)
  96. {
  97. unsigned long long result;
  98. if (multiconvert(arg, &result, conv_strtoull))
  99. result = 0;
  100. return result;
  101. }
  102. static long long my_xstrtoll(const char *arg)
  103. {
  104. long long result;
  105. if (multiconvert(arg, &result, conv_strtoll))
  106. result = 0;
  107. return result;
  108. }
  109. static double my_xstrtod(const char *arg)
  110. {
  111. double result;
  112. multiconvert(arg, &result, conv_strtod);
  113. return result;
  114. }
  115. /* Handles %b; return 1 if output is to be short-circuited by \c */
  116. static int print_esc_string(const char *str)
  117. {
  118. char c;
  119. while ((c = *str) != '\0') {
  120. str++;
  121. if (c == '\\') {
  122. /* %b also accepts 4-digit octals of the form \0### */
  123. if (*str == '0') {
  124. if ((unsigned char)(str[1] - '0') < 8) {
  125. /* 2nd char is 0..7: skip leading '0' */
  126. str++;
  127. }
  128. }
  129. else if (*str == 'c') {
  130. return 1;
  131. }
  132. {
  133. /* optimization: don't force arg to be on-stack,
  134. * use another variable for that. */
  135. const char *z = str;
  136. c = bb_process_escape_sequence(&z);
  137. str = z;
  138. }
  139. }
  140. putchar(c);
  141. }
  142. return 0;
  143. }
  144. static void print_direc(char *format, unsigned fmt_length,
  145. int field_width, int precision,
  146. const char *argument)
  147. {
  148. long long llv;
  149. double dv;
  150. char saved;
  151. char *have_prec, *have_width;
  152. saved = format[fmt_length];
  153. format[fmt_length] = '\0';
  154. have_prec = strstr(format, ".*");
  155. have_width = strchr(format, '*');
  156. if (have_width - 1 == have_prec)
  157. have_width = NULL;
  158. errno = 0;
  159. switch (format[fmt_length - 1]) {
  160. case 'c':
  161. printf(format, *argument);
  162. break;
  163. case 'd':
  164. case 'i':
  165. llv = my_xstrtoll(argument);
  166. print_long:
  167. if (!have_width) {
  168. if (!have_prec)
  169. printf(format, llv);
  170. else
  171. printf(format, precision, llv);
  172. } else {
  173. if (!have_prec)
  174. printf(format, field_width, llv);
  175. else
  176. printf(format, field_width, precision, llv);
  177. }
  178. break;
  179. case 'o':
  180. case 'u':
  181. case 'x':
  182. case 'X':
  183. llv = my_xstrtoull(argument);
  184. /* cheat: unsigned long and long have same width, so... */
  185. goto print_long;
  186. case 's':
  187. /* Are char* and long long the same? */
  188. if (sizeof(argument) == sizeof(llv)) {
  189. llv = (long long)(ptrdiff_t)argument;
  190. goto print_long;
  191. } else {
  192. /* Hope compiler will optimize it out by moving call
  193. * instruction after the ifs... */
  194. if (!have_width) {
  195. if (!have_prec)
  196. printf(format, argument, /*unused:*/ argument, argument);
  197. else
  198. printf(format, precision, argument, /*unused:*/ argument);
  199. } else {
  200. if (!have_prec)
  201. printf(format, field_width, argument, /*unused:*/ argument);
  202. else
  203. printf(format, field_width, precision, argument);
  204. }
  205. break;
  206. }
  207. case 'f':
  208. case 'e':
  209. case 'E':
  210. case 'g':
  211. case 'G':
  212. dv = my_xstrtod(argument);
  213. if (!have_width) {
  214. if (!have_prec)
  215. printf(format, dv);
  216. else
  217. printf(format, precision, dv);
  218. } else {
  219. if (!have_prec)
  220. printf(format, field_width, dv);
  221. else
  222. printf(format, field_width, precision, dv);
  223. }
  224. break;
  225. } /* switch */
  226. format[fmt_length] = saved;
  227. }
  228. /* Handle params for "%*.*f". Negative numbers are ok (compat). */
  229. static int get_width_prec(const char *str)
  230. {
  231. int v = bb_strtoi(str, NULL, 10);
  232. if (errno) {
  233. bb_error_msg("invalid number '%s'", str);
  234. v = 0;
  235. }
  236. return v;
  237. }
  238. /* Print the text in FORMAT, using ARGV for arguments to any '%' directives.
  239. Return advanced ARGV. */
  240. static char **print_formatted(char *f, char **argv, int *conv_err)
  241. {
  242. char *direc_start; /* Start of % directive. */
  243. unsigned direc_length; /* Length of % directive. */
  244. int field_width; /* Arg to first '*' */
  245. int precision; /* Arg to second '*' */
  246. char **saved_argv = argv;
  247. for (; *f; ++f) {
  248. switch (*f) {
  249. case '%':
  250. direc_start = f++;
  251. direc_length = 1;
  252. field_width = precision = 0;
  253. if (*f == '%') {
  254. bb_putchar('%');
  255. break;
  256. }
  257. if (*f == 'b') {
  258. if (*argv) {
  259. if (print_esc_string(*argv))
  260. return saved_argv; /* causes main() to exit */
  261. ++argv;
  262. }
  263. break;
  264. }
  265. if (strchr("-+ #", *f)) {
  266. ++f;
  267. ++direc_length;
  268. }
  269. if (*f == '*') {
  270. ++f;
  271. ++direc_length;
  272. if (*argv)
  273. field_width = get_width_prec(*argv++);
  274. } else {
  275. while (isdigit(*f)) {
  276. ++f;
  277. ++direc_length;
  278. }
  279. }
  280. if (*f == '.') {
  281. ++f;
  282. ++direc_length;
  283. if (*f == '*') {
  284. ++f;
  285. ++direc_length;
  286. if (*argv)
  287. precision = get_width_prec(*argv++);
  288. } else {
  289. while (isdigit(*f)) {
  290. ++f;
  291. ++direc_length;
  292. }
  293. }
  294. }
  295. /* Remove "lLhz" size modifiers, repeatedly.
  296. * bash does not like "%lld", but coreutils
  297. * happily takes even "%Llllhhzhhzd"!
  298. * We are permissive like coreutils */
  299. while ((*f | 0x20) == 'l' || *f == 'h' || *f == 'z') {
  300. overlapping_strcpy(f, f + 1);
  301. }
  302. /* Add "ll" if integer modifier, then print */
  303. {
  304. static const char format_chars[] ALIGN1 = "diouxXfeEgGcs";
  305. char *p = strchr(format_chars, *f);
  306. /* needed - try "printf %" without it */
  307. if (p == NULL) {
  308. bb_error_msg("%s: invalid format", direc_start);
  309. /* causes main() to exit with error */
  310. return saved_argv - 1;
  311. }
  312. ++direc_length;
  313. if (p - format_chars <= 5) {
  314. /* it is one of "diouxX" */
  315. p = xmalloc(direc_length + 3);
  316. memcpy(p, direc_start, direc_length);
  317. p[direc_length + 1] = p[direc_length - 1];
  318. p[direc_length - 1] = 'l';
  319. p[direc_length] = 'l';
  320. //bb_error_msg("<%s>", p);
  321. direc_length += 2;
  322. direc_start = p;
  323. } else {
  324. p = NULL;
  325. }
  326. if (*argv) {
  327. print_direc(direc_start, direc_length, field_width,
  328. precision, *argv++);
  329. } else {
  330. print_direc(direc_start, direc_length, field_width,
  331. precision, "");
  332. }
  333. *conv_err |= errno;
  334. free(p);
  335. }
  336. break;
  337. case '\\':
  338. if (*++f == 'c') {
  339. return saved_argv; /* causes main() to exit */
  340. }
  341. bb_putchar(bb_process_escape_sequence((const char **)&f));
  342. f--;
  343. break;
  344. default:
  345. putchar(*f);
  346. }
  347. }
  348. return argv;
  349. }
  350. int printf_main(int argc UNUSED_PARAM, char **argv)
  351. {
  352. int conv_err;
  353. char *format;
  354. char **argv2;
  355. /* We must check that stdout is not closed.
  356. * The reason for this is highly non-obvious.
  357. * printf_main is used from shell.
  358. * Shell must correctly handle 'printf "%s" foo'
  359. * if stdout is closed. With stdio, output gets shoveled into
  360. * stdout buffer, and even fflush cannot clear it out. It seems that
  361. * even if libc receives EBADF on write attempts, it feels determined
  362. * to output data no matter what. So it will try later,
  363. * and possibly will clobber future output. Not good. */
  364. // TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR?
  365. if (fcntl(1, F_GETFL) == -1)
  366. return 1; /* match coreutils 6.10 (sans error msg to stderr) */
  367. //if (dup2(1, 1) != 1) - old way
  368. // return 1;
  369. /* bash builtin errors out on "printf '-%s-\n' foo",
  370. * coreutils-6.9 works. Both work with "printf -- '-%s-\n' foo".
  371. * We will mimic coreutils. */
  372. if (argv[1] && argv[1][0] == '-' && argv[1][1] == '-' && !argv[1][2])
  373. argv++;
  374. if (!argv[1]) {
  375. if (ENABLE_ASH_BUILTIN_PRINTF
  376. && applet_name[0] != 'p'
  377. ) {
  378. bb_error_msg("usage: printf FORMAT [ARGUMENT...]");
  379. return 2; /* bash compat */
  380. }
  381. bb_show_usage();
  382. }
  383. format = argv[1];
  384. argv2 = argv + 2;
  385. conv_err = 0;
  386. do {
  387. argv = argv2;
  388. argv2 = print_formatted(format, argv, &conv_err);
  389. } while (argv2 > argv && *argv2);
  390. /* coreutils compat (bash doesn't do this):
  391. if (*argv)
  392. fprintf(stderr, "excess args ignored");
  393. */
  394. return (argv2 < argv) /* if true, print_formatted errored out */
  395. || conv_err; /* print_formatted saw invalid number */
  396. }