#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <alpm.h>
#include <alpm_list.h>
#include "pacman.h"
#include "package.h"
#include "conf.h"
#include "util.h"
#define LOCAL_PREFIX "local/"
static int search_path(char **filename, struct stat *bufptr)
{
char *envpath, *envpathsplit, *path, *fullname;
size_t flen;
if((envpath = getenv("PATH")) == NULL) {
return -1;
}
if((envpath = envpathsplit = strdup(envpath)) == NULL) {
return -1;
}
flen = strlen(*filename);
while((path = strsep(&envpathsplit, ":")) != NULL) {
size_t plen = strlen(path);
while(path[plen - 1] == '/') {
path[--plen] = '\0';
}
fullname = malloc(plen + flen + 2);
if(!fullname) {
free(envpath);
return -1;
}
sprintf(fullname, "%s/%s", path, *filename);
if(lstat(fullname, bufptr) == 0) {
free(*filename);
*filename = fullname;
free(envpath);
return 0;
}
free(fullname);
}
free(envpath);
return -1;
}
static void print_query_fileowner(const char *filename, alpm_pkg_t *info)
{
if(!config->quiet) {
printf(_("%s is owned by %s %s\n"), filename,
alpm_pkg_get_name(info), alpm_pkg_get_version(info));
} else {
printf("%s\n", alpm_pkg_get_name(info));
}
}
static int query_fileowner(alpm_list_t *targets)
{
int ret = 0;
char path[PATH_MAX];
size_t rootlen;
alpm_list_t *t;
alpm_db_t *db_local;
if(targets == NULL) {
pm_printf(ALPM_LOG_ERROR, _("no file was specified for --owns\n"));
return 1;
}
if(!realpath(alpm_option_get_root(config->handle), path)) {
pm_printf(ALPM_LOG_ERROR, _("cannot determine real path for '%s': %s\n"),
path, strerror(errno));
return 1;
}
rootlen = strlen(path);
if(rootlen + 2 > PATH_MAX) {
pm_printf(ALPM_LOG_ERROR, _("path too long: %s%s\n"), path, "");
return 1;
}
path[rootlen++] = '/';
path[rootlen] = '\0';
db_local = alpm_get_localdb(config->handle);
for(t = targets; t; t = alpm_list_next(t)) {
char *filename = NULL, *dname = NULL, *rpath = NULL;
const char *bname;
struct stat buf;
alpm_list_t *i;
size_t len;
unsigned int found = 0;
|