commit 2b91953273eb7c9ebd5ba4d38e4fde73d78cb147 Author: Kyle Evans Date: Wed Feb 17 13:34:28 2021 -0600 grep: only read devices named on cmdline by default Assume a default of "read if explicitly mentioned" rather than "read" for -D. In particular, recursing into /dev will no longer get stuck on a tty, but one can still grep /dev/random. :100644 100644 338af7e6d45 3da48cbcbc7 M usr.bin/grep/grep.1 :100644 100644 166d3451774 a8d65930c71 M usr.bin/grep/grep.c :100644 100644 62e82c7f56b 0777760ab23 M usr.bin/grep/grep.h :100644 100644 a2520e24de8 9f191689cc5 M usr.bin/grep/util.c diff --git a/usr.bin/grep/grep.1 b/usr.bin/grep/grep.1 index 338af7e6d45..3da48cbcbc7 100644 --- a/usr.bin/grep/grep.1 +++ b/usr.bin/grep/grep.1 @@ -30,7 +30,7 @@ .\" .\" @(#)grep.1 8.3 (Berkeley) 4/18/94 .\" -.Dd February 4, 2021 +.Dd February 17, 2021 .Dt GREP 1 .Os .Sh NAME @@ -162,6 +162,10 @@ If the is set to .Dq Cm skip , devices are silently skipped. +Note that the default +.Dq Cm read +operates differently in recursive mode, as the default behavior is to skip +reading devices that were not explicitly listed as arguments. .It Fl d Ar action , Fl Fl directories= Ns Ar action Specify the demanded .Ar action diff --git a/usr.bin/grep/grep.c b/usr.bin/grep/grep.c index 166d3451774..a8d65930c71 100644 --- a/usr.bin/grep/grep.c +++ b/usr.bin/grep/grep.c @@ -113,7 +113,7 @@ const char *color; /* --color */ int grepbehave = GREP_BASIC; /* -EFGP: type of the regex */ int binbehave = BINFILE_BIN; /* -aIU: handling of binary files */ int filebehave = FILE_STDIO; -int devbehave = DEV_READ; /* -D: handling of devices */ +int devbehave = DEV_READ_NAMED; /* -D: handling of devices */ int dirbehave = DIR_READ; /* -dRr: handling of directories */ int linkbehave = LINK_READ; /* -OpS: handling of symlinks */ @@ -703,7 +703,7 @@ main(int argc, char *argv[]) initqueue(); if (aargc == 0 && dirbehave != DIR_RECURSE) - exit(!procfile("-")); + exit(!procfile("-", 0)); if (dirbehave == DIR_RECURSE) matched = grep_tree(aargv); @@ -711,7 +711,7 @@ main(int argc, char *argv[]) for (matched = false; aargc--; ++aargv) { if ((finclude || fexclude) && !file_matching(*aargv)) continue; - if (procfile(*aargv)) + if (procfile(*aargv, 0)) matched = true; } diff --git a/usr.bin/grep/grep.h b/usr.bin/grep/grep.h index 62e82c7f56b..0777760ab23 100644 --- a/usr.bin/grep/grep.h +++ b/usr.bin/grep/grep.h @@ -61,8 +61,9 @@ extern const char *errstr[]; #define DIR_SKIP 1 #define DIR_RECURSE 2 -#define DEV_READ 0 -#define DEV_SKIP 1 +#define DEV_READ_NAMED 0 +#define DEV_READ 1 +#define DEV_SKIP 2 #define LINK_READ 0 #define LINK_EXPLICIT 1 @@ -140,7 +141,7 @@ extern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */ /* util.c */ bool file_matching(const char *fname); -bool procfile(const char *fn); +bool procfile(const char *fn, long depth); bool grep_tree(char **argv); void *grep_malloc(size_t size); void *grep_calloc(size_t nmemb, size_t size); diff --git a/usr.bin/grep/util.c b/usr.bin/grep/util.c index a2520e24de8..9f191689cc5 100644 --- a/usr.bin/grep/util.c +++ b/usr.bin/grep/util.c @@ -182,7 +182,7 @@ grep_tree(char **argv) if (fexclude || finclude) ok &= file_matching(p->fts_path); - if (ok && procfile(p->fts_path)) + if (ok && procfile(p->fts_path, p->fts_level)) matched = true; break; } @@ -288,7 +288,7 @@ procmatches(struct mprintc *mc, struct parsec *pc, bool matched) * passing the lines to procline(). */ bool -procfile(const char *fn) +procfile(const char *fn, long depth) { struct parsec pc; struct mprintc mc; @@ -307,9 +307,13 @@ procfile(const char *fn) s = sb.st_mode & S_IFMT; if (dirbehave == DIR_SKIP && s == S_IFDIR) return (false); - if (devbehave == DEV_SKIP && (s == S_IFIFO || - s == S_IFCHR || s == S_IFBLK || s == S_IFSOCK)) - return (false); + if (s == S_IFIFO || s == S_IFCHR || s == S_IFBLK || + s == S_IFSOCK) { + if (devbehave == DEV_SKIP) + return (false); + if (devbehave == DEV_READ_NAMED && depth != 0) + return (false); + } } f = grep_open(fn); }