diff --git a/usr.bin/wc/wc.c b/usr.bin/wc/wc.c --- a/usr.bin/wc/wc.c +++ b/usr.bin/wc/wc.c @@ -50,10 +50,10 @@ #include #include -#include #include #include #include +#include #include #include #include @@ -68,7 +68,7 @@ static fileargs_t *fa; static uintmax_t tlinect, twordct, tcharct, tlongline; -static int doline, doword, dochar, domulti, dolongline; +static bool doline, doword, dochar, domulti, dolongline; static volatile sig_atomic_t siginfo; static xo_handle_t *stderr_handle; @@ -107,21 +107,21 @@ while ((ch = getopt(argc, argv, "clmwL")) != -1) switch((char)ch) { case 'l': - doline = 1; + doline = true; break; case 'w': - doword = 1; + doword = true; break; case 'c': - dochar = 1; - domulti = 0; + dochar = true; + domulti = false; break; case 'L': - dolongline = 1; + dolongline = true; break; case 'm': - domulti = 1; - dochar = 0; + domulti = true; + dochar = false; break; case '?': default: @@ -162,19 +162,19 @@ errors = 0; total = 0; - if (!*argv) { - xo_open_instance("file"); - if (cnt((char *)NULL) != 0) + if (argc == 0) { + xo_open_instance("file"); + if (cnt(NULL) != 0) ++errors; - xo_close_instance("file"); + xo_close_instance("file"); } else { - do { - xo_open_instance("file"); - if (cnt(*argv) != 0) + while (argc--) { + xo_open_instance("file"); + if (cnt(*argv++) != 0) ++errors; - xo_close_instance("file"); + xo_close_instance("file"); ++total; - } while(*++argv); + } } xo_close_list("file"); @@ -187,7 +187,8 @@ fileargs_free(fa); xo_close_container("wc"); - xo_finish(); + if (xo_finish() < 0) + xo_err(1, "stdout"); exit(errors == 0 ? 0 : 1); } @@ -221,15 +222,15 @@ static int cnt(const char *file) { + char buf[MAXBSIZE], *p; struct stat sb; + mbstate_t mbs; uintmax_t linect, wordct, charct, llct, tmpll; - int fd, len, warned; + ssize_t len; size_t clen; - short gotsp; - u_char *p; - u_char buf[MAXBSIZE]; + int fd; wchar_t wch; - mbstate_t mbs; + bool gotsp, warned; linect = wordct = charct = llct = tmpll = 0; if (file == NULL) @@ -266,7 +267,7 @@ * handling. */ while ((len = read(fd, buf, MAXBSIZE))) { - if (len == -1) { + if (len < 0) { xo_warn("%s: read", file != NULL ? file : "stdin"); (void)close(fd); return (1); @@ -275,14 +276,16 @@ show_cnt(file, linect, wordct, charct, llct); charct += len; if (doline || dolongline) { - for (p = buf; len--; ++p) + for (p = buf; len > 0; --len, ++p) { if (*p == '\n') { if (tmpll > llct) llct = tmpll; tmpll = 0; ++linect; - } else + } else { tmpll++; + } + } } } reset_siginfo(); @@ -297,11 +300,11 @@ return (0); /* Do it the hard way... */ -word: gotsp = 1; - warned = 0; +word: gotsp = true; + warned = false; memset(&mbs, 0, sizeof(mbs)); while ((len = read(fd, buf, MAXBSIZE)) != 0) { - if (len == -1) { + if (len < 0) { xo_warn("%s: read", file != NULL ? file : "stdin"); (void)close(fd); return (1); @@ -319,7 +322,7 @@ errno = EILSEQ; xo_warn("%s", file != NULL ? file : "stdin"); - warned = 1; + warned = true; } memset(&mbs, 0, sizeof(mbs)); clen = 1; @@ -340,9 +343,9 @@ ++linect; } if (iswspace(wch)) - gotsp = 1; + gotsp = true; else if (gotsp) { - gotsp = 0; + gotsp = false; ++wordct; } }