perl versie van mycp
This commit is contained in:
parent
b6f103adcc
commit
64048592e4
1 changed files with 77 additions and 0 deletions
77
mycp/mycp
Executable file
77
mycp/mycp
Executable file
|
|
@ -0,0 +1,77 @@
|
|||
#!/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]";
|
||||
unless ( -d "$dir/OLD" ) { mkdir "$dir/OLD" or die "Couldn't create $dir/OLD: $!\n"; }
|
||||
$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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue