3
0

printf.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 GPL v2 or later, see file LICENSE in this tarball for details.
  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. // 19990508 Busy Boxed! Dave Cinege
  28. #include "libbb.h"
  29. typedef void (*converter)(const char *arg, void *result);
  30. static void multiconvert(const char *arg, void *result, converter convert)
  31. {
  32. char s[sizeof(int)*3 + 2];
  33. if (*arg == '"' || *arg == '\'') {
  34. sprintf(s, "%d", (unsigned char)arg[1]);
  35. arg = s;
  36. }
  37. convert(arg, result);
  38. /* if there was conversion error, print unconverted string */
  39. if (errno)
  40. fputs(arg, stderr);
  41. }
  42. static void conv_strtoul(const char *arg, void *result)
  43. {
  44. *(unsigned long*)result = bb_strtoul(arg, NULL, 0);
  45. }
  46. static void conv_strtol(const char *arg, void *result)
  47. {
  48. *(long*)result = bb_strtol(arg, NULL, 0);
  49. }
  50. static void conv_strtod(const char *arg, void *result)
  51. {
  52. char *end;
  53. /* Well, this one allows leading whitespace... so what */
  54. /* What I like much less is that "-" is accepted too! :( */
  55. *(double*)result = strtod(arg, &end);
  56. if (end[0]) errno = ERANGE;
  57. }
  58. static unsigned long my_xstrtoul(const char *arg)
  59. {
  60. unsigned long result;
  61. multiconvert(arg, &result, conv_strtoul);
  62. return result;
  63. }
  64. static long my_xstrtol(const char *arg)
  65. {
  66. long result;
  67. multiconvert(arg, &result, conv_strtol);
  68. return result;
  69. }
  70. static double my_xstrtod(const char *arg)
  71. {
  72. double result;
  73. multiconvert(arg, &result, conv_strtod);
  74. return result;
  75. }
  76. static void print_esc_string(char *str)
  77. {
  78. for (; *str; str++) {
  79. if (*str == '\\') {
  80. str++;
  81. bb_putchar(bb_process_escape_sequence((const char **)&str));
  82. } else {
  83. bb_putchar(*str);
  84. }
  85. }
  86. }
  87. static void print_direc(char *start, size_t length, int field_width, int precision,
  88. const char *argument)
  89. {
  90. char *p; /* Null-terminated copy of % directive. */
  91. p = xmalloc((unsigned) (length + 1));
  92. strncpy(p, start, length);
  93. p[length] = 0;
  94. switch (p[length - 1]) {
  95. case 'd':
  96. case 'i':
  97. if (field_width < 0) {
  98. if (precision < 0)
  99. printf(p, my_xstrtol(argument));
  100. else
  101. printf(p, precision, my_xstrtol(argument));
  102. } else {
  103. if (precision < 0)
  104. printf(p, field_width, my_xstrtol(argument));
  105. else
  106. printf(p, field_width, precision, my_xstrtol(argument));
  107. }
  108. break;
  109. case 'o':
  110. case 'u':
  111. case 'x':
  112. case 'X':
  113. if (field_width < 0) {
  114. if (precision < 0)
  115. printf(p, my_xstrtoul(argument));
  116. else
  117. printf(p, precision, my_xstrtoul(argument));
  118. } else {
  119. if (precision < 0)
  120. printf(p, field_width, my_xstrtoul(argument));
  121. else
  122. printf(p, field_width, precision, my_xstrtoul(argument));
  123. }
  124. break;
  125. case 'f':
  126. case 'e':
  127. case 'E':
  128. case 'g':
  129. case 'G':
  130. if (field_width < 0) {
  131. if (precision < 0)
  132. printf(p, my_xstrtod(argument));
  133. else
  134. printf(p, precision, my_xstrtod(argument));
  135. } else {
  136. if (precision < 0)
  137. printf(p, field_width, my_xstrtod(argument));
  138. else
  139. printf(p, field_width, precision, my_xstrtod(argument));
  140. }
  141. break;
  142. case 'c':
  143. printf(p, *argument);
  144. break;
  145. case 's':
  146. if (field_width < 0) {
  147. if (precision < 0)
  148. printf(p, argument);
  149. else
  150. printf(p, precision, argument);
  151. } else {
  152. if (precision < 0)
  153. printf(p, field_width, argument);
  154. else
  155. printf(p, field_width, precision, argument);
  156. }
  157. break;
  158. }
  159. free(p);
  160. }
  161. /* Print the text in FORMAT, using ARGV (with ARGC elements) for
  162. arguments to any '%' directives.
  163. Return the number of elements of ARGV used. */
  164. static int print_formatted(char *format, int argc, char **argv)
  165. {
  166. int save_argc = argc; /* Preserve original value. */
  167. char *f; /* Pointer into 'format'. */
  168. char *direc_start; /* Start of % directive. */
  169. size_t direc_length; /* Length of % directive. */
  170. int field_width; /* Arg to first '*', or -1 if none. */
  171. int precision; /* Arg to second '*', or -1 if none. */
  172. for (f = format; *f; ++f) {
  173. switch (*f) {
  174. case '%':
  175. direc_start = f++;
  176. direc_length = 1;
  177. field_width = precision = -1;
  178. if (*f == '%') {
  179. bb_putchar('%');
  180. break;
  181. }
  182. if (*f == 'b') {
  183. if (argc > 0) {
  184. print_esc_string(*argv);
  185. ++argv;
  186. --argc;
  187. }
  188. break;
  189. }
  190. if (strchr("-+ #", *f)) {
  191. ++f;
  192. ++direc_length;
  193. }
  194. if (*f == '*') {
  195. ++f;
  196. ++direc_length;
  197. if (argc > 0) {
  198. field_width = my_xstrtoul(*argv);
  199. ++argv;
  200. --argc;
  201. } else
  202. field_width = 0;
  203. } else {
  204. while (isdigit(*f)) {
  205. ++f;
  206. ++direc_length;
  207. }
  208. }
  209. if (*f == '.') {
  210. ++f;
  211. ++direc_length;
  212. if (*f == '*') {
  213. ++f;
  214. ++direc_length;
  215. if (argc > 0) {
  216. precision = my_xstrtoul(*argv);
  217. ++argv;
  218. --argc;
  219. } else
  220. precision = 0;
  221. } else
  222. while (isdigit(*f)) {
  223. ++f;
  224. ++direc_length;
  225. }
  226. }
  227. if (*f == 'l' || *f == 'L' || *f == 'h') {
  228. ++f;
  229. ++direc_length;
  230. }
  231. /*
  232. if (!strchr ("diouxXfeEgGcs", *f))
  233. fprintf(stderr, "%%%c: invalid directive", *f);
  234. */
  235. ++direc_length;
  236. if (argc > 0) {
  237. print_direc(direc_start, direc_length, field_width,
  238. precision, *argv);
  239. ++argv;
  240. --argc;
  241. } else
  242. print_direc(direc_start, direc_length, field_width,
  243. precision, "");
  244. break;
  245. case '\\':
  246. if (*++f == 'c')
  247. exit(0);
  248. bb_putchar(bb_process_escape_sequence((const char **)&f));
  249. f--;
  250. break;
  251. default:
  252. bb_putchar(*f);
  253. }
  254. }
  255. return save_argc - argc;
  256. }
  257. int printf_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  258. int printf_main(int argc, char **argv)
  259. {
  260. char *format;
  261. int args_used;
  262. if (argc <= 1 || argv[1][0] == '-') {
  263. bb_show_usage();
  264. }
  265. format = argv[1];
  266. argc -= 2;
  267. argv += 2;
  268. do {
  269. args_used = print_formatted(format, argc, argv);
  270. argc -= args_used;
  271. argv += args_used;
  272. } while (args_used > 0 && argc > 0);
  273. /* if (argc > 0)
  274. fprintf(stderr, "excess args ignored");
  275. */
  276. return EXIT_SUCCESS;
  277. }