More updates to the docs, and fixes to sync things with the docs.
[oweals/busybox.git] / coreutils / wc.c
1 /* vi: set sw=4 ts=4: */
2 /*
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
26 static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n"
27         "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\n"
29         "Options:\n"
30         "\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";
35
36 static int total_lines, total_words, total_chars, max_length;
37 static int print_lines, print_words, print_chars, print_length;
38
39 void print_counts(int lines, int words, int chars, int length,
40                                   const char *name)
41 {
42         char const *space = "";
43
44         if (print_lines) {
45                 printf("%7d", lines);
46                 space = " ";
47         }
48         if (print_words) {
49                 printf("%s%7d", space, words);
50                 space = " ";
51         }
52         if (print_chars) {
53                 printf("%s%7d", space, chars);
54                 space = " ";
55         }
56         if (print_length)
57                 printf("%s%7d", space, length);
58         if (*name)
59                 printf(" %s", name);
60         putchar('\n');
61 }
62
63 static void wc_file(FILE * file, const char *name)
64 {
65         int lines, words, chars, length;
66         int in_word = 0, linepos = 0;
67         int c;
68
69         lines = words = chars = length = 0;
70         while ((c = getc(file)) != EOF) {
71                 chars++;
72                 switch (c) {
73                 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;
97                 }
98         }
99         if (linepos > length)
100                 length = linepos;
101         if (in_word)
102                 words++;
103         print_counts(lines, words, chars, length, name);
104         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
113 int wc_main(int argc, char **argv)
114 {
115         FILE *file;
116
117         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) {
123                         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);
137                         }
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);
146         } else if (argc == 1) {
147                 file = fopen(*argv, "r");
148                 if (file == NULL) {
149                         perror(*argv);
150                         exit(FALSE);
151                 }
152                 wc_file(file, *argv);
153         } else {
154                 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                 }
163                 print_counts(total_lines, total_words, total_chars,
164                                          max_length, "total");
165         }
166         exit(TRUE);
167 }