bug-gnu-utils
[Top][All Lists]
Advanced

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

grep -R vs. symlinks to directories


From: Jim Meyering
Subject: grep -R vs. symlinks to directories
Date: Sun, 13 Jul 2003 19:25:03 +0200

Hi,

I really like grep's -R (--recursive) option and use it a lot.
However, I'm regularly annoyed to see that it makes grep follow symlinks
to directories.  In my case, that behavior has always been undesirable, so
I've simply turned it off in my copy.  This should probably be controlled
by an option.  It's a shame that the -P, -H, and -L options (typically
used to control how recursion works wrt symlinks) are already taken.
Maybe --dereference={always,never,command-line}?

Here's a patch relative to grep-2.5.1:

--- src/grep.c.~1~      2002-03-26 16:54:12.000000000 +0100
+++ src/grep.c  2003-07-13 19:11:02.000000000 +0200
@@ -767,11 +767,15 @@ grep (int fd, char const *file, struct s
   if (file && directories == RECURSE_DIRECTORIES
       && S_ISDIR (stats->stat.st_mode))
     {
-      /* Close fd now, so that we don't open a lot of file descriptors
-        when we recurse deeply.  */
-      if (close (fd) != 0)
-       error (0, errno, "%s", file);
-      return grepdir (file, stats) - 2;
+      struct stat sb;
+      if (lstat (file, &sb) == 0 && S_ISDIR (sb.st_mode))
+       {
+         /* Close fd now, so that we don't open a lot of file descriptors
+            when we recurse deeply.  */
+         if (close (fd) != 0)
+           error (0, errno, "%s", file);
+         return grepdir (file, stats) - 2;
+       }
     }
 
   totalcc = 0;




reply via email to

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