summaryrefslogtreecommitdiff
path: root/6/main.c
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2014-09-24 00:50:22 -0400
committerOri Bernstein <ori@eigenstate.org>2014-09-24 00:50:22 -0400
commit3b868baad030d2002b5c15920d228130b7dd96c8 (patch)
treeb82fd0dadf60639e65f5b44a5a680f4acfd307d7 /6/main.c
parent13433d8aa8f5e97e4754916a70e177ff14214cfd (diff)
downloadmc-3b868baad030d2002b5c15920d228130b7dd96c8.tar.gz
Remove dependency on system() call.
Diffstat (limited to '6/main.c')
-rw-r--r--6/main.c34
1 files changed, 27 insertions, 7 deletions
diff --git a/6/main.c b/6/main.c
index a5e8ccc..b0ba3b2 100644
--- a/6/main.c
+++ b/6/main.c
@@ -4,13 +4,15 @@
#include <ctype.h>
#include <string.h>
#include <assert.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <err.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/wait.h>
+
#include "parse.h"
#include "opt.h"
#include "asm.h"
@@ -44,13 +46,31 @@ static void usage(char *prog)
static void assem(char *asmsrc, char *input)
{
+ char *asmcmd[] = Asmcmd;
char objfile[1024];
- char cmd[2048];
+ char **p, **cmd;
+ size_t ncmd;
+ int pid, status;
swapsuffix(objfile, 1024, input, ".myr", ".o");
- snprintf(cmd, 1024, Asmcmd, objfile, asmsrc);
- if (system(cmd) == -1)
- die("Couldn't run assembler");
+ cmd = NULL;
+ ncmd = 0;
+ for (p = asmcmd; *p != NULL; p++)
+ lappend(&cmd, &ncmd, *p);
+ lappend(&cmd, &ncmd, objfile);
+ lappend(&cmd, &ncmd, asmsrc);
+ lappend(&cmd, &ncmd, NULL);
+
+ pid = fork();
+ if (pid == -1) {
+ die("couldn't fork");
+ } else if (pid == 0) {
+ execvp(cmd[0], cmd);
+ } else {
+ waitpid(pid, &status, 0);
+ if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+ die("Couldn't run assembler");
+ }
}
static char *gentemp(char *buf, size_t bufsz, char *path, char *suffix)