Debian Bug report #526355

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>
This commit is contained in:
Alfredo Esteban 2012-08-18 02:20:27 +02:00 committed by Craig Small
parent 00d7d4e37a
commit f12277c74d
4 changed files with 51 additions and 25 deletions

View File

@ -81,6 +81,11 @@ List the process name as well as the process ID.
.RB ( pgrep .RB ( pgrep
only.) only.)
.TP .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 \fB\-n\fR, \fB\-\-newest\fR
Select only the newest (most recently started) of the matching processes. Select only the newest (most recently started) of the matching processes.
.TP .TP

63
pgrep.c
View File

@ -41,6 +41,8 @@
#define EXIT_FATAL 3 #define EXIT_FATAL 3
#define XALLOC_EXIT_CODE EXIT_FATAL #define XALLOC_EXIT_CODE EXIT_FATAL
#define CMDSTRSIZE 4096
#include "c.h" #include "c.h"
#include "fileutils.h" #include "fileutils.h"
#include "nls.h" #include "nls.h"
@ -62,6 +64,7 @@ struct el {
static int opt_full = 0; static int opt_full = 0;
static int opt_long = 0; static int opt_long = 0;
static int opt_longlong = 0;
static int opt_oldest = 0; static int opt_oldest = 0;
static int opt_newest = 0; static int opt_newest = 0;
static int opt_negate = 0; static int opt_negate = 0;
@ -449,7 +452,9 @@ static struct el * select_procs (int *num)
regex_t *preg; regex_t *preg;
pid_t myself = getpid(); pid_t myself = getpid();
struct el *list = NULL; struct el *list = NULL;
char cmd[4096]; char cmdline[CMDSTRSIZE];
char cmdsearch[CMDSTRSIZE];
char cmdoutput[CMDSTRSIZE];
ptp = do_openproc(); ptp = do_openproc();
preg = do_regcomp(); preg = do_regcomp();
@ -494,30 +499,38 @@ static struct el * select_procs (int *num)
match = match_strlist (tty, opt_term); match = match_strlist (tty, opt_term);
} }
} }
if (opt_long || (match && opt_pattern)) { if (task.cmdline && (opt_longlong || opt_full) && match && opt_pattern) {
if (opt_full && task.cmdline) { int i = 0;
int i = 0; int bytes = sizeof (cmdline) - 1;
int bytes = sizeof (cmd) - 1;
/* make sure it is always NUL-terminated */ /* make sure it is always NUL-terminated */
cmd[bytes] = 0; cmdline[bytes] = 0;
/* make room for SPC in loop below */ /* make room for SPC in loop below */
--bytes; --bytes;
strncpy (cmd, task.cmdline[i], bytes); strncpy (cmdline, task.cmdline[i], bytes);
bytes -= strlen (task.cmdline[i++]); bytes -= strlen (task.cmdline[i++]);
while (task.cmdline[i] && bytes > 0) { while (task.cmdline[i] && bytes > 0) {
strncat (cmd, " ", bytes); strncat (cmdline, " ", bytes);
strncat (cmd, task.cmdline[i], bytes); strncat (cmdline, task.cmdline[i], bytes);
bytes -= strlen (task.cmdline[i++]) + 1; bytes -= strlen (task.cmdline[i++]) + 1;
}
} else {
strcpy (cmd, task.cmd);
} }
} }
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 (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; match = 0;
} }
@ -542,9 +555,9 @@ static struct el * select_procs (int *num)
size = size * 5 / 4 + 4; size = size * 5 / 4 + 4;
list = xrealloc(list, size * sizeof *list); 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].num = task.XXXID;
list[matches++].str = xstrdup (cmd); list[matches++].str = xstrdup (cmdoutput);
} else if (list) { } else if (list) {
list[matches++].num = task.XXXID; list[matches++].num = task.XXXID;
} else { } else {
@ -593,6 +606,7 @@ static void parse_opts (int argc, char **argv)
{"count", no_argument, NULL, 'c'}, {"count", no_argument, NULL, 'c'},
{"delimeter", required_argument, NULL, 'd'}, {"delimeter", required_argument, NULL, 'd'},
{"list-name", no_argument, NULL, 'l'}, {"list-name", no_argument, NULL, 'l'},
{"list-full", no_argument, NULL, 'a'},
{"full", no_argument, NULL, 'f'}, {"full", no_argument, NULL, 'f'},
{"pgroup", required_argument, NULL, 'g'}, {"pgroup", required_argument, NULL, 'g'},
{"group", required_argument, NULL, 'G'}, {"group", required_argument, NULL, 'G'},
@ -623,7 +637,7 @@ static void parse_opts (int argc, char **argv)
strcat (opts, "e"); strcat (opts, "e");
} else { } else {
/* These options are for pgrep only */ /* 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"); 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? */ case 'l': /* Solaris: long output format (pgrep only) Should require -f for beyond argv[0] maybe? */
opt_long = 1; opt_long = 1;
break; break;
case 'a':
opt_longlong = 1;
break;
case 'n': /* Solaris: match only the newest */ case 'n': /* Solaris: match only the newest */
if (opt_oldest|opt_negate|opt_newest) if (opt_oldest|opt_negate|opt_newest)
usage (opt); usage (opt);
@ -814,7 +831,7 @@ int main (int argc, char **argv)
if (opt_count) { if (opt_count) {
fprintf(stdout, "%d\n", num); fprintf(stdout, "%d\n", num);
} else { } else {
if (opt_long) if (opt_long || opt_longlong)
output_strlist (procs,num); output_strlist (procs,num);
else else
output_numlist (procs,num); output_numlist (procs,num);

View File

@ -111,11 +111,11 @@ proc expect_table_dsc { test match_header match_item } {
} }
proc make_testproc { } { proc make_testproc { } {
global testproc_path testproc_comm testproc1_pid testproc2_pid sleep_time
# Time to run the whole job # Time to run the whole job
set sleep_time 300 set sleep_time 300
global testproc_path testproc_comm testproc1_pid testproc2_pid
set testproc_path [ exec mktemp -u ] set testproc_path [ exec mktemp -u ]
set sleep_path [ exec which sleep ] set sleep_path [ exec which sleep ]
exec ln -s $sleep_path $testproc_path exec ln -s $sleep_path $testproc_path

View File

@ -51,6 +51,10 @@ set test "pgrep with process name"
spawn $pgrep -l $testproc_comm spawn $pgrep -l $testproc_comm
expect_pass "$test" "^$testproc1_pid\\s+$testproc_comm\\s+$testproc2_pid\\s+$testproc_comm\\s*$" 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" set test "pgrep find newest test pid"
spawn $pgrep -n $testproc_comm spawn $pgrep -n $testproc_comm
expect_pass "$test" "^$testproc2_pid\\s*$" expect_pass "$test" "^$testproc2_pid\\s*$"