$fhdr = 0; $fdst = 0; $access = $devch = $devn = $fd = $inode = $lock = $name = ""; $offset = $proto = $size = $state = $stream = $type = ""; $pidst = 0; $cmd = $login = $pgrp = $pid = $ppid = $uid = "";
while (<>) {
chop;
if (/^p(.*)/) {
$tpid = $1;
if ($pidst) { &list_proc }
$pidst = 1;
$pid = $tpid;
if ($fdst) { &list_fd; $fdst = 0; }
next;
}
if (/^g(.*)/) { $pgrp = $1; next; }
if (/^c(.*)/) { $cmd = $1; next; }
if (/^u(.*)/) { $uid = $1; next; }
if (/^L(.*)/) { $login = $1; next; }
if (/^R(.*)/) { $ppid = $1; next; }
if (/^f(.*)/) {
$tfd = $1;
if ($pidst) { &list_proc }
if ($fdst) { &list_fd }
$fd = $tfd;
$fdst = 1;
next;
}
if (/^a(.*)/) { $access = $1; next; }
if (/^C(.*)/) { next; }
if (/^d(.*)/) { $devch = $1; next; }
if (/^D(.*)/) { $devn = $1; next; }
if (/^F(.*)/) { next; }
if (/^G(.*)/) { next; }
if (/^i(.*)/) { $inode = $1; next; }
if (/^k(.*)/) { next; }
if (/^l(.*)/) { $lock = $1; next; }
if (/^N(.*)/) { next; }
if (/^o(.*)/) { $offset = $1; next; }
if (/^P(.*)/) { $proto = $1; next; }
if (/^s(.*)/) { $size = $1; next; }
if (/^S(.*)/) { $stream = $1; next; }
if (/^t(.*)/) { $type = $1; next; }
if (/^T(.*)/) {
if ($state eq "") { $state = "(" . $1; }
else { $state = $state . " " . $1; }
next;
}
if (/^n(.*)/) { $name = $1; next; }
print "ERROR: unrecognized: \"$_\"\n";
}
if ($fdst) { &list_fd }
if ($pidst) { &list_proc }
exit(0);
sub list_fd {
if ( ! $fhdr) {
print " FD TYPE DEVICE SIZE/OFF INODE NAME\n";
$fhdr = 1;
}
printf " %4s%1.1s%1.1s %4.4s", $fd, $access, $lock, $type;
$tmp = $devn; if ($devch ne "") { $tmp = $devch }
printf " %10.10s", $tmp;
$tmp = $size; if ($offset ne "") { $tmp = $offset }
printf " %10.10s", $tmp;
$tmp = $inode; if ($proto ne "") { $tmp = $proto }
printf " %10.10s", $tmp;
$tmp = $stream; if ($name ne "") { $tmp = $name }
print " ", $tmp;
if ($state ne "") { printf " %s)\n", $state; } else { print "\n"; }
$access = $devch = $devn = $fd = $inode = $lock = $name = "";
$offset = $proto = $size = $state = $stream = $type = "";
}
sub list_proc {
print "COMMAND PID PGRP PPID USER\n";
$tmp = $uid; if ($login ne "") {$tmp = $login }
printf "%-9.9s %6d %6d %6d %s\n", $cmd, $pid, $pgrp, $ppid, $tmp;
$cmd = $login = $pgrp = $pid = $uid = "";
$fhdr = $pidst = 0;
}