wc.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * wc implementation for busybox
  4. *
  5. * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 _NOT_ compliant -- option -m is not currently supported. */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/wc.html */
  11. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  12. *
  13. * Rewritten to fix a number of problems and do some size optimizations.
  14. * Problems in the previous busybox implementation (besides bloat) included:
  15. * 1) broken 'wc -c' optimization (read note below)
  16. * 2) broken handling of '-' args
  17. * 3) no checking of ferror on EOF returns
  18. * 4) isprint() wasn't considered when word counting.
  19. *
  20. * TODO:
  21. *
  22. * When locale support is enabled, count multibyte chars in the '-m' case.
  23. *
  24. * NOTES:
  25. *
  26. * The previous busybox wc attempted an optimization using stat for the
  27. * case of counting chars only. I omitted that because it was broken.
  28. * It didn't take into account the possibility of input coming from a
  29. * pipe, or input from a file with file pointer not at the beginning.
  30. *
  31. * To implement such a speed optimization correctly, not only do you
  32. * need the size, but also the file position. Note also that the
  33. * file position may be past the end of file. Consider the example
  34. * (adapted from example in gnu wc.c)
  35. *
  36. * echo hello > /tmp/testfile &&
  37. * (dd ibs=1k skip=1 count=0 &> /dev/null ; wc -c) < /tmp/testfile
  38. *
  39. * for which 'wc -c' should output '0'.
  40. */
  41. #include "busybox.h"
  42. #ifdef CONFIG_LOCALE_SUPPORT
  43. #define isspace_given_isprint(c) isspace(c)
  44. #else
  45. #undef isspace
  46. #undef isprint
  47. #define isspace(c) ((((c) == ' ') || (((unsigned int)((c) - 9)) <= (13 - 9))))
  48. #define isprint(c) (((unsigned int)((c) - 0x20)) <= (0x7e - 0x20))
  49. #define isspace_given_isprint(c) ((c) == ' ')
  50. #endif
  51. #if ENABLE_FEATURE_WC_LARGE
  52. #define COUNT_T unsigned long long
  53. #define COUNT_FMT "llu"
  54. #else
  55. #define COUNT_T unsigned
  56. #define COUNT_FMT "u"
  57. #endif
  58. enum {
  59. WC_LINES = 0,
  60. WC_WORDS = 1,
  61. WC_CHARS = 2,
  62. WC_LENGTH = 3
  63. };
  64. int wc_main(int argc, char **argv)
  65. {
  66. FILE *fp;
  67. const char *s, *arg;
  68. const char *start_fmt = "%9"COUNT_FMT;
  69. const char *fname_fmt = " %s\n";
  70. COUNT_T *pcounts;
  71. COUNT_T counts[4];
  72. COUNT_T totals[4];
  73. unsigned linepos;
  74. unsigned u;
  75. int num_files = 0;
  76. int c;
  77. char status = EXIT_SUCCESS;
  78. char in_word;
  79. unsigned print_type;
  80. print_type = getopt32(argc, argv, "lwcL");
  81. if (print_type == 0) {
  82. print_type = (1 << WC_LINES) | (1 << WC_WORDS) | (1 << WC_CHARS);
  83. }
  84. argv += optind;
  85. if (!argv[0]) {
  86. *--argv = (char *) bb_msg_standard_input;
  87. fname_fmt = "\n";
  88. if (!((print_type-1) & print_type)) /* exactly one option? */
  89. start_fmt = "%"COUNT_FMT;
  90. }
  91. memset(totals, 0, sizeof(totals));
  92. pcounts = counts;
  93. while ((arg = *argv++) != 0) {
  94. ++num_files;
  95. fp = fopen_or_warn_stdin(arg);
  96. if (!fp) {
  97. status = EXIT_FAILURE;
  98. continue;
  99. }
  100. memset(counts, 0, sizeof(counts));
  101. linepos = 0;
  102. in_word = 0;
  103. do {
  104. /* Our -w doesn't match GNU wc exactly... oh well */
  105. ++counts[WC_CHARS];
  106. c = getc(fp);
  107. if (isprint(c)) {
  108. ++linepos;
  109. if (!isspace_given_isprint(c)) {
  110. in_word = 1;
  111. continue;
  112. }
  113. } else if (((unsigned int)(c - 9)) <= 4) {
  114. /* \t 9
  115. * \n 10
  116. * \v 11
  117. * \f 12
  118. * \r 13
  119. */
  120. if (c == '\t') {
  121. linepos = (linepos | 7) + 1;
  122. } else { /* '\n', '\r', '\f', or '\v' */
  123. DO_EOF:
  124. if (linepos > counts[WC_LENGTH]) {
  125. counts[WC_LENGTH] = linepos;
  126. }
  127. if (c == '\n') {
  128. ++counts[WC_LINES];
  129. }
  130. if (c != '\v') {
  131. linepos = 0;
  132. }
  133. }
  134. } else if (c == EOF) {
  135. if (ferror(fp)) {
  136. bb_perror_msg("%s", arg);
  137. status = EXIT_FAILURE;
  138. }
  139. --counts[WC_CHARS];
  140. goto DO_EOF; /* Treat an EOF as '\r'. */
  141. } else {
  142. continue;
  143. }
  144. counts[WC_WORDS] += in_word;
  145. in_word = 0;
  146. if (c == EOF) {
  147. break;
  148. }
  149. } while (1);
  150. if (totals[WC_LENGTH] < counts[WC_LENGTH]) {
  151. totals[WC_LENGTH] = counts[WC_LENGTH];
  152. }
  153. totals[WC_LENGTH] -= counts[WC_LENGTH];
  154. fclose_if_not_stdin(fp);
  155. OUTPUT:
  156. /* coreutils wc tries hard to print pretty columns
  157. * (saves results for all files, find max col len etc...)
  158. * we won't try that hard, it will bloat us too much */
  159. s = start_fmt;
  160. u = 0;
  161. do {
  162. if (print_type & (1 << u)) {
  163. printf(s, pcounts[u]);
  164. s = " %9"COUNT_FMT; /* Ok... restore the leading space. */
  165. }
  166. totals[u] += pcounts[u];
  167. } while (++u < 4);
  168. printf(fname_fmt, arg);
  169. }
  170. /* If more than one file was processed, we want the totals. To save some
  171. * space, we set the pcounts ptr to the totals array. This has the side
  172. * effect of trashing the totals array after outputting it, but that's
  173. * irrelavent since we no longer need it. */
  174. if (num_files > 1) {
  175. num_files = 0; /* Make sure we don't get here again. */
  176. arg = "total";
  177. pcounts = totals;
  178. --argv;
  179. goto OUTPUT;
  180. }
  181. fflush_stdout_and_exit(status);
  182. }