diff options
author | Ron Yorston <rmy@pobox.com> | 2015-07-24 14:28:08 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2015-07-31 16:22:07 +0200 |
commit | 70b84be9e85969491e542cecc3ae28fa7558a7ec (patch) | |
tree | 15b5b57b0dd60215e00e0d586ad744452b819026 /miscutils/less.c | |
parent | 159e032bf4cd24535e57daaf29a381b0d5163368 (diff) | |
download | busybox-w32-70b84be9e85969491e542cecc3ae28fa7558a7ec.tar.gz busybox-w32-70b84be9e85969491e542cecc3ae28fa7558a7ec.tar.bz2 busybox-w32-70b84be9e85969491e542cecc3ae28fa7558a7ec.zip |
less: rearrange detection of non-regular files
Move the code to detect non-regular files to the point where the
file is being opened. If num_lines == READING_FILE guarantees
that the file is regular.
Detect when a file becomes unreadable between it first being opened
and the call to update_num_lines. Mark the file as being non-regular
so we don't try that again.
function old new delta
reinitialize 197 245 +48
update_num_lines 159 127 -32
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 48/-32) Total: 16 bytes
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.c | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/miscutils/less.c b/miscutils/less.c index 8fd0874e2..91a933a3a 100644 --- a/miscutils/less.c +++ b/miscutils/less.c | |||
@@ -167,7 +167,8 @@ enum { pattern_valid = 0 }; | |||
167 | 167 | ||
168 | enum { | 168 | enum { |
169 | READING_FILE = -1, | 169 | READING_FILE = -1, |
170 | READING_STDIN = -2 | 170 | READING_STDIN = -2, |
171 | READING_NONREG = -3 | ||
171 | }; | 172 | }; |
172 | 173 | ||
173 | struct globals { | 174 | struct globals { |
@@ -615,15 +616,16 @@ static void update_num_lines(void) | |||
615 | int count, fd; | 616 | int count, fd; |
616 | ssize_t len, i; | 617 | ssize_t len, i; |
617 | char buf[4096]; | 618 | char buf[4096]; |
618 | struct stat stbuf; | ||
619 | 619 | ||
620 | /* only do this for regular files */ | ||
620 | if (num_lines == READING_FILE) { | 621 | if (num_lines == READING_FILE) { |
621 | count = 0; | 622 | count = 0; |
622 | fd = open(filename, O_RDONLY); | 623 | fd = open(filename, O_RDONLY); |
623 | if (fd < 0) | 624 | if (fd < 0) { |
624 | goto skip; | 625 | /* somebody stole my file! */ |
625 | if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) | 626 | num_lines = READING_NONREG; |
626 | goto do_close; | 627 | return; |
628 | } | ||
627 | while ((len = safe_read(fd, buf, sizeof(buf))) > 0) { | 629 | while ((len = safe_read(fd, buf, sizeof(buf))) > 0) { |
628 | for (i = 0; i < len; ++i) { | 630 | for (i = 0; i < len; ++i) { |
629 | if (buf[i] == '\n' && ++count == MAXLINES) | 631 | if (buf[i] == '\n' && ++count == MAXLINES) |
@@ -632,9 +634,7 @@ static void update_num_lines(void) | |||
632 | } | 634 | } |
633 | done: | 635 | done: |
634 | num_lines = count; | 636 | num_lines = count; |
635 | do_close: | ||
636 | close(fd); | 637 | close(fd); |
637 | skip: ; | ||
638 | } | 638 | } |
639 | } | 639 | } |
640 | 640 | ||
@@ -943,6 +943,13 @@ static void buffer_line(int linenum) | |||
943 | static void open_file_and_read_lines(void) | 943 | static void open_file_and_read_lines(void) |
944 | { | 944 | { |
945 | if (filename) { | 945 | if (filename) { |
946 | #if ENABLE_FEATURE_LESS_FLAGS | ||
947 | struct stat stbuf; | ||
948 | |||
949 | xstat(filename, &stbuf); | ||
950 | if (!S_ISREG(stbuf.st_mode)) | ||
951 | num_lines = READING_NONREG; | ||
952 | #endif | ||
946 | xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO); | 953 | xmove_fd(xopen(filename, O_RDONLY), STDIN_FILENO); |
947 | } else { | 954 | } else { |
948 | /* "less" with no arguments in argv[] */ | 955 | /* "less" with no arguments in argv[] */ |