diff --git a/src/uu/pgrep/src/pgrep.rs b/src/uu/pgrep/src/pgrep.rs index 7a9ac31..169324a 100644 --- a/src/uu/pgrep/src/pgrep.rs +++ b/src/uu/pgrep/src/pgrep.rs @@ -85,7 +85,15 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { let formatted: Vec<_> = if matches.get_flag("list-full") { pids.into_iter() - .map(|it| format!("{} {}", it.pid, it.cmdline)) + .map(|it| { + // pgrep from procps-ng outputs the process name inside square brackets + // if /proc//cmdline is empty + if it.cmdline.is_empty() { + format!("{} [{}]", it.pid, it.clone().status().get("Name").unwrap()) + } else { + format!("{} {}", it.pid, it.cmdline) + } + }) .collect() } else if matches.get_flag("list-name") { pids.into_iter() diff --git a/tests/by-util/test_pgrep.rs b/tests/by-util/test_pgrep.rs index 1ccfdd5..e220ff7 100644 --- a/tests/by-util/test_pgrep.rs +++ b/tests/by-util/test_pgrep.rs @@ -139,3 +139,26 @@ fn test_ignore_case() { new_ucmd!().arg("SH").arg(arg).succeeds(); } } + +#[test] +#[cfg(target_os = "linux")] +fn test_list_full() { + for arg in ["-a", "--list-full"] { + new_ucmd!() + .arg("sh") + .arg(arg) + .succeeds() + // (?m) enables multi-line mode + .stdout_matches(&Regex::new("(?m)^[1-9][0-9]* .+$").unwrap()); + } +} + +#[test] +#[cfg(target_os = "linux")] +fn test_list_full_process_with_empty_cmdline() { + new_ucmd!() + .arg("kthreadd") + .arg("--list-full") + .succeeds() + .stdout_matches(&Regex::new(r"^[1-9][0-9]* \[kthreadd\]\n$").unwrap()); +}