pkg: clean up pkg_find() by adding pkg_try_specific_path(). (issue #16)

feature/tap-sh
William Pitcock 2012-05-07 03:57:28 -05:00
parent 2aaa1ce0df
commit 4b193018a4
1 changed files with 28 additions and 56 deletions

84
pkg.c
View File

@ -211,13 +211,33 @@ pkg_free(pkg_t *pkg)
free(pkg); free(pkg);
} }
static inline pkg_t *
pkg_try_specific_path(const char *path, const char *name, unsigned int flags)
{
pkg_t *pkg = NULL;
FILE *f;
char locbuf[PKG_CONFIG_PATH_SZ];
char uninst_locbuf[PKG_CONFIG_PATH_SZ];
snprintf(locbuf, sizeof locbuf, "%s/%s" PKG_CONFIG_EXT, path, name);
snprintf(uninst_locbuf, sizeof uninst_locbuf, "%s/%s-uninstalled" PKG_CONFIG_EXT, path, name);
if (!(flags & PKGF_NO_UNINSTALLED) && (f = fopen(uninst_locbuf, "r")) != NULL)
{
pkg = pkg_new_from_file(uninst_locbuf, f);
pkg->uninstalled = true;
}
else if ((f = fopen(locbuf, "r")) != NULL)
pkg = pkg_new_from_file(locbuf, f);
return pkg;
}
#ifdef _WIN32 #ifdef _WIN32
pkg_t * pkg_t *
pkg_find_in_registry_key(HKEY hkey, const char *name, unsigned int flags) pkg_find_in_registry_key(HKEY hkey, const char *name, unsigned int flags)
{ {
pkg_t *pkg = NULL; pkg_t *pkg = NULL;
char locbuf[PKG_CONFIG_PATH_SZ];
char uninst_locbuf[PKG_CONFIG_PATH_SZ];
HKEY key; HKEY key;
int i = 0; int i = 0;
@ -238,30 +258,11 @@ pkg_find_in_registry_key(HKEY hkey, const char *name, unsigned int flags)
if (RegQueryValueEx(key, buf, NULL, &type, pathbuf, &pathbuflen) if (RegQueryValueEx(key, buf, NULL, &type, pathbuf, &pathbuflen)
== ERROR_SUCCESS && type == REG_SZ) == ERROR_SUCCESS && type == REG_SZ)
{ {
FILE *f; pkg = pkg_try_specific_path(pathbuf, name, flags);
/* XXX: support REG_EXPAND_SZ? */ if (pkg != NULL)
snprintf(locbuf, sizeof locbuf, "%s/%s" PKG_CONFIG_EXT,
pathbuf, name);
snprintf(uninst_locbuf, sizeof uninst_locbuf,
"%s/%s-uninstalled" PKG_CONFIG_EXT, pathbuf, name);
if (!(flags & PKGF_NO_UNINSTALLED)
&& (f = fopen(uninst_locbuf, "r")) != NULL)
{
pkg = pkg_new_from_file(locbuf, f);
pkg->uninstalled = true;
break; break;
} }
if ((f = fopen(locbuf, "r")) != NULL)
{
pkg = pkg_new_from_file(locbuf, f);
break;
}
}
bufsize = sizeof buf; bufsize = sizeof buf;
} }
@ -273,8 +274,6 @@ pkg_find_in_registry_key(HKEY hkey, const char *name, unsigned int flags)
pkg_t * pkg_t *
pkg_find(const char *name, unsigned int flags) pkg_find(const char *name, unsigned int flags)
{ {
char locbuf[PKG_CONFIG_PATH_SZ];
char uninst_locbuf[PKG_CONFIG_PATH_SZ];
char **path = NULL; char **path = NULL;
size_t count = 0, iter = 0; size_t count = 0, iter = 0;
const char *env_path; const char *env_path;
@ -296,23 +295,10 @@ pkg_find(const char *name, unsigned int flags)
for (iter = 0; iter < count; iter++) for (iter = 0; iter < count; iter++)
{ {
snprintf(locbuf, sizeof locbuf, "%s/%s" PKG_CONFIG_EXT, path[iter], name); pkg = pkg_try_specific_path(path[iter], name, flags);
snprintf(uninst_locbuf, sizeof uninst_locbuf, "%s/%s-uninstalled" PKG_CONFIG_EXT, path[iter], name); if (pkg != NULL)
if (!(flags & PKGF_NO_UNINSTALLED) && (f = fopen(uninst_locbuf, "r")) != NULL)
{
pkg = pkg_new_from_file(locbuf, f);
pkg->uninstalled = true;
goto out; goto out;
} }
if ((f = fopen(locbuf, "r")) != NULL)
{
pkg = pkg_new_from_file(locbuf, f);
goto out;
}
}
} }
env_path = getenv("PKG_CONFIG_LIBDIR"); env_path = getenv("PKG_CONFIG_LIBDIR");
@ -325,28 +311,14 @@ pkg_find(const char *name, unsigned int flags)
for (iter = 0; iter < count; iter++) for (iter = 0; iter < count; iter++)
{ {
snprintf(locbuf, sizeof locbuf, "%s/%s" PKG_CONFIG_EXT, path[iter], name); pkg = pkg_try_specific_path(path[iter], name, flags);
snprintf(uninst_locbuf, sizeof uninst_locbuf, "%s/%s-uninstalled" PKG_CONFIG_EXT, path[iter], name); if (pkg != NULL)
if (!(flags & PKGF_NO_UNINSTALLED) && (f = fopen(uninst_locbuf, "r")) != NULL)
{
pkg_t *pkg = pkg_new_from_file(locbuf, f);
pkg->uninstalled = true;
goto out; goto out;
} }
if ((f = fopen(locbuf, "r")) != NULL)
{
pkg = pkg_new_from_file(locbuf, f);
goto out;
}
}
} }
#ifdef _WIN32 #ifdef _WIN32
/* support getting PKG_CONFIG_PATH from registry */ /* support getting PKG_CONFIG_PATH from registry */
pkg = pkg_find_in_registry_key(HKEY_CURRENT_USER, name, flags); pkg = pkg_find_in_registry_key(HKEY_CURRENT_USER, name, flags);
if (!pkg) if (!pkg)
pkg = pkg_find_in_registry_key(HKEY_LOCAL_MACHINE, name, flags); pkg = pkg_find_in_registry_key(HKEY_LOCAL_MACHINE, name, flags);