From 64048592e479c8cbb22d99ec62248bb5ee572361 Mon Sep 17 00:00:00 2001 From: Ward Wouts Date: Mon, 3 Feb 2003 12:11:12 +0000 Subject: [PATCH] perl versie van mycp --- mycp/mycp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 mycp/mycp diff --git a/mycp/mycp b/mycp/mycp new file mode 100755 index 0000000..211ec7c --- /dev/null +++ b/mycp/mycp @@ -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 $/; }; + close(FH); + open(FH, "<$target") or die "Couldn't open file $target: $!\n"; + my $tf = do { local $/; }; + close(FH); + $tf eq $sf; +} + +sub help { + print < [file2] ..."; + +EOT + exit; +}