ego uses fork/exec too, so factor out the new system() code from ack and use it
there.
This commit is contained in:
parent
cf3f5bf64e
commit
bb0236cbf7
|
@ -8,6 +8,7 @@ clibrary {
|
|||
"./rename.c", "./seek.c", "./stop.c", "./system.c",
|
||||
--"./unlock.c”,”./tmpdir.c”,
|
||||
"./write.c",
|
||||
"./syssystem.c",
|
||||
},
|
||||
hdrs = { "./system.h" },
|
||||
}
|
||||
|
|
42
modules/src/system/syssystem.c
Normal file
42
modules/src/system/syssystem.c
Normal 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;
|
||||
}
|
||||
|
|
@ -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 *);
|
||||
|
|
|
@ -34,6 +34,7 @@ cprogram {
|
|||
deps = {
|
||||
"h+emheaders",
|
||||
"h+local",
|
||||
"modules/src/system+lib",
|
||||
"./ack.h",
|
||||
"./data.h",
|
||||
"./dmach.h",
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -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,40 +290,28 @@ static void
|
|||
break;
|
||||
}
|
||||
}
|
||||
if ((pid = fork()) < 0)
|
||||
{
|
||||
fatal("Could not fork");
|
||||
}
|
||||
else if (pid == 0)
|
||||
{
|
||||
if (v_flag)
|
||||
{
|
||||
register int i = 0;
|
||||
|
||||
while (phargs[i])
|
||||
{
|
||||
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
|
||||
if (v_flag)
|
||||
{
|
||||
while (wait(&status) != pid) /* nothing */
|
||||
;
|
||||
if ((status & 0177) != 0)
|
||||
register int i = 0;
|
||||
|
||||
while (phargs[i])
|
||||
{
|
||||
fatal("%s got a unix signal", phargs[0]);
|
||||
}
|
||||
if (((status >> 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue