wc: Set flag defaults if none were specified

This commit is contained in:
Michael Forney 2019-11-02 12:59:02 -07:00
parent 45b9b26cae
commit abf068492e

15
wc.c
View File

@ -12,20 +12,19 @@ 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 noflags = !cmode && !lflag && !wflag;
int first = 1;
if (lflag || noflags) {
if (lflag) {
if (!first)
putchar(' ');
printf("%*.1zu", first ? (first = 0) : 6, nl);
}
if (wflag || noflags) {
if (wflag) {
if (!first)
putchar(' ');
printf("%*.1zu", first ? (first = 0) : 6, nw);
}
if (cmode || noflags) {
if (cmode) {
if (!first)
putchar(' ');
printf("%*.1zu", first ? (first = 0) : 6, nc);
@ -43,7 +42,7 @@ wc(FILE *fp, const char *str)
size_t nc = 0, nl = 0, nw = 0;
while ((rlen = efgetrune(&c, fp, str))) {
nc += (cmode == 'c' || !cmode) ? rlen : (c != Runeerror);
nc += (cmode == 'c') ? rlen : (c != Runeerror);
if (c == '\n')
nl++;
if (!isspacerune(c))
@ -91,6 +90,12 @@ main(int argc, char *argv[])
usage();
} ARGEND
if (!lflag && !wflag && !cmode) {
cmode = 'c';
lflag = 1;
wflag = 1;
}
if (!argc) {
wc(stdin, NULL);
} else {