On Mon, Dec 02 2019, Jeremie Courreges-Anglas <j...@wxcvbn.org> wrote:

[...]

> I have had a similar diff for some time but didn't push for it because
> nitpicking: GNU grep doesn't prepend "./" to file names in this case.
> I have a diff to do this but let's keep the nitpicking for later.

Current:
--8<--
ritchie /usr/src/usr.bin/grep$ grep -R grep_tree
./grep.c:               c = grep_tree(argv);
./grep.h:int             grep_tree(char **argv);
./util.c:grep_tree(char **argv)
Binary file ./grep matches
./grep.c.orig:          c = grep_tree(argv);
./util.c.orig:grep_tree(char **argv)
-->8--

With the diff below (matches GNU grep)
--8<--
ritchie /usr/src/usr.bin/grep$ obj/grep -R grep_tree
grep.c:         c = grep_tree(argv);
grep.h:int               grep_tree(char **argv);
util.c:grep_tree(char **argv)
Binary file grep matches
grep.c.orig:            c = grep_tree(argv);
util.c.orig:grep_tree(char **argv)
-->88

Explicit "."
--8<--
ritchie /usr/src/usr.bin/grep$ obj/grep -R grep_tree .
./grep.c:               c = grep_tree(argv);
./grep.h:int             grep_tree(char **argv);
./util.c:grep_tree(char **argv)
Binary file ./grep matches
./grep.c.orig:          c = grep_tree(argv);
./util.c.orig:grep_tree(char **argv)
-->8--

This diff moves the fake argv logic moves into grep_tree() where we can
opt for pretty-printing.  ok?


Index: grep.c
===================================================================
RCS file: /cvs/src/usr.bin/grep/grep.c,v
retrieving revision 1.63
diff -u -p -r1.63 grep.c
--- grep.c      2 Dec 2019 21:50:11 -0000       1.63
+++ grep.c      2 Dec 2019 21:53:14 -0000
@@ -473,12 +473,6 @@ main(int argc, char *argv[])
                ++argv;
        }
 
-       if (Rflag && argc == 0) {
-               /* default to . if no path given */
-               static char *dot_argv[] = { ".", NULL };
-               argv = dot_argv;
-               argc = 1;
-       }
        if (Eflag)
                cflags |= REG_EXTENDED;
        if (Fflag)
@@ -516,7 +510,7 @@ main(int argc, char *argv[])
        if ((argc == 0 || argc == 1) && !Rflag && !Hflag)
                hflag = 1;
 
-       if (argc == 0)
+       if (argc == 0 && !Rflag)
                exit(!procfile(NULL));
 
        if (Rflag)
Index: util.c
===================================================================
RCS file: /cvs/src/usr.bin/grep/util.c,v
retrieving revision 1.61
diff -u -p -r1.61 util.c
--- util.c      7 Oct 2019 17:47:32 -0000       1.61
+++ util.c      2 Dec 2019 21:53:15 -0000
@@ -61,6 +61,12 @@ grep_tree(char **argv)
        FTS     *fts;
        FTSENT  *p;
        int     c, fts_flags;
+       char    *dot_argv[] = { ".", NULL };
+       char    *path;
+
+       /* default to . if no path given */
+       if (argv[0] == NULL)
+               argv = dot_argv;
 
        c = 0;
 
@@ -81,7 +87,11 @@ grep_tree(char **argv)
                case FTS_DP:
                        break;
                default:
-                       c += procfile(p->fts_path);
+                       path = p->fts_path;
+                       /* skip "./" if implied */
+                       if (argv == dot_argv && p->fts_pathlen >= 2)
+                               path += 2;
+                       c += procfile(path);
                        break;
                }
        }

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to