Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
new option -q (quiet) -o (shows origin) for pkg which
Baptiste Daroussin committed 14 years ago
commit 3676e637b3b976514d3575b6282053662b0b3f16
parent 032fc49
2 files changed +39 -9
modified pkg/pkg-which.8
@@ -15,7 +15,7 @@
.\"     @(#)pkg.8
.\" $FreeBSD$
.\"
-
.Dd February 16, 2012
+
.Dd April 12, 2012
.Dt PKG-WHICH 8
.Os
.Sh NAME
@@ -23,15 +23,20 @@
.Nd displays which package installed a specific file
.Sh SYNOPSIS
.Nm
+
.Op Fl qo
.Ar <file>
.Sh DESCRIPTION
.Nm
is used for displaying the package that installed
.Ar <file>
.Sh OPTIONS
-
The following commands are supported by
+
The following options are supported by
.Nm :
.Bl -tag -width F1
+
.It Fl q
+
Be quiet
+
.It Fl o
+
Shows the origin of the package instead of name-version
.El
.Sh ENVIRONMENT
The following environment variables affect the execution of
modified pkg/which.c
@@ -31,6 +31,7 @@
#include <stdio.h>
#include <pkg.h>
#include <string.h>
+
#include <unistd.h>
#include <sysexits.h>

#include "pkgcli.h"
@@ -38,7 +39,7 @@
void
usage_which(void)
{
-
	fprintf(stderr, "usage: pkg which <file>\n\n");
+
	fprintf(stderr, "usage: pkg which [-qo] <file>\n\n");
	fprintf(stderr, "For more information see 'pkg help which'.\n");
}

@@ -50,9 +51,25 @@ exec_which(int argc, char **argv)
	struct pkg *pkg = NULL;
	char pathabs[MAXPATHLEN + 1];
	int ret = EPKG_OK, retcode = EPKG_OK;
-
	const char *name, *version;
+
	const char *name, *version, *origin;
+
	int ch;
+
	bool orig = false;

-
	if (argc != 2) {
+
	while ((ch = getopt(argc, argv, "qo")) != -1) {
+
		switch (ch) {
+
			case 'q':
+
				quiet = true;
+
				break;
+
			case 'o':
+
				orig = true;
+
				break;
+
		}
+
	}
+

+
	argc -= optind;
+
	argv += optind;
+

+
	if (argc != 1) {
		usage_which();
		return (EX_USAGE);
	}
@@ -62,7 +79,7 @@ exec_which(int argc, char **argv)
		return (EX_IOERR);
	}

-
	absolutepath(argv[1], pathabs, sizeof(pathabs));
+
	absolutepath(argv[0], pathabs, sizeof(pathabs));

	if ((it = pkgdb_query_which(db, pathabs)) == NULL) {
		return (EX_IOERR);
@@ -70,12 +87,20 @@ exec_which(int argc, char **argv)

	if (( ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
		retcode = EPKG_OK;
-
		pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version);
-
		printf("%s was installed by package %s-%s\n", pathabs, name, version);
+
		pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version, PKG_ORIGIN, &origin);
+
		if (quiet && orig)
+
			printf("%s\n", origin);
+
		else if (quiet && !orig)
+
			printf("%s-%s\n", name, version);
+
		else if (!quiet && orig)
+
			printf("%s was installed by package %s\n", pathabs, origin);
+
		else if (!quiet && !orig)
+
			printf("%s was installed by package %s-%s\n", pathabs, name, version);
	} else if (ret != EPKG_END) {
		retcode = EPKG_WARN;
	} else {
-
		printf("%s was not found in the database\n", pathabs);
+
		if (!quiet)
+
			printf("%s was not found in the database\n", pathabs);
		retcode = EPKG_WARN;
	}