2003-02-03 12:11:12 +00:00
|
|
|
#!/usr/bin/perl -w
|
|
|
|
|
|
|
|
|
|
# $Id$
|
|
|
|
|
# $Source$
|
|
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
|
use Getopt::Std;
|
|
|
|
|
use File::Basename;
|
|
|
|
|
use File::Copy;
|
|
|
|
|
|
|
|
|
|
my $editor = "vi";
|
|
|
|
|
my %option = ();
|
|
|
|
|
getopts("eh", \%option);
|
|
|
|
|
|
|
|
|
|
if ( $option{h} ) { &help; }
|
|
|
|
|
if ( scalar(@ARGV) == 0 ) { &help; }
|
|
|
|
|
|
|
|
|
|
foreach ( @ARGV ) {
|
|
|
|
|
&backup($_);
|
|
|
|
|
if ( $option{e} ) {
|
|
|
|
|
if ( exists $ENV{'EDITOR'} ) {
|
|
|
|
|
$editor = $ENV{'EDITOR'};
|
|
|
|
|
}
|
|
|
|
|
system("$editor $_");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sub backup($) {
|
|
|
|
|
my $file = shift;
|
|
|
|
|
my ( $base, $dir, $backup, @stat, @timestamp, $sfx, $n );
|
|
|
|
|
$base = basename($file);
|
|
|
|
|
$dir = dirname($file);
|
|
|
|
|
my @months = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
|
|
|
|
|
if ( -f $file ) {
|
|
|
|
|
@stat = stat($file);
|
|
|
|
|
@timestamp = localtime($stat[9]);
|
|
|
|
|
$timestamp[5] += 1900;
|
|
|
|
|
$sfx = "$timestamp[3]$months[$timestamp[4]]$timestamp[5]";
|
2003-02-03 12:58:22 +00:00
|
|
|
unless ( -d "$dir/OLD" ) { mkdir "$dir/OLD", 0777 or die "Couldn't create $dir/OLD: $!\n"; }
|
2003-02-03 12:11:12 +00:00
|
|
|
$backup = "$dir/OLD/$base.$sfx";
|
|
|
|
|
|
|
|
|
|
if ( -e $backup && ! &cmp($file, $backup) ) {
|
|
|
|
|
$n = 1;
|
|
|
|
|
while ( -e "$backup-$n" && ! &cmp($file, "$backup-$n") ) {
|
|
|
|
|
$n++;
|
|
|
|
|
}
|
|
|
|
|
$backup = "$backup-$n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copy("$file", "$backup") or die "Couldn't copy $file to $backup: $!\n";
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
die "No such file: $file\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub cmp($$) {
|
|
|
|
|
my $source = shift;
|
|
|
|
|
my $target = shift;
|
|
|
|
|
open(FH, "<$source") or die "Couldn't open file $source: $!\n";
|
|
|
|
|
my $sf = do { local $/; <FH> };
|
|
|
|
|
close(FH);
|
|
|
|
|
open(FH, "<$target") or die "Couldn't open file $target: $!\n";
|
|
|
|
|
my $tf = do { local $/; <FH> };
|
|
|
|
|
close(FH);
|
|
|
|
|
$tf eq $sf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub help {
|
|
|
|
|
print <<EOT;
|
|
|
|
|
|
|
|
|
|
Usage: mycp [-e] [-h] <file1> [file2] ...";
|
|
|
|
|
|
|
|
|
|
EOT
|
|
|
|
|
exit;
|
|
|
|
|
}
|