[Toybox] [PATCH 2/2] pgrep: Implement -a

Brian Norris briannorris at google.com
Wed Feb 5 10:21:11 PST 2025


pgrep currently implements -l and -f, but not -a. It also inadvertently
treats -f as a combination of -a and -f, where we both filter via the
full command line and display the full command line. This violates
--help, where -f says "Check full command line for PATTERN".

Implement -a, which takes care of displaying the full command line, and
return -f to simply checking (filtering) via command line.
---
 tests/pgrep.test | 12 +++++++++++-
 toys/posix/ps.c  |  9 +++++----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/tests/pgrep.test b/tests/pgrep.test
index 8db848f8a180..595700663498 100644
--- a/tests/pgrep.test
+++ b/tests/pgrep.test
@@ -11,7 +11,7 @@ killall yes >/dev/null 2>&1
 #testing "name" "command" "result" "infile" "stdin"
 
 # Starting processes to test pgrep command
-yes >/dev/null &
+yes and no >/dev/null &
 proc=$!
 #echo "# Process created with id: $proc"
 sleep .1
@@ -24,11 +24,21 @@ testing "pattern" "pgrep yes" "$proc\n" "" ""
 testing "wildCardPattern" "pgrep ^y.*s$" "$proc\n" "" ""
 testing "-l pattern" "pgrep -l yes" "$proc yes\n" "" ""
 testing "-f pattern" "pgrep -f yes" "$proc\n" "" ""
+testing "-a pattern" "pgrep -a yes" "$proc yes and no\n" "" ""
+testing "-la pattern" "pgrep -la yes" "$proc yes and no\n" "" ""
+testing "-fa pattern" "pgrep -fa yes" "$proc yes and no\n" "" ""
+testing "-lf pattern" "pgrep -lf yes" "$proc yes\n" "" ""
+testing "-fa pattern" "pgrep -fa yes" "$proc yes and no\n" "" ""
+testing "-lfa pattern" "pgrep -lfa yes" "$proc yes and no\n" "" ""
 testing "-n pattern" "pgrep -n yes" "$proc\n" "" ""
 testing "-o pattern" "pgrep -o yes" "$proc\n" "" ""
 testing "-s" "pgrep -s $session_id yes" "$proc\n" "" ""
 testing "-P" "pgrep -P $proc_parent yes" "$proc\n" "" ""
 
+testing "-f 'full command line'" "pgrep -f 'yes and no'" "$proc\n" "" ""
+testing "-l 'full command line nomatch'" "pgrep -l 'yes and no'" "" "" ""
+testing "-a 'full command line nomatch'" "pgrep -a 'yes and no'" "" "" ""
+
 testing "return success" "pgrep yes && echo success" "$proc\nsuccess\n" "" ""
 testing "return failure" "pgrep almost-certainly-not || echo failure" \
     "failure\n" "" ""
diff --git a/toys/posix/ps.c b/toys/posix/ps.c
index 3a745321c794..0fc81a847835 100644
--- a/toys/posix/ps.c
+++ b/toys/posix/ps.c
@@ -51,7 +51,7 @@ USE_PS(NEWTOY(ps, "k(sort)*P(ppid)*aAdeflMno*O*p(pid)*s*t*Tu*U*g*G*wZ[!ol][+Ae][
 // the default values are different but the flags are in the same order.
 USE_TOP(NEWTOY(top, ">0O*h" "Hk*o*p*u*s#<1d%<100=3000m#n#<1bq[!oO]", TOYFLAG_USR|TOYFLAG_BIN))
 USE_IOTOP(NEWTOY(iotop, ">0AaKO" "Hk*o*p*u*s#<1=7d%<100=3000m#n#<1bq", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_STAYROOT))
-USE_PGREP(NEWTOY(pgrep, "cld:u*U*t*s*P*g*G*fnovxL:[-no]", TOYFLAG_USR|TOYFLAG_BIN))
+USE_PGREP(NEWTOY(pgrep, "acld:u*U*t*s*P*g*G*fnovxL:[-no]", TOYFLAG_USR|TOYFLAG_BIN))
 USE_PKILL(NEWTOY(pkill,    "?Vu*U*t*s*P*g*G*fnovxl:[-no]", TOYFLAG_USR|TOYFLAG_BIN))
 
 config PS
@@ -142,11 +142,12 @@ config PGREP
   bool "pgrep"
   default y
   help
-    usage: pgrep [-clfnovx] [-d DELIM] [-L SIGNAL] [PATTERN] [-G GID,] [-g PGRP,] [-P PPID,] [-s SID,] [-t TERM,] [-U UID,] [-u EUID,]
+    usage: pgrep [-aclfnovx] [-d DELIM] [-L SIGNAL] [PATTERN] [-G GID,] [-g PGRP,] [-P PPID,] [-s SID,] [-t TERM,] [-U UID,] [-u EUID,]
 
     Search for process(es). PATTERN is an extended regular expression checked
     against command names.
 
+    -a	Show the full command line
     -c	Show only count of matches
     -d	Use DELIM instead of newline
     -L	Send SIGNAL instead of printing name
@@ -1902,7 +1903,7 @@ static void do_pgk(struct procpid *tb)
   }
   if (!FLAG(c) && (!TT.pgrep.signal || TT.tty)) {
     printf("%lld", *tb->slot);
-    if (FLAG(l)) printf(" %s", tb->str+tb->offset[4]*FLAG(f));
+    if (FLAG(a)|FLAG(l)) printf(" %s", tb->str+tb->offset[4]*FLAG(a));
     printf("%s", TT.pgrep.d ? TT.pgrep.d : "\n");
   }
 }
@@ -1971,7 +1972,7 @@ void pgrep_main(void)
       !(toys.optflags&(FLAG_G|FLAG_g|FLAG_P|FLAG_s|FLAG_t|FLAG_U|FLAG_u)))
     if (!toys.optc) help_exit("No PATTERN");
 
-  if (FLAG(f)) TT.bits |= _PS_CMDLINE;
+  if (FLAG(f)|FLAG(a)) TT.bits |= _PS_CMDLINE;
   for (arg = toys.optargs; *arg; arg++) {
     reg = xmalloc(sizeof(struct regex_list));
     xregcomp(&reg->reg, *arg, REG_EXTENDED);
-- 
2.48.1.362



More information about the Toybox mailing list