02e2b2aa661b99b81f4ca33fab0d61b49ffe2939
[platform/upstream/busybox.git] / 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 #include <getopt.h>
26
27 static int total_lines, total_words, total_chars, max_length;
28 static int print_lines, print_words, print_chars, print_length;
29
30 void print_counts(int lines, int words, int chars, int length,
31                                   const char *name)
32 {
33         char const *space = "";
34
35         if (print_lines) {
36                 printf("%7d", lines);
37                 space = " ";
38         }
39         if (print_words) {
40                 printf("%s%7d", space, words);
41                 space = " ";
42         }
43         if (print_chars) {
44                 printf("%s%7d", space, chars);
45                 space = " ";
46         }
47         if (print_length)
48                 printf("%s%7d", space, length);
49         if (*name)
50                 printf(" %s", name);
51         putchar('\n');
52 }
53
54 static void wc_file(FILE * file, const char *name)
55 {
56         int lines, words, chars, length;
57         int in_word = 0, linepos = 0;
58         int c;
59
60         lines = words = chars = length = 0;
61         while ((c = getc(file)) != EOF) {
62                 chars++;
63                 switch (c) {
64                 case '\n':
65                         lines++;
66                 case '\r':
67                 case '\f':
68                         if (linepos > length)
69                                 length = linepos;
70                         linepos = 0;
71                         goto word_separator;
72                 case '\t':
73                         linepos += 8 - (linepos % 8);
74                         goto word_separator;
75                 case ' ':
76                         linepos++;
77                 case '\v':
78                   word_separator:
79                         if (in_word) {
80                                 in_word = 0;
81                                 words++;
82                         }
83                         break;
84                 default:
85                         linepos++;
86                         in_word = 1;
87                         break;
88                 }
89         }
90         if (linepos > length)
91                 length = linepos;
92         if (in_word)
93                 words++;
94         print_counts(lines, words, chars, length, name);
95         total_lines += lines;
96         total_words += words;
97         total_chars += chars;
98         if (length > max_length)
99                 max_length = length;
100         fclose(file);
101         fflush(stdout);
102 }
103
104 int wc_main(int argc, char **argv)
105 {
106         FILE *file;
107         unsigned int num_files_counted = 0;
108         int opt;
109
110         total_lines = total_words = total_chars = max_length = 0;
111         print_lines = print_words = print_chars = print_length = 0;
112
113         while ((opt = getopt(argc, argv, "clLw")) > 0) {
114                         switch (opt) {
115                         case 'c':
116                                 print_chars = 1;
117                                 break;
118                         case 'l':
119                                 print_lines = 1;
120                                 break;
121                         case 'L':
122                                 print_length = 1;
123                                 break;
124                         case 'w':
125                                 print_words = 1;
126                                 break;
127                         default:
128                                 usage(wc_usage);
129                         }
130         }
131
132         if (!print_lines && !print_words && !print_chars && !print_length)
133                 print_lines = print_words = print_chars = 1;
134
135         if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
136                 wc_file(stdin, "");
137                 exit(TRUE);
138         } else {
139                 while (optind < argc) {
140                         file = fopen(argv[optind], "r");
141                         if (file == NULL) {
142                                 fatalError(argv[optind]);
143                         }
144                         wc_file(file, argv[optind]);
145                         num_files_counted++;
146                         optind++;
147                 }
148         }
149
150         if (num_files_counted > 1)
151                 print_counts(total_lines, total_words, total_chars,
152                                          max_length, "total");
153
154         return 0 ;
155 }