aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--coreutils/wc.c62
1 files changed, 33 insertions, 29 deletions
diff --git a/coreutils/wc.c b/coreutils/wc.c
index 15c7692af..9f818ea72 100644
--- a/coreutils/wc.c
+++ b/coreutils/wc.c
@@ -26,9 +26,6 @@
26#include <stdlib.h> 26#include <stdlib.h>
27#include "busybox.h" 27#include "busybox.h"
28 28
29static int total_lines, total_words, total_chars, max_length;
30//static int print_lines, print_words, print_chars, print_length;
31static char print_type = 0;
32enum print_e { 29enum print_e {
33 print_lines = 1, 30 print_lines = 1,
34 print_words = 2, 31 print_words = 2,
@@ -36,8 +33,14 @@ enum print_e {
36 print_length = 8 33 print_length = 8
37}; 34};
38 35
39static void print_counts(int lines, int words, int chars, int length, 36static unsigned int total_lines = 0;
40 const char *name) 37static unsigned int total_words = 0;
38static unsigned int total_chars = 0;
39static unsigned int max_length = 0;
40static char print_type = 0;
41
42static void print_counts(const unsigned int lines, const unsigned int words,
43 const unsigned int chars, const unsigned int length, const char *name)
41{ 44{
42 if (print_type & print_lines) { 45 if (print_type & print_lines) {
43 printf("%7d ", lines); 46 printf("%7d ", lines);
@@ -59,11 +62,14 @@ static void print_counts(int lines, int words, int chars, int length,
59 62
60static void wc_file(FILE * file, const char *name) 63static void wc_file(FILE * file, const char *name)
61{ 64{
62 int lines, words, chars, length; 65 unsigned int lines = 0;
63 int in_word = 0, linepos = 0; 66 unsigned int words = 0;
64 int c; 67 unsigned int chars = 0;
68 unsigned int length = 0;
69 unsigned int linepos = 0;
70 char in_word = 0;
71 char c;
65 72
66 lines = words = chars = length = 0;
67 while ((c = getc(file)) != EOF) { 73 while ((c = getc(file)) != EOF) {
68 chars++; 74 chars++;
69 switch (c) { 75 switch (c) {
@@ -81,7 +87,7 @@ static void wc_file(FILE * file, const char *name)
81 case ' ': 87 case ' ':
82 linepos++; 88 linepos++;
83 case '\v': 89 case '\v':
84 word_separator: 90word_separator:
85 if (in_word) { 91 if (in_word) {
86 in_word = 0; 92 in_word = 0;
87 words++; 93 words++;
@@ -93,30 +99,27 @@ static void wc_file(FILE * file, const char *name)
93 break; 99 break;
94 } 100 }
95 } 101 }
96 if (linepos > length) 102 if (linepos > length) {
97 length = linepos; 103 length = linepos;
98 if (in_word) 104 }
105 if (in_word) {
99 words++; 106 words++;
107 }
100 print_counts(lines, words, chars, length, name); 108 print_counts(lines, words, chars, length, name);
101 total_lines += lines; 109 total_lines += lines;
102 total_words += words; 110 total_words += words;
103 total_chars += chars; 111 total_chars += chars;
104 if (length > max_length) 112 if (length > max_length) {
105 max_length = length; 113 max_length = length;
106 fclose(file); 114 }
107 fflush(stdout);
108} 115}
109 116
110int wc_main(int argc, char **argv) 117int wc_main(int argc, char **argv)
111{ 118{
112 FILE *file; 119 int opt;
113 unsigned int num_files_counted = 0;
114 int opt, status = EXIT_SUCCESS;
115
116 total_lines = total_words = total_chars = max_length = 0;
117 120
118 while ((opt = getopt(argc, argv, "clLw")) > 0) { 121 while ((opt = getopt(argc, argv, "clLw")) > 0) {
119 switch (opt) { 122 switch (opt) {
120 case 'c': 123 case 'c':
121 print_type |= print_chars; 124 print_type |= print_chars;
122 break; 125 break;
@@ -131,7 +134,7 @@ int wc_main(int argc, char **argv)
131 break; 134 break;
132 default: 135 default:
133 show_usage(); 136 show_usage();
134 } 137 }
135 } 138 }
136 139
137 if (print_type == 0) { 140 if (print_type == 0) {
@@ -140,8 +143,8 @@ int wc_main(int argc, char **argv)
140 143
141 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { 144 if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
142 wc_file(stdin, ""); 145 wc_file(stdin, "");
143 return EXIT_SUCCESS;
144 } else { 146 } else {
147 unsigned short num_files_counted = 0;
145 while (optind < argc) { 148 while (optind < argc) {
146 if (print_type == print_chars) { 149 if (print_type == print_chars) {
147 struct stat statbuf; 150 struct stat statbuf;
@@ -149,17 +152,18 @@ int wc_main(int argc, char **argv)
149 print_counts(0, 0, statbuf.st_size, 0, argv[optind]); 152 print_counts(0, 0, statbuf.st_size, 0, argv[optind]);
150 total_chars += statbuf.st_size; 153 total_chars += statbuf.st_size;
151 } else { 154 } else {
155 FILE *file;
152 file = xfopen(argv[optind], "r"); 156 file = xfopen(argv[optind], "r");
153 wc_file(file, argv[optind]); 157 wc_file(file, argv[optind]);
158 fclose(file);
154 } 159 }
155 num_files_counted++;
156 optind++; 160 optind++;
161 num_files_counted++;
162 }
163 if (num_files_counted > 1) {
164 print_counts(total_lines, total_words, total_chars, max_length, "total");
157 } 165 }
158 } 166 }
159 167
160 if (num_files_counted > 1) 168 return(EXIT_SUCCESS);
161 print_counts(total_lines, total_words, total_chars,
162 max_length, "total");
163
164 return status;
165} 169}