cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit 18c5bbf727faf6fb30a8809b6f7900a59c07897f
parent 6397cd8abe96c0b915d03ec564e35f0d3fba3714
Author: Michael Forney <mforney@mforney.org>
Date:   Sat,  6 Apr 2019 13:35:23 -0700

driver: Use argv[0] to determine cc-qbe path if /proc/self/exe is not available

Diffstat:
Mdriver.c | 16++++++++++------
1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/driver.c b/driver.c @@ -305,16 +305,20 @@ nextarg(char ***argv) } static char * -compilecommand(void) +compilecommand(char *arg) { char self[PATH_MAX], *cmd; - ssize_t n; + size_t n; n = readlink("/proc/self/exe", self, sizeof(self) - 5); - if (n < 0) - fatal("readlink /proc/self/exe:"); - if (n == sizeof(self) - 5) + if (n == -1) { + n = strlen(arg); + if (n > sizeof(self) - 5) + fatal("argv[0] is too large"); + memcpy(self, arg, n); + } else if (n == sizeof(self) - 5) { fatal("target of /proc/self/exe is too large"); + } strcpy(self + n, "-qbe"); if (access(self, X_OK) < 0) return NULL; @@ -338,7 +342,7 @@ main(int argc, char *argv[]) arrayaddbuf(&phases[CODEGEN].cmd, codegencmd, sizeof(codegencmd)); arrayaddbuf(&phases[ASSEMBLE].cmd, assemblecmd, sizeof(assemblecmd)); arrayaddbuf(&phases[LINK].cmd, linkcmd, sizeof(linkcmd)); - arg = compilecommand(); + arg = compilecommand(argv[0]); if (arg) *(char **)phases[COMPILE].cmd.val = arg;