From 4914f864162f94e1476266ee62876bebde82bb35 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 31 Dec 2023 01:29:42 +0100 Subject: Make rebuild() work properly when in a different directory --- cbs.h | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/cbs.h b/cbs.h index 1c3b833..c7eab15 100644 --- a/cbs.h +++ b/cbs.h @@ -644,30 +644,42 @@ _getcwd(void) void _rebuild(char *src) { - char *cwd; + char *cwd, *cpy1, *cpy2, *dn, *bn; cmd_t cmd = {0}; cwd = _getcwd(); + if (!(cpy1 = strdup(*_cbs_argv))) + die("strdup"); + if (!(cpy2 = strdup(*_cbs_argv))) + die("strdup"); + dn = dirname(cpy1); + bn = basename(cpy2); - if (chdir(dirname(src)) == -1) - die("chdir: %s", dirname(src)); - if (foutdated(*_cbs_argv, src)) { + if (chdir(dn) == -1) + die("chdir: %s", dn); + if (!foutdated(bn, src)) { if (chdir(cwd) == -1) die("chdir: %s", cwd); + free(cpy1); + free(cpy2); + free(cwd); return; } - free(cwd); cmdadd(&cmd, "cc"); #ifdef CBS_PTHREAD cmdadd(&cmd, "-lpthread"); #endif - cmdadd(&cmd, "-o", *_cbs_argv, src); + cmdadd(&cmd, "-o", bn, src); cmdput(cmd); if (cmdexec(cmd)) diex("Compilation of build script failed"); cmdclr(&cmd); + + if (chdir(cwd) == -1) + die("chdir: %s", cwd); + cmdaddv(&cmd, _cbs_argv, _cbs_argc); execvp(*cmd._argv, cmd._argv); die("execvp: %s", *cmd._argv); -- cgit v1.2.3