2011-05-22 21:36:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-05-23 20:13:34 -04:00
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-01-31 21:01:11 -05:00
|
|
|
static int lflag = 0;
|
|
|
|
static int wflag = 0;
|
|
|
|
static char cmode = 0;
|
|
|
|
static size_t tc = 0, tl = 0, tw = 0;
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2015-01-31 21:01:11 -05:00
|
|
|
void
|
|
|
|
output(const char *str, size_t nc, size_t nl, size_t nw)
|
|
|
|
{
|
|
|
|
int noflags = !cmode && !lflag && !wflag;
|
|
|
|
|
|
|
|
if (lflag || noflags)
|
|
|
|
printf(" %5zu", nl);
|
|
|
|
if (wflag || noflags)
|
|
|
|
printf(" %5zu", nw);
|
|
|
|
if (cmode || noflags)
|
|
|
|
printf(" %5zu", nc);
|
|
|
|
if (str)
|
|
|
|
printf(" %s", str);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wc(FILE *fp, const char *str)
|
|
|
|
{
|
|
|
|
int word = 0;
|
|
|
|
int c;
|
|
|
|
size_t nc = 0, nl = 0, nw = 0;
|
|
|
|
|
|
|
|
while ((c = getc(fp)) != EOF) {
|
|
|
|
if (cmode != 'm' || UTF8_POINT(c))
|
|
|
|
nc++;
|
|
|
|
if (c == '\n')
|
|
|
|
nl++;
|
|
|
|
if (!isspace(c))
|
|
|
|
word = 1;
|
|
|
|
else if (word) {
|
|
|
|
word = 0;
|
|
|
|
nw++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (word)
|
|
|
|
nw++;
|
|
|
|
tc += nc;
|
|
|
|
tl += nl;
|
|
|
|
tw += nw;
|
|
|
|
output(str, nc, nl, nw);
|
|
|
|
}
|
2011-05-22 21:36:34 -04:00
|
|
|
|
2013-10-07 12:13:01 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-31 21:01:11 -05:00
|
|
|
eprintf("usage: %s [-c | -m] [-lw] [file ...]\n", argv0);
|
2013-10-07 12:13:01 -04:00
|
|
|
}
|
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2013-03-10 20:12:10 -04:00
|
|
|
int i;
|
2013-07-20 01:27:42 -04:00
|
|
|
|
2013-03-10 20:12:10 -04:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'c':
|
|
|
|
cmode = 'c';
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
cmode = 'm';
|
|
|
|
break;
|
|
|
|
case 'l':
|
2014-11-13 15:24:47 -05:00
|
|
|
lflag = 1;
|
2013-03-10 20:12:10 -04:00
|
|
|
break;
|
|
|
|
case 'w':
|
2014-11-13 15:24:47 -05:00
|
|
|
wflag = 1;
|
2013-03-10 20:12:10 -04:00
|
|
|
break;
|
|
|
|
default:
|
2013-10-07 12:13:01 -04:00
|
|
|
usage();
|
2013-03-10 20:12:10 -04:00
|
|
|
} ARGEND;
|
2013-07-20 01:27:42 -04:00
|
|
|
|
2013-03-10 20:12:10 -04:00
|
|
|
if (argc == 0) {
|
2011-05-22 21:36:34 -04:00
|
|
|
wc(stdin, NULL);
|
2013-03-10 20:12:10 -04:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < argc; i++) {
|
2014-11-13 12:29:30 -05:00
|
|
|
if (!(fp = fopen(argv[i], "r"))) {
|
2013-11-13 06:39:24 -05:00
|
|
|
weprintf("fopen %s:", argv[i]);
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-10 20:12:10 -04:00
|
|
|
wc(fp, argv[i]);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
if (argc > 1)
|
|
|
|
output("total", tc, tl, tw);
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
2014-10-02 18:46:04 -04:00
|
|
|
return 0;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|