From 37b884aa54811f35b8c3e447586c80f2b2a2620f Mon Sep 17 00:00:00 2001 From: Sebastien GODARD Date: Fri, 7 Oct 2016 14:41:23 +0200 Subject: [PATCH] pidstat: Use either short or full command line when looking for a pattern Use either the command name string or the full command line when looking for a substring with options -C or -G. The full command line is used if option -l has been entered, otherwise we use the short command name string. Signed-off-by: Sebastien GODARD --- pidstat.c | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/pidstat.c b/pidstat.c index 35c780e..e06d4ef 100644 --- a/pidstat.c +++ b/pidstat.c @@ -275,6 +275,23 @@ int update_pid_array(unsigned int *pid_array_nr, unsigned int pid) return i; } +/* + *************************************************************************** + * Get pointer on task's command string. + * + * IN: + * @pst Pointer on structure with process stats and command line. + *************************************************************************** + */ +char *get_tcmd(struct pid_stats *pst) +{ + if (DISPLAY_CMDLINE(pidflag) && strlen(pst->cmdline)) + /* Option "-l" used */ + return pst->cmdline; + else + return pst->comm; +} + /* *************************************************************************** * Display process command name or command line. @@ -287,12 +304,8 @@ void print_comm(struct pid_stats *pst) { char *p; - if (DISPLAY_CMDLINE(pidflag) && strlen(pst->cmdline)) { - p = pst->cmdline; - } - else { - p = pst->comm; - } + /* Get pointer on task's command string */ + p = get_tcmd(pst); if (pst->tgid) { cprintf_s(IS_ZERO, " |__%s\n", p); @@ -1042,6 +1055,7 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity, int q, rc; regex_t regex; struct passwd *pwdent; + char *pc; *pstc = st_pid_list[curr] + p; @@ -1192,7 +1206,8 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity, /* Error in preparing regex structure */ return -1; - rc = regexec(®ex, (*pstc)->comm, 0, NULL, 0); + pc = get_tcmd(*pstc); /* Get pointer on task's command string */ + rc = regexec(®ex, pc, 0, NULL, 0); regfree(®ex); if (rc) @@ -1209,7 +1224,8 @@ int get_pid_to_display(int prev, int curr, int p, unsigned int activity, return -1; } - rc = regexec(®ex, (*pstc)->comm, 0, NULL, 0); + pc = get_tcmd(*pstc); /* Get pointer on task's command string */ + rc = regexec(®ex, pc, 0, NULL, 0); regfree(®ex); if (rc) { -- 2.40.0