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>
This commit is contained in:
parent
ad27a6d326
commit
b27cf31003
@ -615,11 +615,12 @@ static int safe_lineno(int fline)
|
|||||||
static void update_num_lines(void)
|
static void update_num_lines(void)
|
||||||
{
|
{
|
||||||
int count, fd;
|
int count, fd;
|
||||||
|
struct stat stbuf;
|
||||||
ssize_t len, i;
|
ssize_t len, i;
|
||||||
char buf[4096];
|
char buf[4096];
|
||||||
|
|
||||||
/* only do this for regular files */
|
/* only do this for regular files */
|
||||||
if (num_lines != NOT_REGULAR_FILE) {
|
if (num_lines == REOPEN_AND_COUNT || num_lines == REOPEN_STDIN) {
|
||||||
count = 0;
|
count = 0;
|
||||||
fd = open("/proc/self/fd/0", O_RDONLY);
|
fd = open("/proc/self/fd/0", O_RDONLY);
|
||||||
if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
|
if (fd < 0 && num_lines == REOPEN_AND_COUNT) {
|
||||||
@ -631,17 +632,10 @@ static void update_num_lines(void)
|
|||||||
num_lines = NOT_REGULAR_FILE;
|
num_lines = NOT_REGULAR_FILE;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#if ENABLE_FEATURE_LESS_FLAGS
|
if (fstat(fd, &stbuf) != 0 || !S_ISREG(stbuf.st_mode)) {
|
||||||
{
|
|
||||||
struct stat stbuf;
|
|
||||||
if (fstat(fd, &stbuf) != 0
|
|
||||||
|| !S_ISREG(stbuf.st_mode)
|
|
||||||
) {
|
|
||||||
num_lines = NOT_REGULAR_FILE;
|
num_lines = NOT_REGULAR_FILE;
|
||||||
goto do_close;
|
goto do_close;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
#endif
|
|
||||||
while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
|
while ((len = safe_read(fd, buf, sizeof(buf))) > 0) {
|
||||||
for (i = 0; i < len; ++i) {
|
for (i = 0; i < len; ++i) {
|
||||||
if (buf[i] == '\n' && ++count == MAXLINES)
|
if (buf[i] == '\n' && ++count == MAXLINES)
|
||||||
|
Loading…
Reference in New Issue
Block a user