commit abf068492e16ef52b262cf903e8b39ac10c2bb59
parent 45b9b26cae5c5d6633ea42107428b4e6c5c3cee0
Author: Michael Forney <mforney@mforney.org>
Date: Sat, 2 Nov 2019 12:59:02 -0700
wc: Set flag defaults if none were specified
Diffstat:
M | wc.c | | | 15 | ++++++++++----- |
1 file changed, 10 insertions(+), 5 deletions(-)
diff --git a/wc.c b/wc.c
@@ -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 {