blob: 030afa9d60c92b8ab537ba71efd8770ba6061886 [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"
Erik Andersene5b6c7d2000-04-17 16:16:10 +000028 "more than one FILE is specified. With no FILE, read standard input.\n\n"
29 "Options:\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000030 "\t-c\tprint the byte counts\n"
31 "\t-l\tprint the newline counts\n"
32
33 "\t-L\tprint the length of the longest line\n"
34 "\t-w\tprint the word counts\n";
Erik Andersen31638212000-01-15 22:28:50 +000035
36static int total_lines, total_words, total_chars, max_length;
37static int print_lines, print_words, print_chars, print_length;
38
Erik Andersene49d5ec2000-02-08 19:58:47 +000039void print_counts(int lines, int words, int chars, int length,
40 const char *name)
41{
Erik Andersen31638212000-01-15 22:28:50 +000042 char const *space = "";
Erik Andersene49d5ec2000-02-08 19:58:47 +000043
Erik Andersen31638212000-01-15 22:28:50 +000044 if (print_lines) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000045 printf("%7d", lines);
Erik Andersen31638212000-01-15 22:28:50 +000046 space = " ";
47 }
48 if (print_words) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000049 printf("%s%7d", space, words);
Erik Andersen31638212000-01-15 22:28:50 +000050 space = " ";
51 }
52 if (print_chars) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 printf("%s%7d", space, chars);
Erik Andersen31638212000-01-15 22:28:50 +000054 space = " ";
55 }
56 if (print_length)
Erik Andersene49d5ec2000-02-08 19:58:47 +000057 printf("%s%7d", space, length);
Erik Andersen31638212000-01-15 22:28:50 +000058 if (*name)
Erik Andersene49d5ec2000-02-08 19:58:47 +000059 printf(" %s", name);
60 putchar('\n');
Erik Andersen31638212000-01-15 22:28:50 +000061}
62
Erik Andersene49d5ec2000-02-08 19:58:47 +000063static void wc_file(FILE * file, const char *name)
Erik Andersen31638212000-01-15 22:28:50 +000064{
65 int lines, words, chars, length;
66 int in_word = 0, linepos = 0;
67 int c;
Erik Andersene49d5ec2000-02-08 19:58:47 +000068
Erik Andersen31638212000-01-15 22:28:50 +000069 lines = words = chars = length = 0;
70 while ((c = getc(file)) != EOF) {
71 chars++;
72 switch (c) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 case '\n':
74 lines++;
75 case '\r':
76 case '\f':
77 if (linepos > length)
78 length = linepos;
79 linepos = 0;
80 goto word_separator;
81 case '\t':
82 linepos += 8 - (linepos % 8);
83 goto word_separator;
84 case ' ':
85 linepos++;
86 case '\v':
87 word_separator:
88 if (in_word) {
89 in_word = 0;
90 words++;
91 }
92 break;
93 default:
94 linepos++;
95 in_word = 1;
96 break;
Erik Andersen31638212000-01-15 22:28:50 +000097 }
98 }
99 if (linepos > length)
100 length = linepos;
101 if (in_word)
102 words++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000103 print_counts(lines, words, chars, length, name);
Erik Andersen31638212000-01-15 22:28:50 +0000104 total_lines += lines;
105 total_words += words;
106 total_chars += chars;
107 if (length > max_length)
108 max_length = length;
109 fclose(file);
110 fflush(stdout);
111}
112
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113int wc_main(int argc, char **argv)
114{
Erik Andersen31638212000-01-15 22:28:50 +0000115 FILE *file;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000116
Erik Andersen31638212000-01-15 22:28:50 +0000117 total_lines = total_words = total_chars = max_length = 0;
118 print_lines = print_words = print_chars = print_length = 0;
119
120 while (--argc && **(++argv) == '-') {
121 while (*++(*argv))
122 switch (**argv) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000123 case 'c':
124 print_chars = 1;
125 break;
126 case 'l':
127 print_lines = 1;
128 break;
129 case 'L':
130 print_length = 1;
131 break;
132 case 'w':
133 print_words = 1;
134 break;
135 default:
136 usage(wc_usage);
Erik Andersen31638212000-01-15 22:28:50 +0000137 }
138 }
139
140 if (!print_lines && !print_words && !print_chars && !print_length)
141 print_lines = print_words = print_chars = 1;
142
143 if (argc == 0) {
144 wc_file(stdin, "");
145 exit(TRUE);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000146 } else if (argc == 1) {
Erik Andersen31638212000-01-15 22:28:50 +0000147 file = fopen(*argv, "r");
148 if (file == NULL) {
149 perror(*argv);
150 exit(FALSE);
151 }
152 wc_file(file, *argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000153 } else {
Erik Andersen31638212000-01-15 22:28:50 +0000154 while (argc-- > 0 && *argv != '\0' && strlen(*argv)) {
155 file = fopen(*argv, "r");
156 if (file == NULL) {
157 perror(*argv);
158 exit(FALSE);
159 }
160 wc_file(file, *argv);
161 argv++;
162 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000163 print_counts(total_lines, total_words, total_chars,
164 max_length, "total");
Erik Andersen31638212000-01-15 22:28:50 +0000165 }
166 exit(TRUE);
167}