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",
--"./unlock.c”,”./tmpdir.c”,
"./write.c",
"./syssystem.c",
},
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);
/* 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 *);

View file

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

View file

@ -15,6 +15,7 @@
#include "grows.h"
#include "data.h"
#include <signal.h>
#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)
{

View file

@ -11,7 +11,6 @@
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <limits.h>
#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<nphase_args; i++)
@ -291,12 +290,7 @@ static void
break;
}
}
if ((pid = fork()) < 0)
{
fatal("Could not fork");
}
else if (pid == 0)
{
if (v_flag)
{
register int i = 0;
@ -308,14 +302,8 @@ static void
}
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 */
;
status = sys_system(phargs[0], (const char* const*) phargs);
if ((status & 0177) != 0)
{
fatal("%s got a unix signal", phargs[0]);
@ -326,7 +314,6 @@ static void
sys_stop(S_EXIT);
}
}
}
int main(int argc, char* argv[])
{