diff options
author | Conrad Meyer <cem@FreeBSD.org> | 2017-12-10 17:56:03 +0000 |
---|---|---|
committer | Conrad Meyer <cem@FreeBSD.org> | 2017-12-10 17:56:03 +0000 |
commit | 0dc7c9e63509ff2a50be85270de98e83b22f23b3 (patch) | |
tree | dc1e552e9a5a4532d4a57893a38b69ec18796a2d /usr.bin | |
parent | 240076e595c7afa2e206600508773d29e8cc2b52 (diff) | |
download | src-test2-0dc7c9e63509ff2a50be85270de98e83b22f23b3.tar.gz src-test2-0dc7c9e63509ff2a50be85270de98e83b22f23b3.zip |
Notes
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/wc/wc.c | 85 |
1 files changed, 41 insertions, 44 deletions
diff --git a/usr.bin/wc/wc.c b/usr.bin/wc/wc.c index c0c6810fd4df..67d6f51092d4 100644 --- a/usr.bin/wc/wc.c +++ b/usr.bin/wc/wc.c @@ -213,51 +213,10 @@ cnt(const char *file) if (doword || (domulti && MB_CUR_MAX != 1)) goto word; /* - * Line counting is split out because it's a lot faster to get - * lines than to get words, since the word count requires some - * logic. + * If all we need is the number of characters and it's a regular file, + * just stat it. */ - if (doline || dochar) { - while ((len = read(fd, buf, MAXBSIZE))) { - if (len == -1) { - xo_warn("%s: read", file); - (void)close(fd); - return (1); - } - if (siginfo) { - show_cnt(file, linect, wordct, charct, - llct); - } - charct += len; - if (doline) { - for (p = buf; len--; ++p) - if (*p == '\n') { - if (tmpll > llct) - llct = tmpll; - tmpll = 0; - ++linect; - } else - tmpll++; - } - } - reset_siginfo(); - if (doline) - tlinect += linect; - if (dochar) - tcharct += charct; - if (dolongline) { - if (llct > tlongline) - tlongline = llct; - } - show_cnt(file, linect, wordct, charct, llct); - (void)close(fd); - return (0); - } - /* - * If all we need is the number of characters and it's a - * regular file, just stat the puppy. - */ - if (dochar || domulti) { + if (doline == 0) { if (fstat(fd, &sb)) { xo_warn("%s: fstat", file); (void)close(fd); @@ -272,6 +231,44 @@ cnt(const char *file) return (0); } } + /* + * For files we can't stat, or if we need line counting, slurp the + * file. Line counting is split out because it's a lot faster to get + * lines than to get words, since the word count requires locale + * handling. + */ + while ((len = read(fd, buf, MAXBSIZE))) { + if (len == -1) { + xo_warn("%s: read", file); + (void)close(fd); + return (1); + } + if (siginfo) + show_cnt(file, linect, wordct, charct, llct); + charct += len; + if (doline) { + for (p = buf; len--; ++p) + if (*p == '\n') { + if (tmpll > llct) + llct = tmpll; + tmpll = 0; + ++linect; + } else + tmpll++; + } + } + reset_siginfo(); + if (doline) + tlinect += linect; + if (dochar) + tcharct += charct; + if (dolongline) { + if (llct > tlongline) + tlongline = llct; + } + show_cnt(file, linect, wordct, charct, llct); + (void)close(fd); + return (0); /* Do it the hard way... */ word: gotsp = 1; |