Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Introduce the new pkg which (pkg_info -W)
Baptiste Daroussin committed 15 years ago
commit ec40886ac07f86dd3c13ab9800861aecb718edf4
parent 02f08f5
4 files changed +72 -1
modified pkg/Makefile
@@ -1,7 +1,8 @@
PROG=	pkg
SRCS=	main.c \
		info.c \
-
		create.c
+
		create.c \
+
		which.c

CFLAGS+=	-I${.CURDIR}/../libpkg -I${.CURDIR}/../external/tinycdb/
LDADD+=	-L${.CURDIR}/../external -L../libpkg -lpkg
modified pkg/main.c
@@ -6,6 +6,7 @@

#include "create.h"
#include "info.h"
+
#include "which.h"

static void usage(void);

@@ -19,6 +20,7 @@ static struct commands {
	{ "info", cmd_info},
	{ "install", NULL},
	{ "update", NULL},
+
	{ "which", cmd_which},
	{ "help", NULL},
	{ NULL, NULL },
};
added pkg/which.c
@@ -0,0 +1,62 @@
+
#include <err.h>
+
#include <stdio.h>
+
#include <pkg.h>
+
#include <pkgdb.h>
+
#include <pkg_manifest.h>
+
#include <libgen.h>
+
#include <stdlib.h>
+
#include <string.h>
+
#include <unistd.h>
+
#include <sys/param.h>
+

+
#include "which.h"
+

+
int
+
cmd_which(int argc, char **argv)
+
{
+
	struct pkgdb db;
+
	struct pkg pkg;
+
	struct pkg_manifest *m;
+
	char pathabs[MAXPATHLEN];
+
	char pathabsdir[MAXPATHLEN];
+

+
	if (argc < 2 || argc > 4) {
+
		warnx("No file given");
+
		return (-1);
+
	}
+

+
	argc--;
+
	argv++;
+

+
	if (pkgdb_open(&db) == -1) {
+
		pkgdb_warn(&db);
+
		return (-1);
+
	}
+

+
	if (pkgdb_query_init(&db, NULL, MATCH_ALL) == -1) {
+
		pkgdb_warn(&db);
+
		return (-1);
+
	}
+

+
	realpath(dirname(argv[0]), pathabsdir);
+
	snprintf(pathabs, sizeof(pathabs), "%s/%s", pathabsdir, basename(argv[0]));
+

+
	while (pkgdb_query(&db, &pkg) == 0) {
+
		manifest_from_pkg(&pkg, &m);
+
		pkg_manifest_file_init(m);
+
		while (pkg_manifest_file_next(m) == 0) {
+
			if (strcmp(pathabs, pkg_manifest_file_path(m)) == 0) {
+
				printf("%s is owned by %s-%s\n", pathabs, pkg_name(&pkg), pkg_version(&pkg));
+
				pkgdb_query_free(&db);
+
				pkgdb_close(&db);
+
				return (0);
+
			}
+
		}
+

+
	}
+

+
	warnx("No packages owns %s", pathabs);
+
	pkgdb_query_free(&db);
+
	pkgdb_close(&db);
+
	return (-1);
+
}
added pkg/which.h
@@ -0,0 +1,6 @@
+

+
#ifndef _WHICH_H
+
#define _WHICH_H
+

+
int cmd_which(int argc, char **argv);
+
#endif