pkg: improve pkg scandir target building

feature/tap-sh
William Pitcock 2013-03-15 17:55:44 -05:00
parent 874bd58ef0
commit 48c6e4c383
1 changed files with 48 additions and 30 deletions

78
pkg.c
View File

@ -38,8 +38,13 @@
#define PKG_DIR_SEP_S '/' #define PKG_DIR_SEP_S '/'
#endif #endif
typedef struct {
char *path;
pkg_node_t node;
} pkg_path_t;
static inline size_t static inline size_t
path_split(const char *text, char ***parv) path_split(const char *text, pkg_list_t *dirlist)
{ {
size_t count = 0; size_t count = 0;
char *workbuf, *p, *iter; char *workbuf, *p, *iter;
@ -47,15 +52,17 @@ path_split(const char *text, char ***parv)
if (text == NULL) if (text == NULL)
return 0; return 0;
*parv = malloc(sizeof (void *));
iter = workbuf = strdup(text); iter = workbuf = strdup(text);
while ((p = strtok(iter, PKG_CONFIG_PATH_SEP_S)) != NULL) while ((p = strtok(iter, PKG_CONFIG_PATH_SEP_S)) != NULL)
{ {
(*parv)[count] = strdup(p); pkg_path_t *pkg_path;
count++, iter = NULL;
*parv = realloc(*parv, sizeof (void *) * (count + 1)); pkg_path = calloc(sizeof(pkg_path_t), 1);
pkg_path->path = strdup(p);
pkg_node_insert_tail(&pkg_path->node, pkg_path, dirlist);
count++, iter = NULL;
} }
free(workbuf); free(workbuf);
@ -63,17 +70,17 @@ path_split(const char *text, char ***parv)
} }
static inline void static inline void
path_free(char **parv, size_t count) path_free(pkg_list_t *dirlist)
{ {
size_t iter; pkg_node_t *n, *tn;
if (parv == NULL) PKG_FOREACH_LIST_ENTRY_SAFE(dirlist->head, tn, n)
return; {
pkg_path_t *pkg_path = n->data;
for (iter = 0; iter < count; iter++) free(pkg_path->path);
free(parv[iter]); free(pkg_path);
}
free(parv);
} }
static inline bool static inline bool
@ -363,19 +370,23 @@ pkg_scan_dir(const char *path, pkg_iteration_func_t func)
void void
pkg_scan(const char *search_path, pkg_iteration_func_t func) pkg_scan(const char *search_path, pkg_iteration_func_t func)
{ {
char **path = NULL; pkg_list_t path = PKG_LIST_INITIALIZER;
size_t count = 0, iter = 0; pkg_node_t *n;
/* PKG_CONFIG_PATH has to take precedence */ /* PKG_CONFIG_PATH has to take precedence */
if (search_path == NULL) if (search_path == NULL)
return; return;
count = path_split(search_path, &path); path_split(search_path, &path);
for (iter = 0; iter < count; iter++) PKG_FOREACH_LIST_ENTRY(path.head, n)
pkg_scan_dir(path[iter], func); {
pkg_path_t *pkg_path = n->data;
path_free(path, count); pkg_scan_dir(pkg_path->path, func);
}
path_free(&path);
} }
void void
@ -434,8 +445,7 @@ 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 **path = NULL; pkg_list_t path = PKG_LIST_INITIALIZER;
size_t count = 0, iter = 0;
const char *env_path; const char *env_path;
pkg_t *pkg = NULL; pkg_t *pkg = NULL;
FILE *f; FILE *f;
@ -461,26 +471,34 @@ pkg_find(const char *name, unsigned int flags)
env_path = getenv("PKG_CONFIG_PATH"); env_path = getenv("PKG_CONFIG_PATH");
if (env_path) if (env_path)
{ {
count = path_split(env_path, &path); pkg_node_t *n;
for (iter = 0; iter < count; iter++) path_split(env_path, &path);
PKG_FOREACH_LIST_ENTRY(path.head, n)
{ {
pkg = pkg_try_specific_path(path[iter], name, flags); pkg_path_t *pkg_path = n->data;
pkg = pkg_try_specific_path(pkg_path->path, name, flags);
if (pkg != NULL) if (pkg != NULL)
goto out; goto out;
} }
path_free(path, count); path_free(&path);
} }
env_path = get_pkgconfig_path(); env_path = get_pkgconfig_path();
if (!(flags & PKGF_ENV_ONLY)) if (!(flags & PKGF_ENV_ONLY))
{ {
count = path_split(env_path, &path); pkg_node_t *n;
for (iter = 0; iter < count; iter++) path_split(env_path, &path);
PKG_FOREACH_LIST_ENTRY(path.head, n)
{ {
pkg = pkg_try_specific_path(path[iter], name, flags); pkg_path_t *pkg_path = n->data;
pkg = pkg_try_specific_path(pkg_path->path, name, flags);
if (pkg != NULL) if (pkg != NULL)
goto out; goto out;
} }
@ -495,7 +513,7 @@ pkg_find(const char *name, unsigned int flags)
out: out:
pkg_cache_add(pkg); pkg_cache_add(pkg);
path_free(path, count); path_free(&path);
return pkg; return pkg;
} }