X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=plocate.cpp;h=423aaf7e632f408bcdedcb48163c1918e7a7510b;hb=894e2278390c1f9d3bf4246685554246abfc0600;hp=e08c019b84d58f7b74054533010cfb61adfe4c16;hpb=88057cd64f62c3f39e8ccf158fd0c3df101e02cb;p=plocate diff --git a/plocate.cpp b/plocate.cpp index e08c019..423aaf7 100644 --- a/plocate.cpp +++ b/plocate.cpp @@ -52,8 +52,10 @@ 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(); +bool stdout_is_tty = false; steady_clock::time_point start; ZSTD_DDict *ddict = nullptr; @@ -194,9 +196,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; } @@ -489,6 +501,8 @@ void do_search_file(const vector &needles, const char *filename) // the pattern and done a union of them, but that's a lot of // work for fairly unclear gain.) uint64_t matched = scan_all_docids(needles, fd, corpus); + dprintf("Done in %.1f ms, found %" PRId64 " matches.\n", + 1e3 * duration(steady_clock::now() - start).count(), matched); if (only_count) { printf("%" PRId64 "\n", matched); } @@ -588,7 +602,7 @@ void do_search_file(const vector &needles, const char *filename) if (done) return; - uint32_t trgm __attribute__((unused)) = trgmptr.trgm; + uint32_t trgm = trgmptr.trgm; const unsigned char *pldata = reinterpret_cast(s.data()); size_t num = trgmptr.num_docids; decoded.resize(num); @@ -638,6 +652,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 +661,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 +683,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 +691,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 +701,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:0rwVD", long_options, &option_index); if (c == -1) { break; } switch (c) { + case 'b': + match_basename = true; + break; case 'c': only_count = true; break; @@ -718,6 +739,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; @@ -744,6 +768,10 @@ int main(int argc, char **argv) } } + if (!print_nul) { + stdout_is_tty = isatty(1); + } + vector needles; for (int i = optind; i < argc; ++i) { Needle needle;