plan9front/rc/bin/pstree

47 lines
775 B
Bash
Executable file

#!/bin/rc
if(~ $#* 0)
*=0
cd /proc && ls | sort -n | awk -v 'this='^$1 '
function cat(f, v){
getline v < f
close(f)
return v
}
BEGIN{
OFS="\t"
}
/^[0-9]+/ {
s = cat($1"/args")
if(s == ""){
s = cat($1"/status")
s = substr(s, 1, 27)
gsub(" *", "", s)
}
p = int(cat($1"/ppid"))
child[p,++nchild[p]] = $1
proc[$1] = s
ppid[$1] = p
pid[NR] = $1
}
function out(p, sep, k, m, i, n, c, s){
n = nchild[p]
if(p != 0 && proc[p] != "")
printf "%-11s%s%s%s\n", p, sep, k, proc[p]
s = m > 0 ? "│" : " "
k = "├"
for(i=1; i<=n; i++){
c = child[p,i]
if(i == n)
k = "└"
out(c, sep s, k, n - i)
}
}
END{
for(i=1; i<=length(pid); i++){
p = pid[i]
if(p && !(ppid[p] in proc) && ppid[p] != 0)
child[0,++nchild[0]] = p
}
out(this, "", "", 0)
}
'