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