shithub: riscv

Download patch

ref: 01d28b9d89c069afe4a43afe04d31ddb89524b24
parent: 6f1efd37a23d8ae0a1907cce14749079a3aabb3c
author: cinap_lenrek <[email protected]>
date: Wed Nov 7 19:06:54 EST 2012

pstree: manpage, print pid in first column, bio, silly walks

--- a/sys/man/1/ps
+++ b/sys/man/1/ps
@@ -109,7 +109,10 @@
 to print the arguments for the process.  Newlines in arguments will be translated to spaces for display.
 .PP
 .I Pstree
-shows the processes as a tree.
+prints the processes as a tree in a two colum layout where
+the first colum being the process id and second column
+the program name and arguments indented and prefixed with
+line drawing runes to reflect the nesting in the hierarchy.
 .SH FILES
 .B /proc/*/status
 .SH SOURCE
--- a/sys/src/cmd/pstree.c
+++ b/sys/src/cmd/pstree.c
@@ -1,5 +1,6 @@
 #include <u.h>
 #include <libc.h>
+#include <bio.h>
 
 typedef struct Proc Proc;
 struct Proc {
@@ -8,8 +9,9 @@
 	Proc *hash;
 };
 
-Proc *hash[1024];
-Rune buf[512];
+Proc	*hash[1024];
+Rune	buf[512];
+Biobuf	bout;
 
 Proc *
 getproc(int pid)
@@ -42,7 +44,7 @@
 	int fd, ppid;
 	
 	ppid = 0;
-	snprint(b, sizeof(b), "/proc/%d/ppid", pid);
+	snprint(b, sizeof(b), "%d/ppid", pid);
 	fd = open(b, OREAD);
 	if(fd >= 0){
 		memset(b, 0, sizeof b);
@@ -79,7 +81,7 @@
 	int fd, rc, i;
 	Dir *d;
 	
-	fd = open("/proc", OREAD);
+	fd = open(".", OREAD);
 	if(fd < 0)
 		sysfatal("open: %r");
 	rc = dirreadall(fd, &d);
@@ -109,8 +111,7 @@
 		for(i=0; i<rc; i++)
 			if(b[i] == '\n')
 				b[i] = ' ';
-		write(1, b, rc);
-		n += rc;
+		n += Bwrite(&bout, b, rc);
 	}
 	close(fd);
 	return n;
@@ -122,25 +123,21 @@
 	char b[128], *p;
 	int fd;
 
-	if(pid == 0)
-		return;
-	snprint(b, sizeof(b), "/proc/%d/args", pid);
+	snprint(b, sizeof(b), "%d/args", pid);
 	if(readout(b) > 0)
 		return;
-	snprint(b, sizeof(b), "/proc/%d/status", pid);
+	snprint(b, sizeof(b), "%d/status", pid);
 	fd = open(b, OREAD);
-	if(fd < 0)
-		return;
-	memset(b, 0, sizeof b);
-	if(read(fd, b, 27) <= 0){
+	if(fd >= 0){
+		memset(b, 0, sizeof b);
+		if(read(fd, b, 27) > 0){
+			p = b + strlen(b);
+			while(p > b && p[-1] == ' ')
+				*--p = 0;
+			Bprint(&bout, "%s", b);
+		}
 		close(fd);
-		return;
 	}
-	p = b + strlen(b)-1;
-	while(p > b && *p == ' ')
-		*p-- = 0;
-	print("%s", b);
-	close(fd);
 }
 
 void
@@ -152,9 +149,9 @@
 	last = *--r;
 	*r = last == L' ' ? L'└' : L'├';
 	if(p->pid != 0){
-		print("%S", buf);
+		Bprint(&bout, "%-11d %S", p->pid, buf);
 		printargs(p->pid);
-		print(" [%d]\n", p->pid);
+		Bprint(&bout, "\n");
 	}
 	*r = last;
 	*++r = L'│';
@@ -175,7 +172,13 @@
 void
 main()
 {
+	Binit(&bout, 1, OWRITE);
+	if(chdir("/proc")==-1)
+		sysfatal("chdir: %r");
+
 	addproc(0);
 	addprocs();
 	printprocs();
+
+	exits(0);
 }