diff --git a/src/uu/kill/src/kill.rs b/src/uu/kill/src/kill.rs index 7638dcc44..18b45e2ef 100644 --- a/src/uu/kill/src/kill.rs +++ b/src/uu/kill/src/kill.rs @@ -17,6 +17,11 @@ use uucore::{format_usage, help_about, help_usage, show}; static ABOUT: &str = help_about!("kill.md"); const USAGE: &str = help_usage!("kill.md"); +// When the -l option is selected, the program displays the type of signal related to a certain +// value or string. In case of a value, the program should control the lower 8 bits, but there is +// a particular case in which if the value is in range [128, 159], it is translated to a signal +const OFFSET: usize = 128; + pub mod options { pub static PIDS_OR_SIGNALS: &str = "pids_or_signals"; pub static LIST: &str = "list"; @@ -164,13 +169,24 @@ fn table() { } fn print_signal(signal_name_or_value: &str) -> UResult<()> { + // Closure used to track the last 8 bits of the signal value + // when the -l option is passed only the lower 8 bits are important + // or the value is in range [128, 159] + // Example: kill -l 143 => TERM because 143 = 15 + 128 + // Example: kill -l 2304 => EXIT + let lower_8_bits = |x: usize| x & 0xff; + let option_num_parse = signal_name_or_value.parse::().ok(); + for (value, &signal) in ALL_SIGNALS.iter().enumerate() { if signal.eq_ignore_ascii_case(signal_name_or_value) || format!("SIG{signal}").eq_ignore_ascii_case(signal_name_or_value) { println!("{value}"); return Ok(()); - } else if signal_name_or_value == value.to_string() { + } else if signal_name_or_value == value.to_string() + || option_num_parse.is_some_and(|signal_value| lower_8_bits(signal_value) == value) + || option_num_parse.is_some_and(|signal_value| signal_value == value + OFFSET) + { println!("{signal}"); return Ok(()); } diff --git a/tests/by-util/test_kill.rs b/tests/by-util/test_kill.rs index b42e34828..0cdfd9aae 100644 --- a/tests/by-util/test_kill.rs +++ b/tests/by-util/test_kill.rs @@ -334,6 +334,39 @@ fn test_kill_with_signal_and_list() { .fails(); } +#[test] +fn test_kill_with_list_lower_bits() { + new_ucmd!() + .arg("-l") + .arg("128") + .succeeds() + .stdout_contains("EXIT"); + + new_ucmd!() + .arg("-l") + .arg("143") + .succeeds() + .stdout_contains("TERM"); + + new_ucmd!() + .arg("-l") + .arg("256") + .succeeds() + .stdout_contains("EXIT"); + + new_ucmd!() + .arg("-l") + .arg("2304") + .succeeds() + .stdout_contains("EXIT"); +} + +#[test] +fn test_kill_with_list_lower_bits_unrecognized() { + new_ucmd!().arg("-l").arg("111").fails(); + new_ucmd!().arg("-l").arg("384").fails(); +} + #[test] fn test_kill_with_signal_and_table() { let target = Target::new(); diff --git a/util/why-error.md b/util/why-error.md index 6c7865e43..b387bf331 100644 --- a/util/why-error.md +++ b/util/why-error.md @@ -27,7 +27,6 @@ This file documents why some tests are failing: * gnu/tests/ls/stat-free-symlinks.sh * gnu/tests/misc/close-stdout.sh * gnu/tests/misc/comm.pl -* gnu/tests/misc/kill.sh - https://github.com/uutils/coreutils/issues/7218 * gnu/tests/misc/nohup.sh * gnu/tests/misc/numfmt.pl - https://github.com/uutils/coreutils/issues/7219 / https://github.com/uutils/coreutils/issues/7221 * gnu/tests/misc/stdbuf.sh - https://github.com/uutils/coreutils/issues/7072