Skip to content

Commit 5bb5f76

Browse files
committed
Convert rt::sched::new_sched_rng to use open/read/close rather than f*.
1 parent 9db32a2 commit 5bb5f76

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/libstd/rt/sched.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -859,42 +859,36 @@ fn new_sched_rng() -> XorShiftRng {
859859
use libc;
860860
use sys;
861861
use c_str::ToCStr;
862-
use ptr::RawPtr;
863862
use vec::MutableVector;
864863
use iter::Iterator;
865864
use rand::SeedableRng;
866865

867866
// XXX: this could use io::native::file, when it works.
868-
let file = do "/dev/urandom".with_c_str |name| {
869-
do "r".with_c_str |mode| {
870-
unsafe { libc::fopen(name, mode) }
871-
}
867+
let fd = do "/dev/urandom".with_c_str |name| {
868+
unsafe { libc::open(name, libc::O_RDONLY, 0) }
872869
};
873-
if file.is_null() {
870+
if fd == -1 {
874871
rtabort!("could not open /dev/urandom for reading.")
875872
}
876873

877874
let mut seeds = [0u32, .. 4];
875+
let size = sys::size_of_val(&seeds);
878876
loop {
879-
let nbytes = do seeds.as_mut_buf |buf, len| {
877+
let nbytes = do seeds.as_mut_buf |buf, _| {
880878
unsafe {
881-
libc::fread(buf as *mut libc::c_void,
882-
sys::size_of::<u32>() as libc::size_t,
883-
len as libc::size_t,
884-
file)
879+
libc::read(fd,
880+
buf as *mut libc::c_void,
881+
size as libc::size_t)
885882
}
886883
};
887-
rtassert!(nbytes == seeds.len() as libc::size_t);
884+
rtassert!(nbytes as uint == size);
888885

889886
if !seeds.iter().all(|x| *x == 0) {
890887
break;
891888
}
892889
}
893890

894-
// XXX: do we need to guarantee that this is closed with a finally
895-
// block (is that even possible without a scheduler?), or do we
896-
// know that the only way that we can fail here is `abort`ing?
897-
unsafe {libc::fclose(file);}
891+
unsafe {libc::close(fd);}
898892

899893
SeedableRng::from_seed(seeds)
900894
}

0 commit comments

Comments
 (0)