shithub: mc

Download patch

ref: 3b868baad030d2002b5c15920d228130b7dd96c8
parent: 13433d8aa8f5e97e4754916a70e177ff14214cfd
author: Ori Bernstein <[email protected]>
date: Tue Sep 23 20:50:22 EDT 2014

Remove dependency on system() call.

--- 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 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)
--- a/configure
+++ b/configure
@@ -36,17 +36,17 @@
 echo '#define Instroot "'$prefix'"' > config.h
 case $OS in
     *Linux*)
-        echo '#define Asmcmd "as -g -o %s %s"' >> config.h
+        echo '#define Asmcmd {"as", "-g", "-o", NULL}' >> config.h
         echo '#define symprefix ""' >> config.h
         echo 'export SYS=linux' >> config.mk
         ;;
     *Darwin*)
-        echo '#define Asmcmd "as -g -o %s %s"' >> config.h
+        echo '#define Asmcmd {"as", "-g", "-o", NULL}' >> config.h
         echo '#define symprefix "_"' >> config.h
         echo 'export SYS=osx' >> config.mk
         ;;
     *FreeBSD*)
-        echo '#define Asmcmd "as -g -o %s %s"' >> config.h
+        echo '#define Asmcmd {"as", "-g", "-o", NULL}' >> config.h
         echo '#define symprefix ""' >> config.h
         echo 'export SYS=freebsd' >> config.mk
         ;;