From 1e93bac383ca8fa3dbbb865a63634945f0fec24f Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Fri, 17 Feb 2012 15:35:23 -0600 Subject: [PATCH] pkg: handle PKG_DEFAULT_PATH and PKG_CONFIG_PATH envvar more cleanly --- Makefile | 1 + pkg.c | 73 ++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index d5ef9f6..0db1d0d 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ SRCS = main.c parse.c pkg.c include buildsys.mk +CFLAGS += -DPKG_DEFAULT_PATH=\"${libdir}/pkgconfig\" LIBS = -lpopt include .deps diff --git a/pkg.c b/pkg.c index 9366631..4e4d464 100644 --- a/pkg.c +++ b/pkg.c @@ -2,7 +2,7 @@ * pkg.c * higher-level dependency graph compilation, management and manipulation * - * Copyright (c) 2011 William Pitcock . + * Copyright (c) 2011, 2012 William Pitcock . * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -23,47 +23,64 @@ #include "pkg.h" +#define PKG_CONFIG_EXT ".pc" +#define PKG_CONFIG_PATH_SZ (65535) + +static inline const char * +pkg_get_pkgconfig_path(void) +{ + static bool computed = false; + static char path[PKG_CONFIG_PATH_SZ]; + char *env_path; + + if (computed) + return path; + + strncpy(path, PKG_DEFAULT_PATH, sizeof path); + + env_path = getenv("PKG_CONFIG_PATH"); + if (env_path != NULL) + { + strncat(path, ":", sizeof path); + strncat(path, env_path, sizeof path); + } + + return path; +} + pkg_t * pkg_find(const char *name) { char locbuf[BUFSIZ]; char path[BUFSIZ]; - char *env_path; + const char *env_path; int count = 0, pcount = 0; FILE *f; - snprintf(locbuf, sizeof locbuf, "/usr/lib/pkgconfig/%s.pc", name); - if (!(f = fopen(locbuf, "r"))) + env_path = pkg_get_pkgconfig_path(); + while (env_path[count] != '\0') { - env_path = getenv("PKG_CONFIG_PATH"); - if (env_path == NULL) - return NULL; - - while (env_path[count] != '\0') + if (env_path[count] != ':') { - if (env_path[count] != ':') - { - path[pcount] = env_path[count]; - pcount++; - } - - if (env_path[count] == ':') - { - snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); - if (f = fopen(locbuf, "r")) - return parse_file(locbuf, f); - path[0] = '\0'; - pcount = 0; - } - - count++; + path[pcount] = env_path[count]; + pcount++; } - - if (path[0] != '\0') + else { snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); - f = fopen(locbuf, "r"); + if (f = fopen(locbuf, "r")) + return parse_file(locbuf, f); + path[0] = '\0'; + pcount = 0; } + + count++; + } + + if (path[0] != '\0') + { + snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); + f = fopen(locbuf, "r"); } return parse_file(locbuf, f);