Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini wc implementation for busybox |
| 4 | * |
| 5 | * by Edward Betts <edward@debian.org> |
| 6 | * |
| 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. |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include "internal.h" |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n" |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 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" |
| 31 | |
| 32 | "\t-L\tprint the length of the longest line\n" |
| 33 | "\t-w\tprint the word counts\n"; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 34 | |
| 35 | static int total_lines, total_words, total_chars, max_length; |
| 36 | static int print_lines, print_words, print_chars, print_length; |
| 37 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | void print_counts(int lines, int words, int chars, int length, |
| 39 | const char *name) |
| 40 | { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 41 | char const *space = ""; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 43 | if (print_lines) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | printf("%7d", lines); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 45 | space = " "; |
| 46 | } |
| 47 | if (print_words) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 48 | printf("%s%7d", space, words); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 49 | space = " "; |
| 50 | } |
| 51 | if (print_chars) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | printf("%s%7d", space, chars); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 53 | space = " "; |
| 54 | } |
| 55 | if (print_length) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | printf("%s%7d", space, length); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 57 | if (*name) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | printf(" %s", name); |
| 59 | putchar('\n'); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 62 | static void wc_file(FILE * file, const char *name) |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 63 | { |
| 64 | int lines, words, chars, length; |
| 65 | int in_word = 0, linepos = 0; |
| 66 | int c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 67 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 68 | lines = words = chars = length = 0; |
| 69 | while ((c = getc(file)) != EOF) { |
| 70 | chars++; |
| 71 | switch (c) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | case '\n': |
| 73 | lines++; |
| 74 | case '\r': |
| 75 | case '\f': |
| 76 | if (linepos > length) |
| 77 | length = linepos; |
| 78 | linepos = 0; |
| 79 | goto word_separator; |
| 80 | case '\t': |
| 81 | linepos += 8 - (linepos % 8); |
| 82 | goto word_separator; |
| 83 | case ' ': |
| 84 | linepos++; |
| 85 | case '\v': |
| 86 | word_separator: |
| 87 | if (in_word) { |
| 88 | in_word = 0; |
| 89 | words++; |
| 90 | } |
| 91 | break; |
| 92 | default: |
| 93 | linepos++; |
| 94 | in_word = 1; |
| 95 | break; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | if (linepos > length) |
| 99 | length = linepos; |
| 100 | if (in_word) |
| 101 | words++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 102 | print_counts(lines, words, chars, length, name); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 103 | total_lines += lines; |
| 104 | total_words += words; |
| 105 | total_chars += chars; |
| 106 | if (length > max_length) |
| 107 | max_length = length; |
| 108 | fclose(file); |
| 109 | fflush(stdout); |
| 110 | } |
| 111 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | int wc_main(int argc, char **argv) |
| 113 | { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 114 | FILE *file; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 116 | total_lines = total_words = total_chars = max_length = 0; |
| 117 | print_lines = print_words = print_chars = print_length = 0; |
| 118 | |
| 119 | while (--argc && **(++argv) == '-') { |
| 120 | while (*++(*argv)) |
| 121 | switch (**argv) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 122 | case 'c': |
| 123 | print_chars = 1; |
| 124 | break; |
| 125 | case 'l': |
| 126 | print_lines = 1; |
| 127 | break; |
| 128 | case 'L': |
| 129 | print_length = 1; |
| 130 | break; |
| 131 | case 'w': |
| 132 | print_words = 1; |
| 133 | break; |
| 134 | default: |
| 135 | usage(wc_usage); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | if (!print_lines && !print_words && !print_chars && !print_length) |
| 140 | print_lines = print_words = print_chars = 1; |
| 141 | |
| 142 | if (argc == 0) { |
| 143 | wc_file(stdin, ""); |
| 144 | exit(TRUE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 145 | } else if (argc == 1) { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 146 | file = fopen(*argv, "r"); |
| 147 | if (file == NULL) { |
| 148 | perror(*argv); |
| 149 | exit(FALSE); |
| 150 | } |
| 151 | wc_file(file, *argv); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | } else { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 153 | while (argc-- > 0 && *argv != '\0' && strlen(*argv)) { |
| 154 | file = fopen(*argv, "r"); |
| 155 | if (file == NULL) { |
| 156 | perror(*argv); |
| 157 | exit(FALSE); |
| 158 | } |
| 159 | wc_file(file, *argv); |
| 160 | argv++; |
| 161 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 162 | print_counts(total_lines, total_words, total_chars, |
| 163 | max_length, "total"); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 164 | } |
| 165 | exit(TRUE); |
| 166 | } |