From 64be323ce83d4f6681d3ef13012e8ca1e391196f Mon Sep 17 00:00:00 2001 From: Franklin-Qi <43199883+Franklin-Qi@users.noreply.github.com> Date: Mon, 9 Feb 2026 16:41:24 +0800 Subject: [PATCH] pgrep: The pattern is too long (more than 15 characters) and should be able to match pid correctly. (#617) * pgrep: The pattern is too long (more than 15 characters) and should be able to match pid correctly. The PIDs should be obtained in advance. If the PIDs exist, it means the pattern matches, and there should be no length error. * Placing length error messages in subsequent could avoid performance penalties. --- src/uu/pgrep/src/process_matcher.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/uu/pgrep/src/process_matcher.rs b/src/uu/pgrep/src/process_matcher.rs index 49197fa..3aa6c85 100644 --- a/src/uu/pgrep/src/process_matcher.rs +++ b/src/uu/pgrep/src/process_matcher.rs @@ -161,18 +161,19 @@ pub fn get_match_settings(matches: &ArgMatches) -> UResult { )); } - if !settings.full && pattern.len() > 15 { - let msg = format!("pattern that searches for process name longer than 15 characters will result in zero matches\n\ - Try `{} -f' option to match against the complete command line.", uucore::util_name()); - return Err(USimpleError::new(1, msg)); - } - Ok(settings) } pub fn find_matching_pids(settings: &Settings) -> UResult> { let mut pids = collect_matched_pids(settings)?; + if pids.is_empty() { + if !settings.full && settings.regex.as_str().len() > 15 { + let msg = format!("pattern that searches for process name longer than 15 characters will result in zero matches\n\ + Try `{} -f' option to match against the complete command line.", uucore::util_name()); + return Err(USimpleError::new(1, msg)); + } + uucore::error::set_exit_code(1); Ok(pids) } else {