git: d668d96259d5 - main - top: allow filtering by command name

From: Roman Bogorodskiy <novel_at_FreeBSD.org>
Date: Wed, 11 Nov 2026 17:31:17 UTC
The branch main has been updated by novel:

URL: https://cgit.FreeBSD.org/src/commit/?id=d668d96259d568877fa863095fc28bd6c323dd01

commit d668d96259d568877fa863095fc28bd6c323dd01
Author:     Roman Bogorodskiy <novel@FreeBSD.org>
AuthorDate: 2026-11-03 16:54:38 +0000
Commit:     Roman Bogorodskiy <novel@FreeBSD.org>
CommitDate: 2026-11-11 17:29:08 +0000

    top: allow filtering by command name
    
    top(1) already supports interactive command filtering using
    the '/' key. Support it also with a command line
    argument '-g' / '--grep'.
    
    Reviewed by:    kib
    Obtained from:  OpenBSD
    MFC after:      2 weeks
    Differential Revision:  https://reviews.freebsd.org/D61160
---
 usr.bin/top/top.1 | 10 +++++++++-
 usr.bin/top/top.c | 14 ++++++++++++--
 2 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/usr.bin/top/top.1 b/usr.bin/top/top.1
index 9b1860246de9..6c2597aa1334 100644
--- a/usr.bin/top/top.1
+++ b/usr.bin/top/top.1
@@ -1,4 +1,4 @@
-.Dd June 9, 2025
+.Dd November 11, 2026
 .Dt TOP 1
 .Os
 .Sh NAME
@@ -8,6 +8,7 @@
 .Nm
 .Op Fl abCHIijnPqSTtuvwz
 .Op Fl d Ar count
+.Op Fl g Ar string
 .Op Fl J Ar jail
 .Op Fl m Ar cpu | io
 .Op Fl o Ar field
@@ -95,6 +96,13 @@ Note that for
 .Ar count
 = 1
 no information is available about the percentage of time spent by the CPU in every state.
+.It Fl g Ar string
+Display only processes that contain
+.Ar string
+in their command name.
+If displaying of arguments is enabled with
+.Fl a ,
+the arguments are searched too.
 .It Fl H
 Display each thread for a multithreaded process individually.
 By default a single summary line is displayed for each process.
diff --git a/usr.bin/top/top.c b/usr.bin/top/top.c
index f0458a4037af..3ca766250297 100644
--- a/usr.bin/top/top.c
+++ b/usr.bin/top/top.c
@@ -98,6 +98,7 @@ static const struct option longopts[] = {
     { "batch", no_argument, NULL, 'b' },
     /* c reserved */
     { "displays", required_argument, NULL, 'd' },
+    { "grep", required_argument, NULL, 'g' },
     { "interactive", no_argument, NULL, 'i' },
     { "jail-id", no_argument, NULL, 'j' },
     { "display-mode", required_argument, NULL, 'm' },
@@ -311,7 +312,8 @@ main(int argc, const char *argv[])
 	    optind = 1;
 	}
 
-	while ((i = getopt_long(ac, __DECONST(char * const *, av), "CSIHPabijJ:nquvzs:d:U:m:o:p:Ttw", longopts, NULL)) != EOF)
+	while ((i = getopt_long(ac, __DECONST(char * const *, av),
+	    "CSIHPabg:ijJ:nquvzs:d:U:m:o:p:Ttw", longopts, NULL)) != EOF)
 	{
 	    switch(i)
 	    {
@@ -349,6 +351,7 @@ main(int argc, const char *argv[])
 
 	      case 'a':
 		fmt_flags ^= FMT_SHOWARGS;
+		show_args = fmt_flags & FMT_SHOWARGS;
 		break;
 
 	      case 'd':			/* number of displays to show */
@@ -362,6 +365,13 @@ main(int argc, const char *argv[])
 		    displays = i;
 		}
 		break;
+
+	      case 'g':			/* grep command name */
+		free(ps.command);
+		if ((ps.command = strdup(optarg)) == NULL)
+			err(1, NULL);
+		break;
+
 	      case 'p': {
 		unsigned long long num;
 		const char *errstr;
@@ -463,7 +473,7 @@ main(int argc, const char *argv[])
 
 	      default:
 		errx(1, 
-"[-abCHIijnPqSTtuvwz] [-d count] [-J jail] [-m cpu | io] [-o field]\n"
+"[-abCHIijnPqSTtuvwz] [-d count] [-g string] [-J jail] [-m cpu | io] [-o field]\n"
 "     [-p pid] [-s time] [-U username] [number]");
 	    }
 	}