Bug 237873 - Inconsistent behavior between the program and its man page bsdgrep(1)
Summary: Inconsistent behavior between the program and its man page bsdgrep(1)
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords: patch
Depends on:
Blocks: 230332
  Show dependency treegraph
 
Reported: 2019-05-13 09:29 UTC by WHR
Modified: 2024-04-29 09:02 UTC (History)
5 users (show)

See Also:


Attachments
bsdgrep-fix-manpage.diff (912 bytes, patch)
2019-05-13 09:31 UTC, WHR
no flags Details | Diff
bsdgrep-fix-program.diff (602 bytes, patch)
2019-05-13 09:31 UTC, WHR
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description WHR 2019-05-13 09:29:02 UTC
The man page bsdgrep(1) says the program won't follow any symbolic links when performing recursive search (with option '-r' or '-R').
But it follows! (try it!)

From the man page:

     -O      If -R is specified, follow symbolic links only if they were
             explicitly listed on the command line.  The default is not to
             follow symbolic links.

...

     -p      If -R is specified, no symbolic links are followed.  This is the
             default.

...

     -R, -r, --recursive
             Recursively search subdirectories listed.

     -S      If -R is specified, all symbolic links are followed.  The default
             is not to follow symbolic links.


This seems indicating that the option '-p' was the default, however the actual default was '-S', according to the source code (https://svnweb.freebsd.org/base/head/usr.bin/grep/grep.c?revision=334806&view=markup#l126).
Comment 1 WHR 2019-05-13 09:31:12 UTC
Created attachment 204352 [details]
bsdgrep-fix-manpage.diff
Comment 2 WHR 2019-05-13 09:31:39 UTC
Created attachment 204353 [details]
bsdgrep-fix-program.diff
Comment 3 WHR 2019-05-13 09:31:53 UTC
The attached 2 patches could fix the program or the man page.
Comment 4 Wolfram Schneider freebsd_committer freebsd_triage 2024-04-29 08:26:29 UTC
It seems that the -p option does not work in bsdgrep. How to reproduce:


date > date
ln -s date symlink

# two results expected
grep -r .
./date:Mon Apr 29 08:21:22 UTC 2024
./symlink:Mon Apr 29 08:21:22 UTC 2024

# one result expected
grep -pr .
./date:Mon Apr 29 08:21:22 UTC 2024
./symlink:Mon Apr 29 08:21:22 UTC 2024


# gnu grep works fine, two hits for -R and one for -r options
ggrep -R .
date:Mon Apr 29 08:21:22 UTC 2024
symlink:Mon Apr 29 08:21:22 UTC 2024

ggrep -r .
date:Mon Apr 29 08:21:22 UTC 2024
Comment 5 WHR 2024-04-29 09:02:42 UTC
> It seems that the -p option does not work in bsdgrep

By digging futher into the source code, it appears that bsdgrep(1) simply translates its 'LINK_SKIP' for '-p' into 'FTS_PHYSICAL' flag for fts_open(3); after that it didn't have any special handling for symbolic links. Thus for any symbolic link found by fts_read(3) will be followed in function 'procfile'.

It should have cases for 'FTS_SL' and 'FTS_SLNONE' to filter out symbolic links in case of 'LINK_SKIP', in this 'switch':
https://cgit.freebsd.org/src/tree/usr.bin/grep/util.c?id=e116e040f3091eca914a06dcd0bdd9f1aea23add#n155