2 * Mini wc implementation for busybox
4 * by Edward Betts <edward@debian.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n"
26 "Print line, word, and byte counts for each FILE, and a total line if\n"
27 "more than one FILE is specified. With no FILE, read standard input.\n"
28 "\t-c\tprint the byte counts\n"
29 "\t-l\tprint the newline counts\n"
30 "\t-L\tprint the length of the longest line\n"
31 "\t-L\tprint the length of the longest line\n"
32 "\t-w\tprint the word counts\n";
34 static int total_lines, total_words, total_chars, max_length;
35 static int print_lines, print_words, print_chars, print_length;
37 void print_counts (int lines, int words, int chars, int length,
39 char const *space = "";
41 printf ("%7d", lines);
45 printf ("%s%7d", space, words);
49 printf ("%s%7d", space, chars);
53 printf ("%s%7d", space, length);
59 static void wc_file(FILE *file, const char *name)
61 int lines, words, chars, length;
62 int in_word = 0, linepos = 0;
64 lines = words = chars = length = 0;
65 while ((c = getc(file)) != EOF) {
77 linepos += 8 - (linepos % 8);
98 print_counts (lines, words, chars, length, name);
100 total_words += words;
101 total_chars += chars;
102 if (length > max_length)
108 int wc_main(int argc, char **argv) {
110 total_lines = total_words = total_chars = max_length = 0;
111 print_lines = print_words = print_chars = print_length = 0;
113 while (--argc && **(++argv) == '-') {
133 if (!print_lines && !print_words && !print_chars && !print_length)
134 print_lines = print_words = print_chars = 1;
140 else if (argc == 1) {
141 file = fopen(*argv, "r");
146 wc_file(file, *argv);
149 while (argc-- > 0 && *argv != '\0' && strlen(*argv)) {
150 file = fopen(*argv, "r");
155 wc_file(file, *argv);
158 print_counts (total_lines, total_words, total_chars,
159 max_length, "total");