1 /* vi: set sw=4 ts=4: */
3 * Mini wc implementation for busybox
5 * by Edward Betts <edward@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n"
27 "Print line, word, and byte counts for each FILE, and a total line if\n"
28 "more than one FILE is specified. With no FILE, read standard input.\n"
29 "\t-c\tprint the byte counts\n"
30 "\t-l\tprint the newline counts\n"
32 "\t-L\tprint the length of the longest line\n"
33 "\t-w\tprint the word counts\n";
35 static int total_lines, total_words, total_chars, max_length;
36 static int print_lines, print_words, print_chars, print_length;
38 void print_counts(int lines, int words, int chars, int length,
41 char const *space = "";
48 printf("%s%7d", space, words);
52 printf("%s%7d", space, chars);
56 printf("%s%7d", space, length);
62 static void wc_file(FILE * file, const char *name)
64 int lines, words, chars, length;
65 int in_word = 0, linepos = 0;
68 lines = words = chars = length = 0;
69 while ((c = getc(file)) != EOF) {
81 linepos += 8 - (linepos % 8);
102 print_counts(lines, words, chars, length, name);
103 total_lines += lines;
104 total_words += words;
105 total_chars += chars;
106 if (length > max_length)
112 int wc_main(int argc, char **argv)
116 total_lines = total_words = total_chars = max_length = 0;
117 print_lines = print_words = print_chars = print_length = 0;
119 while (--argc && **(++argv) == '-') {
139 if (!print_lines && !print_words && !print_chars && !print_length)
140 print_lines = print_words = print_chars = 1;
145 } else if (argc == 1) {
146 file = fopen(*argv, "r");
151 wc_file(file, *argv);
153 while (argc-- > 0 && *argv != '\0' && strlen(*argv)) {
154 file = fopen(*argv, "r");
159 wc_file(file, *argv);
162 print_counts(total_lines, total_words, total_chars,
163 max_length, "total");