When just counting chars of a file stat file instead of reading the whole file (Fixes...
[platform/upstream/busybox.git] / coreutils / wc.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini wc implementation for busybox
4  *
5  * Copyright (C) 2000  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 <stdio.h>
24 #include <getopt.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "busybox.h"
28
29 static int total_lines, total_words, total_chars, max_length;
30 //static int print_lines, print_words, print_chars, print_length;
31 static char print_type = 0;
32 enum print_e {
33         print_lines = 1,
34         print_words = 2,
35         print_chars = 4,
36         print_length = 8
37 };
38
39 static void print_counts(int lines, int words, int chars, int length,
40                                                  const char *name)
41 {
42         if (print_type & print_lines) {
43                 printf("%7d ", lines);
44         }
45         if (print_type & print_words) {
46                 printf("%7d ", words);
47         }
48         if (print_type & print_chars) {
49                 printf("%7d ", chars);
50         }
51         if (print_type & print_length) {
52                 printf("%7d ", length);
53         }
54         if (*name) {
55                 printf("%s", name);
56         }
57         putchar('\n');
58 }
59
60 static void wc_file(FILE * file, const char *name)
61 {
62         int lines, words, chars, length;
63         int in_word = 0, linepos = 0;
64         int c;
65
66         lines = words = chars = length = 0;
67         while ((c = getc(file)) != EOF) {
68                 chars++;
69                 switch (c) {
70                 case '\n':
71                         lines++;
72                 case '\r':
73                 case '\f':
74                         if (linepos > length)
75                                 length = linepos;
76                         linepos = 0;
77                         goto word_separator;
78                 case '\t':
79                         linepos += 8 - (linepos % 8);
80                         goto word_separator;
81                 case ' ':
82                         linepos++;
83                 case '\v':
84                   word_separator:
85                         if (in_word) {
86                                 in_word = 0;
87                                 words++;
88                         }
89                         break;
90                 default:
91                         linepos++;
92                         in_word = 1;
93                         break;
94                 }
95         }
96         if (linepos > length)
97                 length = linepos;
98         if (in_word)
99                 words++;
100         print_counts(lines, words, chars, length, name);
101         total_lines += lines;
102         total_words += words;
103         total_chars += chars;
104         if (length > max_length)
105                 max_length = length;
106         fclose(file);
107         fflush(stdout);
108 }
109
110 int wc_main(int argc, char **argv)
111 {
112         FILE *file;
113         unsigned int num_files_counted = 0;
114         int opt, status = EXIT_SUCCESS;
115
116         total_lines = total_words = total_chars = max_length = 0;
117
118         while ((opt = getopt(argc, argv, "clLw")) > 0) {
119                         switch (opt) {
120                         case 'c':
121                                 print_type |= print_chars;
122                                 break;
123                         case 'l':
124                                 print_type |= print_lines;
125                                 break;
126                         case 'L':
127                                 print_type |= print_length;
128                                 break;
129                         case 'w':
130                                 print_type |= print_words;
131                                 break;
132                         default:
133                                 show_usage();
134                         }
135         }
136
137         if (print_type == 0) {
138                 print_type = print_lines | print_words | print_chars;
139         }
140
141         if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
142                 wc_file(stdin, "");
143                 return EXIT_SUCCESS;
144         } else {
145                 while (optind < argc) {
146                         if (print_type == print_chars) {
147                                 struct stat statbuf;
148                                 stat(argv[optind], &statbuf);
149                                 print_counts(0, 0, statbuf.st_size, 0, argv[optind]);
150                                 total_chars += statbuf.st_size;
151                         } else {
152                                 file = xfopen(argv[optind], "r");
153                                 wc_file(file, argv[optind]);
154                         }
155                         num_files_counted++;
156                         optind++;
157                 }
158         }
159
160         if (num_files_counted > 1)
161                 print_counts(total_lines, total_words, total_chars,
162                                          max_length, "total");
163
164         return status;
165 }