ego uses fork/exec too, so factor out the new system() code from ack and use it

there.
This commit is contained in:
David Given 2022-07-16 23:14:39 +02:00
parent cf3f5bf64e
commit bb0236cbf7
6 changed files with 67 additions and 69 deletions

View file

@ -8,6 +8,7 @@ clibrary {
"./rename.c", "./seek.c", "./stop.c", "./system.c", "./rename.c", "./seek.c", "./stop.c", "./system.c",
--"./unlock.c”,”./tmpdir.c”, --"./unlock.c”,”./tmpdir.c”,
"./write.c", "./write.c",
"./syssystem.c",
}, },
hdrs = { "./system.h" }, hdrs = { "./system.h" },
} }

View file

@ -0,0 +1,42 @@
#include <stdlib.h>
#include <string.h>
#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;
}

View file

@ -48,6 +48,8 @@ off_t sys_filesize(char *);
int sys_chmode(char *, int); int sys_chmode(char *, int);
/* Return the temporary directory location */ /* Return the temporary directory location */
char* sys_gettmpdir(void); char* sys_gettmpdir(void);
/* Call another program. */
int sys_system(const char* prog, const char* const* argv);
#if 0 #if 0
int sys_lock(char *); int sys_lock(char *);
int sys_unlock(char *); int sys_unlock(char *);

View file

@ -34,6 +34,7 @@ cprogram {
deps = { deps = {
"h+emheaders", "h+emheaders",
"h+local", "h+local",
"modules/src/system+lib",
"./ack.h", "./ack.h",
"./data.h", "./data.h",
"./dmach.h", "./dmach.h",

View file

@ -15,6 +15,7 @@
#include "grows.h" #include "grows.h"
#include "data.h" #include "data.h"
#include <signal.h> #include <signal.h>
#include "system.h"
#ifndef NORCSID #ifndef NORCSID
static char rcs_id[] = "$Id$"; static char rcs_id[] = "$Id$";
@ -84,40 +85,6 @@ int runphase(trf* phase)
return result; 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) static int run_exec(trf* phase, const char* prog)
{ {
int status, child, waitchild; int status, child, waitchild;
@ -159,9 +126,7 @@ static int run_exec(trf* phase, const char* prog)
} }
} }
char* cmdline = build_command_line(prog); status = sys_system(prog, arglist);
status = system(cmdline);
free(cmdline);
if (oldstdin != -1) if (oldstdin != -1)
{ {

View file

@ -11,7 +11,6 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h>
#include <string.h> #include <string.h>
#include <limits.h> #include <limits.h>
#include "em_path.h" #include "em_path.h"
@ -281,7 +280,7 @@ static void
if (descr_file) if (descr_file)
{ {
phargs[argc++] = "-M"; phargs[argc++] = "-M";
phargs[argc++] = descr_file; phargs[argc++] = (char*) descr_file;
} }
for (i=0; i<nphase_args; i++) for (i=0; i<nphase_args; i++)
@ -291,40 +290,28 @@ static void
break; break;
} }
} }
if ((pid = fork()) < 0)
{
fatal("Could not fork");
}
else if (pid == 0)
{
if (v_flag)
{
register int i = 0;
while (phargs[i]) if (v_flag)
{
fprint(STDERR, "%s ", phargs[i]);
i++;
}
fprint(STDERR, "\n");
}
(void)execv(phargs[0], phargs);
fatal("Could not exec %s", phargs[0]);
sys_stop(S_EXIT);
}
else
{ {
while (wait(&status) != pid) /* nothing */ register int i = 0;
;
if ((status & 0177) != 0) while (phargs[i])
{ {
fatal("%s got a unix signal", phargs[0]); fprint(STDERR, "%s ", phargs[i]);
} i++;
if (((status >> 8) & 0377) != 0)
{
cleanup();
sys_stop(S_EXIT);
} }
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);
} }
} }