pkg: use path_split() instead of a static buffer.

pull/4/head
William Pitcock 2012-05-02 19:06:32 +00:00
parent 538317f6c5
commit b91bdcd31a
1 changed files with 20 additions and 33 deletions

53
pkg.c
View File

@ -36,70 +36,57 @@
#define PKG_CONFIG_PATH_SEP ':' #define PKG_CONFIG_PATH_SEP ':'
#endif #endif
static inline int static inline size_t
path_split(char *text, char ***parv) path_split(const char *text, char ***parv)
{ {
int count = 0; size_t count = 0;
char *p; char *workbuf, *p, *iter;
if (text == NULL) if (text == NULL)
return 0; return 0;
*parv = malloc(sizeof (void *)); *parv = malloc(sizeof (void *));
p = text; iter = workbuf = strdup(text);
while ((*parv[count] = strtok(p, " ")) != NULL) while ((p = strtok(iter, " ")) != NULL)
{ {
count++, p = NULL; *parv[count] = strdup(p);
count++, iter = NULL;
*parv = realloc(*parv, sizeof (void *) * count); *parv = realloc(*parv, sizeof (void *) * count);
} }
free(workbuf);
return count; return count + 1;
} }
pkg_t * pkg_t *
pkg_find(const char *name) pkg_find(const char *name)
{ {
char locbuf[PKG_CONFIG_PATH_SZ]; char locbuf[PKG_CONFIG_PATH_SZ];
char *path; char **path;
size_t count, iter;
const char *env_path; const char *env_path;
int count = 0, pcount = 0;
FILE *f; FILE *f;
/* PKG_CONFIG_PATH has to take precedence */ /* PKG_CONFIG_PATH has to take precedence */
env_path = getenv("PKG_CONFIG_PATH"); env_path = getenv("PKG_CONFIG_PATH");
if (env_path) if (env_path)
{ {
while (1) count = path_split(env_path, &path);
while (iter < count)
{ {
snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path[iter], name);
if (env_path[count] && env_path[count] != PKG_CONFIG_PATH_SEP) if (f = fopen(locbuf, "r"))
{ return parse_file(locbuf, f);
path[pcount] = env_path[count];
pcount++;
}
else
{
path[pcount] = '\0';
if (path[0] != '\0')
{
snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name);
if (f = fopen(locbuf, "r"))
return parse_file(locbuf, f);
}
if (env_path[count] == '\0')
break;
pcount = 0;
}
count++;
} }
} }
snprintf(locbuf, sizeof locbuf, "%s/%s.pc", PKG_DEFAULT_PATH, name); snprintf(locbuf, sizeof locbuf, "%s/%s.pc", PKG_DEFAULT_PATH, name);
if (f = fopen(locbuf, "r")) if (f = fopen(locbuf, "r"))
return parse_file(locbuf, f); return parse_file(locbuf, f);
return NULL; return NULL;
} }