2011-05-23 01:36:34 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-05-24 00:13:34 +00:00
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-05-23 01:36:34 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
static void output(const char *, long, long, long);
|
|
|
|
static void wc(FILE *, const char *);
|
|
|
|
|
|
|
|
static bool lflag = false;
|
|
|
|
static bool wflag = false;
|
|
|
|
static char cmode = 0;
|
|
|
|
static long tc = 0, tl = 0, tw = 0;
|
|
|
|
|
2013-10-07 16:13:01 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-clmw] [files...]\n", argv0);
|
|
|
|
}
|
|
|
|
|
2011-05-23 01:36:34 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2013-03-11 00:12:10 +00:00
|
|
|
int i;
|
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':
|
|
|
|
lflag = true;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
wflag = true;
|
|
|
|
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
|
|
|
|
2013-03-11 00:12:10 +00:00
|
|
|
if (argc == 0) {
|
2011-05-23 01:36:34 +00:00
|
|
|
wc(stdin, NULL);
|
2013-03-11 00:12:10 +00:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < argc; i++) {
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!(fp = fopen(argv[i], "r"))) {
|
2013-11-13 11:39:24 +00:00
|
|
|
weprintf("fopen %s:", argv[i]);
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-11 00:12:10 +00:00
|
|
|
wc(fp, argv[i]);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
if (argc > 1)
|
|
|
|
output("total", tc, tl, tw);
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
2014-10-02 22:46:04 +00:00
|
|
|
return 0;
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
output(const char *str, long nc, long nl, long nw)
|
|
|
|
{
|
|
|
|
bool noflags = !cmode && !lflag && !wflag;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (lflag || noflags)
|
2011-05-23 01:36:34 +00:00
|
|
|
printf(" %5ld", nl);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (wflag || noflags)
|
2011-05-23 01:36:34 +00:00
|
|
|
printf(" %5ld", nw);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (cmode || noflags)
|
2011-05-23 01:36:34 +00:00
|
|
|
printf(" %5ld", nc);
|
2014-11-13 17:29:30 +00:00
|
|
|
if (str)
|
2011-05-23 01:36:34 +00:00
|
|
|
printf(" %s", str);
|
2011-05-26 03:01:20 +00:00
|
|
|
putchar('\n');
|
2011-05-23 01:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wc(FILE *fp, const char *str)
|
|
|
|
{
|
|
|
|
bool word = false;
|
2013-07-20 11:09:42 +00:00
|
|
|
int c;
|
2011-05-23 01:36:34 +00:00
|
|
|
long nc = 0, nl = 0, nw = 0;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
while ((c = getc(fp)) != EOF) {
|
|
|
|
if (cmode != 'm' || UTF8_POINT(c))
|
2011-05-23 01:36:34 +00:00
|
|
|
nc++;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (c == '\n')
|
2011-05-23 01:36:34 +00:00
|
|
|
nl++;
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!isspace(c))
|
2011-05-23 01:36:34 +00:00
|
|
|
word = true;
|
2014-11-13 17:29:30 +00:00
|
|
|
else if (word) {
|
2011-05-23 01:36:34 +00:00
|
|
|
word = false;
|
|
|
|
nw++;
|
|
|
|
}
|
|
|
|
}
|
2014-01-10 22:51:13 +00:00
|
|
|
if (word)
|
|
|
|
nw++;
|
2011-05-23 01:36:34 +00:00
|
|
|
tc += nc;
|
|
|
|
tl += nl;
|
|
|
|
tw += nw;
|
|
|
|
output(str, nc, nl, nw);
|
|
|
|
}
|