[Bug 280676] grep default recursive behavior differs from manpage

From: <bugzilla-noreply_at_freebsd.org>
Date: Fri, 09 Aug 2024 18:33:32 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=280676

--- Comment #3 from John Baldwin <jhb@FreeBSD.org> ---
Looks like macOS uses BSD grep, and it fixed the code to match the manpage. 
BSD grep treats '-r' and '-R' as identical, whereas GNU grep uses '-r' to mean
the equivalent of '-rO' in BSD grep and '-R' to mean the equivalent of '-rS'.

However, it seems like the grep in FreeBSD is a bit more broken.  To test, I
created a hierarchy like so:

```
> ls -l
total 2
drwxr-xr-x  2 john john  3 Aug  9 14:20 bar
drwxr-xr-x  2 john john  3 Aug  9 14:20 baz
lrwxr-xr-x  1 john john 11 Aug  9 14:20 foo -> bar/foo.txt
> ls -l bar/
total 1
-rw-r--r--  1 john john 5 Aug  9 14:19 foo.txt
> ls -l baz
total 1
lrwxr-xr-x  1 john john 14 Aug  9 14:20 bar.txt -> ../bar/foo.txt
> cat bar/foo.txt 
blah
```

I then tested grep -r on macOS with various options which gave the following:

```
> grep -r blah foo bar baz
bar/foo.txt:blah

> grep -rS blah foo bar baz
foo:blah
bar/foo.txt:blah
baz/bar.txt:blah

> grep -rO blah foo bar baz
foo:blah
bar/foo.txt:blah
```

On FreeBSD it seems that the options don't really work at all:

```
> grep -rS blah foo bar baz
foo:blah
bar/foo.txt:blah
baz/bar.txt:blah

> grep -rO blah foo bar baz
foo:blah
bar/foo.txt:blah
baz/bar.txt:blah

> grep -rp blah foo bar baz
foo:blah
bar/foo.txt:blah
baz/bar.txt:blah
```

That is to say, it acts like -S is always enabled.  One issue is that when -O
is specified, we only include FTS_COMFOLLOW in the flags passed to fts_open()
in grep_tree(), when it should be FTS_COMFOLLOW | FTS_PHYSICAL in that case
(the fts manpage says that one of FTS_LOGICAL or FTS_PHYSICAL must be
specified).  But it is also true that in the code the default setting of
'link_behave' is indeed -S.

-- 
You are receiving this mail because:
You are the assignee for the bug.