Skip to content

Commit

Permalink
Add -m to pkg-which. Shows what files matched using globs
Browse files Browse the repository at this point in the history
  • Loading branch information
beorn- authored and bapt committed Feb 28, 2017
1 parent 62d13b4 commit 7aa190b
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/which.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,24 @@

#include <err.h>
#include <getopt.h>
#include <pkg.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <kvec.h>
#include <fnmatch.h>

#include <utstring.h>

#include <pkg.h>
#include "pkgcli.h"

typedef kvec_t(char *) charlist;

void
usage_which(void)
{
fprintf(stderr, "Usage: pkg which [-qgop] <file>\n\n");
fprintf(stderr, "Usage: pkg which [-mqgop] <file>\n\n");
fprintf(stderr, "For more information see 'pkg help which'.\n");
}

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 7aa190b

Please sign in to comment.