Tags: patch

This patch include the following changes:

- A new flag -a modify output format and shows full command line.
- Output format depend on -l and -a flags, not anymore on -f.
- -f flag only modify where to match the pattern, not output format.

For example:

> pgrep firefox
1911
1920

> pgrep -l firefox
1911 firefox
1920 firefox-bin

> pgrep -a firefox
1911 /bin/sh /usr/lib/firefox-3.6.13/firefox
1920 /usr/lib/firefox-3.6.13/firefox-bin

> pgrep -f firefox
1911
1916
1920
1964

> pgrep -fl firefox
1911 firefox
1916 run-mozilla.sh
1920 firefox-bin
1964 plugin-containe

> pgrep -fa firefox
1911 /bin/sh /usr/lib/firefox-3.6.13/firefox
1916 /bin/sh /usr/lib/firefox-3.6.13/run-mozilla.sh
/usr/lib/firefox-3.6.13/firefox-bin
1920 /usr/lib/firefox-3.6.13/firefox-bin
1964 /usr/lib/firefox-3.6.13/plugin-container
/usr/lib/flashplugin-installer/libflashplayer.so 1920 plugin

Alfredo
diff -ur procps-3.2.8/pgrep.1 procps-3.2.8.new/pgrep.1
--- procps-3.2.8/pgrep.1	2010-12-31 15:20:51.699780975 +0100
+++ procps-3.2.8.new/pgrep.1	2010-12-31 15:14:49.569780976 +0100
@@ -8,7 +8,7 @@
 
 .SH SYNOPSIS
 .na
-\fBpgrep\fR [\fB\-cflvx\fR] [\fB\-d\ \fIdelimiter\fR] [\fB\-n\fR|\fB\-o\fR] \
+\fBpgrep\fR [\fB\-cflavx\fR] [\fB\-d\ \fIdelimiter\fR] [\fB\-n\fR|\fB\-o\fR] \
 [\fB\-P\ \fIppid\fR,...] [\fB\-g\ \fIpgrp\fR,...] [\fB\-s\ \fIsid\fR,...] \
 [\fB\-u\ \fIeuid\fR,...] [\fB\-U\ \fIuid\fR,...] [\fB\-G\ \fIgid\fR,...] \
 [\fB\-t\ \fIterm\fR,...] [\fIpattern\fR]
@@ -64,6 +64,9 @@
 \fB\-l\fR
 List the process name as well as the process ID. (\fBpgrep\fP only.)
 .TP
+\fB\-a\fR
+List the full command line as well as the process ID. (\fBpgrep\fP only.)
+.TP
 \fB\-n\fR
 Select only the newest (most recently started) of the matching
 processes.
diff -ur procps-3.2.8/pgrep.c procps-3.2.8.new/pgrep.c
--- procps-3.2.8/pgrep.c	2010-12-31 15:20:53.549780978 +0100
+++ procps-3.2.8.new/pgrep.c	2010-12-31 15:22:58.119780975 +0100
@@ -38,6 +38,8 @@
 #define EXIT_USAGE 2
 #define EXIT_FATAL 3
 
+#define CMDSTRSIZE 4096
+
 static int i_am_pkill = 0;
 static const char *progname = "pgrep";
 
@@ -48,6 +50,7 @@
 
 /* User supplied arguments */
 
+static int opt_longlong = 0;
 static int opt_full = 0;
 static int opt_long = 0;
 static int opt_oldest = 0;
@@ -425,7 +428,9 @@
 	regex_t *preg;
 	pid_t myself = getpid();
 	union el *list = NULL;
-	char cmd[4096];
+	char cmdline[CMDSTRSIZE];
+	char searchcmd[CMDSTRSIZE];
+	char outputcmd[CMDSTRSIZE];
 
 	ptp = do_openproc();
 	preg = do_regcomp();
@@ -470,30 +475,43 @@
 				match = match_strlist (tty, opt_term);
 			}
 		}
-		if (opt_long || (match && opt_pattern)) {
+
+		if ( (opt_full || opt_longlong) && task.cmdline) {
+			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 (match && opt_pattern) {
 			if (opt_full && task.cmdline) {
-				int i = 0;
-				int bytes = sizeof (cmd) - 1;
+				strncpy (searchcmd, cmdline, CMDSTRSIZE);
+			} else {
+				strcpy (searchcmd, task.cmd);
+			}
+		}
 
-				/* 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;
-				}
+		if (opt_long || opt_longlong) {
+			if (opt_longlong && task.cmdline) {
+				strncpy (outputcmd, cmdline, CMDSTRSIZE);
 			} else {
-				strcpy (cmd, task.cmd);
+				strcpy (outputcmd, task.cmd);
 			}
 		}
 
 		if (match && opt_pattern) {
-			if (regexec (preg, cmd, 0, NULL, 0) != 0)
+			if (regexec (preg, searchcmd, 0, NULL, 0) != 0)
 				match = 0;
 		}
 
@@ -520,9 +538,9 @@
 				if (list == NULL)
 					exit (EXIT_FATAL);
 			}
-			if (opt_long) {
+			if (opt_long || opt_longlong) {
 				char buff[5096];  // FIXME
-				sprintf (buff, "%d %s", task.XXXID, cmd);
+				sprintf (buff, "%d %s", task.XXXID, outputcmd);
 				list[matches++].str = strdup (buff);
 			} else {
 				list[matches++].num = task.XXXID;
@@ -563,7 +581,7 @@
 		}
 	} else {
 		/* These options are for pgrep only */
-		strcat (opts, "ld:");
+		strcat (opts, "lad:");
 	}
 			
 	strcat (opts, "LF:cfnovxP:g:s:u:U:G:t:?V");
@@ -639,6 +657,9 @@
 		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);
@@ -731,7 +752,7 @@
 		if (opt_count) {
 			fprintf(stdout, "%ld\n", num);
 		} else {
-			if (opt_long)
+			if (opt_long || opt_longlong)
 				output_strlist (procs,num);
 			else
 				output_numlist (procs,num);

Reply via email to