require 'getopts.pl';
my @args = @_;
if (($LSOF = &isexec("../lsof")) eq "") { if (($LSOF = &isexec("lsof")) eq "") { if (($LSOF = &isexec("../lsof")) eq "") { print "can't execute $LSOF\n"; exit 1
}
}
}
if ($ARGV[0] ne ""){
$cmd="$LSOF -nPl -Fcns -c".$ARGV[0]."|";
}else{
$cmd="$LSOF -nPl -Fcns|";
}
$i=0;
if (open(FILE, $cmd)){
while (defined ($line=<FILE>)){
$cline=$line;
$cline =~ s"^(.)"";
$cline =~ s/^\s+|\s+$//g;
if($line=~m/^p/){
$pid=$cline;
}else{
if($line=~/^s/){
$size = $cline;
}else{
if($line=~/^c/){
$command = $cline;
}else{
if($line=~/^n/){
$name = $cline;
$data{$i} = { command => $command, name => $name,
pid => $pid , size => $size};
$size=0;
$i = $i+1;
}
}
}
}
}
}
#Resource name sorting
sub byresname { $data{$a}{name} cmp $data{$b}{name}}
@ks=sort byresname (keys %data);
#Resource grouping
$i=0;
$cname="a";
foreach $k (@ks){
if ($data{$k}{name} ne $cname){
$dgroup{$i} = { name => $data{$k}{name}, size => $data{$k}{size}};
$cname = $data{$k}{name};
$i++;
}
}
#Size sort on resource hash
sub bysize { $dgroup{$a}{size} <=> $dgroup{$b}{size} }
@ks=sort bysize (keys %dgroup);
$gsize=0;
printf(" -- KB -- -- Resource --\n", );
foreach $k (@ks){
printf("%10d %s\n", $dgroup{$k}{size}/1024, $dgroup{$k}{name});
$gsize+=$dgroup{$k}{size};
}
printf("Total KB : %10d\n", $gsize/1024);
## isexec($path) -- is $path executable
#
# $path = absolute or relative path to file to test for executabiity.
# Paths that begin with neither '/' nor '.' that arent't found as
# simple references are also tested with the path prefixes of the
# PATH environment variable.
sub
isexec {
my ($path) = @_;
my ($i, @P, $PATH);
$path =~ s/^\s+|\s+$//g;
if ($path eq "") { return(""); }
if (($path =~ m#^[\/\.]#)) {
if (-x $path) { return($path); }
return("");
}
$PATH = $ENV{PATH};
@P = split(":", $PATH);
for ($i = 0; $i <= $#P; $i++) {
if (-x "$P[$i]/$path") { return("$P[$i]/$path"); }
}
return("");
}