#! /usr/bin/perl -w
# uof -- try to find processes that have open but unlinked files
# Author: David C Lawrence <tale@isc.org>

# originally written on BSD/0S, but should work on other BSD systems
# best run as root, so 'find' can see the whole filesystem

$ENV{'PATH'} = '/usr/bin';

$0 =~ s%^.*/%%;

die "usage: $0 FILESYSTEM\n" unless $#ARGV == 0;

$filesys = shift;

chdir $filesys || die "$0: chdir $filesys: $!\n";

open(FSTAT, "fstat -f $filesys |") ||
  die "$0: fstat -f $filesys: errno $!, status $?\n";

$header = <FSTAT>;

while (<FSTAT>) {
  $inum = (split)[5];
  $lines{$inum} = '' if ! $lines{$inum};
  $lines{$inum} .= $_;
  $ifind{$inum} = 1;
}
close(FSTAT);

$findcmd = 'find -x . \(';
for (keys %ifind) {
  $findcmd .= " -inum $_ -o";
}
$findcmd =~ s/ -o$/ \\) -ls/;

open(FIND, "$findcmd |") || die "$0: $findcmd: errno $!, status $?\n";

while (<FIND>) {
  undef $lines{(split(' '))[0]};
}
close(FIND);

print $header;
for (keys %lines) {
  print $lines{$_} if $lines{$_};
}

exit(0);
