# This is TAC # reverse of cat # # Take a list of files as command line parameters # and display the files on STDOUT # last file first # displaying each file backwards # The files are concatenated in 'normal' order to a temporary file # which is then read and displayed in reverse # check the number of parameters.. my $argCount = $#ARGV+1; if ($argCount<2) { exit 0; } # open the temporary file use Fcntl; use POSIX; my $outputFile; do { $outputFile = tmpnam(); } until sysopen(OUTPUTFILE, $outputFile, O_RDWR|O_CREAT|O_EXCL, 0666); # for every file.. while (@ARGV) { # open it my $fileName = shift; open ( MYFILE, " $fileName" ) or die "Cant open file $fileName"; # and write each line to the temporary file.. foreach () { print OUTPUTFILE $_; } close MYFILE; print OUTPUTFILE "\n"; print "Done $fileName -\n"; } close OUTPUTFILE; # now read it in backwards.. my $inputFile = $outputFile; open ( INPUTFILE, "< $inputFile"); my $char; my $fromEnd=1; my $fileSize = (stat INPUTFILE)[7]; # we seek to a position a distance from the end, and read a single character # the distance from end increases till we hit the start while ($fromEnd<$fileSize+1) { seek INPUTFILE, -$fromEnd, 2; # last char read INPUTFILE, $char, 1; print $char; ++$fromEnd; } close INPUTFILE; unlink $inputFile;