From: Steinar H. Gunderson Date: Sat, 17 Oct 2020 07:46:55 +0000 (+0200) Subject: Implement the -b (--basename) option. X-Git-Tag: 1.0.5~6 X-Git-Url: https://git.sesse.net/?p=plocate;a=commitdiff_plain;h=b6d598e43038ad053e2b15141fcb3c18c13ba025 Implement the -b (--basename) option. --- diff --git a/plocate.1 b/plocate.1 index e58ad58..85c6fcc 100644 --- a/plocate.1 +++ b/plocate.1 @@ -57,6 +57,13 @@ bit set to access the index (which is built as root), but by testing visibility as the calling user. .SH OPTIONS +.TP +\fB\-b\fR, \fB\-\-basename\fR +Match only against the file name portion of the path name, +ie., the directory names will be excluded from the match +(but still printed). This does not speed up the search, +but can suppress uninteresting matches. + .TP \fB\-c\fR, \fB\-\-count\fR Do not print each match. Instead, count them, and print out a total @@ -110,6 +117,13 @@ be POSIX .I extended regular expressions. +.TP +\fB\-w\fR, \fB\-\-wholename\fR +Match against the entire path name. This is the default, +so unless \fB-b\fR is given first (see above), it will not do +anything. This option thus exists only as compatibility with +.BR mlocate (1). + .TP .B \-\-help Print out usage information, then exit successfully. diff --git a/plocate.cpp b/plocate.cpp index e08c019..55e1934 100644 --- a/plocate.cpp +++ b/plocate.cpp @@ -52,6 +52,7 @@ bool use_debug = false; bool flush_cache = false; bool patterns_are_regex = false; bool use_extended_regex = false; +bool match_basename = false; int64_t limit_matches = numeric_limits::max(); int64_t limit_left = numeric_limits::max(); @@ -194,9 +195,19 @@ void scan_file_block(const vector &needles, string_view compressed, for (const char *filename = block.data(); filename != block.data() + block.size(); filename += strlen(filename) + 1) { + const char *haystack = filename; + if (match_basename) { + haystack = strrchr(filename, '/'); + if (haystack == nullptr) { + haystack = filename; + } else { + ++haystack; + } + } + bool found = true; for (const Needle &needle : needles) { - if (!matches(needle, filename)) { + if (!matches(needle, haystack)) { found = false; break; } @@ -638,6 +649,7 @@ void usage() printf( "Usage: plocate [OPTION]... PATTERN...\n" "\n" + " -b, --basename search only the file name portion of path names\n" " -c, --count print number of matches instead of the matches\n" " -d, --database DBPATH search for files in DBPATH\n" " (default is " DEFAULT_DBPATH ")\n" @@ -646,6 +658,7 @@ void usage() " -0, --null delimit matches by NUL instead of newline\n" " -r, --regexp interpret patterns as basic regexps (slow)\n" " --regex interpret patterns as extended regexps (slow)\n" + " -w, --wholename search the entire path name (default; see -b)\n" " --help print this help\n" " --version print version information\n"); } @@ -667,6 +680,7 @@ int main(int argc, char **argv) static const struct option long_options[] = { { "help", no_argument, 0, 'h' }, { "count", no_argument, 0, 'c' }, + { "basename", no_argument, 0, 'b' }, { "database", required_argument, 0, 'd' }, { "ignore-case", no_argument, 0, 'i' }, { "limit", required_argument, 0, 'l' }, @@ -674,6 +688,7 @@ int main(int argc, char **argv) { "version", no_argument, 0, 'V' }, { "regexp", no_argument, 0, 'r' }, { "regex", no_argument, 0, EXTENDED_REGEX }, + { "wholename", no_argument, 0, 'w' }, { "debug", no_argument, 0, 'D' }, // Not documented. // Enable to test cold-cache behavior (except for access()). Not documented. { "flush-cache", no_argument, 0, FLUSH_CACHE }, @@ -683,11 +698,14 @@ int main(int argc, char **argv) setlocale(LC_ALL, ""); for (;;) { int option_index = 0; - int c = getopt_long(argc, argv, "cd:hil:n:0VD", long_options, &option_index); + int c = getopt_long(argc, argv, "bcd:hil:n:0wVD", long_options, &option_index); if (c == -1) { break; } switch (c) { + case 'b': + match_basename = true; + break; case 'c': only_count = true; break; @@ -718,6 +736,9 @@ int main(int argc, char **argv) patterns_are_regex = true; use_extended_regex = true; break; + case 'w': + match_basename = false; // No-op unless -b is given first. + break; case 'D': use_debug = true; break;