sub interrupt { wait; print "\n"; exit 0; }
$Pn = "watch_a_file";
if ($#ARGV != 0) { print "$#ARGV\n"; die "$Pn usage: file_name\n"; }
$fnm = $ARGV[0];
if (! -r $fnm) { die "$Pn: can't read $fnm\n"; }
$RPT = 15; $| = 1; $SIG{'INT'} = 'interrupt';
if (($LSOF = &isexec("../lsof")) eq "") { if (($LSOF = &isexec("lsof")) eq "") { print "can't execute $LSOF\n"; exit 1
}
}
open(P, "$LSOF -nPFpf $fnm|") || die "$Pn: can't pipe to $LSOF\n";
$curpid = -1;
$pids = "";
while (<P>) {
chop;
if (/^p(.*)/) { $curpid = $1; next; } if (/^f/) {
if ($curpid > 0) {
if ($pids eq "") { $pids = $curpid; }
else { $pids = $pids . "," . $curpid; }
$curpid = -1;
}
}
}
close(P);
wait;
if ($pids eq "") { die "$Pn: no processes using $fnm located.\n"; }
print "watch_file: $fnm being used by processes:\n\t$pids\n\n";
$pipe = "$LSOF -ap $pids -r $RPT $fnm";
open(P, "$pipe|") || die "$Pn: can't pipe: $pipe\n";
while (<P>) { print $_; }
close(P);
print "$Pn: unexpected EOF from \"$pipe\"\n";
exit 1;
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("");
}