Use enums for selected functionality, Reduce the size by nearly 100 Bytes
[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         char const *space = "";
43
44         if (print_type & print_lines) {
45                 printf("%7d", lines);
46                 space = " ";
47         }
48         if (print_type & print_words) {
49                 printf("%s%7d", space, words);
50                 space = " ";
51         }
52         if (print_type & print_chars) {
53                 printf("%s%7d", space, chars);
54                 space = " ";
55         }
56         if (print_type & print_length)
57                 printf("%s%7d", space, length);
58         if (*name)
59                 printf(" %s", name);
60         putchar('\n');
61 }
62
63 static void wc_file(FILE * file, const char *name)
64 {
65         int lines, words, chars, length;
66         int in_word = 0, linepos = 0;
67         int c;
68
69         lines = words = chars = length = 0;
70         while ((c = getc(file)) != EOF) {
71                 chars++;
72                 switch (c) {
73                 case '\n':
74                         lines++;
75                 case '\r':
76                 case '\f':
77                         if (linepos > length)
78                                 length = linepos;
79                         linepos = 0;
80                         goto word_separator;
81                 case '\t':
82                         linepos += 8 - (linepos % 8);
83                         goto word_separator;
84                 case ' ':
85                         linepos++;
86                 case '\v':
87                   word_separator:
88                         if (in_word) {
89                                 in_word = 0;
90                                 words++;
91                         }
92                         break;
93                 default:
94                         linepos++;
95                         in_word = 1;
96                         break;
97                 }
98         }
99         if (linepos > length)
100                 length = linepos;
101         if (in_word)
102                 words++;
103         print_counts(lines, words, chars, length, name);
104         total_lines += lines;
105         total_words += words;
106         total_chars += chars;
107         if (length > max_length)
108                 max_length = length;
109         fclose(file);
110         fflush(stdout);
111 }
112
113 int wc_main(int argc, char **argv)
114 {
115         FILE *file;
116         unsigned int num_files_counted = 0;
117         int opt, status = EXIT_SUCCESS;
118
119         total_lines = total_words = total_chars = max_length = 0;
120
121         while ((opt = getopt(argc, argv, "clLw")) > 0) {
122                         switch (opt) {
123                         case 'c':
124                                 print_type |= print_chars;
125                                 break;
126                         case 'l':
127                                 print_type |= print_lines;
128                                 break;
129                         case 'L':
130                                 print_type |= print_length;
131                                 break;
132                         case 'w':
133                                 print_type |= print_words;
134                                 break;
135                         default:
136                                 show_usage();
137                         }
138         }
139
140         if (print_type == 0) {
141                 print_type = print_lines | print_words | print_chars;
142         }
143
144         if (argv[optind] == NULL || strcmp(argv[optind], "-") == 0) {
145                 wc_file(stdin, "");
146                 return EXIT_SUCCESS;
147         } else {
148                 while (optind < argc) {
149                         file = wfopen(argv[optind], "r");
150                         if (file != NULL)
151                                 wc_file(file, argv[optind]);
152                         else
153                                 status = EXIT_FAILURE;
154                         num_files_counted++;
155                         optind++;
156                 }
157         }
158
159         if (num_files_counted > 1)
160                 print_counts(total_lines, total_words, total_chars,
161                                          max_length, "total");
162
163         return status;
164 }