From owner-svn-src-all@freebsd.org Fri Jan 5 01:46:42 2018 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 9B859EA4F8A; Fri, 5 Jan 2018 01:46:42 +0000 (UTC) (envelope-from kevans@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 666226C660; Fri, 5 Jan 2018 01:46:42 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w051kfhG095605; Fri, 5 Jan 2018 01:46:41 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w051kfUa095604; Fri, 5 Jan 2018 01:46:41 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201801050146.w051kfUa095604@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Fri, 5 Jan 2018 01:46:41 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r327567 - head/usr.bin/hexdump X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: head/usr.bin/hexdump X-SVN-Commit-Revision: 327567 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: Fri, 05 Jan 2018 01:46:42 -0000 Author: kevans Date: Fri Jan 5 01:46:41 2018 New Revision: 327567 URL: https://svnweb.freebsd.org/changeset/base/327567 Log: hexdump(1): Speed up -s flag on devices Using the -s flag on devices is extraordinarily slow due to using fseek(3) a little too conservatively. Address this by using fseek on character/block devices as well, falling back to getchar(3) only if we fail to seek or we're operating on tape drives, where fseek may succeed while not actually being supported. PR: 86485 Submitted by: arundel (originally; modified since then) Reviewed by: cem Differential Revision: https://reviews.freebsd.org/D10939 Modified: head/usr.bin/hexdump/display.c Modified: head/usr.bin/hexdump/display.c ============================================================================== --- head/usr.bin/hexdump/display.c Thu Jan 4 23:07:51 2018 (r327566) +++ head/usr.bin/hexdump/display.c Fri Jan 5 01:46:41 2018 (r327567) @@ -39,6 +39,8 @@ __FBSDID("$FreeBSD$"); #include #include +#include +#include #include #include @@ -57,6 +59,7 @@ static off_t address; /* address/offset in stream */ static off_t eaddress; /* end address */ static void print(PR *, u_char *); +static void noseek(void); void display(void) @@ -386,7 +389,7 @@ next(char **argv) void doskip(const char *fname, int statok) { - int cnt; + int type; struct stat sb; if (statok) { @@ -398,16 +401,37 @@ doskip(const char *fname, int statok) return; } } - if (statok && S_ISREG(sb.st_mode)) { - if (fseeko(stdin, skip, SEEK_SET)) + if (!statok || S_ISFIFO(sb.st_mode) || S_ISSOCK(sb.st_mode)) { + noseek(); + return; + } + if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode)) { + if (ioctl(fileno(stdin), FIODTYPE, &type)) err(1, "%s", fname); - address += skip; - skip = 0; - } else { - for (cnt = 0; cnt < skip; ++cnt) - if (getchar() == EOF) - break; - address += cnt; - skip -= cnt; + /* + * Most tape drives don't support seeking, + * yet fseek() would succeed. + */ + if (type & D_TAPE) { + noseek(); + return; + } } + if (fseeko(stdin, skip, SEEK_SET)) { + noseek(); + return; + } + address += skip; + skip = 0; +} + +static void +noseek(void) +{ + int count; + for (count = 0; count < skip; ++count) + if (getchar() == EOF) + break; + address += count; + skip -= count; }