From 4db812d34dfdf7896d8630307b4608f10f652614 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Tue, 20 Mar 2018 19:31:58 -0700 Subject: [PATCH 1/4] implement rand and srand --- src/stdlib/Cargo.toml | 1 + src/stdlib/src/lib.rs | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/stdlib/Cargo.toml b/src/stdlib/Cargo.toml index 97f5d44187..cc96a5dc07 100644 --- a/src/stdlib/Cargo.toml +++ b/src/stdlib/Cargo.toml @@ -12,3 +12,4 @@ platform = { path = "../platform" } ralloc = { path = "../../ralloc", default-features = false } ctype = { path = "../ctype" } errno = { path = "../errno" } +rand = { git = "https://github.com/rust-lang-nursery/rand/", default-features = false } diff --git a/src/stdlib/src/lib.rs b/src/stdlib/src/lib.rs index 575f759a26..8a5535f739 100644 --- a/src/stdlib/src/lib.rs +++ b/src/stdlib/src/lib.rs @@ -1,15 +1,18 @@ -//! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html - #![no_std] #![feature(core_intrinsics)] #![feature(global_allocator)] + +///! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html + extern crate ctype; extern crate errno; extern crate platform; extern crate ralloc; +extern crate rand; use core::{ptr, str}; +use rand::{Rng, XorShiftRng, SeedableRng}; use errno::*; use platform::types::*; @@ -19,8 +22,10 @@ static ALLOCATOR: ralloc::Allocator = ralloc::Allocator; pub const EXIT_FAILURE: c_int = 1; pub const EXIT_SUCCESS: c_int = 0; +pub const RAND_MAX: c_int = 2147483647; static mut ATEXIT_FUNCS: [Option; 32] = [None; 32]; +static mut RNG: Option = None; #[no_mangle] pub unsafe extern "C" fn a64l(s: *const c_char) -> c_long { @@ -375,8 +380,18 @@ pub extern "C" fn qsort( } #[no_mangle] -pub extern "C" fn rand() -> c_int { - unimplemented!(); +pub unsafe extern "C" fn rand() -> c_int { + match RNG { + Some(ref mut rng) => { + rng.gen_range::(0, RAND_MAX) + }, + None => { + let mut rng = XorShiftRng::from_seed([1; 16]); + let ret = rng.gen_range::(0, RAND_MAX); + RNG = Some(rng); + ret + }, + } } #[no_mangle] @@ -425,8 +440,8 @@ pub extern "C" fn setstate(state: *const c_char) -> *mut c_char { } #[no_mangle] -pub extern "C" fn srand(seed: c_uint) { - unimplemented!(); +pub unsafe extern "C" fn srand(seed: c_uint) { + RNG = Some(XorShiftRng::from_seed([seed as u8; 16])); } #[no_mangle] From a24f537a3834dd235057ae00128244df50af5458 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Tue, 20 Mar 2018 19:42:49 -0700 Subject: [PATCH 2/4] test --- tests/.gitignore | 1 + tests/Makefile | 1 + tests/stdlib/rand.c | 8 ++++++++ 3 files changed, 10 insertions(+) create mode 100644 tests/stdlib/rand.c diff --git a/tests/.gitignore b/tests/.gitignore index ff5cc28a8c..e611cfdbec 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -28,6 +28,7 @@ /stdlib/bsearch /stdlib/strtol /stdlib/a64l +/stdlib/rand /string/strncmp /string/strcspn /string/strchr diff --git a/tests/Makefile b/tests/Makefile index e1b95b1839..a4d455d1da 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -23,6 +23,7 @@ EXPECT_BINS=\ stdlib/bsearch \ stdlib/strtol \ stdlib/a64l \ + stdlib/rand \ string/strncmp \ string/strcspn \ string/strchr \ diff --git a/tests/stdlib/rand.c b/tests/stdlib/rand.c new file mode 100644 index 0000000000..484b09906b --- /dev/null +++ b/tests/stdlib/rand.c @@ -0,0 +1,8 @@ +#include +#include + +int main(int argc, char** argv) { + printf("%d\n", rand()); + srand(259); + printf("%d\n", rand()); +} From cc5a4e92ce85153bd625119dbad5c6ec61dcb65f Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Tue, 20 Mar 2018 19:45:45 -0700 Subject: [PATCH 3/4] fmt --- src/stdlib/src/lib.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/stdlib/src/lib.rs b/src/stdlib/src/lib.rs index 8a5535f739..acc68d08d1 100644 --- a/src/stdlib/src/lib.rs +++ b/src/stdlib/src/lib.rs @@ -2,9 +2,7 @@ #![feature(core_intrinsics)] #![feature(global_allocator)] - ///! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html - extern crate ctype; extern crate errno; extern crate platform; @@ -12,7 +10,7 @@ extern crate ralloc; extern crate rand; use core::{ptr, str}; -use rand::{Rng, XorShiftRng, SeedableRng}; +use rand::{Rng, SeedableRng, XorShiftRng}; use errno::*; use platform::types::*; @@ -382,15 +380,13 @@ pub extern "C" fn qsort( #[no_mangle] pub unsafe extern "C" fn rand() -> c_int { match RNG { - Some(ref mut rng) => { - rng.gen_range::(0, RAND_MAX) - }, + Some(ref mut rng) => rng.gen_range::(0, RAND_MAX), None => { let mut rng = XorShiftRng::from_seed([1; 16]); let ret = rng.gen_range::(0, RAND_MAX); RNG = Some(rng); ret - }, + } } } From cdc6209ff48cac5714df7b958e9cd2063816caad Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Wed, 21 Mar 2018 20:12:22 -0700 Subject: [PATCH 4/4] fix comment --- src/stdlib/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/stdlib/src/lib.rs b/src/stdlib/src/lib.rs index acc68d08d1..9c948d6779 100644 --- a/src/stdlib/src/lib.rs +++ b/src/stdlib/src/lib.rs @@ -1,8 +1,9 @@ +//! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html + #![no_std] #![feature(core_intrinsics)] #![feature(global_allocator)] -///! stdlib implementation for Redox, following http://pubs.opengroup.org/onlinepubs/7908799/xsh/stdlib.h.html extern crate ctype; extern crate errno; extern crate platform;