Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Add -m to pkg-which. Shows what files matched using globs
Aurelien ROUGEMONT committed 9 years ago
commit 7aa190bb98ffa739bb3ebb4cf09f59bef786f8de
parent 62d13b4
1 file changed +29 -8
modified src/which.c
@@ -32,13 +32,16 @@

#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;
@@ -46,7 +49,7 @@ 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");
}

@@ -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));