Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/pkg
Path: blob/main/src/rwhich.c
5235 views
1
/*-
2
* Copyright (c) 2025 Baptiste Daroussin <[email protected]>
3
*
4
* SPDX-License-Identifier: BSD-2-Clause
5
*/
6
7
#include <sys/param.h>
8
9
#include <err.h>
10
#include <getopt.h>
11
#include <stdio.h>
12
#include <string.h>
13
#include <unistd.h>
14
15
#include <pkg.h>
16
#include "pkgcli.h"
17
18
void
19
usage_rwhich(void)
20
{
21
fprintf(stderr, "Usage: pkg rwhich [-gq] [-r reponame] <file>\n\n");
22
fprintf(stderr, "For more information see 'pkg help rwhich'.\n");
23
}
24
25
int
26
exec_rwhich(int argc, char **argv)
27
{
28
struct pkgdb *db = NULL;
29
struct pkgdb_it *it = NULL;
30
struct pkg *pkg = NULL;
31
int retcode = EXIT_FAILURE;
32
int ch;
33
bool glob = false;
34
const char *reponame = NULL;
35
c_charv_t repos = vec_init();
36
37
struct option longopts[] = {
38
{ "glob", no_argument, NULL, 'g' },
39
{ "quiet", no_argument, NULL, 'q' },
40
{ "repository", required_argument, NULL, 'r' },
41
{ NULL, 0, NULL, 0 },
42
};
43
44
while ((ch = getopt_long(argc, argv, "+gqr:", longopts, NULL)) != -1) {
45
switch (ch) {
46
case 'g':
47
glob = true;
48
break;
49
case 'q':
50
quiet = true;
51
break;
52
case 'r':
53
reponame = optarg;
54
break;
55
default:
56
usage_rwhich();
57
return (EXIT_FAILURE);
58
}
59
}
60
61
argc -= optind;
62
argv += optind;
63
64
if (argc < 1) {
65
usage_rwhich();
66
return (EXIT_FAILURE);
67
}
68
69
if (pkgdb_open_all(&db, PKGDB_REMOTE, reponame) != EPKG_OK)
70
return (EXIT_FAILURE);
71
72
if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
73
pkgdb_close(db);
74
warnx("Cannot get a read lock on a database, "
75
"it is locked by another process");
76
return (EXIT_FAILURE);
77
}
78
79
if (reponame != NULL)
80
vec_push(&repos, reponame);
81
82
while (argc >= 1) {
83
if ((it = pkgdb_repo_which(db, argv[0], glob, &repos)) == NULL) {
84
retcode = EXIT_FAILURE;
85
goto cleanup;
86
}
87
88
pkg = NULL;
89
while (pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC) == EPKG_OK) {
90
retcode = EXIT_SUCCESS;
91
if (quiet)
92
pkg_printf("%n-%v\n", pkg, pkg);
93
else
94
pkg_printf("%S is provided by package %n-%v\n",
95
argv[0], pkg, pkg);
96
}
97
if (retcode != EXIT_SUCCESS && !quiet)
98
printf("%s was not found in the repository catalogue\n",
99
argv[0]);
100
101
pkg_free(pkg);
102
pkgdb_it_free(it);
103
104
argc--;
105
argv++;
106
}
107
108
cleanup:
109
vec_free(&repos);
110
pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
111
pkgdb_close(db);
112
113
return (retcode);
114
}
115
116