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 | * |
Eric Andersen | 1b355eb | 2000-09-05 17:37:48 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 Edward Betts <edward@debian.org> |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 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 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 24 | #include <getopt.h> |
Eric Andersen | eba8ed7 | 2001-03-09 14:36:42 +0000 | [diff] [blame] | 25 | #include <string.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 27 | #include "busybox.h" |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 28 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 29 | static int total_lines, total_words, total_chars, max_length; |
| 30 | static int print_lines, print_words, print_chars, print_length; |
| 31 | |
Eric Andersen | 3e6ff90 | 2001-03-09 21:24:12 +0000 | [diff] [blame] | 32 | static void print_counts(int lines, int words, int chars, int length, |
| 33 | const char *name) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 35 | char const *space = ""; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 36 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 37 | if (print_lines) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 38 | printf("%7d", lines); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 39 | space = " "; |
| 40 | } |
| 41 | if (print_words) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 42 | printf("%s%7d", space, words); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 43 | space = " "; |
| 44 | } |
| 45 | if (print_chars) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 46 | printf("%s%7d", space, chars); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 47 | space = " "; |
| 48 | } |
| 49 | if (print_length) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 50 | printf("%s%7d", space, length); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 51 | if (*name) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 52 | printf(" %s", name); |
| 53 | putchar('\n'); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 56 | static void wc_file(FILE * file, const char *name) |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 57 | { |
| 58 | int lines, words, chars, length; |
| 59 | int in_word = 0, linepos = 0; |
| 60 | int c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 62 | lines = words = chars = length = 0; |
| 63 | while ((c = getc(file)) != EOF) { |
| 64 | chars++; |
| 65 | switch (c) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 66 | case '\n': |
| 67 | lines++; |
| 68 | case '\r': |
| 69 | case '\f': |
| 70 | if (linepos > length) |
| 71 | length = linepos; |
| 72 | linepos = 0; |
| 73 | goto word_separator; |
| 74 | case '\t': |
| 75 | linepos += 8 - (linepos % 8); |
| 76 | goto word_separator; |
| 77 | case ' ': |
| 78 | linepos++; |
| 79 | case '\v': |
| 80 | word_separator: |
| 81 | if (in_word) { |
| 82 | in_word = 0; |
| 83 | words++; |
| 84 | } |
| 85 | break; |
| 86 | default: |
| 87 | linepos++; |
| 88 | in_word = 1; |
| 89 | break; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | if (linepos > length) |
| 93 | length = linepos; |
| 94 | if (in_word) |
| 95 | words++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | print_counts(lines, words, chars, length, name); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 97 | total_lines += lines; |
| 98 | total_words += words; |
| 99 | total_chars += chars; |
| 100 | if (length > max_length) |
| 101 | max_length = length; |
| 102 | fclose(file); |
| 103 | fflush(stdout); |
| 104 | } |
| 105 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | int wc_main(int argc, char **argv) |
| 107 | { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 108 | FILE *file; |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 109 | unsigned int num_files_counted = 0; |
Eric Andersen | 7a86e61 | 2000-10-09 18:21:44 +0000 | [diff] [blame] | 110 | int opt, status = EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 112 | total_lines = total_words = total_chars = max_length = 0; |
| 113 | print_lines = print_words = print_chars = print_length = 0; |
| 114 | |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 115 | while ((opt = getopt(argc, argv, "clLw")) > 0) { |
| 116 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | case 'c': |
| 118 | print_chars = 1; |
| 119 | break; |
| 120 | case 'l': |
| 121 | print_lines = 1; |
| 122 | break; |
| 123 | case 'L': |
| 124 | print_length = 1; |
| 125 | break; |
| 126 | case 'w': |
| 127 | print_words = 1; |
| 128 | break; |
| 129 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 130 | show_usage(); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| 134 | if (!print_lines && !print_words && !print_chars && !print_length) |
| 135 | print_lines = print_words = print_chars = 1; |
| 136 | |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 137 | if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 138 | wc_file(stdin, ""); |
Matt Kraai | bbaef66 | 2000-09-27 02:43:35 +0000 | [diff] [blame] | 139 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 140 | } else { |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 141 | while (optind < argc) { |
Eric Andersen | 7a86e61 | 2000-10-09 18:21:44 +0000 | [diff] [blame] | 142 | if ((file = wfopen(argv[optind], "r")) != NULL) |
| 143 | wc_file(file, argv[optind]); |
| 144 | else |
| 145 | status = EXIT_FAILURE; |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 146 | num_files_counted++; |
| 147 | optind++; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 148 | } |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (num_files_counted > 1) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | print_counts(total_lines, total_words, total_chars, |
| 153 | max_length, "total"); |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 154 | |
Eric Andersen | 7a86e61 | 2000-10-09 18:21:44 +0000 | [diff] [blame] | 155 | return status; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 156 | } |