2011-05-23 01:36:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-02-01 03:06:06 +00:00
|
|
|
#include "utf.h"
|
2011-05-23 01:36:34 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-02-01 02:01:11 +00:00
|
|
|
static int lflag = 0;
|
|
|
|
static int wflag = 0;
|
|
|
|
static char cmode = 0;
|
|
|
|
static size_t tc = 0, tl = 0, tw = 0;
|
2011-05-23 01:36:34 +00:00
|
|
|
|
2015-03-07 12:33:39 +00:00
|
|
|
static void
|
2015-02-01 02:01:11 +00:00
|
|
|
output(const char *str, size_t nc, size_t nl, size_t nw)
|
|
|
|
{
|
|
|
|
int noflags = !cmode && !lflag && !wflag;
|
2015-02-06 19:46:37 +00:00
|
|
|
int first = 1;
|
2015-02-01 02:01:11 +00:00
|
|
|
|
|
|
|
if (lflag || noflags)
|
2015-04-01 00:15:17 +00:00
|
|
|
printf("%*.1zu", first ? (first = 0) : 7, nl);
|
2015-02-01 02:01:11 +00:00
|
|
|
if (wflag || noflags)
|
2015-04-01 00:15:17 +00:00
|
|
|
printf("%*.1zu", first ? (first = 0) : 7, nw);
|
2015-02-01 02:01:11 +00:00
|
|
|
if (cmode || noflags)
|
2015-04-01 00:15:17 +00:00
|
|
|
printf("%*.1zu", first ? (first = 0) : 7, nc);
|
2015-02-01 02:01:11 +00:00
|
|
|
if (str)
|
|
|
|
printf(" %s", str);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
2015-03-07 12:33:39 +00:00
|
|
|
static void
|
2015-02-01 02:01:11 +00:00
|
|
|
wc(FILE *fp, const char *str)
|
|
|
|
{
|
2015-02-01 10:22:11 +00:00
|
|
|
int word = 0, rlen;
|
2015-02-01 03:06:06 +00:00
|
|
|
Rune c;
|
2015-02-01 02:01:11 +00:00
|
|
|
size_t nc = 0, nl = 0, nw = 0;
|
|
|
|
|
2015-02-11 19:13:43 +00:00
|
|
|
while ((rlen = efgetrune(&c, fp, str))) {
|
2015-02-01 10:22:11 +00:00
|
|
|
nc += (cmode == 'c') ? rlen :
|
2015-02-01 03:06:06 +00:00
|
|
|
(c != Runeerror) ? 1 : 0;
|
2015-02-01 02:01:11 +00:00
|
|
|
if (c == '\n')
|
|
|
|
nl++;
|
2015-02-11 12:24:59 +00:00
|
|
|
if (!isspacerune(c))
|
2015-02-01 02:01:11 +00:00
|
|
|
word = 1;
|
|
|
|
else if (word) {
|
|
|
|
word = 0;
|
|
|
|
nw++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (word)
|
|
|
|
nw++;
|
|
|
|
tc += nc;
|
|
|
|
tl += nl;
|
|
|
|
tw += nw;
|
|
|
|
output(str, nc, nl, nw);
|
|
|
|
}
|
2011-05-23 01:36:34 +00:00
|
|
|
|
2013-10-07 16:13:01 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-02-01 02:01:11 +00:00
|
|
|
eprintf("usage: %s [-c | -m] [-lw] [file ...]\n", argv0);
|
2013-10-07 16:13:01 +00:00
|
|
|
}
|
|
|
|
|
2011-05-23 01:36:34 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-03-17 23:20:19 +00:00
|
|
|
int many;
|
2015-02-06 19:11:33 +00:00
|
|
|
int ret = 0;
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2013-03-11 00:12:10 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
|
|
|
cmode = 'c';
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
cmode = 'm';
|
|
|
|
break;
|
|
|
|
case 'l':
|
2014-11-13 20:24:47 +00:00
|
|
|
lflag = 1;
|
2013-03-11 00:12:10 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
2014-11-13 20:24:47 +00:00
|
|
|
wflag = 1;
|
2013-03-11 00:12:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-10-07 16:13:01 +00:00
|
|
|
usage();
|
2013-03-11 00:12:10 +00:00
|
|
|
} ARGEND;
|
2013-07-20 05:27:42 +00:00
|
|
|
|
2015-03-17 23:20:19 +00:00
|
|
|
if (!argc) {
|
2011-05-23 01:36:34 +00:00
|
|
|
wc(stdin, NULL);
|
2013-03-11 00:12:10 +00:00
|
|
|
} else {
|
2015-03-17 23:20:19 +00:00
|
|
|
for (many = (argc > 1); *argv; argc--, argv++) {
|
|
|
|
if (!(fp = fopen(*argv, "r"))) {
|
|
|
|
weprintf("fopen %s:", *argv);
|
2015-02-06 19:11:33 +00:00
|
|
|
ret = 1;
|
2013-11-13 11:39:24 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-03-17 23:20:19 +00:00
|
|
|
wc(fp, *argv);
|
2013-03-11 00:12:10 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2015-03-17 23:20:19 +00:00
|
|
|
if (many)
|
2013-03-11 00:12:10 +00:00
|
|
|
output("total", tc, tl, tw);
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
2015-03-17 23:20:19 +00:00
|
|
|
|
2015-02-06 19:11:33 +00:00
|
|
|
return ret;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|