aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2023-12-31 01:29:42 +0100
committerThomas Voss <mail@thomasvoss.com> 2023-12-31 01:29:42 +0100
commit4914f864162f94e1476266ee62876bebde82bb35 (patch)
treec5067111aabe95a94d5fbe2346eefc97f2cce21a
parent270ed4f02578a85d4c98ab80c78f6203066d0055 (diff)
Make rebuild() work properly when in a different directory
-rw-r--r--cbs.h24
1 files 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);