Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
pkg-which: iteration over arguments
Elvira Khabirova committed 12 years ago
commit 49599f003babc8f0e7f7a382cb95197540b557cd
parent 01cb437
1 file changed +85 -62
modified src/which.c
@@ -55,13 +55,14 @@ exec_which(int argc, char **argv)
	struct pkgdb_it *it = NULL;
	struct pkg *pkg = NULL;
	char pathabs[MAXPATHLEN];
-
	char *p, *path;
+
	char *p, *path, *match;
	int ret = EPKG_OK, retcode = EX_SOFTWARE;
	int ch;
	int res, pathlen;
	bool orig = false;
	bool glob = false;
	bool search = false;
+
	bool search_s = false;

	while ((ch = getopt(argc, argv, "qgop")) != -1) {
		switch (ch) {
@@ -75,7 +76,7 @@ exec_which(int argc, char **argv)
			orig = true;
			break;
		case 'p':
-
			search = true;
+
			search_s = true;
			break;
		default:
			usage_which();
@@ -86,86 +87,108 @@ exec_which(int argc, char **argv)
	argc -= optind;
	argv += optind;

-
	if (argc != 1) {
+
	if (argc < 1) {
		usage_which();
		return (EX_USAGE);
	}

-
	if (search) {
-
		if ((p = getenv("PATH")) == NULL) {
-
			printf("$PATH not set, falling back to non-search behaviour\n");
-
			search = false;
-
		} else {
-
			pathlen = strlen(p) + 1;
-

-
			path = malloc(pathlen);
-
			if (path == NULL)
-
				return (EX_OSERR);
+
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
+
		return (EX_IOERR);
+
	}

-
			strlcpy(path, p, pathlen);
+
	if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY, 0, 0) != EPKG_OK) {
+
		pkgdb_close(db);
+
		warnx("Cannot get a read lock on a database, it is locked by another process");
+
		return (EX_TEMPFAIL);
+
	}

-
			if (strlen(argv[0]) >= FILENAME_MAX)
-
				return (EX_USAGE);
+
	if (search_s) {
+
		if ((path = getenv("PATH")) == NULL) {
+
			printf("$PATH is not set, falling back to non-search behaviour\n");
+
			search_s = false;
+
		} else {
+
			pathlen = strlen(path) + 1;
+
		}
+
	}

-
			p = NULL;
-
			res = get_match(&p, path, argv[0]);
-
			if (res == (EX_USAGE)) {
-
				printf("%s was not found in PATH, falling back to non-search behaviour\n", argv[0]);
+
	while (argc >= 1) {
+
		retcode = EX_SOFTWARE;
+
		if (search_s) {
+
			if ((argv[0][0] == '.') || (argv[0][0] == '/')) {
				search = false;
-
				free(path);
-
			} else if (res == (EX_OSERR)) {
-
				free(path);
-
				return (EX_OSERR);
			} else {
-
				absolutepath(p, pathabs, sizeof(pathabs));
+
				search = true;
+

+
				if (strlen(argv[0]) >= FILENAME_MAX) {
+
					retcode = EX_USAGE;
+
					goto cleanup;
+
				}
+

+
				p = malloc(pathlen);
+
				if (p == NULL) {
+
					retcode = EX_OSERR;
+
					goto cleanup;
+
				}
+
				strlcpy(p, path, pathlen);
+

+
				match = NULL;
+
				res = get_match(&match, p, argv[0]);
				free(p);
+

+
				if (res == (EX_USAGE)) {
+
					printf("%s was not found in PATH, falling back to non-search behaviour\n", argv[0]);
+
					search = false;
+
				} else if (res == (EX_OSERR)) {
+
					retcode = EX_OSERR;
+
					goto cleanup;
+
				} else {
+
					absolutepath(match, pathabs, sizeof(pathabs));
+
					free(match);
+
				}
			}
		}
-
	}

-
	if (!glob && !search)
-
		absolutepath(argv[0], pathabs, sizeof(pathabs));
-
	else if (!search) {
-
		if (strlcpy(pathabs, argv[0], sizeof(pathabs)) >= sizeof(pathabs))
-
			return (EX_USAGE);
-
	}
+
		if (!glob && !search)
+
			absolutepath(argv[0], pathabs, sizeof(pathabs));
+
		else if (!search) {
+
			if (strlcpy(pathabs, argv[0], sizeof(pathabs)) >= sizeof(pathabs))
+
				retcode = EX_USAGE;
+
				goto cleanup;
+
		}

-
	if (pkgdb_open(&db, PKGDB_DEFAULT) != EPKG_OK) {
-
		return (EX_IOERR);
-
	}

-
	if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY, 0, 0) != EPKG_OK) {
-
		pkgdb_close(db);
-
		warnx("Cannot get a read lock on a database, it is locked by another process");
-
		return (EX_TEMPFAIL);
-
	}
+
		if ((it = pkgdb_query_which(db, pathabs, glob)) == NULL) {
+
			retcode = EX_IOERR;
+
			goto cleanup;
+
		}

-
	if ((it = pkgdb_query_which(db, pathabs, glob)) == NULL) {
-
		retcode = EX_IOERR;
-
		goto cleanup;
-
	}
+
		pkg = NULL;
+
		while ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
+
			retcode = EX_OK;
+
			if (quiet && orig)
+
				pkg_printf("%o\n", pkg);
+
			else if (quiet && !orig)
+
				pkg_printf("%n-%v\n", pkg, pkg);
+
			else if (!quiet && orig)
+
				pkg_printf("%S was installed by package %o\n", pathabs, pkg);
+
			else if (!quiet && !orig)
+
				pkg_printf("%S was installed by package %n-%v\n", pathabs, pkg, pkg);
+
		}

-
	while ((ret = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
-
		retcode = EX_OK;
-
		if (quiet && orig)
-
			pkg_printf("%o\n", pkg);
-
		else if (quiet && !orig)
-
			pkg_printf("%n-%v\n", pkg, pkg);
-
		else if (!quiet && orig)
-
			pkg_printf("%S was installed by package %o\n", pathabs, pkg);
-
		else if (!quiet && !orig)
-
			pkg_printf("%S was installed by package %n-%v\n", pathabs, pkg, pkg);
-
	}
+
		if (retcode != EX_OK && !quiet)
+
			printf("%s was not found in the database\n", pathabs);
+

+
		pkg_free(pkg);
+
		pkgdb_it_free(it);

-
	if (retcode != EX_OK && !quiet)
-
		printf("%s was not found in the database\n", pathabs);
+
		argc--;
+
		argv++;

-
	pkg_free(pkg);
-
	pkgdb_it_free(it);
+
	}

-
cleanup:
-
	pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
-
	pkgdb_close(db);
+
	cleanup:
+
		pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
+
		pkgdb_close(db);

	return (retcode);
}