From bb0236cbf76db82e5520a17ce4ea952b3e2c6d28 Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 16 Jul 2022 23:14:39 +0200 Subject: [PATCH] ego uses fork/exec too, so factor out the new system() code from ack and use it there. --- modules/src/system/build.lua | 1 + modules/src/system/syssystem.c | 42 ++++++++++++++++++++++++++++ modules/src/system/system.h | 2 ++ util/ack/build.lua | 1 + util/ack/run.c | 39 ++------------------------ util/ego/em_ego/em_ego.c | 51 +++++++++++++--------------------- 6 files changed, 67 insertions(+), 69 deletions(-) create mode 100644 modules/src/system/syssystem.c diff --git a/modules/src/system/build.lua b/modules/src/system/build.lua index b409f697d..81d28d3af 100644 --- a/modules/src/system/build.lua +++ b/modules/src/system/build.lua @@ -8,6 +8,7 @@ clibrary { "./rename.c", "./seek.c", "./stop.c", "./system.c", --"./unlock.cā€,ā€./tmpdir.cā€, "./write.c", + "./syssystem.c", }, hdrs = { "./system.h" }, } diff --git a/modules/src/system/syssystem.c b/modules/src/system/syssystem.c new file mode 100644 index 000000000..c86b0be65 --- /dev/null +++ b/modules/src/system/syssystem.c @@ -0,0 +1,42 @@ +#include +#include +#include "system.h" + +int sys_system(const char* prog, const char* const* arglist) +{ + /* Calculate the maximum length of the command line. */ + + int len = strlen(prog); + for (const char* const* arg = arglist+1; *arg; arg++) + len += strlen(*arg) * 2 + 1; + + /* Now actually build the command line. */ + + char* cmdline = malloc(len + 1); + strcpy(cmdline, prog); + char* p = cmdline + strlen(prog); + + for (const char* const* arg = arglist+1; *arg; arg++) + { + const char* word = *arg; + *p++ = ' '; + + for (;;) + { + char c = *word++; + if (!c) + break; + if ((c == ' ') || (c == '\"') || (c == '\'')) + *p++ = '\\'; + *p++ = c; + } + } + + *p = 0; + + int status = system(cmdline); + + free(cmdline); + return status; +} + diff --git a/modules/src/system/system.h b/modules/src/system/system.h index 598ceb03f..12c27916b 100644 --- a/modules/src/system/system.h +++ b/modules/src/system/system.h @@ -48,6 +48,8 @@ off_t sys_filesize(char *); int sys_chmode(char *, int); /* Return the temporary directory location */ char* sys_gettmpdir(void); +/* Call another program. */ +int sys_system(const char* prog, const char* const* argv); #if 0 int sys_lock(char *); int sys_unlock(char *); diff --git a/util/ack/build.lua b/util/ack/build.lua index cb9b66fd4..cf4dc9035 100644 --- a/util/ack/build.lua +++ b/util/ack/build.lua @@ -34,6 +34,7 @@ cprogram { deps = { "h+emheaders", "h+local", + "modules/src/system+lib", "./ack.h", "./data.h", "./dmach.h", diff --git a/util/ack/run.c b/util/ack/run.c index d1c4fe730..71eea0592 100644 --- a/util/ack/run.c +++ b/util/ack/run.c @@ -15,6 +15,7 @@ #include "grows.h" #include "data.h" #include +#include "system.h" #ifndef NORCSID static char rcs_id[] = "$Id$"; @@ -84,40 +85,6 @@ int runphase(trf* phase) return result; } -static char* build_command_line(const char* prog) -{ - /* Calculate the maximum length of the command line. */ - - int len = strlen(prog); - for (int i = 1; i < (argcount - 1); i++) - len += strlen(arglist[i]) * 2 + 1; - - /* Now actually build the command line. */ - - char* cmdline = malloc(len + 1); - strcpy(cmdline, prog); - char* p = cmdline + strlen(prog); - - for (int i = 1; i < (argcount - 1); i++) - { - const char* word = arglist[i]; - *p++ = ' '; - - for (;;) - { - char c = *word++; - if (!c) - break; - if ((c == ' ') || (c == '\"') || (c == '\'')) - *p++ = '\\'; - *p++ = c; - } - } - - *p = 0; - return cmdline; -} - static int run_exec(trf* phase, const char* prog) { int status, child, waitchild; @@ -159,9 +126,7 @@ static int run_exec(trf* phase, const char* prog) } } - char* cmdline = build_command_line(prog); - status = system(cmdline); - free(cmdline); + status = sys_system(prog, arglist); if (oldstdin != -1) { diff --git a/util/ego/em_ego/em_ego.c b/util/ego/em_ego/em_ego.c index e9f9eb9f8..74ca2c83a 100644 --- a/util/ego/em_ego/em_ego.c +++ b/util/ego/em_ego/em_ego.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include "em_path.h" @@ -281,7 +280,7 @@ static void if (descr_file) { phargs[argc++] = "-M"; - phargs[argc++] = descr_file; + phargs[argc++] = (char*) descr_file; } for (i=0; i> 8) & 0377) != 0) - { - cleanup(); - sys_stop(S_EXIT); + fprint(STDERR, "%s ", phargs[i]); + i++; } + fprint(STDERR, "\n"); + } + + status = sys_system(phargs[0], (const char* const*) phargs); + if ((status & 0177) != 0) + { + fatal("%s got a unix signal", phargs[0]); + } + if (((status >> 8) & 0377) != 0) + { + cleanup(); + sys_stop(S_EXIT); } }