Sync up busybox with the latest and greatest. This is not stuff for
[oweals/busybox.git] / coreutils / wc.c
1 /*
2  * Mini wc implementation for busybox
3  *
4  * by Edward Betts <edward@debian.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21
22 #include "internal.h"
23 #include <stdio.h>
24
25 static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n"
26 "Print line, word, and byte counts for each FILE, and a total line if\n"
27 "more than one FILE is specified.  With no FILE, read standard input.\n"
28 "\t-c\tprint the byte counts\n"
29 "\t-l\tprint the newline counts\n"
30 "\t-L\tprint the length of the longest line\n"
31 "\t-L\tprint the length of the longest line\n"
32 "\t-w\tprint the word counts\n";
33
34 static int total_lines, total_words, total_chars, max_length;
35 static int print_lines, print_words, print_chars, print_length;
36
37 void print_counts (int lines, int words, int chars, int length, 
38                 const char *name) {
39         char const *space = "";
40         if (print_lines) {
41                 printf ("%7d", lines);
42                 space = " ";
43         }
44         if (print_words) {
45                 printf ("%s%7d", space, words);
46                 space = " ";
47         }
48         if (print_chars) {
49                 printf ("%s%7d", space, chars);
50                 space = " ";
51         }
52         if (print_length)
53                 printf ("%s%7d", space, length);
54         if (*name)
55                 printf (" %s", name);
56         putchar ('\n');
57 }
58
59 static void wc_file(FILE *file, const char *name) 
60 {
61         int lines, words, chars, length;
62         int in_word = 0, linepos = 0;
63         int c;
64         lines = words = chars = length = 0;
65         while ((c = getc(file)) != EOF) {
66                 chars++;
67                 switch (c) {
68                         case '\n': 
69                                 lines++;
70                         case '\r':
71                         case '\f': 
72                                 if (linepos > length)
73                                         length = linepos;
74                                 linepos = 0;
75                                 goto word_separator;
76                         case '\t':
77                                 linepos += 8 - (linepos % 8);
78                                 goto word_separator;
79                         case ' ':
80                                 linepos++;
81                         case '\v':
82                         word_separator:
83                                 if (in_word) {
84                                         in_word = 0;
85                                         words++;
86                                 }
87                                 break;
88                         default:
89                                 linepos++;
90                                 in_word = 1;
91                                 break;
92                 }
93         }
94         if (linepos > length)
95                 length = linepos;
96         if (in_word)
97                 words++;
98         print_counts (lines, words, chars, length, name);
99         total_lines += lines;
100         total_words += words;
101         total_chars += chars;
102         if (length > max_length)
103                 max_length = length;
104         fclose(file);
105         fflush(stdout);
106 }
107
108 int wc_main(int argc, char **argv) {
109         FILE *file;
110         total_lines = total_words = total_chars = max_length = 0;
111         print_lines = print_words = print_chars = print_length = 0;
112
113         while (--argc && **(++argv) == '-') {
114                 while (*++(*argv))
115                         switch (**argv) {
116                                 case 'c':
117                                         print_chars = 1;
118                                         break;
119                                 case 'l':
120                                         print_lines = 1;
121                                         break;
122                                 case 'L':
123                                         print_length = 1;
124                                         break;
125                                 case 'w':
126                                         print_words = 1;
127                                         break;
128                                 default:
129                                         usage (wc_usage);
130                         }
131         }
132
133         if (!print_lines && !print_words && !print_chars && !print_length)
134                 print_lines = print_words = print_chars = 1;
135
136         if (argc == 0) {
137                 wc_file(stdin, "");
138                 exit(TRUE);
139         }
140         else if (argc == 1) {
141                 file = fopen(*argv, "r");
142                 if (file == NULL) {
143                         perror(*argv);
144                         exit(FALSE);
145                 }
146                 wc_file(file, *argv);
147         }
148         else {
149                 while (argc-- > 0 && *argv != '\0' && strlen(*argv)) {
150                         file = fopen(*argv, "r");
151                         if (file == NULL) {
152                                 perror(*argv);
153                                 exit(FALSE);
154                         }
155                         wc_file(file, *argv);
156                         argv++;
157                 }
158                 print_counts (total_lines, total_words, total_chars, 
159                                 max_length, "total");
160         }
161         exit(TRUE);
162 }