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 | |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 29 | enum print_e { |
| 30 | print_lines = 1, |
| 31 | print_words = 2, |
| 32 | print_chars = 4, |
| 33 | print_length = 8 |
| 34 | }; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 35 | |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 36 | static unsigned int total_lines = 0; |
| 37 | static unsigned int total_words = 0; |
| 38 | static unsigned int total_chars = 0; |
| 39 | static unsigned int max_length = 0; |
| 40 | static char print_type = 0; |
| 41 | |
| 42 | static void print_counts(const unsigned int lines, const unsigned int words, |
| 43 | const unsigned int chars, const unsigned int length, const char *name) |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | { |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 45 | if (print_type & print_lines) { |
Glenn L McGrath | 1477ad8 | 2001-11-21 09:58:29 +0000 | [diff] [blame] | 46 | printf("%7d ", lines); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 47 | } |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 48 | if (print_type & print_words) { |
Glenn L McGrath | 1477ad8 | 2001-11-21 09:58:29 +0000 | [diff] [blame] | 49 | printf("%7d ", words); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 50 | } |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 51 | if (print_type & print_chars) { |
Glenn L McGrath | 1477ad8 | 2001-11-21 09:58:29 +0000 | [diff] [blame] | 52 | printf("%7d ", chars); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 53 | } |
Glenn L McGrath | 1477ad8 | 2001-11-21 09:58:29 +0000 | [diff] [blame] | 54 | if (print_type & print_length) { |
| 55 | printf("%7d ", length); |
| 56 | } |
| 57 | if (*name) { |
| 58 | printf("%s", name); |
| 59 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | putchar('\n'); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | static void wc_file(FILE * file, const char *name) |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 64 | { |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 65 | unsigned int lines = 0; |
| 66 | unsigned int words = 0; |
| 67 | unsigned int chars = 0; |
| 68 | unsigned int length = 0; |
| 69 | unsigned int linepos = 0; |
| 70 | char in_word = 0; |
Matt Kraai | 741f40b | 2001-12-11 16:06:02 +0000 | [diff] [blame^] | 71 | int c; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 72 | |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 73 | while ((c = getc(file)) != EOF) { |
| 74 | chars++; |
| 75 | switch (c) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | case '\n': |
| 77 | lines++; |
| 78 | case '\r': |
| 79 | case '\f': |
| 80 | if (linepos > length) |
| 81 | length = linepos; |
| 82 | linepos = 0; |
| 83 | goto word_separator; |
| 84 | case '\t': |
| 85 | linepos += 8 - (linepos % 8); |
| 86 | goto word_separator; |
| 87 | case ' ': |
| 88 | linepos++; |
| 89 | case '\v': |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 90 | word_separator: |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 91 | if (in_word) { |
| 92 | in_word = 0; |
| 93 | words++; |
| 94 | } |
| 95 | break; |
| 96 | default: |
| 97 | linepos++; |
| 98 | in_word = 1; |
| 99 | break; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 102 | if (linepos > length) { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 103 | length = linepos; |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 104 | } |
| 105 | if (in_word) { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 106 | words++; |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 107 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 108 | print_counts(lines, words, chars, length, name); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 109 | total_lines += lines; |
| 110 | total_words += words; |
| 111 | total_chars += chars; |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 112 | if (length > max_length) { |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 113 | max_length = length; |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 114 | } |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 117 | int wc_main(int argc, char **argv) |
| 118 | { |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 119 | int opt; |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 120 | |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 121 | while ((opt = getopt(argc, argv, "clLw")) > 0) { |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 122 | switch (opt) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | case 'c': |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 124 | print_type |= print_chars; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 125 | break; |
| 126 | case 'l': |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 127 | print_type |= print_lines; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 128 | break; |
| 129 | case 'L': |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 130 | print_type |= print_length; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | break; |
| 132 | case 'w': |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 133 | print_type |= print_words; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 134 | break; |
| 135 | default: |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 136 | show_usage(); |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 137 | } |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Glenn L McGrath | 02d090d | 2001-11-21 09:17:00 +0000 | [diff] [blame] | 140 | if (print_type == 0) { |
| 141 | print_type = print_lines | print_words | print_chars; |
| 142 | } |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 143 | |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 144 | if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) { |
Glenn L McGrath | c29ab97 | 2001-11-21 10:01:29 +0000 | [diff] [blame] | 145 | wc_file(stdin, ""); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 146 | } else { |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 147 | unsigned short num_files_counted = 0; |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 148 | while (optind < argc) { |
Glenn L McGrath | 74afa9a | 2001-11-21 10:26:28 +0000 | [diff] [blame] | 149 | if (print_type == print_chars) { |
| 150 | struct stat statbuf; |
| 151 | stat(argv[optind], &statbuf); |
| 152 | print_counts(0, 0, statbuf.st_size, 0, argv[optind]); |
| 153 | total_chars += statbuf.st_size; |
| 154 | } else { |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 155 | FILE *file; |
Glenn L McGrath | 74afa9a | 2001-11-21 10:26:28 +0000 | [diff] [blame] | 156 | file = xfopen(argv[optind], "r"); |
Eric Andersen | 7a86e61 | 2000-10-09 18:21:44 +0000 | [diff] [blame] | 157 | wc_file(file, argv[optind]); |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 158 | fclose(file); |
Glenn L McGrath | 74afa9a | 2001-11-21 10:26:28 +0000 | [diff] [blame] | 159 | } |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 160 | optind++; |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 161 | num_files_counted++; |
| 162 | } |
| 163 | if (num_files_counted > 1) { |
| 164 | print_counts(total_lines, total_words, total_chars, max_length, "total"); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 165 | } |
Mark Whitley | 3950596 | 2000-07-20 00:08:10 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Glenn L McGrath | 9e6c9f7 | 2001-11-21 12:46:36 +0000 | [diff] [blame] | 168 | return(EXIT_SUCCESS); |
Erik Andersen | 3163821 | 2000-01-15 22:28:50 +0000 | [diff] [blame] | 169 | } |