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