-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc.c
56 lines (51 loc) · 1.26 KB
/
wc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <unistd.h>
#include <wctype.h>
#define NEWLINE '\n'
/* returns an array of numbers
* [0] = lines
* [1] = words
* [2] = chars
*/
void word_count(FILE* f, int counts[]) {
/* if we don't init these, we get garbage */
counts[0] = counts[1] = counts[2] = 0;
int chr;
int in_word = 0;
while((chr = getc(f)) != EOF) {
counts[2]++;
if (chr == NEWLINE) counts[0]++;
if (iswspace(chr)) in_word = 1;
else if (in_word) {
in_word = 0;
counts[1]++;
}
}
}
int main(int argc, char *argv[]) {
int retval = 0;
/* if no params, use stdin */
if (argc == 1) {
int counts[3];
word_count(stdin, counts);
printf("\t%d\t%d\t%d\n", counts[0], counts[1], counts[2]);
} else {
int total_lines, total_words, total_chars, total_files;
total_lines = total_words = total_chars = total_files = 0;
for (int i = 1; i < argc; i++) {
int counts[3];
FILE* f = fopen(argv[i], "r");
word_count(f, counts);
total_lines += counts[0];
total_words += counts[1];
total_chars += counts[2];
total_files++;
fclose(f);
printf("\t%d\t%d\t%d\t%s\n", counts[0], counts[1], counts[2], argv[i]);
}
if (total_files > 1) {
printf("\t%d\t%d\t%d\ttotal (%d)\n", total_lines, total_words, total_chars, total_files);
}
}
return retval;
}