From c9cb7ccb40d99f54e35ec5068cb3cba0514bb45f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 21 Mar 2025 20:16:28 -0400 Subject: [PATCH] Clean up executable after running it --- tomo.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tomo.c b/tomo.c index 07c5880..44cce3c 100644 --- a/tomo.c +++ b/tomo.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -198,8 +199,19 @@ int main(int argc, char *argv[]) for (int64_t i = 0; i < args.length; i++) prog_args[i + 1] = Text$as_c_string(*(Text_t*)(args.data + i*args.stride)); prog_args[1 + args.length] = NULL; - execv(prog_args[0], prog_args); - errx(1, "Failed to run compiled program"); + pid_t child; + if (posix_spawn(&child, prog_args[0], NULL, NULL, prog_args, NULL) != 0) + err(1, "Failed to run compiled program: %s", prog_args[0]); + assert(child); + int status; + while (waitpid(child, &status, 0) < 0 && errno == EINTR) { + if (WIFEXITED(status) || WIFSIGNALED(status)) + break; + else if (WIFSTOPPED(status)) + kill(child, SIGCONT); + } + unlink(prog_args[0]); + return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE; } else { env_t *env = global_env(); Array_t object_files = {},