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