#!/usr/bin/perl -dw
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#NAME
#  holdopen
#
#SYNOPSIS
#  holdopen n file...
#REQUIRES
#
#DESCRIPTION
# Open n copies (default=1) of the file(s) named.
#
# This program simply opens the  files  for  reading,  then
# sleeps  indefinitely.   Its  primary  use  is in software
# testing, to validate the  effects  of  multiple  programs
# opening files and using them for a long time.
#
# The only way to get this  program  to  stop  holding  the
# files open is to kill it.
#
#OPTIONS
# None yet, but there may be some eventually.
#
#EXAMPLES
#
# holdopen 3 example.log
#   This will open example.log 3 times, then sleep forever.
#
#FILES
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

$| = 1;
$exitstat = 0;
($P = $0) =~ s".*/"";
$V = $ENV{"V_$P"} || $ENV{"D_$P"} || 2;	# Verbose level
$opens = 1;		# Default number of opens
$count = 0;		# Total number of opens so far

arg:
for $arg (@ARGV) {
	if ($arg =~ /^\d+$/) {
		print "$P: Open $arg times.\n" if $V>1;
		$opens = int($arg);
		next arg;
	}
	$open = 0;
	while ($open < $opens) {
		++$count;	# Count all the open attempts
		$filesym = "Open$count";	# Name for each open file
		if (open($filesym,">>$arg")) {
			++$open;	# Count the opens for this file
		#	print $filesym "P $$ F $filesym\n";
		} else {
			print STDERR "$P: Can't open \"$arg\" [$!]\n" if $V>0;
			$exitstat = int($!);	
			next arg;	# Abandone this file name
		}
	}
}
while (1) {sleep 1000}	# Go to sleep now

exit $exitstat;
