$OpenBSD: patch-pgrep_pgrep_c,v 1.1.1.1 2003/01/06 18:03:44 lebel Exp $
Support for -a and -v options.
Fix return values.
--- pgrep/pgrep.c.orig	Mon May 21 18:35:09 2001
+++ pgrep/pgrep.c	Mon Jan  6 12:33:31 2003
@@ -1,8 +1,9 @@
 /*
- * pgrep [-flnvx] [-d <delim>] [-g <pgrplist>] [-G <gidlist>]
+ * pgrep [-aflnvVx] [-d <delim>] [-g <pgrplist>] [-G <gidlist>]
  *       [-P <ppidlist>] [-t <termlist>] [-u <euidlist>]
  *       [-U <uidlist>] [<pattern>]
  *
+ *  -a : very long output (includes command arguments)
  *  -d <delim> : output delimiter (default newline)
  *  -f : match against full name, not just executable name
  *  -g <pgrplist> : matches process groups
@@ -13,7 +14,8 @@
  *  -t <termlist> : matches terminal
  *  -u <euidlist> : matches effective uids
  *  -U <uidlist> : matches real uids
- *  -v : invert match
+ *  -v : invert match (overrides -V)
+ *  -V : invert flags but not the pattern
  *  -x : exact match (default regex)
  *
  *  <pattern> : regex (or exact string if -x) to match
@@ -28,6 +30,11 @@
 #include <sysexits.h>
 #include <unistd.h>
 
+#include <kvm.h>
+#include <sys/param.h>
+#include <sys/sysctl.h>
+#include <fcntl.h>
+
 #include "../proctools/proctools.h"
 
 static void usage();
@@ -40,14 +47,16 @@ main(argc, argv)
 	int argc;
 	char *argv[];
 {
+	kvm_t *kd = 0;
 	struct proclist *proclist, *temppl;
 	struct grouplist *gidl;
 	struct pidlist *pgroupl, *ppidl, *sidl;
 	struct termlist *terml;
 	struct uidlist *euidl, *uidl;
+	struct kinfo_proc *ki;
 	int ch;
-	int fflag, lflag, nflag, vflag, xflag;
-	int first;
+	int aflag, fflag, lflag, nflag, vflag, Vflag, xflag;
+	int first, nentries;
 	char *delim;
 
 	proclist = NULL;
@@ -55,12 +64,15 @@ main(argc, argv)
 	pgroupl = ppidl = sidl = NULL;
 	terml = NULL;
 	euidl = uidl = NULL;
-	fflag = lflag = xflag = nflag = vflag = FALSE;
+	aflag = fflag = lflag = xflag = nflag = vflag = Vflag = FALSE;
 	first = TRUE;
 	delim = NULL;
 
-	while ((ch = getopt(argc, argv, "d:fg:G:lnP:t:u:U:vx")) != -1)
+	while ((ch = getopt(argc, argv, "ad:fg:G:lnP:t:u:U:vVx")) != -1)
 		switch ((char)ch) {
+		case 'a':
+			aflag = TRUE;
+			break;
 		case 'd':
 			if (delim != NULL)
 				free(delim);
@@ -97,6 +109,9 @@ main(argc, argv)
 		case 'v':
 			vflag = TRUE;
 			break;
+		case 'V':
+			Vflag = TRUE;
+			break;
 		case 'x':
 			xflag = TRUE;
 			break;
@@ -113,18 +128,30 @@ main(argc, argv)
 		usage();
 	}
 
-	getProcList(&proclist, euidl, uidl, gidl, ppidl, pgroupl, terml, fflag, nflag, vflag, xflag, ((argc > 0)?argv[0]:NULL));
+	if (aflag) {
+		kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, NULL);
+	}
+	/* if (kd == 0) errx(EX_UNAVAILABLE, "%s", errbuf); */
+
+	getProcList(&proclist, euidl, uidl, gidl, ppidl, pgroupl, terml, fflag, nflag, vflag, Vflag, xflag, ((argc > 0)?argv[0]:NULL));
 
 	temppl = proclist;
 	while (temppl != NULL) {
-		printf("%s%d%s%s", (first?(first = FALSE, ""):(delim != NULL?delim:"\n")), temppl->pid, (lflag?" ":""), (lflag?temppl->name:""));
+		printf("%s%d%s%s", (first?(first = FALSE, ""):(delim != NULL?delim:"\n")), temppl->pid, ((aflag || lflag)?" ":""), ((lflag && !aflag)?temppl->name:""));
+		if (aflag && kd != 0) {
+			ki = kvm_getprocs(kd, KERN_PROC_PID, temppl->pid, &nentries);
+			if (nentries == 1 && ki != NULL) {
+				putchar(' ');
+				fmt_argv(kd, ki);
+			}
+		}
 		temppl = temppl->next;
 	}
 
 	if (!first)
 		printf("\n");
 
-	return (1);
+	return proclist == NULL ? 1 : EX_OK;
 }
 
 void
