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

Add system

parent b8cab5f0
No related branches found
No related tags found
No related merge requests found
......@@ -435,6 +435,7 @@ dependencies = [
"rand 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)",
"string 0.1.0",
"time 0.1.0",
"unistd 0.1.0",
"wchar 0.1.0",
]
......
......@@ -12,6 +12,7 @@ platform = { path = "../platform" }
ctype = { path = "../ctype" }
errno = { path = "../errno" }
rand = { version = "0.5.2", default-features = false }
string = { path = "../string" }
time = { path = "../time" }
unistd = { path = "../unistd" }
wchar = { path = "../wchar" }
string = { path = "../string" }
......@@ -9,6 +9,7 @@ extern crate platform;
extern crate rand;
extern crate string;
extern crate time;
extern crate unistd;
extern crate wchar;
use core::{ptr, str};
......@@ -783,8 +784,37 @@ pub unsafe extern "C" fn strtol(s: *const c_char, endptr: *mut *mut c_char, base
}
#[no_mangle]
pub extern "C" fn system(command: *const c_char) -> c_int {
unimplemented!();
pub unsafe extern "C" fn system(command: *const c_char) -> c_int {
let child_pid = unistd::fork();
if child_pid == 0 {
let command_nonnull = if command.is_null() {
"exit 0\0".as_ptr()
} else {
command as *const u8
};
let shell = "/bin/sh\0".as_ptr();
let args = [
"sh\0".as_ptr(),
"-c\0".as_ptr(),
command_nonnull,
ptr::null()
];
unistd::execv(shell as *const c_char, args.as_ptr() as *const *mut c_char);
exit(127);
unreachable!();
} else {
let mut wstatus = 0;
if platform::waitpid(child_pid, &mut wstatus, 0) < 0 {
return -1;
}
wstatus
}
}
#[no_mangle]
......
# Automatically generated by 'make ignore'
/*.out
/gen/
/alloc
/assert
/args
/atof
/atoi
/brk
/chdir
/create
/ctype
/dup
/error
/exec
/fchdir
/fcntl
/fsync
/ftruncate
/getc_unget
/gethostname
/getid
/link
/locale
/math
/mem
/pipe
/printf
/rmdir
/rename
/scanf
/setid
/setjmp
/sleep
/sprintf
/strings
/stdlib/a64l
/stdlib/bsearch
/stdlib/mktemp
/stdlib/rand
/stdlib/strtol
/stdlib/strtoul
/stdlib/a64l
/stdio/fwrite
/stdio/all
/stdio/freopen
/string/strchr
/string/strcspn
/string/strncmp
/string/strpbrk
/string/strrchr
/string/strspn
/string/strstr
/string/strtok
/string/strtok_r
/unistd/getopt
/unlink
/waitpid
/wchar/mbrtowc
/wchar/mbsrtowcs
/wchar/putwchar
/wchar/wcrtomb
/write
/time
/gmtime
/asctime
assert
atof
atoi
brk
args
create
ctype
dup
error
exec
fchdir
fcntl
fsync
ftruncate
getc_unget
locale
math
mem
pipe
printf
rename
rmdir
scanf
setjmp
sleep
sprintf
strings
stdio/fwrite
stdio/all
stdio/freopen
stdlib/strtol
stdlib/strtoul
stdlib/a64l
stdlib/rand
string/strncmp
string/strcspn
string/strchr
string/strrchr
string/strspn
string/strstr
string/strpbrk
string/strtok
string/strtok_r
system
unistd/getopt
waitpid
wchar/mbrtowc
wchar/mbsrtowcs
wchar/putwchar
wchar/wcrtomb
write
time
gmtime
asctime
alloc
chdir
gethostname
getid
link
setid
stdlib/bsearch
stdlib/mktemp
unlink
......@@ -43,6 +43,7 @@ EXPECT_BINS=\
string/strpbrk \
string/strtok \
string/strtok_r \
system \
unistd/getopt \
waitpid \
wchar/mbrtowc \
......@@ -72,6 +73,15 @@ all: $(BINS)
clean:
rm -f $(BINS) *.out
ignore: $(BINS)
echo "# Automatically generated by 'make ignore'" > .gitignore
echo "/*.out" >> .gitignore
echo "/gen/" >> .gitignore
for bin in $^; \
do \
echo "$${bin}" >> .gitignore; \
done
run: $(BINS)
for bin in $^; \
do \
......
test of system
#include <stdlib.h>
int main(int argc, char ** argv) {
system("echo test of system");
return 0;
}
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