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