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

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

Re: Simple feature request for grep


From: Paul Eggert
Subject: Re: Simple feature request for grep
Date: Tue, 12 Jul 2005 09:56:56 -0700
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

Bruno Haible <address@hidden> writes:

> Ben Tilly wrote:
>> If the -r option has been specified but no directories or files are 
>> specified,
>> please print a warning to STDERR.

A much better solution, in my opinion, would be for "grep -r pattern"
to be equivalent to "grep -r pattern .", for the same reason that "ls
-Rl" defaults to "ls -RL .", and "cvs diff -u" defaults to "cvs diff
-u .".  I am constantly using "grep -r pattern" and expecting it to
find everything in the current directory.

Here's a patch to do that.

2005-07-12  Paul Eggert  <address@hidden>

        * NEWS: grep -r now searches the working directory if no operands are
        given.
        * doc/grep.texi (Grep Programs): Document this.
        * src/grep.c (grepfile, grepdir, usage, main): Implement this.

Index: NEWS
===================================================================
RCS file: /cvsroot/grep/grep/NEWS,v
retrieving revision 1.39
diff -p -u -r1.39 NEWS
--- NEWS        8 Dec 2004 14:18:29 -0000       1.39
+++ NEWS        12 Jul 2005 16:53:51 -0000
@@ -1,3 +1,5 @@
+  - grep -r (without any operands) recursively searches the working directory.
+
 Version 2.5.1
   - This is a bugfix release. No new features.
 
Index: doc/grep.texi
===================================================================
RCS file: /cvsroot/grep/grep/doc/grep.texi,v
retrieving revision 1.55
diff -p -u -r1.55 grep.texi
--- doc/grep.texi       5 Jul 2005 01:03:32 -0000       1.55
+++ doc/grep.texi       12 Jul 2005 16:53:51 -0000
@@ -709,10 +709,15 @@ is found.
 @node Grep Programs
 @chapter @command{grep} programs
 
address@hidden searches the named input files (or standard input if no
-files are named, or the file name @file{-} is given) for lines containing
-a match to the given pattern.  By default, @command{grep} prints the
-matching lines.  There are four major variants of @command{grep},
address@hidden searches input files for lines containing a match to
+the given pattern.  By default, @command{grep} prints the matching
+lines.  The names of the input files are given on the command line;
address@hidden denotes standard input.  If no input files are given,
address@hidden searches standard input unless a recursive search is
+requested with @option{-r} or its equivalent, in which case it
+recursively searches the working directory.
+
+There are four major variants of @command{grep},
 controlled by the following options.
 
 @table @samp
Index: src/grep.c
===================================================================
RCS file: /cvsroot/grep/grep/src/grep.c,v
retrieving revision 1.112
diff -p -u -r1.112 grep.c
--- src/grep.c  7 Jul 2005 05:32:30 -0000       1.112
+++ src/grep.c  12 Jul 2005 16:53:51 -0000
@@ -1143,11 +1143,13 @@ grep (int fd, char const *file, struct s
 }
 
 static int
-grepfile (char const *file, struct stats *stats)
+grepfile (char const *fname, struct stats *stats,
+         int recursive_grep_with_no_operands)
 {
   int desc;
   int count;
   int status;
+  char const *file = recursive_grep_with_no_operands ? "." : fname;
 
   if (! file)
     {
@@ -1184,7 +1186,7 @@ grepfile (char const *file, struct stats
                  return 1;
                }
 
-             return grepdir (file, stats);
+             return grepdir (fname, stats);
            }
 
          if (!suppress_errors)
@@ -1265,8 +1267,9 @@ grepfile (char const *file, struct stats
 }
 
 static int
-grepdir (char const *dir, struct stats const *stats)
+grepdir (char const *dname, struct stats const *stats)
 {
+  char const *dir = dname ? dname : ".";
   int status = 1;
   struct stats const *ancestor;
   char *name_space;
@@ -1296,7 +1299,7 @@ grepdir (char const *dir, struct stats c
     }
   else
     {
-      size_t dirlen = strlen (dir);
+      size_t dirlen = dname ? strlen (dname) : 0;
       int needs_slash = ! (dirlen == FILESYSTEM_PREFIX_LEN (dir)
                           || IS_SLASH (dir[dirlen - 1]));
       char *file = NULL;
@@ -1312,7 +1315,7 @@ grepdir (char const *dir, struct stats c
          file[dirlen] = '/';
          strcpy (file + dirlen + needs_slash, namep);
          namep += namelen + 1;
-         status &= grepfile (file, &child);
+         status &= grepfile (file, &child, 0);
        }
       out_file -= !no_filenames;
       if (file)
@@ -1405,8 +1408,10 @@ Context control:\n\
   -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)\n\
 \n\
 `egrep' means `grep -E'.  `fgrep' means `grep -F'.\n\
-With no FILE, or when FILE is -, read standard input.  If less than two 
FILEs\n\
-are given, assume -h.  Exit status is 0 if any line was selected, 1 
otherwise;\n\
+When FILE is -, or with no FILE and without -r, read standard input.\n\
+With no FILE and with -r, recurse from the working directory.\n\
+If less than two FILEs are given, assume -h.\n\
+Exit status is 0 if any line was selected, 1 otherwise;\n\
 if any error occurs and -q was not given, the exit status is 2.\n"));
       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     }
@@ -2145,12 +2150,13 @@ warranty; not even for MERCHANTABILITY o
                continue;
            }
          status &= grepfile (strcmp (file, "-") == 0 ? (char *) NULL : file,
-                             &stats_base);
+                             &stats_base, 0);
        }
        while ( ++optind < argc);
     }
   else
-    status = grepfile ((char *) NULL, &stats_base);
+    status = grepfile ((char *) NULL, &stats_base,
+                      directories == RECURSE_DIRECTORIES);
 
   /* We register via atexit() to test stdout.  */
   exit (errseen ? 2 : status);




reply via email to

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