printf.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* vi: set sw=4 ts=4: */
  2. /* printf - format and print data
  3. Copyright (C) 90, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software Foundation,
  14. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  15. /* Usage: printf format [argument...]
  16. A front end to the printf function that lets it be used from the shell.
  17. Backslash escapes:
  18. \" = double quote
  19. \\ = backslash
  20. \a = alert (bell)
  21. \b = backspace
  22. \c = produce no further output
  23. \f = form feed
  24. \n = new line
  25. \r = carriage return
  26. \t = horizontal tab
  27. \v = vertical tab
  28. \0ooo = octal number (ooo is 0 to 3 digits)
  29. \xhhh = hexadecimal number (hhh is 1 to 3 digits)
  30. Additional directive:
  31. %b = print an argument string, interpreting backslash escapes
  32. The `format' argument is re-used as many times as necessary
  33. to convert all of the given arguments.
  34. David MacKenzie <djm@gnu.ai.mit.edu> */
  35. // 19990508 Busy Boxed! Dave Cinege
  36. #include <unistd.h>
  37. #include <stdio.h>
  38. #include <sys/types.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #include <stdlib.h>
  42. #include <fcntl.h>
  43. #include <ctype.h>
  44. #include <assert.h>
  45. #include "busybox.h"
  46. static double xstrtod __P((char *s));
  47. static long xstrtol __P((char *s));
  48. static unsigned long xstrtoul __P((char *s));
  49. static void print_esc_string __P((char *str));
  50. static int print_formatted __P((char *format, int argc, char **argv));
  51. static void print_direc __P( (char *start, size_t length,
  52. int field_width, int precision, char *argument));
  53. int printf_main(int argc, char **argv)
  54. {
  55. char *format;
  56. int args_used;
  57. if (argc <= 1 || **(argv + 1) == '-') {
  58. bb_show_usage();
  59. }
  60. format = argv[1];
  61. argc -= 2;
  62. argv += 2;
  63. do {
  64. args_used = print_formatted(format, argc, argv);
  65. argc -= args_used;
  66. argv += args_used;
  67. }
  68. while (args_used > 0 && argc > 0);
  69. /*
  70. if (argc > 0)
  71. fprintf(stderr, "excess args ignored");
  72. */
  73. return EXIT_SUCCESS;
  74. }
  75. /* Print the text in FORMAT, using ARGV (with ARGC elements) for
  76. arguments to any `%' directives.
  77. Return the number of elements of ARGV used. */
  78. static int print_formatted(char *format, int argc, char **argv)
  79. {
  80. int save_argc = argc; /* Preserve original value. */
  81. char *f; /* Pointer into `format'. */
  82. char *direc_start; /* Start of % directive. */
  83. size_t direc_length; /* Length of % directive. */
  84. int field_width; /* Arg to first '*', or -1 if none. */
  85. int precision; /* Arg to second '*', or -1 if none. */
  86. for (f = format; *f; ++f) {
  87. switch (*f) {
  88. case '%':
  89. direc_start = f++;
  90. direc_length = 1;
  91. field_width = precision = -1;
  92. if (*f == '%') {
  93. putchar('%');
  94. break;
  95. }
  96. if (*f == 'b') {
  97. if (argc > 0) {
  98. print_esc_string(*argv);
  99. ++argv;
  100. --argc;
  101. }
  102. break;
  103. }
  104. if (strchr("-+ #", *f)) {
  105. ++f;
  106. ++direc_length;
  107. }
  108. if (*f == '*') {
  109. ++f;
  110. ++direc_length;
  111. if (argc > 0) {
  112. field_width = xstrtoul(*argv);
  113. ++argv;
  114. --argc;
  115. } else
  116. field_width = 0;
  117. } else
  118. while (isdigit(*f)) {
  119. ++f;
  120. ++direc_length;
  121. }
  122. if (*f == '.') {
  123. ++f;
  124. ++direc_length;
  125. if (*f == '*') {
  126. ++f;
  127. ++direc_length;
  128. if (argc > 0) {
  129. precision = xstrtoul(*argv);
  130. ++argv;
  131. --argc;
  132. } else
  133. precision = 0;
  134. } else
  135. while (isdigit(*f)) {
  136. ++f;
  137. ++direc_length;
  138. }
  139. }
  140. if (*f == 'l' || *f == 'L' || *f == 'h') {
  141. ++f;
  142. ++direc_length;
  143. }
  144. /*
  145. if (!strchr ("diouxXfeEgGcs", *f))
  146. fprintf(stderr, "%%%c: invalid directive", *f);
  147. */
  148. ++direc_length;
  149. if (argc > 0) {
  150. print_direc(direc_start, direc_length, field_width,
  151. precision, *argv);
  152. ++argv;
  153. --argc;
  154. } else
  155. print_direc(direc_start, direc_length, field_width,
  156. precision, "");
  157. break;
  158. case '\\':
  159. if (*++f == 'c')
  160. exit(0);
  161. putchar(bb_process_escape_sequence((const char **)&f));
  162. f--;
  163. break;
  164. default:
  165. putchar(*f);
  166. }
  167. }
  168. return save_argc - argc;
  169. }
  170. static void
  171. print_direc(char *start, size_t length, int field_width, int precision,
  172. char *argument)
  173. {
  174. char *p; /* Null-terminated copy of % directive. */
  175. p = xmalloc((unsigned) (length + 1));
  176. strncpy(p, start, length);
  177. p[length] = 0;
  178. switch (p[length - 1]) {
  179. case 'd':
  180. case 'i':
  181. if (field_width < 0) {
  182. if (precision < 0)
  183. printf(p, xstrtol(argument));
  184. else
  185. printf(p, precision, xstrtol(argument));
  186. } else {
  187. if (precision < 0)
  188. printf(p, field_width, xstrtol(argument));
  189. else
  190. printf(p, field_width, precision, xstrtol(argument));
  191. }
  192. break;
  193. case 'o':
  194. case 'u':
  195. case 'x':
  196. case 'X':
  197. if (field_width < 0) {
  198. if (precision < 0)
  199. printf(p, xstrtoul(argument));
  200. else
  201. printf(p, precision, xstrtoul(argument));
  202. } else {
  203. if (precision < 0)
  204. printf(p, field_width, xstrtoul(argument));
  205. else
  206. printf(p, field_width, precision, xstrtoul(argument));
  207. }
  208. break;
  209. case 'f':
  210. case 'e':
  211. case 'E':
  212. case 'g':
  213. case 'G':
  214. if (field_width < 0) {
  215. if (precision < 0)
  216. printf(p, xstrtod(argument));
  217. else
  218. printf(p, precision, xstrtod(argument));
  219. } else {
  220. if (precision < 0)
  221. printf(p, field_width, xstrtod(argument));
  222. else
  223. printf(p, field_width, precision, xstrtod(argument));
  224. }
  225. break;
  226. case 'c':
  227. printf(p, *argument);
  228. break;
  229. case 's':
  230. if (field_width < 0) {
  231. if (precision < 0)
  232. printf(p, argument);
  233. else
  234. printf(p, precision, argument);
  235. } else {
  236. if (precision < 0)
  237. printf(p, field_width, argument);
  238. else
  239. printf(p, field_width, precision, argument);
  240. }
  241. break;
  242. }
  243. free(p);
  244. }
  245. static unsigned long xstrtoul(char *arg)
  246. {
  247. unsigned long result;
  248. if (safe_strtoul(arg, &result))
  249. fprintf(stderr, "%s", arg);
  250. return result;
  251. }
  252. static long xstrtol(char *arg)
  253. {
  254. long result;
  255. if (safe_strtol(arg, &result))
  256. fprintf(stderr, "%s", arg);
  257. return result;
  258. }
  259. static double xstrtod(char *arg)
  260. {
  261. double result;
  262. if (safe_strtod(arg, &result))
  263. fprintf(stderr, "%s", arg);
  264. return result;
  265. }
  266. static void print_esc_string(char *str)
  267. {
  268. for (; *str; str++) {
  269. if (*str == '\\') {
  270. str++;
  271. putchar(bb_process_escape_sequence((const char **)&str));
  272. } else {
  273. putchar(*str);
  274. }
  275. }
  276. }