pgrep: Suppress warning if using regex

pgrep gives a warning if the match string is longer than
15 characters and there was no match. That often does not
make sense when using regex or at the very least difficult
to know when to warn users. e.g
"1234567890|123456789X" is a 21 character string but only
matching two 10 string words.

pgrep has a simple check for regex and will now suppress
that warning if that has been used.

References:
 https://bugs.debian.org/1037450

Signed-off-by: Craig Small <csmall@dropbear.xyz>
This commit is contained in:
Craig Small 2023-06-13 16:23:35 +10:00
parent bc688d630a
commit cfb3c482a5
2 changed files with 23 additions and 1 deletions

1
NEWS
View File

@ -5,6 +5,7 @@ procps-ng-NEXT
restore the proper main thread tics valuations issue #280
* free: -L one line output issue #156
* pgrep: Use only --signal option for signal Debian #1031765
* pgrep: suppress >15 warning if using regex Debian #1037450
* ps: fixed missing or corrupted fields with -m option Debian #1036631, issue #279
* tests: dont compare floats with == issue #271
* top: bad command line arguments yield EXIT_FAILURE issue #273

View File

@ -636,6 +636,27 @@ static size_t get_arg_max(void)
return val;
}
/*
* Check if we have a long simple (non-regex) match
* Returns true if the string:
* 1) is longer than 15 characters
* 2) Doesn't have | or [ which are used by regex
* This is not an exhaustive list but catches most instances
* It's only used to suppress the warning
*/
static bool is_long_match(const char *str)
{
int i, len;
if (str == NULL)
return FALSE;
if (15 >= (len = strlen(str)))
return FALSE;
for (i=0; i<len; i++)
if (str[i] == '|' || str[i] == '[')
return FALSE;
return TRUE;
}
static struct el * select_procs (int *num)
{
#define PIDS_GETINT(e) PIDS_VAL(EU_ ## e, s_int, stack, info)
@ -784,7 +805,7 @@ static struct el * select_procs (int *num)
*num = matches;
if ((!matches) && (!opt_full) && opt_pattern && (strlen(opt_pattern) > 15))
if ((!matches) && (!opt_full) && is_long_match(opt_pattern))
xwarnx(_("pattern that searches for process name longer than 15 characters will result in zero matches\n"
"Try `%s -f' option to match against the complete command line."),
program_invocation_short_name);