Skip to content
Snippets Groups Projects
Commit acbe3236 authored by Jeremy Soller's avatar Jeremy Soller
Browse files

Merge branch 'functions/system/return_shell_exists' into 'master'

system(): on command == NULL, return nonzero if shell exists

See merge request redox-os/relibc!403
parents 63b7b76a 14709b3d
No related branches found
No related tags found
No related merge requests found
...@@ -1170,13 +1170,19 @@ pub unsafe extern "C" fn strtoll( ...@@ -1170,13 +1170,19 @@ pub unsafe extern "C" fn strtoll(
pub unsafe extern "C" fn system(command: *const c_char) -> c_int { pub unsafe extern "C" fn system(command: *const c_char) -> c_int {
//TODO: share code with popen //TODO: share code with popen
// handle shell detection on command == NULL
if command.is_null() {
let status = system("exit 0\0".as_ptr() as *const c_char);
if status == 0 {
return 1;
} else {
return 0;
}
}
let child_pid = unistd::fork(); let child_pid = unistd::fork();
if child_pid == 0 { if child_pid == 0 {
let command_nonnull = if command.is_null() { let command_nonnull = command as *const u8;
"exit 0\0".as_ptr()
} else {
command as *const u8
};
let shell = "/bin/sh\0".as_ptr(); let shell = "/bin/sh\0".as_ptr();
......
shell found: 1
test of system test of system
...@@ -3,6 +3,16 @@ ...@@ -3,6 +3,16 @@
#include "test_helpers.h" #include "test_helpers.h"
int main(void) { int main(void) {
int status = system("echo test of system"); // testing shell detection
// this means, because we don't detect if a shell actually exists but just assume it does, this test case breaks in
// environments without `sh`. I think that is a reasonable tradeoff.
// (And if there isn't a shell, system() won't work anyways)
int status = system(NULL);
printf("shell found: %i\n", status);
fflush(stdout);
ERROR_IF(system, status, == 0);
// base case
status = system("echo test of system");
ERROR_IF(system, status, == -1); ERROR_IF(system, status, == -1);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment