printf.c 11 KB

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