about summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorHuon Wilson <dbau.pp+github@gmail.com>2013-10-08 23:19:20 +1100
committerHuon Wilson <dbau.pp+github@gmail.com>2013-10-09 22:22:43 +1100
commit5bb5f7678530c32caa11a1bc31e27187ff74bc19 (patch)
tree12847e63ec8cc1a756714f680f582777324a46af /src/libstd
parent9db32a2f1d2cfafc519941475f5e660a9ae076f0 (diff)
downloadrust-5bb5f7678530c32caa11a1bc31e27187ff74bc19.tar.gz
rust-5bb5f7678530c32caa11a1bc31e27187ff74bc19.zip
Convert rt::sched::new_sched_rng to use open/read/close rather than f*.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/rt/sched.rs26
1 files changed, 10 insertions, 16 deletions
diff --git a/src/libstd/rt/sched.rs b/src/libstd/rt/sched.rs
index 618921f8e08..e5cbadda23d 100644
--- a/src/libstd/rt/sched.rs
+++ b/src/libstd/rt/sched.rs
@@ -859,42 +859,36 @@ fn new_sched_rng() -> XorShiftRng {
     use libc;
     use sys;
     use c_str::ToCStr;
-    use ptr::RawPtr;
     use vec::MutableVector;
     use iter::Iterator;
     use rand::SeedableRng;
 
     // XXX: this could use io::native::file, when it works.
-    let file = do "/dev/urandom".with_c_str |name| {
-        do "r".with_c_str |mode| {
-            unsafe { libc::fopen(name, mode) }
-        }
+    let fd = do "/dev/urandom".with_c_str |name| {
+        unsafe { libc::open(name, libc::O_RDONLY, 0) }
     };
-    if file.is_null() {
+    if fd == -1 {
         rtabort!("could not open /dev/urandom for reading.")
     }
 
     let mut seeds = [0u32, .. 4];
+    let size = sys::size_of_val(&seeds);
     loop {
-        let nbytes = do seeds.as_mut_buf |buf, len| {
+        let nbytes = do seeds.as_mut_buf |buf, _| {
             unsafe {
-                libc::fread(buf as *mut libc::c_void,
-                            sys::size_of::<u32>() as libc::size_t,
-                            len as libc::size_t,
-                            file)
+                libc::read(fd,
+                           buf as *mut libc::c_void,
+                           size as libc::size_t)
             }
         };
-        rtassert!(nbytes == seeds.len() as libc::size_t);
+        rtassert!(nbytes as uint == size);
 
         if !seeds.iter().all(|x| *x == 0) {
             break;
         }
     }
 
-    // XXX: do we need to guarantee that this is closed with a finally
-    // block (is that even possible without a scheduler?), or do we
-    // know that the only way that we can fail here is `abort`ing?
-    unsafe {libc::fclose(file);}
+    unsafe {libc::close(fd);}
 
     SeedableRng::from_seed(seeds)
 }