Blob Blame History Raw
commit cfb3c482a5c9235a9899e107f2f90873164f7049
Author: Craig Small <csmall@dropbear.xyz>
Date:   Tue Jun 13 16:23:35 2023 +1000

    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>

diff --git a/src/pgrep.c b/src/pgrep.c
index 442dbfcc..838bb2d0 100644
--- a/src/pgrep.c
+++ b/src/pgrep.c
@@ -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);