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