aboutsummaryrefslogtreecommitdiffstats
path: root/src/blogc-make
diff options
context:
space:
mode:
authorRafael G. Martins <rafael@rafaelmartins.eng.br>2017-03-01 23:10:54 +0100
committerRafael G. Martins <rafael@rafaelmartins.eng.br>2017-03-01 23:11:53 +0100
commit6125e8a367a77684aa245e898f80dac3009a301a (patch)
treedf34817da345ca25441b188403ab9a0ce595a69f /src/blogc-make
parenta2e4641865f5b1478c4243767abd208772a303b3 (diff)
downloadblogc-6125e8a367a77684aa245e898f80dac3009a301a.tar.gz
blogc-6125e8a367a77684aa245e898f80dac3009a301a.tar.bz2
blogc-6125e8a367a77684aa245e898f80dac3009a301a.zip
make: implement binary lookup in a more portable way
Diffstat (limited to 'src/blogc-make')
-rw-r--r--src/blogc-make/exec.c38
-rw-r--r--src/blogc-make/main.c9
2 files changed, 20 insertions, 27 deletions
diff --git a/src/blogc-make/exec.c b/src/blogc-make/exec.c
index 2fc46d8..1dab7fd 100644
--- a/src/blogc-make/exec.c
+++ b/src/blogc-make/exec.c
@@ -14,13 +14,6 @@
#include <sys/wait.h>
#include <errno.h>
#include <libgen.h>
-
-#ifdef __FreeBSD__
-#include <limits.h>
-#include <sys/types.h>
-#include <sys/sysctl.h>
-#endif
-
#include "../common/error.h"
#include "../common/file.h"
#include "../common/utils.h"
@@ -28,6 +21,8 @@
#include "exec.h"
#include "settings.h"
+extern const char *argv0;
+
char*
bm_exec_find_binary(const char *bin, const char *env)
@@ -39,26 +34,15 @@ bm_exec_find_binary(const char *bin, const char *env)
}
// second try: same dir as current exec
- char *path = realpath("/proc/self/exe", NULL); // Linux
-#ifdef __FreeBSD__
- if (path == NULL) {
- path = realpath("/proc/curproc/file", NULL); // FreeBSD with /proc
- if (path == NULL) {
- // FreeBSD without /proc (default for 11.0)
- int name[4];
- name[0] = CTL_KERN;
- name[1] = KERN_PROC;
- name[2] = KERN_PROC_PATHNAME;
- name[3] = -1;
- char buf[PATH_MAX];
- size_t buf_len = sizeof(buf);
- if (-1 != sysctl(name, 4, buf, &buf_len, NULL, 0)) {
- path = bc_strdup_printf("%.*s", buf_len, buf);
- }
- }
- }
-#endif
- if (path != NULL) {
+ // we rely on some assumptions here:
+ //
+ // - if binary is called without a directory, that means location will
+ // be resolved from $PATH, we don't care about doing a dir lookup.
+ // - we *never* call chdir anywhere in the code, so we can assume
+ // that relative paths will work as expected.
+ // - windows path sep is not supported
+ if (argv0 != NULL && (NULL != strchr(argv0, '/'))) {
+ char *path = bc_strdup(argv0);
char *dir = bc_strdup(dirname(path));
free(path);
char *tmp = bc_strdup_printf("%s/%s", dir, bin);
diff --git a/src/blogc-make/main.c b/src/blogc-make/main.c
index 05b3205..e5a5ac4 100644
--- a/src/blogc-make/main.c
+++ b/src/blogc-make/main.c
@@ -19,6 +19,10 @@
#include "ctx.h"
#include "rules.h"
+// is this beautiful? no. but there's no point in passing something that is
+// essentially global to every function in exec.c
+const char *argv0 = NULL;
+
static void
print_help(void)
@@ -57,6 +61,11 @@ main(int argc, char **argv)
{
setlocale(LC_ALL, "");
+ // i really hope that no operating system omits argv[0], but ...
+ if (argc > 0) {
+ argv0 = argv[0];
+ }
+
int rv = 0;
bc_error_t *err = NULL;