Skip to content

Commit cfe4cb3

Browse files
committed
pkill: Support --inverse
pkill --help doesn't list this option, but it is listed in the manpage (and does work). Unlike pgrep, the short option '-v' doesn't exists for pkill though.
1 parent 75c2bd5 commit cfe4cb3

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/uu/pkill/src/pkill.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ struct Settings {
3636
exact: bool,
3737
full: bool,
3838
ignore_case: bool,
39+
inverse: bool,
3940
newest: bool,
4041
oldest: bool,
4142
older: Option<u64>,
@@ -64,6 +65,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
6465
exact: matches.get_flag("exact"),
6566
full: matches.get_flag("full"),
6667
ignore_case: matches.get_flag("ignore-case"),
68+
inverse: matches.get_flag("inverse"),
6769
newest: matches.get_flag("newest"),
6870
oldest: matches.get_flag("oldest"),
6971
parent: matches
@@ -235,11 +237,12 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
235237
_ => true,
236238
};
237239

238-
if run_state_matched
240+
if (run_state_matched
239241
&& pattern_matched
240242
&& tty_matched
241243
&& older_matched
242-
&& parent_matched
244+
&& parent_matched)
245+
^ settings.inverse
243246
{
244247
tmp_vec.push(pid);
245248
}
@@ -343,12 +346,13 @@ pub fn uu_app() -> Command {
343346
.about(ABOUT)
344347
.override_usage(format_usage(USAGE))
345348
.args_override_self(true)
346-
.group(ArgGroup::new("oldest_newest").args(["oldest", "newest"]))
349+
.group(ArgGroup::new("oldest_newest").args(["oldest", "newest", "inverse"]))
347350
.args([
348351
// arg!(-<sig> "signal to send (either number or name)"),
349352
arg!(-H --"require-handler" "match only if signal handler is present"),
350353
// arg!(-q --queue <value> "integer value to be sent with the signal"),
351354
arg!(-e --echo "display what is killed"),
355+
arg!(--inverse "negates the matching"),
352356
arg!(-c --count "count of matching processes"),
353357
arg!(-f --full "use full process name to match"),
354358
// arg!(-g --pgroup <PGID> "match listed process group IDs")

tests/by-util/test_pkill.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ fn test_invalid_arg() {
4444
new_ucmd!().arg("--definitely-invalid").fails().code_is(1);
4545
}
4646

47+
#[cfg(target_os = "linux")]
48+
#[test]
49+
fn test_inverse() {
50+
new_ucmd!()
51+
.arg("-0")
52+
.arg("--inverse")
53+
.arg("NONEXISTENT")
54+
.fails()
55+
.stderr_contains("Permission denied");
56+
}
57+
4758
#[cfg(unix)]
4859
#[test]
4960
fn test_help() {

0 commit comments

Comments
 (0)