1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

Merge pull request #2267 from jhscheer/who_fix_runlevel

who: exclude `--runlevel` from non Linux targets (fix #2239)
This commit is contained in:
Sylvestre Ledru 2021-05-23 13:41:56 +02:00 committed by GitHub
commit 4061ada132
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 36 deletions

View file

@ -29,7 +29,6 @@ mod options {
pub const ONLY_HOSTNAME_USER: &str = "only_hostname_user"; pub const ONLY_HOSTNAME_USER: &str = "only_hostname_user";
pub const PROCESS: &str = "process"; pub const PROCESS: &str = "process";
pub const COUNT: &str = "count"; pub const COUNT: &str = "count";
#[cfg(any(target_os = "linux", target_os = "android"))]
pub const RUNLEVEL: &str = "runlevel"; pub const RUNLEVEL: &str = "runlevel";
pub const SHORT: &str = "short"; pub const SHORT: &str = "short";
pub const TIME: &str = "time"; pub const TIME: &str = "time";
@ -41,6 +40,11 @@ mod options {
static VERSION: &str = env!("CARGO_PKG_VERSION"); static VERSION: &str = env!("CARGO_PKG_VERSION");
static ABOUT: &str = "Print information about users who are currently logged in."; static ABOUT: &str = "Print information about users who are currently logged in.";
#[cfg(any(target_os = "linux"))]
static RUNLEVEL_HELP: &str = "print current runlevel";
#[cfg(not(target_os = "linux"))]
static RUNLEVEL_HELP: &str = "print current runlevel (This is meaningless on non Linux)";
fn get_usage() -> String { fn get_usage() -> String {
format!("{0} [OPTION]... [ FILE | ARG1 ARG2 ]", executable!()) format!("{0} [OPTION]... [ FILE | ARG1 ARG2 ]", executable!())
} }
@ -119,13 +123,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
.help("all login names and number of users logged on"), .help("all login names and number of users logged on"),
) )
.arg( .arg(
#[cfg(any(target_os = "linux", target_os = "android"))]
Arg::with_name(options::RUNLEVEL) Arg::with_name(options::RUNLEVEL)
.long(options::RUNLEVEL) .long(options::RUNLEVEL)
.short("r") .short("r")
.help("print current runlevel"), .help(RUNLEVEL_HELP),
#[cfg(any(target_vendor = "apple", target_os = "freebsd"))]
Arg::with_name(""),
) )
.arg( .arg(
Arg::with_name(options::SHORT) Arg::with_name(options::SHORT)
@ -267,13 +268,10 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
assumptions = false; assumptions = false;
} }
#[cfg(any(target_os = "linux", target_os = "android"))] if matches.is_present(options::RUNLEVEL) {
{ need_runlevel = true;
if matches.is_present(options::RUNLEVEL) { include_idle = true;
need_runlevel = true; assumptions = false;
include_idle = true;
assumptions = false;
}
} }
if matches.is_present(options::SHORT) { if matches.is_present(options::SHORT) {
@ -389,15 +387,12 @@ fn current_tty() -> String {
impl Who { impl Who {
fn exec(&mut self) { fn exec(&mut self) {
let run_level_chk = |record: i16| { let run_level_chk = |_record: i16| {
#[allow(unused_assignments)] #[cfg(not(target_os = "linux"))]
let mut res = false; return false;
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] #[cfg(target_os = "linux")]
{ return _record == utmpx::RUN_LVL;
res = record == utmpx::RUN_LVL;
}
res
}; };
let f = if self.args.len() == 1 { let f = if self.args.len() == 1 {
@ -430,7 +425,9 @@ impl Who {
if self.need_users && ut.is_user_process() { if self.need_users && ut.is_user_process() {
self.print_user(&ut); self.print_user(&ut);
} else if self.need_runlevel && run_level_chk(ut.record_type()) { } else if self.need_runlevel && run_level_chk(ut.record_type()) {
self.print_runlevel(&ut); if cfg!(target_os = "linux") {
self.print_runlevel(&ut);
}
} else if self.need_boottime && ut.record_type() == utmpx::BOOT_TIME { } else if self.need_boottime && ut.record_type() == utmpx::BOOT_TIME {
self.print_boottime(&ut); self.print_boottime(&ut);
} else if self.need_clockchange && ut.record_type() == utmpx::NEW_TIME { } else if self.need_clockchange && ut.record_type() == utmpx::NEW_TIME {

View file

@ -83,23 +83,17 @@ fn test_process() {
} }
} }
#[cfg(target_os = "linux")]
#[test] #[test]
fn test_runlevel() { fn test_runlevel() {
for opt in vec!["-r", "--runlevel"] { for opt in vec!["-r", "--runlevel"] {
#[cfg(any(target_vendor = "apple", target_os = "linux"))]
new_ucmd!() new_ucmd!()
.arg(opt) .arg(opt)
.succeeds() .succeeds()
.stdout_is(expected_result(&[opt])); .stdout_is(expected_result(&[opt]));
}
}
#[cfg(any(target_vendor = "apple", target_os = "freebsd"))] #[cfg(not(target_os = "linux"))]
#[test] new_ucmd!().arg(opt).succeeds().stdout_is("");
fn test_runlevel() {
let expected = "error: Found argument";
for opt in vec!["-r", "--runlevel"] {
new_ucmd!().arg(opt).fails().stderr_contains(expected);
} }
} }
@ -131,7 +125,6 @@ fn test_mesg() {
} }
} }
#[cfg(target_os = "linux")]
#[test] #[test]
fn test_arg1_arg2() { fn test_arg1_arg2() {
let args = ["am", "i"]; let args = ["am", "i"];
@ -142,7 +135,6 @@ fn test_arg1_arg2() {
.stdout_is(expected_result(&args)); .stdout_is(expected_result(&args));
} }
#[cfg(target_os = "linux")]
#[test] #[test]
fn test_too_many_args() { fn test_too_many_args() {
const EXPECTED: &str = const EXPECTED: &str =
@ -164,11 +156,11 @@ fn test_users() {
let mut v_actual: Vec<&str> = actual.split_whitespace().collect(); let mut v_actual: Vec<&str> = actual.split_whitespace().collect();
let mut v_expect: Vec<&str> = expect.split_whitespace().collect(); let mut v_expect: Vec<&str> = expect.split_whitespace().collect();
// TODO: `--users` differs from GNU's output on manOS running in CI // TODO: `--users` differs from GNU's output on macOS
// Diff < left / right > : // Diff < left / right > :
// <"runner console 2021-05-20 22:03 00:08 196\n" // <"runner console 2021-05-20 22:03 00:08 196\n"
// >"runner console 2021-05-20 22:03 old 196\n" // >"runner console 2021-05-20 22:03 old 196\n"
if is_ci() && cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
v_actual.remove(4); v_actual.remove(4);
v_expect.remove(4); v_expect.remove(4);
} }
@ -202,7 +194,7 @@ fn test_dead() {
#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[cfg(any(target_vendor = "apple", target_os = "linux"))]
#[test] #[test]
fn test_all_separately() { fn test_all_separately() {
if is_ci() && cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
// TODO: fix `-u`, see: test_users // TODO: fix `-u`, see: test_users
return; return;
} }
@ -225,7 +217,7 @@ fn test_all_separately() {
#[cfg(any(target_vendor = "apple", target_os = "linux"))] #[cfg(any(target_vendor = "apple", target_os = "linux"))]
#[test] #[test]
fn test_all() { fn test_all() {
if is_ci() && cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
// TODO: fix `-u`, see: test_users // TODO: fix `-u`, see: test_users
return; return;
} }