pactree: get DBPath from the pacman config
The pacman database location is hardcoded at build time and initialized with static const char *dbpath = DBPATH
.
It is possible for the user to set an alternate path using the -b
command-line option, but it would be nice if the current value was automatically detected without having to do anything special, something like this:
int main(int argc, char *argv[])
{
/*...*/
char cur_dbpath[PATH_MAX];
FILE *f = popen("pacman-conf DBPath", "r");
if (f) {
if (fgets(cur_dbpath, sizeof(cur_dbpath) - 1, f) != NULL) {
size_t len = strlen(cur_dbpath);
if (cur_dbpath[len - 1] == '\n') {
cur_dbpath[len - 1] = '\0';
}
dbpath = cur_dbpath;
}
pclose(f);
}
if((ret = parse_options(argc, argv)) != 0) {
/*...*/
}
Other tools available in pacman-contrib such as checkupdates or pacdiff do the exact same thing.