DragonFly BSD
DragonFly submit List (threaded) for 2005-06
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

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


From: "Simon 'corecode' Schubert" <corecode@xxxxxxxxxxxx>
Date: Fri, 17 Jun 2005 11:33:08 +0200

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
Description: PGP signature



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