From 353ba1eafe991aa96ae664d36665c1e0e5421daa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 2 May 2012 11:24:58 +0200 Subject: [PATCH 1/7] Use ';' as path separator on win32. This is what pkg-config does, and it is necessary because ':' is part of path specification on win32. --- pkg.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg.c b/pkg.c index fe73525..c190d66 100644 --- a/pkg.c +++ b/pkg.c @@ -27,6 +27,15 @@ #define PKG_CONFIG_EXT ".pc" #define PKG_CONFIG_PATH_SZ (65535) +#ifdef _WIN32 +/* pkg-config uses ';' on win32 as ':' is part of path */ +#define PKG_CONFIG_PATH_SEP_S ";" +#define PKG_CONFIG_PATH_SEP ';' +#else +#define PKG_CONFIG_PATH_SEP_S ":" +#define PKG_CONFIG_PATH_SEP ':' +#endif + static inline const char * pkg_get_pkgconfig_path(void) { @@ -42,7 +51,7 @@ pkg_get_pkgconfig_path(void) env_path = getenv("PKG_CONFIG_PATH"); if (env_path != NULL) { - strlcat(path, ":", sizeof path); + strlcat(path, PKG_CONFIG_PATH_SEP_S, sizeof path); strlcat(path, env_path, sizeof path); } @@ -63,7 +72,7 @@ pkg_find(const char *name) env_path = pkg_get_pkgconfig_path(); while (env_path[count] != '\0') { - if (env_path[count] && env_path[count] != ':') + if (env_path[count] && env_path[count] != PKG_CONFIG_PATH_SEP) { path[pcount] = env_path[count]; pcount++; -- 2.41.0 From 742fad9251d48200ad291ee900dc89fdec01034b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 2 May 2012 11:38:54 +0200 Subject: [PATCH 2/7] Fix uninitialized vars in deplist parsing. --- parse.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parse.c b/parse.c index b570399..abef361 100644 --- a/parse.c +++ b/parse.c @@ -185,7 +185,7 @@ parse_deplist(pkg_t *pkg, const char *depends) char *start = buf; char *ptr = buf; char *vstart = NULL; - char *package, *version; + char *package = NULL, *version = NULL; strlcpy(buf, kvdepends, sizeof buf); strlcat(buf, " ", sizeof buf); -- 2.41.0 From f26001c3f8fbea3233e1b2d3303590c12d0d3337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 2 May 2012 11:40:38 +0200 Subject: [PATCH 3/7] File lookup: wind in last occurence to the loop. Instead of repeating the parsing stage after reaching the null terminator, just handle it inside the loop and terminate the loop afterwards. --- pkg.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/pkg.c b/pkg.c index c190d66..cdba222 100644 --- a/pkg.c +++ b/pkg.c @@ -70,7 +70,7 @@ pkg_find(const char *name) bzero(path, BUFSIZ); env_path = pkg_get_pkgconfig_path(); - while (env_path[count] != '\0') + while (1) { if (env_path[count] && env_path[count] != PKG_CONFIG_PATH_SEP) { @@ -83,6 +83,8 @@ pkg_find(const char *name) snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); if (f = fopen(locbuf, "r")) return parse_file(locbuf, f); + else if (env_path[count] == '\0') + break; bzero(path, BUFSIZ); pcount = 0; @@ -91,13 +93,7 @@ pkg_find(const char *name) count++; } - if (path[0] != '\0') - { - snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); - f = fopen(locbuf, "r"); - } - - return parse_file(locbuf, f); + return NULL; } /* -- 2.41.0 From 218b3ccd256eeaedbbc97dbe5704875069e05810 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 2 May 2012 11:42:52 +0200 Subject: [PATCH 4/7] Remove unnecessary buffer zeroing. We always replace that current pos with a null terminator, so it doesn't matter what follows it. --- pkg.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/pkg.c b/pkg.c index cdba222..6360d1c 100644 --- a/pkg.c +++ b/pkg.c @@ -67,8 +67,6 @@ pkg_find(const char *name) int count = 0, pcount = 0; FILE *f; - bzero(path, BUFSIZ); - env_path = pkg_get_pkgconfig_path(); while (1) { @@ -86,7 +84,6 @@ pkg_find(const char *name) else if (env_path[count] == '\0') break; - bzero(path, BUFSIZ); pcount = 0; } -- 2.41.0 From 0e0bf1b0fb16913b8d865ad8204d80e983e3a1e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 2 May 2012 11:45:31 +0200 Subject: [PATCH 5/7] Omit empty paths in file lookup. This avoids looking for '/foo.pc'. --- pkg.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pkg.c b/pkg.c index 6360d1c..fbae31a 100644 --- a/pkg.c +++ b/pkg.c @@ -78,10 +78,13 @@ pkg_find(const char *name) else { path[pcount] = '\0'; - snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); - if (f = fopen(locbuf, "r")) - return parse_file(locbuf, f); - else if (env_path[count] == '\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; -- 2.41.0 From ee62bedd596345bf06ad9071914126b8f6e09091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 2 May 2012 16:01:17 +0200 Subject: [PATCH 6/7] parse_file() no longer needs to handle f == NULL. That condition is now handled within pkg_find() completely. --- parse.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/parse.c b/parse.c index abef361..094ea73 100644 --- a/parse.c +++ b/parse.c @@ -373,9 +373,6 @@ parse_file(const char *filename, FILE *f) pkg_t *pkg; char readbuf[BUFSIZ]; - if (f == NULL) - return NULL; - pkg = calloc(sizeof(pkg_t), 1); pkg->filename = strdup(filename); -- 2.41.0 From 3707ccd2212f9f1aaca42f8118fc28876af328de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20G=C3=B3rny?= Date: Wed, 2 May 2012 15:55:13 +0200 Subject: [PATCH 7/7] Fix PKG_CONFIG_PATH precedence, simplify. PKG_CONFIG_PATH paths must take precedence over the default path. Otherwise, we would be unable to override default .pc files. And while I'm at it, simplify the whole code. It is pointless to introduce another buffer and a lot of string mangling for one additional path. --- pkg.c | 65 +++++++++++++++++++++++------------------------------------ 1 file changed, 25 insertions(+), 40 deletions(-) diff --git a/pkg.c b/pkg.c index fbae31a..82681f6 100644 --- a/pkg.c +++ b/pkg.c @@ -36,28 +36,6 @@ #define PKG_CONFIG_PATH_SEP ':' #endif -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; - - strlcpy(path, PKG_DEFAULT_PATH, sizeof path); - - env_path = getenv("PKG_CONFIG_PATH"); - if (env_path != NULL) - { - strlcat(path, PKG_CONFIG_PATH_SEP_S, sizeof path); - strlcat(path, env_path, sizeof path); - } - - return path; -} - pkg_t * pkg_find(const char *name) { @@ -67,32 +45,39 @@ pkg_find(const char *name) int count = 0, pcount = 0; FILE *f; - env_path = pkg_get_pkgconfig_path(); - while (1) + /* PKG_CONFIG_PATH has to take precedence */ + env_path = getenv("PKG_CONFIG_PATH"); + if (env_path) { - if (env_path[count] && env_path[count] != PKG_CONFIG_PATH_SEP) + while (1) { - path[pcount] = env_path[count]; - pcount++; - } - else - { - path[pcount] = '\0'; - if (path[0] != '\0') + if (env_path[count] && env_path[count] != PKG_CONFIG_PATH_SEP) { - snprintf(locbuf, sizeof locbuf, "%s/%s.pc", path, name); - if (f = fopen(locbuf, "r")) - return parse_file(locbuf, f); + path[pcount] = env_path[count]; + pcount++; } - if (env_path[count] == '\0') - break; + 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; + pcount = 0; + } + + count++; } - - count++; } + snprintf(locbuf, sizeof locbuf, "%s/%s.pc", PKG_DEFAULT_PATH, name); + if (f = fopen(locbuf, "r")) + return parse_file(locbuf, f); return NULL; } -- 2.41.0