summaryrefslogtreecommitdiffstats
path: root/tests/langbench/wc.l
blob: f24ec07d8f558525334bb74bdbbc6f85a2be686e (plain)
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
string []
wordsplit(string str)
{
	string	chars[];
	string	list[];
	string	c, word;
	int	i;

	word = "";
	chars = split(str, "");
	foreach (c in chars) {
		if (string("is", "space", c)) {
			if (length(word) > 0) {
				push(&list, word);
			}
			word = "";
		} else {
			append(&word, c);
		}
	}
	if (length(word) > 0) {
		push(&list, word);
	}
	return (list);
}

int
doit(string file)
{
	FILE	f = open(file, "rb");
	string	buf;
	string	words[];
	int	n;

	while (gets(f, &buf) >= 0) {
		words = wordsplit(buf);
		n += llength(words);
	}
	close(f);
	return (n);
}

void
main(int ac, string av[])
{
	int	total, i;

	for (i = 1; i < ac; ++i) {
		total += doit(av[i]);
	}
	printf("%d\n", total);
}