David Grayson wrote:
We can call that Bug C.  If a grep
maintainer is interested in fixing Bug C, they should improve the logic at
this line in grepdirent in grep.c:

   filename = ent->fts_path + filename_prefix_len;

Thanks for reporting this bug.  I installed the attached patch for that.
>From 61903c057376190f386430131342de8170e3c25a Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Wed, 11 Mar 2015 22:44:36 -0700
Subject: [PATCH] grep: output "." file name in diagnostic

This is bug C as reported by David Grayson in:
http://bugs.gnu.org/16444#18
This bug occurs only in obscure circumstances, and I didn't see
how to write a reasonable test case for it.
* src/grep.c (filename_prefix_len): Remove, replacing with ...
(omit_dot_slash): New static var.  All uses of the former replaced
with uses of the latter.
(grepdirent): Don't add 2 if the filename is just ".".
---
 src/grep.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index f720a8a..81b8716 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -384,7 +384,8 @@ static char const *matcher;
 /* For error messages. */
 /* The input file name, or (if standard input) "-" or a --label argument.  */
 static char const *filename;
-static size_t filename_prefix_len;
+/* Omit leading "./" from file names in diagnostics.  */
+static bool omit_dot_slash;
 static bool errseen;
 static bool write_error_seen;
 
@@ -640,7 +641,7 @@ skipped_file (char const *name, bool command_line, bool is_dir)
 {
   return (is_dir
           ? (directories == SKIP_DIRECTORIES
-             || (! (command_line && filename_prefix_len != 0)
+             || (! (command_line && omit_dot_slash)
                  && excluded_directory_patterns
                  && excluded_file_name (excluded_directory_patterns, name)))
           : (excluded_patterns
@@ -1483,7 +1484,9 @@ grepdirent (FTS *fts, FTSENT *ent, bool command_line)
       return true;
     }
 
-  filename = ent->fts_path + filename_prefix_len;
+  filename = ent->fts_path;
+  if (omit_dot_slash && filename[1])
+    filename += 2;
   follow = (fts->fts_options & FTS_LOGICAL
             || (fts->fts_options & FTS_COMFOLLOW && command_line));
 
@@ -2595,8 +2598,7 @@ main (int argc, char **argv)
     }
   else if (directories == RECURSE_DIRECTORIES && prepended < last_recursive)
     {
-      /* Grep through ".", omitting leading "./" from diagnostics.  */
-      filename_prefix_len = 2;
+      omit_dot_slash = true;
       ok = grep_command_line_arg (".");
     }
   else
-- 
2.1.0

Reply via email to