grep --dont-read-devices-or-symlinks

Simon 'corecode' Schubert corecode at fs.ei.tum.de
Fri Jun 17 02:34:13 PDT 2005


hey.

find attached a patch to grep to only read real files, ignoring symlinks,
devices, etc.(-O --only-files). I felt I need this for grepping my /src/sys
which also contains some compile/* directories where modules produce recursive
loops...

cheers
  simon

-- 
Serve - BSD     +++  RENT this banner advert  +++    ASCII Ribbon   /"\
Work - Mac      +++  space for low $$$ NOW!1  +++      Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \
Index: grep.c
===================================================================
RCS file: /space/cvs/dragonfly/src/gnu/usr.bin/grep/grep.c,v
retrieving revision 1.2
diff -u -r1.2 grep.c
--- grep.c	17 Jun 2003 04:25:45 -0000	1.2
+++ grep.c	17 Jun 2005 09:20:23 -0000
@@ -67,7 +67,7 @@
 
 /* Short options.  */
 static char const short_options[] =
-"0123456789A:B:C::EFGHIRUVX:abcd:e:f:hiLlnqrsuvwxyZz";
+"0123456789A:B:C::EFGHIORUVX:abcd:e:f:hiLlnqrsuvwxyZz";
 
 /* Non-boolean long options that have no corresponding short equivalents.  */
 enum
@@ -106,6 +106,7 @@
   {"null", no_argument, NULL, 'Z'},
 #endif
   {"null-data", no_argument, NULL, 'z'},
+  {"only-files", no_argument, NULL, 'O'},
   {"quiet", no_argument, NULL, 'q'},
   {"recursive", no_argument, NULL, 'r'},
   {"regexp", required_argument, NULL, 'e'},
@@ -140,6 +141,9 @@
     SKIP_DIRECTORIES
   } directories;
 
+/* How to dir/device/links. */
+static int only_files;
+
 static int  ck_atoi PARAMS ((char const *, int *));
 static void usage PARAMS ((int)) __attribute__((noreturn));
 static void error PARAMS ((const char *, int));
@@ -832,6 +836,10 @@
     }
   else
     {
+      if (only_files && (lstat(file, &stats->stat) != 0 ||
+	  !(S_ISREG(stats->stat.st_mode) ||
+	  directories == RECURSE_DIRECTORIES && S_ISDIR(stats->stat.st_mode))))
+        return 1;
       while ((desc = open (file, O_RDONLY)) < 0 && errno == EINTR)
 	continue;
 
@@ -1289,6 +1297,9 @@
       case 'I':
 	binary_files = WITHOUT_MATCH_BINARY_FILES;
 	break;
+      case 'O':
+	only_files = 1;
+	break;
       case 'U':
 #if O_BINARY
 	dos_use_file_type = DOS_BINARY;
Attachment:
pgp00003.pgp
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pgp00003.pgp
Type: application/octet-stream
Size: 189 bytes
Desc: "Description: PGP signature"
URL: <http://lists.dragonflybsd.org/pipermail/submit/attachments/20050617/58413774/attachment-0018.obj>


More information about the Submit mailing list