I implemented a minimal version of a pure Perl version :
#! /usr/bin/perl
# Perl clone of since(1)
# http://welz.org.za/projects/since
#
use strict;
use warnings;
use Fcntl qw/ SEEK_SET O_RDWR O_CREAT /;
use NDBM_File;
my $state_file = "$ENV{HOME}/.psince";
my %states;
tie(%states, 'NDBM_File', $state_file, O_CREAT | O_RDWR, 0660)
or die("cannot tie state to $state_file : $!");
while (my $filename = shift) {
if (! -r $filename) {
# Ignore
next;
}
my @file_stats = stat($filename);
my $device = $file_stats[0];
my $inode = $file_stats[1];
my $size = $file_stats[7];
my $state_key = $device . "/" .$inode;
print STDERR "state_key=$state_key\n";
if (! open(FILE, $filename) ) {
print STDERR "cannot open $filename : $!";
next;
}
# Reverting to the last cursor position
my $offset = $states{$state_key} || 0;
if ($offset <= $size) {
sysseek(FILE, $offset, SEEK_SET);
} else {
# file was truncated, restarting from the beginning
$offset = 0;
}
# Reading until the end
my $buffer;
while ((my $read_count = sysread(FILE, $buffer, 4096)) > 0) {
$offset += $read_count;
print $buffer;
}
# Nothing to read
close(FILE);
$states{$state_key} = $offset;
}
# Sync the states
untie(%states);
@Dave: It's almost like your algorithm, except that i don't use tell, but an internal maintained counter.