]> granicus.if.org Git - procps-ng/commitdiff
Debian Bug report #526355
authorAlfredo Esteban <aedelatorre@gmail.com>
Sat, 18 Aug 2012 00:20:27 +0000 (02:20 +0200)
committerCraig Small <csmall@enc.com.au>
Wed, 29 Aug 2012 06:47:51 +0000 (16:47 +1000)
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=526355

Flag -f doesn't modify output anymore.

There is a new flag -a to show full command line processes.

Signed-off-by: Alfredo Esteban <aedelatorre@gmail.com>
pgrep.1
pgrep.c
testsuite/config/unix.exp
testsuite/pgrep.test/pgrep.exp

diff --git a/pgrep.1 b/pgrep.1
index 6948b323a621e61d3eac250de9b8e02053f802b0..bc0c9720fc25bda8a8e07841f741ed48b1d1677c 100644 (file)
--- a/pgrep.1
+++ b/pgrep.1
@@ -81,6 +81,11 @@ List the process name as well as the process ID.
 .RB ( pgrep
 only.)
 .TP
+\fB\-a\fR, \fB\-\-list\-full\fR
+List the full command line as well as the process ID.
+.RB ( pgrep
+only.)
+.TP
 \fB\-n\fR, \fB\-\-newest\fR
 Select only the newest (most recently started) of the matching processes.
 .TP
diff --git a/pgrep.c b/pgrep.c
index 84f955f911f5df1d698f11aaa5376acafd60d9b1..37c7d6cc22473834e321c2ea56a0e4e382bccd33 100644 (file)
--- a/pgrep.c
+++ b/pgrep.c
@@ -41,6 +41,8 @@
 #define EXIT_FATAL 3
 #define XALLOC_EXIT_CODE EXIT_FATAL
 
+#define CMDSTRSIZE 4096
+
 #include "c.h"
 #include "fileutils.h"
 #include "nls.h"
@@ -62,6 +64,7 @@ struct el {
 
 static int opt_full = 0;
 static int opt_long = 0;
+static int opt_longlong = 0;
 static int opt_oldest = 0;
 static int opt_newest = 0;
 static int opt_negate = 0;
@@ -449,7 +452,9 @@ static struct el * select_procs (int *num)
        regex_t *preg;
        pid_t myself = getpid();
        struct el *list = NULL;
-       char cmd[4096];
+       char cmdline[CMDSTRSIZE];
+       char cmdsearch[CMDSTRSIZE];
+       char cmdoutput[CMDSTRSIZE];
 
        ptp = do_openproc();
        preg = do_regcomp();
@@ -494,30 +499,38 @@ static struct el * select_procs (int *num)
                                match = match_strlist (tty, opt_term);
                        }
                }
-               if (opt_long || (match && opt_pattern)) {
-                       if (opt_full && task.cmdline) {
-                               int i = 0;
-                               int bytes = sizeof (cmd) - 1;
-
-                               /* make sure it is always NUL-terminated */
-                               cmd[bytes] = 0;
-                               /* make room for SPC in loop below */
-                               --bytes;
-
-                               strncpy (cmd, task.cmdline[i], bytes);
-                               bytes -= strlen (task.cmdline[i++]);
-                               while (task.cmdline[i] && bytes > 0) {
-                                       strncat (cmd, " ", bytes);
-                                       strncat (cmd, task.cmdline[i], bytes);
-                                       bytes -= strlen (task.cmdline[i++]) + 1;
-                               }
-                       } else {
-                               strcpy (cmd, task.cmd);
+               if (task.cmdline && (opt_longlong || opt_full) && match && opt_pattern) {
+                       int i = 0;
+                       int bytes = sizeof (cmdline) - 1;
+
+                       /* make sure it is always NUL-terminated */
+                       cmdline[bytes] = 0;
+                       /* make room for SPC in loop below */
+                       --bytes;
+
+                       strncpy (cmdline, task.cmdline[i], bytes);
+                       bytes -= strlen (task.cmdline[i++]);
+                       while (task.cmdline[i] && bytes > 0) {
+                               strncat (cmdline, " ", bytes);
+                               strncat (cmdline, task.cmdline[i], bytes);
+                               bytes -= strlen (task.cmdline[i++]) + 1;
                        }
                }
 
+               if (opt_long || opt_longlong || (match && opt_pattern)) {
+                       if (opt_longlong && task.cmdline)
+                               strncpy (cmdoutput, cmdline, CMDSTRSIZE);
+                       else
+                               strncpy (cmdoutput, task.cmd, CMDSTRSIZE);
+               }
+
                if (match && opt_pattern) {
-                       if (regexec (preg, cmd, 0, NULL, 0) != 0)
+                       if (opt_full && task.cmdline)
+                               strncpy (cmdsearch, cmdline, CMDSTRSIZE);
+                       else
+                               strncpy (cmdsearch, task.cmd, CMDSTRSIZE);
+
+                       if (regexec (preg, cmdsearch, 0, NULL, 0) != 0)
                                match = 0;
                }
 
@@ -542,9 +555,9 @@ static struct el * select_procs (int *num)
                                size = size * 5 / 4 + 4;
                                list = xrealloc(list, size * sizeof *list);
                        }
-                       if (list && (opt_long || opt_echo)) {
+                       if (list && (opt_long || opt_longlong || opt_echo)) {
                                list[matches].num = task.XXXID;
-                               list[matches++].str = xstrdup (cmd);
+                               list[matches++].str = xstrdup (cmdoutput);
                        } else if (list) {
                                list[matches++].num = task.XXXID;
                        } else {
@@ -593,6 +606,7 @@ static void parse_opts (int argc, char **argv)
                {"count", no_argument, NULL, 'c'},
                {"delimeter", required_argument, NULL, 'd'},
                {"list-name", no_argument, NULL, 'l'},
+               {"list-full", no_argument, NULL, 'a'},
                {"full", no_argument, NULL, 'f'},
                {"pgroup", required_argument, NULL, 'g'},
                {"group", required_argument, NULL, 'G'},
@@ -623,7 +637,7 @@ static void parse_opts (int argc, char **argv)
                strcat (opts, "e");
        } else {
                /* These options are for pgrep only */
-               strcat (opts, "cld:v");
+               strcat (opts, "clad:v");
        }
                        
        strcat (opts, "LF:fnoxP:g:s:u:U:G:t:?Vh");
@@ -707,6 +721,9 @@ static void parse_opts (int argc, char **argv)
                case 'l':   /* Solaris: long output format (pgrep only) Should require -f for beyond argv[0] maybe? */
                        opt_long = 1;
                        break;
+               case 'a':
+                       opt_longlong = 1;
+                       break;
                case 'n':   /* Solaris: match only the newest */
                        if (opt_oldest|opt_negate|opt_newest)
                                usage (opt);
@@ -814,7 +831,7 @@ int main (int argc, char **argv)
                if (opt_count) {
                        fprintf(stdout, "%d\n", num);
                } else {
-                       if (opt_long)
+                       if (opt_long || opt_longlong)
                                output_strlist (procs,num);
                        else
                                output_numlist (procs,num);
index 9092b9d439aee1e9131a3f0963896c86def5ced5..d9100e4e21319565d225210db3514c63389692f7 100644 (file)
@@ -111,11 +111,11 @@ proc expect_table_dsc { test match_header match_item } {
 }
 
 proc make_testproc { } {
+    global testproc_path testproc_comm testproc1_pid testproc2_pid sleep_time
+
     # Time to run the whole job
     set sleep_time 300
 
-    global testproc_path testproc_comm testproc1_pid testproc2_pid
-
     set testproc_path [ exec mktemp -u ]
     set sleep_path [ exec which sleep ]
     exec ln -s $sleep_path $testproc_path
index 0ec52c3798f8c9f4dc5b86967b3be19b68063578..0cb6db78a1f177073b9dc5d9c9e5786a22a00167 100644 (file)
@@ -51,6 +51,10 @@ set test "pgrep with process name"
 spawn $pgrep -l $testproc_comm
 expect_pass "$test" "^$testproc1_pid\\s+$testproc_comm\\s+$testproc2_pid\\s+$testproc_comm\\s*$"
 
+set test "pgrep with full command line"
+spawn $pgrep -a $testproc_comm
+expect_pass "$test" "^$testproc1_pid\\s+$testproc_path\\s+$sleep_time\\s+$testproc2_pid\\s+$testproc_path\\s+$sleep_time\\s*$"
+
 set test "pgrep find newest test pid"
 spawn $pgrep -n $testproc_comm
 expect_pass "$test" "^$testproc2_pid\\s*$"