blob: 8004e629445e6ecf16c36961797b464a7a1a150a [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen31638212000-01-15 22:28:50 +00002/*
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
26static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000027 "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 Andersen31638212000-01-15 22:28:50 +000034
35static int total_lines, total_words, total_chars, max_length;
36static int print_lines, print_words, print_chars, print_length;
37
Erik Andersene49d5ec2000-02-08 19:58:47 +000038void print_counts(int lines, int words, int chars, int length,
39 const char *name)
40{
Erik Andersen31638212000-01-15 22:28:50 +000041 char const *space = "";
Erik Andersene49d5ec2000-02-08 19:58:47 +000042
Erik Andersen31638212000-01-15 22:28:50 +000043 if (print_lines) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 printf("%7d", lines);
Erik Andersen31638212000-01-15 22:28:50 +000045 space = " ";
46 }
47 if (print_words) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000048 printf("%s%7d", space, words);
Erik Andersen31638212000-01-15 22:28:50 +000049 space = " ";
50 }
51 if (print_chars) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000052 printf("%s%7d", space, chars);
Erik Andersen31638212000-01-15 22:28:50 +000053 space = " ";
54 }
55 if (print_length)
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 printf("%s%7d", space, length);
Erik Andersen31638212000-01-15 22:28:50 +000057 if (*name)
Erik Andersene49d5ec2000-02-08 19:58:47 +000058 printf(" %s", name);
59 putchar('\n');
Erik Andersen31638212000-01-15 22:28:50 +000060}
61
Erik Andersene49d5ec2000-02-08 19:58:47 +000062static void wc_file(FILE * file, const char *name)
Erik Andersen31638212000-01-15 22:28:50 +000063{
64 int lines, words, chars, length;
65 int in_word = 0, linepos = 0;
66 int c;
Erik Andersene49d5ec2000-02-08 19:58:47 +000067
Erik Andersen31638212000-01-15 22:28:50 +000068 lines = words = chars = length = 0;
69 while ((c = getc(file)) != EOF) {
70 chars++;
71 switch (c) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 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 Andersen31638212000-01-15 22:28:50 +000096 }
97 }
98 if (linepos > length)
99 length = linepos;
100 if (in_word)
101 words++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 print_counts(lines, words, chars, length, name);
Erik Andersen31638212000-01-15 22:28:50 +0000103 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 Andersene49d5ec2000-02-08 19:58:47 +0000112int wc_main(int argc, char **argv)
113{
Erik Andersen31638212000-01-15 22:28:50 +0000114 FILE *file;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000115
Erik Andersen31638212000-01-15 22:28:50 +0000116 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 Andersene49d5ec2000-02-08 19:58:47 +0000122 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 Andersen31638212000-01-15 22:28:50 +0000136 }
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 Andersene49d5ec2000-02-08 19:58:47 +0000145 } else if (argc == 1) {
Erik Andersen31638212000-01-15 22:28:50 +0000146 file = fopen(*argv, "r");
147 if (file == NULL) {
148 perror(*argv);
149 exit(FALSE);
150 }
151 wc_file(file, *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000152 } else {
Erik Andersen31638212000-01-15 22:28:50 +0000153 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 Andersene49d5ec2000-02-08 19:58:47 +0000162 print_counts(total_lines, total_words, total_chars,
163 max_length, "total");
Erik Andersen31638212000-01-15 22:28:50 +0000164 }
165 exit(TRUE);
166}