aboutsummaryrefslogtreecommitdiff
path: root/miscutils/less.c
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2015-07-31 17:33:24 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2015-08-03 16:41:19 +0200
commitb27cf31003774e5aa13491ccb8f1f0721dc5b920 (patch)
treef2b72d7111d1033063524dd411f9e267bcac1ee4 /miscutils/less.c
parentad27a6d3265b2e8a27030a01121afed639bed2da (diff)
downloadbusybox-w32-b27cf31003774e5aa13491ccb8f1f0721dc5b920.tar.gz
busybox-w32-b27cf31003774e5aa13491ccb8f1f0721dc5b920.tar.bz2
busybox-w32-b27cf31003774e5aa13491ccb8f1f0721dc5b920.zip
less: further tweaks to regular file detection
Test explicitly for REOPEN flags: update_num_lines is called unconditionally so (num_lines != NOT_REGULAR_FILE) is also true when num_lines contains a valid number of lines. The call to fstat doesn't need to be in #if ENABLE_FEATURE_LESS_FLAGS: the whole function is already in such a test. Signed-off-by: Ron Yorston <rmy@pobox.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'miscutils/less.c')
-rw-r--r--miscutils/less.c16
1 files changed, 5 insertions, 11 deletions
diff --git a/miscutils/less.c b/miscutils/less.c
index be8d20e66..7a441bf7e 100644
--- a/miscutils/less.c
+++ b/miscutils/less.c
@@ -615,11 +615,12 @@ static int safe_lineno(int fline)
615static void update_num_lines(void) 615static void update_num_lines(void)
616{ 616{
617 int count, fd; 617 int count, fd;
618 struct stat stbuf;
618 ssize_t len, i; 619 ssize_t len, i;
619 char buf[4096]; 620 char buf[4096];
620 621
621 /* only do this for regular files */ 622 /* only do this for regular files */
622 if (num_lines != NOT_REGULAR_FILE) { 623 if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
623 count = 0; 624 count = 0;
624 fd = open("/proc/self/fd/0", O_RDONLY); 625 fd = open("/proc/self/fd/0", O_RDONLY);
625 if (fd < 0 && num_lines == REOPEN_AND_COUNT) { 626 if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
@@ -631,17 +632,10 @@ static void update_num_lines(void)
631 num_lines = NOT_REGULAR_FILE; 632 num_lines = NOT_REGULAR_FILE;
632 return; 633 return;
633 } 634 }
634#if ENABLE_FEATURE_LESS_FLAGS 635 if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) {
635 { 636 num_lines = NOT_REGULAR_FILE;
636 struct stat stbuf; 637 goto do_close;
637 if (fstat(fd, &stbuf) != 0
638 || !S_ISREG(stbuf.st_mode)
639 ) {
640 num_lines = NOT_REGULAR_FILE;
641 goto do_close;
642 }
643 } 638 }
644#endif
645 while ((len = safe_read(fd, buf, sizeof(buf))) > 0) { 639 while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
646 for (i = 0; i < len; ++i) { 640 for (i = 0; i < len; ++i) {
647 if (buf[i] == '\n' && ++count == MAXLINES) 641 if (buf[i] == '\n' && ++count == MAXLINES)