diff --git a/Cargo.lock b/Cargo.lock index e5d0b4285..2db13b2bf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1695,6 +1695,7 @@ name = "uptime" version = "0.0.1" dependencies = [ "getopts 0.2.17 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", "uucore 0.0.1", ] diff --git a/src/uptime/Cargo.toml b/src/uptime/Cargo.toml index cb01f2b31..542c5320e 100644 --- a/src/uptime/Cargo.toml +++ b/src/uptime/Cargo.toml @@ -10,6 +10,7 @@ path = "uptime.rs" [dependencies] getopts = "0.2.14" +time = "0.1.38" [dependencies.uucore] path = "../uucore" diff --git a/src/uptime/uptime.rs b/src/uptime/uptime.rs index e32846ddd..4c5269001 100644 --- a/src/uptime/uptime.rs +++ b/src/uptime/uptime.rs @@ -13,18 +13,15 @@ /* last synced with: cat (GNU coreutils) 8.13 */ extern crate getopts; +extern crate time; #[macro_use] extern crate uucore; // import crate time from utmpx -use uucore::utmpx::*; -use uucore::libc::{c_double, time_t}; +use uucore::libc::time_t; pub use uucore::libc; use getopts::Options; -use std::fs::File; -use std::io::Read; -use std::mem::transmute; static NAME: &'static str = "uptime"; static VERSION: &'static str = env!("CARGO_PKG_VERSION"); @@ -70,15 +67,26 @@ pub fn uumain(args: Vec) -> i32 { print_time(); let (boot_time, user_count) = process_utmpx(); - let upsecs = get_uptime(boot_time) / 100; - print_uptime(upsecs); - print_nusers(user_count); - print_loadavg(); + let uptime = get_uptime(boot_time); + if uptime < 0 { + show_error!("could not retrieve system uptime"); - 0 + 1 + } else { + let upsecs = uptime / 100; + print_uptime(upsecs); + print_nusers(user_count); + print_loadavg(); + + 0 + } } +#[cfg(unix)] fn print_loadavg() { + use libc::c_double; + use std::mem::transmute; + let mut avg: [c_double; 3] = [0.0; 3]; let loads: i32 = unsafe { transmute(getloadavg(avg.as_mut_ptr(), 3)) }; @@ -96,8 +104,16 @@ fn print_loadavg() { } } +#[cfg(windows)] +fn print_loadavg() { + // XXX: currently this is a noop as Windows does not seem to have anything comparable to + // getloadavg() +} + #[cfg(unix)] fn process_utmpx() -> (Option, usize) { + use uucore::utmpx::*; + let mut nusers = 0; let mut boot_time = None; @@ -140,6 +156,9 @@ fn print_time() { #[cfg(unix)] fn get_uptime(boot_time: Option) -> i64 { + use std::fs::File; + use std::io::Read; + let mut proc_uptime = String::new(); if let Some(n) = File::open("/proc/uptime") @@ -162,7 +181,7 @@ fn get_uptime(boot_time: Option) -> i64 { } #[cfg(windows)] -fn get_uptime(boot_time: Option) -> i64 { +fn get_uptime(_boot_time: Option) -> i64 { unsafe { GetTickCount() as i64 } }