install __DIE__ handler instead of silly cdie sub

This commit is contained in:
Ward Wouts 2002-12-18 12:34:22 +00:00
parent 61d3aecfa5
commit 67ddd04757

View file

@ -1,6 +1,6 @@
#!/usr/bin/perl -w
# $Dwarf: mvwrap,v 1.40 2002/12/18 12:11:47 ward Exp $
# $Dwarf: mvwrap,v 1.41 2002/12/18 12:14:02 ward Exp $
# $Source$
#
@ -17,6 +17,8 @@ use Getopt::Long;
&lock;
$SIG{__DIE__} = 'cleanup';
&cmdline;
unless (defined @source) { @source = &read_cur_dir; }
@ -24,7 +26,7 @@ if (@pattern) {
@target = @source;
&pattern_edit;
if ( &run_checks ) {
&cdie("Aborting\n");
die("Aborting\n");
}
} else {
$temp_file = &open_temp;
@ -35,7 +37,7 @@ if (@pattern) {
&edit($temp_file);
&read_temp($temp_file);
} else {
&cdie("Aborting\n");
die("Aborting\n");
}
}
}
@ -47,8 +49,7 @@ if (@unsafe = &check_safety(\@source, \@target)) {
}
&move_files(\@source, \@target);
&unlock;
unlink $temp_file or die "Couldn't remove temp file: $!\n";
&cleanup;
########################################################
@ -58,7 +59,7 @@ unlink $temp_file or die "Couldn't remove temp file: $!\n";
#Read current dir
sub read_cur_dir {
my (@dir, $filename);
opendir(DIRHANDLE, ".") or &cdie("couldn't open .: $!\n");
opendir(DIRHANDLE, ".") or die("couldn't open .: $!\n");
while ( defined ($filename = readdir(DIRHANDLE)) ) {
unless ($filename eq "." | $filename eq ".." | $filename eq ".mvwrap" ) {
chomp ($filename);
@ -76,7 +77,7 @@ sub edit($) {
@editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename");
if ($opt_e) { @editor = ($opt_e, "$filename") }
system(@editor) == 0
or &cdie("System @editor failed: $!");
or die("System @editor failed: $!");
}
# make tempfiles and install handler to remove them
@ -84,7 +85,7 @@ sub open_temp {
my $target;
do { $target_name = tmpnam() }
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
END { if ($opt_e) { unlink($target_name) or &cdie("Couldn't unlink $target_name: $!");} }
END { if ($opt_e) { unlink($target_name) or die("Couldn't unlink $target_name: $!");} }
foreach (@source) {
print $target "$_\n";
}
@ -94,7 +95,7 @@ sub open_temp {
sub read_temp($) {
my $target_name = shift;
open TARGET, $target_name or &cdie("Couldn't open tempfile: $target_name: $!");
open TARGET, $target_name or die("Couldn't open tempfile: $target_name: $!");
@target = ();
foreach (<TARGET>) {
chomp;
@ -118,7 +119,7 @@ sub move_files($$) {
}
unless ($opt_n) {
move("$source", "$target")
or &cdie("move failed: $!");
or die("move failed: $!");
}
}
}
@ -126,7 +127,7 @@ sub move_files($$) {
# read pattern file
sub read_pattern {
open PAT, "< $opt_f" or &cdie("Couldn't open pattern file: $!\n");
open PAT, "< $opt_f" or die("Couldn't open pattern file: $!\n");
@pattern = <PAT>;
close PAT;
}
@ -256,7 +257,7 @@ sub safety_belt($$) {
}
unless ($opt_n) {
move("$filename", "$rand")
or &cdie("move failed: $!\n");
or die("move failed: $!\n");
}
$source->[$filenr] = "$rand";
}
@ -284,10 +285,6 @@ sub paranoia($$) {
return 0;
}
# lock, unlock and cdie are the only subs that should use die;
# all others should use &cdie. A handler for this would probably
# be nicer.
# place lockfile in dir, or exit if one exists.
sub lock {
if ( -e ".mvwrap") {
@ -307,12 +304,12 @@ sub unlock {
}
}
# clean up lock & die
sub cdie {
$message = shift;
# clean up
sub cleanup {
&unlock;
unlink $temp_file or die "Couldn't remove temp file: $!\n";
die "$message";
if ( defined($temp_file) ) {
unlink $temp_file or die "Couldn't remove temp file: $!\n";
}
}
sub askyn($) {