From 7aa190bb98ffa739bb3ebb4cf09f59bef786f8de Mon Sep 17 00:00:00 2001 From: Aurelien ROUGEMONT Date: Wed, 22 Feb 2017 22:36:42 +0100 Subject: [PATCH] Add -m to pkg-which. Shows what files matched using globs --- src/which.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/which.c b/src/which.c index c6d52e5a6e..c2382caf72 100644 --- a/src/which.c +++ b/src/which.c @@ -32,13 +32,16 @@ #include #include -#include #include #include #include #include #include +#include + +#include +#include #include "pkgcli.h" typedef kvec_t(char *) charlist; @@ -46,7 +49,7 @@ typedef kvec_t(char *) charlist; void usage_which(void) { - fprintf(stderr, "Usage: pkg which [-qgop] \n\n"); + fprintf(stderr, "Usage: pkg which [-mqgop] \n\n"); fprintf(stderr, "For more information see 'pkg help which'.\n"); } @@ -71,6 +74,7 @@ exec_which(int argc, char **argv) struct pkgdb *db = NULL; struct pkgdb_it *it = NULL; struct pkg *pkg = NULL; + struct pkg_file *file = NULL; char pathabs[MAXPATHLEN]; char *p, *path, *match, *savedpath; int ret = EPKG_OK, retcode = EX_SOFTWARE; @@ -80,19 +84,22 @@ exec_which(int argc, char **argv) bool glob = false; bool search = false; bool search_s = false; + bool show_match = false; charlist patterns; + UT_string *filename; struct option longopts[] = { { "glob", no_argument, NULL, 'g' }, { "origin", no_argument, NULL, 'o' }, { "path-search", no_argument, NULL, 'p' }, { "quiet", no_argument, NULL, 'q' }, + { "show-match", no_argument, NULL, 'm' }, { NULL, 0, NULL, 0 }, }; path = NULL; - while ((ch = getopt_long(argc, argv, "+gopq", longopts, NULL)) != -1) { + while ((ch = getopt_long(argc, argv, "+gopqm", longopts, NULL)) != -1) { switch (ch) { case 'g': glob = true; @@ -106,6 +113,9 @@ exec_which(int argc, char **argv) case 'q': quiet = true; break; + case 'm': + show_match = true; + break; default: usage_which(); return (EX_USAGE); @@ -206,16 +216,27 @@ exec_which(int argc, char **argv) } pkg = NULL; - while ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) { + while ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_FILES)) == EPKG_OK) { retcode = EX_OK; - if (quiet && orig) + if (quiet && orig && !show_match) pkg_printf("%o\n", pkg); - else if (quiet && !orig) + else if (quiet && !orig && !show_match) pkg_printf("%n-%v\n", pkg, pkg); - else if (!quiet && orig) + else if (!quiet && orig && !show_match) pkg_printf("%S was installed by package %o\n", kv_A(patterns, i), pkg); - else if (!quiet && !orig) + else if (!quiet && !orig && !show_match) pkg_printf("%S was installed by package %n-%v\n", kv_A(patterns, i), pkg, pkg); + else if (!quiet && glob && show_match) { + pkg_printf("%S was glob searched and found in package %n-%v\n", kv_A(patterns, i), pkg, pkg, pkg); + + while(pkg_files(pkg, &file) == EPKG_OK) { + utstring_new(filename); + pkg_utstring_printf(filename, "%Fn", file); + if(!fnmatch(kv_A(patterns, i), utstring_body(filename), 0)) + printf("%s\n", utstring_body(filename)); + utstring_free(filename); + } + } } if (retcode != EX_OK && !quiet) printf("%s was not found in the database\n", kv_A(patterns, i));