#!/usr/local/bin/perl -w ###################################################################### # trackcat -- 2004, Mike Schilli ###################################################################### use strict; use File::Basename; use Sysadm::Install qw(:all); use Pod::Usage; use Getopt::Std; my $VERSION = "0.01"; my $CVSVERSION = '$Revision: 1.3 $'; getopts("t:", \my %opts); my $offset = 0; my($file) = @ARGV; pod2usage("No file given") unless defined $file; my $dir = dirname $file; $dir .= '/' if length $dir; my $tracker_file = $dir . "." . basename($file) . ".tracker"; if(exists $opts{t}) { $tracker_file = $opts{t}; } # Check if there's a tracker file if(-e $tracker_file) { $offset = slurp $tracker_file; chomp $offset; if($offset > -s $file) { print STDERR "Invalid offset found in $tracker_file: $offset > ", -s $file, " - reset.\n"; $offset = 0; } } open FILE, "<$file" or die "Cannot open $file"; seek FILE, $offset, 0; while() { $offset += length $_; print $_; } close FILE; blurt "$offset\n", $tracker_file; __END__ =head1 NAME trackcat - cat with a persistent tracker =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS trackcat [-t track_file] filename =head1 DESCRIPTION Upon the first invocation, trackcat prints the content of a given file to STDOUT. It keeps track of the file's length, and upon the next call, it will only print newly appendend lines. The current cursor position within the tracked file will be stored on a separate file on disk. The name of the tracker file can be defined with the C<-t> option. By default, the tracker file is going to be located in the same directory as the original file, under the name C<.file.tracker> if C is the original file name. =head1 EXAMPLES $ trackcat mailfile # prints content of mailfile # ... time passes, lines added to mailfile $ trackcat mailfile # prints newly appended lines only =head1 LEGALESE Copyright 2005 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2005, Mike Schilli