From ff667cc643d325a163d75cfac6616d4dcdeb5a45 Mon Sep 17 00:00:00 2001 From: Brian Norris Date: Wed, 5 Feb 2025 10:21:11 -0800 Subject: [PATCH] pgrep: Implement -a 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 8db848f8..59570066 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 3a745321..0fc81a84 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, *arg, REG_EXTENDED); -- 2.39.5