bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] grep: handle non-devices like regular files


From: Paul Eggert
Subject: [PATCH] grep: handle non-devices like regular files
Date: Tue, 15 May 2012 09:37:12 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120329 Thunderbird/11.0.1

This is a proposed minor patch that lets grep handle a few
weird files more consistently, e.g., to catch infinite loops
when outputting to a shared memory object.


* src/main.c (reset, grepdesc, main): Treat all non-devices like
regular files, with respect to seeking.  This treats shared
memory objects more consistently with regular files.
---
 src/main.c |    9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/main.c b/src/main.c
index 10fbfac..9e5de88 100644
--- a/src/main.c
+++ b/src/main.c
@@ -577,7 +577,7 @@ reset (int fd, struct stat const *st)
   bufbeg[-1] = eolbyte;
   bufdesc = fd;
 
-  if (S_ISREG (st->st_mode))
+  if (! is_device_mode (st->st_mode))
     {
       if (fd != STDIN_FILENO)
         bufoffset = 0;
@@ -1476,7 +1476,7 @@ grepdesc (int desc, int command_line)
      input==output, while there is no risk of infloop, there is a race
      condition that could result in "alternate" output.  */
   if (!out_quiet && list_files == 0 && 1 < max_count
-      && S_ISREG (out_stat.st_mode) && out_stat.st_ino
+      && ! is_device_mode (out_stat.st_mode) && out_stat.st_ino
       && SAME_INODE (st, out_stat))
     {
       if (! suppress_errors)
@@ -1522,7 +1522,7 @@ grepdesc (int desc, int command_line)
           off_t required_offset = outleft ? bufoffset : after_last_match;
           if (required_offset != bufoffset
               && lseek (desc, required_offset, SEEK_SET) < 0
-              && S_ISREG (st.st_mode))
+              && ! is_device_mode (st.st_mode))
             suppressible_error (filename, errno);
         }
     }
@@ -2250,7 +2250,8 @@ main (int argc, char **argv)
     usage (EXIT_SUCCESS);
 
   struct stat tmp_stat;
-  if (fstat (STDOUT_FILENO, &tmp_stat) == 0 && S_ISREG (tmp_stat.st_mode))
+  if (fstat (STDOUT_FILENO, &tmp_stat) == 0
+      && ! is_device_mode (tmp_stat.st_mode))
     out_stat = tmp_stat;
 
   if (keys)
-- 
1.7.6.5




reply via email to

[Prev in Thread] Current Thread [Next in Thread]