Zuletzt geändert: Mo, 29.11.2004

«11C» chat-trunc.pl «PDF», «POD»



Download
#!/usr/bin/perl

use warnings;
use strict;

use Fcntl qw( :DEFAULT :flock );
use POSIX qw( strftime );

if(!$ARGV[0] or !$ARGV[1]) {
  print STDERR <<USAGE;
Usage: $0 logfile num
USAGE
  exit;
}

my ($log, $num) = @ARGV;

sysopen my $fh, $log, O_RDWR|O_CREAT|O_APPEND
  or die "Couldn't open $log for O_RDWE|O_CREAT|O_APPEND: $!\n";
flock $fh, LOCK_EX
  or die "Couldn't flock $log: $!\n";
close $fh
  or die "Couldn't close $log: $!\n";

sysopen $fh, $log, O_RDONLY
  or die "Couldn't open $log for reading: $!\n";
flock $fh, LOCK_EX
  or die "Couldn't flock $log: $!\n";
my @lines = <$fh>;
close $fh
  or die "Couldn't close $log: $!\n";

sysopen $fh, $log, O_RDWR|O_CREAT|O_TRUNC
  or die "Couldn't open $log for O_RDWE|O_CREAT|O_TRUNC: $!\n";
flock $fh, LOCK_EX
  or die "Couldn't flock $log: $!\n";

print $fh $_ for reverse +(reverse @lines)[0 .. $num-1];
#print $fh strftime "datumd: %d.%m. %H:%M\n", localtime;

close $fh
  or die "Couldn't close $log: $!\n";

exit;