From owner-svn-src-all@freebsd.org Sun Dec 10 17:56:05 2017 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 0DC50E92F78; Sun, 10 Dec 2017 17:56:05 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id DBA787A12B; Sun, 10 Dec 2017 17:56:04 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id vBAHu3tw052648; Sun, 10 Dec 2017 17:56:03 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id vBAHu3Qa052647; Sun, 10 Dec 2017 17:56:03 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201712101756.vBAHu3Qa052647@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Sun, 10 Dec 2017 17:56:03 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r326749 - head/usr.bin/wc X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: head/usr.bin/wc X-SVN-Commit-Revision: 326749 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 10 Dec 2017 17:56:05 -0000 Author: cem Date: Sun Dec 10 17:56:03 2017 New Revision: 326749 URL: https://svnweb.freebsd.org/changeset/base/326749 Log: wc(1): Restore regular file char count fast path fstat(2) is going to be a lot faster than reading all of the bytes in a file, if we just need a character count for a regular file. This fast path was accidentally broken in r326736. PR: 224160 Reported by: bde Sponsored by: Dell EMC Isilon Modified: head/usr.bin/wc/wc.c Modified: head/usr.bin/wc/wc.c ============================================================================== --- head/usr.bin/wc/wc.c Sun Dec 10 16:42:59 2017 (r326748) +++ head/usr.bin/wc/wc.c Sun Dec 10 17:56:03 2017 (r326749) @@ -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;