ps -o comm

Jeremy C. Reed reed at reedmedia.net
Thu Jan 1 19:01:59 PST 2009


I noticed that ps didn't have -o comm keyword.

I was pointed to Posix 1003.1.
        http://www.opengroup.org/onlinepubs/000095399/utilities/ps.html

The following patch partially works for me.

I know about -c.

src/bin/ps> ./ps -o comm,ucomm,command
COMMAND          UCOMM            COMMAND
ps               ps               ./ps -o comm,ucomm,command
tcsh             tcsh             -tcsh (tcsh)
src/bin/ps> ./ps -c -o comm,ucomm,command
COMMAND          UCOMM            COMMAND
ps               ps               ps
tcsh             tcsh             tcsh

But the behaviour of -c is almost the same as ucomm. But "comm" should be 
argv[0].

(Note that after I did this I looked at FreeBSD ps and see they just use 
its ucomm() with "COMMAND" header for "comm", but that is not right as 
that isn't argv[0], I think.)

How can I get just argv[0] from kvm to improve following patch?

diff --git a/bin/ps/extern.h b/bin/ps/extern.h
index 72bce80..eae1181 100644
--- a/bin/ps/extern.h
+++ b/bin/ps/extern.h
@@ -48,6 +48,7 @@ extern STAILQ_HEAD(varent_head, varent) var_head;
 extern struct timeval btime;
 
 __BEGIN_DECLS
+void	 comm(const KINFO *, const struct varent *);
 void	 command(const KINFO *, const struct varent *);
 void	 cputime(const KINFO *, const struct varent *);
 int	 donlist(void);
diff --git a/bin/ps/keyword.c b/bin/ps/keyword.c
index 4b5ab11..5c5bece 100644
--- a/bin/ps/keyword.c
+++ b/bin/ps/keyword.c
@@ -87,6 +87,7 @@ static const VAR var[] = {
 	{"batch", "BAT", NULL, 0, lpest, NULL, 3, LPOFF(origcpu), UINT, "d", NULL},
 	{"blocked", "", "sigmask", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
 	{"caught", "", "sigcatch", 0, NULL, NULL, 0, 0, 0, NULL, NULL},
+	{"comm", "COMMAND", NULL, COMM|ARGV0|LJUST, command, NULL, MAXCOMLEN, 0, 0, NULL, NULL},
 	{"command", "COMMAND", NULL, COMM|LJUST|USER, command, NULL, 16, 0, 0, NULL,
 		NULL},
 	{"cpu", "CPU", NULL, 0, lpest, NULL, 3, LPOFF(estcpu), UINT, "d", NULL},
diff --git a/bin/ps/print.c b/bin/ps/print.c
index d5e582c..85a6bd4 100644
--- a/bin/ps/print.c
+++ b/bin/ps/print.c
@@ -103,7 +103,7 @@ command(const KINFO *k, const struct varent *vent)
 
 	v = vent->var;
 
-	if (cflag) {
+	if (cflag || v->flag & ARGV0) {
 		/* Don't pad the last field. */
 		if (STAILQ_NEXT(vent, link) == NULL)
 			printf("%s", make_printable(KI_PROC(k, comm)));
diff --git a/bin/ps/ps.1 b/bin/ps/ps.1
index 40c17b4..f3a1df3 100644
--- a/bin/ps/ps.1
+++ b/bin/ps/ps.1
@@ -351,6 +351,8 @@ accounting flag (alias
 .Cm acflg )
 .It Cm batch
 batchness of the process (higher numbers mean less interactivity)
+.It Cm comm
+command
 .It Cm command
 command and arguments
 .It Cm cpu
diff --git a/bin/ps/ps.h b/bin/ps/ps.h
index f811308..c243897 100644
--- a/bin/ps/ps.h
+++ b/bin/ps/ps.h
@@ -64,6 +64,7 @@ typedef struct var {
 #define	LJUST	    0x02		/* left adjust on output (trailing blanks) */
 #define	USER	    0x04		/* needs user structure */
 #define	DSIZ	    0x08		/* field size is dynamic*/
+#define ARGV0       0x10		/* only print argv[0] */
 	u_int	    flag;
 					/* output routine */
 	void	    (*oproc) (const struct kinfo *, const struct varent *);





More information about the Kernel mailing list