mirror of
https://github.com/uutils/coreutils
synced 2024-11-17 10:18:11 +00:00
Merge pull request #1200 from Arcterus/uptime-fix
uptime: error when uptime cannot be found
This commit is contained in:
commit
e04911e938
3 changed files with 32 additions and 11 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -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",
|
||||
]
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ path = "uptime.rs"
|
|||
|
||||
[dependencies]
|
||||
getopts = "0.2.14"
|
||||
time = "0.1.38"
|
||||
|
||||
[dependencies.uucore]
|
||||
path = "../uucore"
|
||||
|
|
|
@ -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<String>) -> 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<time_t>, 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<time_t>) -> 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<time_t>) -> i64 {
|
|||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
fn get_uptime(boot_time: Option<time_t>) -> i64 {
|
||||
fn get_uptime(_boot_time: Option<time_t>) -> i64 {
|
||||
unsafe { GetTickCount() as i64 }
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue