summaryrefslogtreecommitdiff
path: root/util/sbase/wc.c
blob: c89d19f8d90073ca99462a06e679ab1816dfbd5c (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/* See LICENSE file for copyright and license details. */
#include <string.h>

#include "utf.h"
#include "util.h"

static int    lflag = 0;
static int    wflag = 0;
static char   cmode = 0;
static size_t tc = 0, tl = 0, tw = 0;

static void
output(const char *str, size_t nc, size_t nl, size_t nw)
{
	int first = 1;

	if (lflag) {
		first = 0;
		printf("%zu", nl);
	}
	if (wflag) {
		if (!first)
			putchar(' ');
		first = 0;
		printf("%zu", nw);
	}
	if (cmode) {
		if (!first)
			putchar(' ');
		printf("%zu", nc);
	}
	if (str)
		printf(" %s", str);
	putchar('\n');
}

static void
wc(FILE *fp, const char *str)
{
	int word = 0, rlen;
	Rune c;
	size_t nc = 0, nl = 0, nw = 0;

	while ((rlen = efgetrune(&c, fp, str))) {
		nc += (cmode == 'c') ? rlen : (c != Runeerror);
		if (c == '\n')
			nl++;
		if (!isspacerune(c))
			word = 1;
		else if (word) {
			word = 0;
			nw++;
		}
	}
	if (word)
		nw++;
	tc += nc;
	tl += nl;
	tw += nw;
	output(str, nc, nl, nw);
}

static void
usage(void)
{
	eprintf("usage: %s [-c | -m] [-lw] [file ...]\n", argv0);
}

int
main(int argc, char *argv[])
{
	FILE *fp;
	int many;
	int ret = 0;

	ARGBEGIN {
	case 'c':
		cmode = 'c';
		break;
	case 'm':
		cmode = 'm';
		break;
	case 'l':
		lflag = 1;
		break;
	case 'w':
		wflag = 1;
		break;
	default:
		usage();
	} ARGEND

	if (!lflag && !wflag && !cmode) {
		cmode = 'c';
		lflag = 1;
		wflag = 1;
	}

	if (!argc) {
		wc(stdin, NULL);
	} else {
		for (many = (argc > 1); *argv; argc--, argv++) {
			if (!strcmp(*argv, "-")) {
				*argv = "<stdin>";
				fp = stdin;
			} else if (!(fp = fopen(*argv, "r"))) {
				weprintf("fopen %s:", *argv);
				ret = 1;
				continue;
			}
			wc(fp, *argv);
			if (fp != stdin && fshut(fp, *argv))
				ret = 1;
		}
		if (many)
			output("total", tc, tl, tw);
	}

	ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");

	return ret;
}