From 234632d319ce7b98a5295d1f0ea5fb308654ddbc Mon Sep 17 00:00:00 2001
From: jD91mZM2 <me@krake.one>
Date: Sat, 30 Jun 2018 09:19:06 +0200
Subject: [PATCH] Workaround compilation errors

---
 include/bits/exec.h           |  4 +++
 src/platform/Cargo.toml       |  3 --
 src/platform/src/lib.rs       |  7 -----
 src/platform/src/redox/mod.rs | 53 -------------------------------
 src/unistd/src/lib.rs         | 59 ++++++++++++++++++++++++++++++++---
 tests/expected/exec.stderr    |  0
 tests/expected/exec.stdout    |  1 +
 7 files changed, 60 insertions(+), 67 deletions(-)
 create mode 100644 tests/expected/exec.stderr
 create mode 100644 tests/expected/exec.stdout

diff --git a/include/bits/exec.h b/include/bits/exec.h
index 94024518..242f0278 100644
--- a/include/bits/exec.h
+++ b/include/bits/exec.h
@@ -1,6 +1,8 @@
 #ifndef _BITS_EXEC_H
 #define _BITS_EXEC_H
 
+extern int execv(const char *path, char *const *argv);
+
 int execl(const char *path, const char* argv0, ...)
 {
 	int argc;
@@ -22,6 +24,8 @@ int execl(const char *path, const char* argv0, ...)
 	}
 }
 
+extern int execve(const char *path, char *const *argv, char *const *envp);
+
 int execle(const char *path, const char* argv0, ...)
 {
 	int argc;
diff --git a/src/platform/Cargo.toml b/src/platform/Cargo.toml
index dd5ba725..e25b8251 100644
--- a/src/platform/Cargo.toml
+++ b/src/platform/Cargo.toml
@@ -8,6 +8,3 @@ sc = "0.2"
 
 [target.'cfg(target_os = "redox")'.dependencies]
 redox_syscall = "0.1"
-
-[dependencies]
-ralloc = { path = "../../ralloc" }
diff --git a/src/platform/src/lib.rs b/src/platform/src/lib.rs
index 78bc4c39..4919e613 100644
--- a/src/platform/src/lib.rs
+++ b/src/platform/src/lib.rs
@@ -2,8 +2,6 @@
 
 #![no_std]
 #![allow(non_camel_case_types)]
-#![feature(alloc)]
-#![feature(global_allocator)]
 //TODO #![feature(thread_local)]
 
 #[cfg(all(not(feature = "no_std"), target_os = "linux"))]
@@ -24,17 +22,12 @@ mod sys;
 #[path = "redox/mod.rs"]
 mod sys;
 
-extern crate alloc;
-extern crate ralloc;
-
 pub mod types;
 
 use core::fmt;
 
 use types::*;
 
-#[global_allocator]
-static ALLOCATOR: ralloc::Allocator = ralloc::Allocator;
 //TODO #[thread_local]
 #[allow(non_upper_case_globals)]
 #[no_mangle]
diff --git a/src/platform/src/redox/mod.rs b/src/platform/src/redox/mod.rs
index 71019375..b72e5d72 100644
--- a/src/platform/src/redox/mod.rs
+++ b/src/platform/src/redox/mod.rs
@@ -1,7 +1,6 @@
 use core::ptr;
 use core::slice;
 use core::mem;
-use alloc::Vec;
 use syscall;
 use syscall::flag::*;
 use syscall::data::TimeSpec as redox_timespec;
@@ -68,58 +67,6 @@ pub fn dup2(fd1: c_int, fd2: c_int) -> c_int {
     e(syscall::dup2(fd1 as usize, fd2 as usize, &[])) as c_int
 }
 
-pub fn execve(path: *const c_char, argv: *const *mut c_char, envp: *const *mut c_char) -> c_int {
-    unsafe {
-        let mut env = envp;
-        while !(*env).is_null() {
-            let slice = c_str(*env);
-            // Should always contain a =, but worth checking
-            if let Some(sep) = slice.iter().position(|&c| c == b'=') {
-                // If the environment variable has no name, do not attempt
-                // to add it to the env.
-                if sep > 0 {
-                    let mut path = b"env:".to_vec();
-                    path.extend_from_slice(&slice[..sep]);
-                    match syscall::open(&path, O_WRONLY | O_CREAT) {
-                        Ok(fd) => {
-                            // If the environment variable has no value, there
-                            // is no need to write anything to the env scheme.
-                            if sep + 1 < slice.len() {
-                                let n = match syscall::write(fd, &slice[sep + 1..]) {
-                                    Ok(n) => n,
-                                    err => {
-                                        return e(err) as c_int;
-                                    }
-                                };
-                            }
-                            // Cleanup after adding the variable.
-                            match syscall::close(fd) {
-                                Ok(_) => (),
-                                err => {
-                                    return e(err) as c_int;
-                                }
-                            }
-                        }
-                        err => {
-                            return e(err) as c_int;
-                        }
-                    }
-                }
-            }
-            env = env.offset(1);
-        }
-
-        let mut args: Vec<[usize; 2]> = Vec::new();
-        let mut arg = argv;
-        while !(*arg).is_null() {
-            args.push([*arg as usize, c_str(*arg).len()]);
-            arg = arg.offset(1);
-        }
-
-        e(syscall::execve(c_str(path), &args)) as c_int
-    }
-}
-
 pub fn exit(status: c_int) -> ! {
     let _ = syscall::exit(status as usize);
     loop {}
diff --git a/src/unistd/src/lib.rs b/src/unistd/src/lib.rs
index db90a9fe..6a6bc339 100644
--- a/src/unistd/src/lib.rs
+++ b/src/unistd/src/lib.rs
@@ -120,17 +120,68 @@ pub extern "C" fn encrypt(block: [c_char; 64], edflag: c_int) {
 // }
 
 #[no_mangle]
-pub extern "C" fn execv(path: *const c_char, argv: *const *mut c_char) -> c_int {
-    unsafe { execve(path, argv, environ) }
+pub unsafe extern "C" fn execv(path: *const c_char, argv: *const *mut c_char) -> c_int {
+    execve(path, argv, environ)
 }
 
 #[no_mangle]
-pub extern "C" fn execve(
+pub unsafe extern "C" fn execve(
     path: *const c_char,
     argv: *const *mut c_char,
     envp: *const *mut c_char,
 ) -> c_int {
-    platform::execve(path, argv, envp)
+    #[cfg(target_os = "linux")] {
+        platform::execve(path, argv, envp)
+    }
+    #[cfg(target_os = "redox")] {
+        let mut env = envp;
+        while !(*env).is_null() {
+            let slice = c_str(*env);
+            // Should always contain a =, but worth checking
+            if let Some(sep) = slice.iter().position(|&c| c == b'=') {
+                // If the environment variable has no name, do not attempt
+                // to add it to the env.
+                if sep > 0 {
+                    let mut path = b"env:".to_vec();
+                    path.extend_from_slice(&slice[..sep]);
+                    match platform::syscall::open(&path, O_WRONLY | O_CREAT) {
+                        Ok(fd) => {
+                            // If the environment variable has no value, there
+                            // is no need to write anything to the env scheme.
+                            if sep + 1 < slice.len() {
+                                let n = match syscall::write(fd, &slice[sep + 1..]) {
+                                    Ok(n) => n,
+                                    err => {
+                                        return e(err) as c_int;
+                                    }
+                                };
+                            }
+                            // Cleanup after adding the variable.
+                            match platform::syscall::close(fd) {
+                                Ok(_) => (),
+                                err => {
+                                    return e(err) as c_int;
+                                }
+                            }
+                        }
+                        err => {
+                            return e(err) as c_int;
+                        }
+                    }
+                }
+            }
+            env = env.offset(1);
+        }
+
+        let mut args: Vec<[usize; 2]> = Vec::new();
+        let mut arg = argv;
+        while !(*arg).is_null() {
+            args.push([*arg as usize, c_str(*arg).len()]);
+            arg = arg.offset(1);
+        }
+
+        e(platform::syscall::execve(c_str(path), &args)) as c_int
+    }
 }
 
 #[no_mangle]
diff --git a/tests/expected/exec.stderr b/tests/expected/exec.stderr
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/expected/exec.stdout b/tests/expected/exec.stdout
new file mode 100644
index 00000000..980a0d5f
--- /dev/null
+++ b/tests/expected/exec.stdout
@@ -0,0 +1 @@
+Hello World!
-- 
GitLab