about summary refs log tree commit diff
path: root/src/rt/rust_run_program.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/rt/rust_run_program.cpp')
-rw-r--r--src/rt/rust_run_program.cpp29
1 files changed, 25 insertions, 4 deletions
diff --git a/src/rt/rust_run_program.cpp b/src/rt/rust_run_program.cpp
index df4714758dd..848b015dd6c 100644
--- a/src/rt/rust_run_program.cpp
+++ b/src/rt/rust_run_program.cpp
@@ -1,5 +1,9 @@
 #include "rust_internal.h"
 
+#ifdef __APPLE__
+#include <crt_externs.h>
+#endif
+
 #if defined(__WIN32__)
 
 #include <process.h>
@@ -64,7 +68,10 @@ void append_arg(char *& buf, char const *arg, bool last) {
 }
 
 extern "C" CDECL int
-rust_run_program(const char* argv[], int in_fd, int out_fd, int err_fd) {
+rust_run_program(const char* argv[],
+                 void* envp,
+                 const char* dir,
+                 int in_fd, int out_fd, int err_fd) {
     STARTUPINFO si;
     ZeroMemory(&si, sizeof(STARTUPINFO));
     si.cb = sizeof(STARTUPINFO);
@@ -99,7 +106,7 @@ rust_run_program(const char* argv[], int in_fd, int out_fd, int err_fd) {
 
     PROCESS_INFORMATION pi;
     BOOL created = CreateProcess(NULL, cmd, NULL, NULL, TRUE,
-                                 0, NULL, NULL, &si, &pi);
+                                 0, envp, dir, &si, &pi);
 
     CloseHandle(si.hStdInput);
     CloseHandle(si.hStdOutput);
@@ -130,7 +137,10 @@ rust_process_wait(int proc) {
 #include <termios.h>
 
 extern "C" CDECL int
-rust_run_program(char* argv[], int in_fd, int out_fd, int err_fd) {
+rust_run_program(const char* argv[],
+                 void* envp,
+                 const char* dir,
+                 int in_fd, int out_fd, int err_fd) {
     int pid = fork();
     if (pid != 0) return pid;
 
@@ -143,7 +153,18 @@ rust_run_program(char* argv[], int in_fd, int out_fd, int err_fd) {
     if (err_fd) dup2(err_fd, 2);
     /* Close all other fds. */
     for (int fd = getdtablesize() - 1; fd >= 3; fd--) close(fd);
-    execvp(argv[0], argv);
+    if (dir) { chdir(dir); }
+
+#ifdef __APPLE__
+    if (envp) {
+        *_NSGetEnviron() = (char **)envp;
+    }
+    execvp(argv[0], (char * const *)argv);
+#else
+    if (!envp) { envp = environ; }
+    execvpe(argv[0], (char * const *)argv, (char * const *)envp);
+#endif
+
     exit(1);
 }