diff options
author | cinap_lenrek <cinap_lenrek@gmx.de> | 2012-11-08 00:06:54 +0100 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@gmx.de> | 2012-11-08 00:06:54 +0100 |
commit | 01d28b9d89c069afe4a43afe04d31ddb89524b24 (patch) | |
tree | b84b83f427f331903eeaf62f70d256571f520c75 /sys/src/cmd/pstree.c | |
parent | 6f1efd37a23d8ae0a1907cce14749079a3aabb3c (diff) |
pstree: manpage, print pid in first column, bio, silly walks
Diffstat (limited to 'sys/src/cmd/pstree.c')
-rw-r--r-- | sys/src/cmd/pstree.c | 47 |
1 files changed, 25 insertions, 22 deletions
diff --git a/sys/src/cmd/pstree.c b/sys/src/cmd/pstree.c index d104e04ed..dfe9c0c2c 100644 --- 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 @@ struct Proc { Proc *hash; }; -Proc *hash[1024]; -Rune buf[512]; +Proc *hash[1024]; +Rune buf[512]; +Biobuf bout; Proc * getproc(int pid) @@ -42,7 +44,7 @@ theppid(int pid) 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 @@ addprocs(void) 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 @@ readout(char *file) 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 @@ printargs(int pid) 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 @@ descend(Proc *p, Rune *r) 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 @@ printprocs(void) void main() { + Binit(&bout, 1, OWRITE); + if(chdir("/proc")==-1) + sysfatal("chdir: %r"); + addproc(0); addprocs(); printprocs(); + + exits(0); } |