aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/wc.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/coreutils/wc.c b/coreutils/wc.c
index 695e7e7d4..03dd3c3af 100644
--- a/coreutils/wc.c
+++ b/coreutils/wc.c
@@ -27,26 +27,33 @@
27#include "busybox.h" 27#include "busybox.h"
28 28
29static int total_lines, total_words, total_chars, max_length; 29static int total_lines, total_words, total_chars, max_length;
30static int print_lines, print_words, print_chars, print_length; 30//static int print_lines, print_words, print_chars, print_length;
31static char print_type = 0;
32enum print_e {
33 print_lines = 1,
34 print_words = 2,
35 print_chars = 4,
36 print_length = 8
37};
31 38
32static void print_counts(int lines, int words, int chars, int length, 39static void print_counts(int lines, int words, int chars, int length,
33 const char *name) 40 const char *name)
34{ 41{
35 char const *space = ""; 42 char const *space = "";
36 43
37 if (print_lines) { 44 if (print_type & print_lines) {
38 printf("%7d", lines); 45 printf("%7d", lines);
39 space = " "; 46 space = " ";
40 } 47 }
41 if (print_words) { 48 if (print_type & print_words) {
42 printf("%s%7d", space, words); 49 printf("%s%7d", space, words);
43 space = " "; 50 space = " ";
44 } 51 }
45 if (print_chars) { 52 if (print_type & print_chars) {
46 printf("%s%7d", space, chars); 53 printf("%s%7d", space, chars);
47 space = " "; 54 space = " ";
48 } 55 }
49 if (print_length) 56 if (print_type & print_length)
50 printf("%s%7d", space, length); 57 printf("%s%7d", space, length);
51 if (*name) 58 if (*name)
52 printf(" %s", name); 59 printf(" %s", name);
@@ -110,36 +117,37 @@ int wc_main(int argc, char **argv)
110 int opt, status = EXIT_SUCCESS; 117 int opt, status = EXIT_SUCCESS;
111 118
112 total_lines = total_words = total_chars = max_length = 0; 119 total_lines = total_words = total_chars = max_length = 0;
113 print_lines = print_words = print_chars = print_length = 0;
114 120
115 while ((opt = getopt(argc, argv, "clLw")) > 0) { 121 while ((opt = getopt(argc, argv, "clLw")) > 0) {
116 switch (opt) { 122 switch (opt) {
117 case 'c': 123 case 'c':
118 print_chars = 1; 124 print_type |= print_chars;
119 break; 125 break;
120 case 'l': 126 case 'l':
121 print_lines = 1; 127 print_type |= print_lines;
122 break; 128 break;
123 case 'L': 129 case 'L':
124 print_length = 1; 130 print_type |= print_length;
125 break; 131 break;
126 case 'w': 132 case 'w':
127 print_words = 1; 133 print_type |= print_words;
128 break; 134 break;
129 default: 135 default:
130 show_usage(); 136 show_usage();
131 } 137 }
132 } 138 }
133 139
134 if (!print_lines && !print_words && !print_chars && !print_length) 140 if (print_type == 0) {
135 print_lines = print_words = print_chars = 1; 141 print_type = print_lines | print_words | print_chars;
142 }
136 143
137 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { 144 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
138 wc_file(stdin, ""); 145 wc_file(stdin, "");
139 return EXIT_SUCCESS; 146 return EXIT_SUCCESS;
140 } else { 147 } else {
141 while (optind < argc) { 148 while (optind < argc) {
142 if ((file = wfopen(argv[optind], "r")) != NULL) 149 file = wfopen(argv[optind], "r");
150 if (file != NULL)
143 wc_file(file, argv[optind]); 151 wc_file(file, argv[optind]);
144 else 152 else
145 status = EXIT_FAILURE; 153 status = EXIT_FAILURE;