From 4bb46e20e3e56e908c7d522b3693cf423336f7cb Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Wed, 30 Nov 2016 22:32:17 -0600 Subject: [PATCH] libpkgconf: add path matching and environment building functions --- libpkgconf/libpkgconf.h | 2 ++ libpkgconf/path.c | 31 +++++++++++++++++++++++++++++++ libpkgconf/pkg.c | 21 +++------------------ main.c | 39 ++++++++++++--------------------------- 4 files changed, 48 insertions(+), 45 deletions(-) diff --git a/libpkgconf/libpkgconf.h b/libpkgconf/libpkgconf.h index 3653cb7..136ffec 100644 --- a/libpkgconf/libpkgconf.h +++ b/libpkgconf/libpkgconf.h @@ -238,5 +238,7 @@ void pkgconf_audit_log_dependency(const pkgconf_pkg_t *dep, const pkgconf_depend /* path.c */ void pkgconf_path_add(const char *text, pkgconf_list_t *dirlist); size_t pkgconf_path_split(const char *text, pkgconf_list_t *dirlist); +size_t pkgconf_path_build_from_environ(const char *environ, const char *fallback, pkgconf_list_t *dirlist); +bool pkgconf_path_match_list(const char *path, pkgconf_list_t *dirlist); #endif diff --git a/libpkgconf/path.c b/libpkgconf/path.c index 424c35c..bb9b443 100644 --- a/libpkgconf/path.c +++ b/libpkgconf/path.c @@ -47,3 +47,34 @@ pkgconf_path_split(const char *text, pkgconf_list_t *dirlist) return count; } +size_t +pkgconf_path_build_from_environ(const char *environ, const char *fallback, pkgconf_list_t *dirlist) +{ + const char *data; + + data = getenv(environ); + if (data != NULL) + return pkgconf_path_split(data, dirlist); + + if (fallback != NULL) + return pkgconf_path_split(fallback, dirlist); + + /* no fallback and no environment variable, thusly no nodes added */ + return 0; +} + +bool +pkgconf_path_match_list(const char *path, pkgconf_list_t *dirlist) +{ + pkgconf_node_t *n = NULL; + + PKGCONF_FOREACH_LIST_ENTRY(dirlist->head, n) + { + pkgconf_path_t *pnode = n->data; + + if (!strcmp(pnode->path, path)) + return true; + } + + return false; +} diff --git a/libpkgconf/pkg.c b/libpkgconf/pkg.c index a7eff60..102a161 100644 --- a/libpkgconf/pkg.c +++ b/libpkgconf/pkg.c @@ -66,20 +66,13 @@ str_has_suffix(const char *str, const char *suffix) } static inline const char * -get_pkgconfig_path(void) +get_default_pkgconfig_path(void) { - const char *env_path; #ifdef _WIN32 static char outbuf[MAX_PATH]; char namebuf[MAX_PATH]; char *p; -#endif - env_path = getenv("PKG_CONFIG_LIBDIR"); - if (env_path != NULL) - return env_path; - -#ifdef _WIN32 int sizepath = GetModuleFileName(NULL, namebuf, sizeof namebuf); char * winslash; namebuf[sizepath] = '\0'; @@ -127,21 +120,13 @@ static pkgconf_list_t pkg_dir_list = PKGCONF_LIST_INITIALIZER; static void pkgconf_pkg_dir_list_build(unsigned int flags) { - const char *env_path; - if (pkg_dir_list.head != NULL || pkg_dir_list.tail != NULL) return; - /* PKG_CONFIG_PATH has to take precedence */ - env_path = getenv("PKG_CONFIG_PATH"); - if (env_path) - pkgconf_path_split(env_path, &pkg_dir_list); + pkgconf_path_build_from_environ("PKG_CONFIG_PATH", NULL, &pkg_dir_list); if (!(flags & PKGCONF_PKG_PKGF_ENV_ONLY)) - { - env_path = get_pkgconfig_path(); - pkgconf_path_split(env_path, &pkg_dir_list); - } + pkgconf_path_build_from_environ("PKG_CONFIG_LIBDIR", get_default_pkgconfig_path(), &pkg_dir_list); } typedef void (*pkgconf_pkg_parser_keyword_func_t)(pkgconf_pkg_t *pkg, const ptrdiff_t offset, char *value, unsigned int flags); diff --git a/main.c b/main.c index 2b3eb69..4e017e3 100644 --- a/main.c +++ b/main.c @@ -52,6 +52,9 @@ static unsigned int global_traverse_flags = PKGCONF_PKG_PKGF_NONE; +static pkgconf_list_t filter_libdirs = PKGCONF_LIST_INITIALIZER; +static pkgconf_list_t filter_includedirs = PKGCONF_LIST_INITIALIZER; + static uint64_t want_flags; static int maximum_traverse_depth = 2000; @@ -68,51 +71,30 @@ error_handler(const char *msg) return true; } -static char * -fallback_getenv(const char *envname, const char *fallback) -{ - const char *data = getenv(envname); - - if (data == NULL) - data = fallback; - - return strdup(data); -} - static bool fragment_has_system_dir(pkgconf_fragment_t *frag) { int check_flags = 0; - char *check_paths = NULL; - char *save, *chunk; - bool ret = false; + pkgconf_list_t *check_paths = NULL; switch (frag->type) { case 'L': check_flags = PKG_KEEP_SYSTEM_LIBS; - check_paths = fallback_getenv("PKG_CONFIG_SYSTEM_LIBRARY_PATH", SYSTEM_LIBDIR); + check_paths = &filter_libdirs; break; case 'I': check_flags = PKG_KEEP_SYSTEM_CFLAGS; - check_paths = fallback_getenv("PKG_CONFIG_SYSTEM_INCLUDE_PATH", SYSTEM_INCLUDEDIR); + check_paths = &filter_includedirs; break; default: return false; } - for (chunk = strtok_r(check_paths, ":", &save); chunk != NULL; chunk = strtok_r(NULL, ":", &save)) - { - if ((want_flags & check_flags) == 0 && !strcmp(chunk, frag->data)) - { - ret = true; - break; - } - } + if ((want_flags & check_flags) == 0) + return false; - free(check_paths); - - return ret; + return pkgconf_path_match_list(frag->data, check_paths); } static void @@ -708,6 +690,9 @@ main(int argc, char *argv[]) { NULL, 0, NULL, 0 } }; + pkgconf_path_build_from_environ("PKG_CONFIG_SYSTEM_LIBRARY_PATH", SYSTEM_LIBDIR, &filter_libdirs); + pkgconf_path_build_from_environ("PKG_CONFIG_SYSTEM_INCLUDE_PATH", SYSTEM_INCLUDEDIR, &filter_includedirs); + while ((ret = pkg_getopt_long_only(argc, argv, "", options, NULL)) != -1) { switch (ret)