install __DIE__ handler instead of silly cdie sub
This commit is contained in:
parent
61d3aecfa5
commit
67ddd04757
1 changed files with 18 additions and 21 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/perl -w
|
#!/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$
|
# $Source$
|
||||||
|
|
||||||
#
|
#
|
||||||
|
|
@ -17,6 +17,8 @@ use Getopt::Long;
|
||||||
|
|
||||||
&lock;
|
&lock;
|
||||||
|
|
||||||
|
$SIG{__DIE__} = 'cleanup';
|
||||||
|
|
||||||
&cmdline;
|
&cmdline;
|
||||||
unless (defined @source) { @source = &read_cur_dir; }
|
unless (defined @source) { @source = &read_cur_dir; }
|
||||||
|
|
||||||
|
|
@ -24,7 +26,7 @@ if (@pattern) {
|
||||||
@target = @source;
|
@target = @source;
|
||||||
&pattern_edit;
|
&pattern_edit;
|
||||||
if ( &run_checks ) {
|
if ( &run_checks ) {
|
||||||
&cdie("Aborting\n");
|
die("Aborting\n");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
$temp_file = &open_temp;
|
$temp_file = &open_temp;
|
||||||
|
|
@ -35,7 +37,7 @@ if (@pattern) {
|
||||||
&edit($temp_file);
|
&edit($temp_file);
|
||||||
&read_temp($temp_file);
|
&read_temp($temp_file);
|
||||||
} else {
|
} else {
|
||||||
&cdie("Aborting\n");
|
die("Aborting\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -47,8 +49,7 @@ if (@unsafe = &check_safety(\@source, \@target)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
&move_files(\@source, \@target);
|
&move_files(\@source, \@target);
|
||||||
&unlock;
|
&cleanup;
|
||||||
unlink $temp_file or die "Couldn't remove temp file: $!\n";
|
|
||||||
|
|
||||||
|
|
||||||
########################################################
|
########################################################
|
||||||
|
|
@ -58,7 +59,7 @@ unlink $temp_file or die "Couldn't remove temp file: $!\n";
|
||||||
#Read current dir
|
#Read current dir
|
||||||
sub read_cur_dir {
|
sub read_cur_dir {
|
||||||
my (@dir, $filename);
|
my (@dir, $filename);
|
||||||
opendir(DIRHANDLE, ".") or &cdie("couldn't open .: $!\n");
|
opendir(DIRHANDLE, ".") or die("couldn't open .: $!\n");
|
||||||
while ( defined ($filename = readdir(DIRHANDLE)) ) {
|
while ( defined ($filename = readdir(DIRHANDLE)) ) {
|
||||||
unless ($filename eq "." | $filename eq ".." | $filename eq ".mvwrap" ) {
|
unless ($filename eq "." | $filename eq ".." | $filename eq ".mvwrap" ) {
|
||||||
chomp ($filename);
|
chomp ($filename);
|
||||||
|
|
@ -76,7 +77,7 @@ sub edit($) {
|
||||||
@editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename");
|
@editor = (defined $ENV{EDITOR} ? $ENV{EDITOR} : "vi", "$filename");
|
||||||
if ($opt_e) { @editor = ($opt_e, "$filename") }
|
if ($opt_e) { @editor = ($opt_e, "$filename") }
|
||||||
system(@editor) == 0
|
system(@editor) == 0
|
||||||
or &cdie("System @editor failed: $!");
|
or die("System @editor failed: $!");
|
||||||
}
|
}
|
||||||
|
|
||||||
# make tempfiles and install handler to remove them
|
# make tempfiles and install handler to remove them
|
||||||
|
|
@ -84,7 +85,7 @@ sub open_temp {
|
||||||
my $target;
|
my $target;
|
||||||
do { $target_name = tmpnam() }
|
do { $target_name = tmpnam() }
|
||||||
until $target = IO::File->new($target_name, O_RDWR|O_CREAT|O_EXCL);
|
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) {
|
foreach (@source) {
|
||||||
print $target "$_\n";
|
print $target "$_\n";
|
||||||
}
|
}
|
||||||
|
|
@ -94,7 +95,7 @@ sub open_temp {
|
||||||
|
|
||||||
sub read_temp($) {
|
sub read_temp($) {
|
||||||
my $target_name = shift;
|
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 = ();
|
@target = ();
|
||||||
foreach (<TARGET>) {
|
foreach (<TARGET>) {
|
||||||
chomp;
|
chomp;
|
||||||
|
|
@ -118,7 +119,7 @@ sub move_files($$) {
|
||||||
}
|
}
|
||||||
unless ($opt_n) {
|
unless ($opt_n) {
|
||||||
move("$source", "$target")
|
move("$source", "$target")
|
||||||
or &cdie("move failed: $!");
|
or die("move failed: $!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -126,7 +127,7 @@ sub move_files($$) {
|
||||||
|
|
||||||
# read pattern file
|
# read pattern file
|
||||||
sub read_pattern {
|
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>;
|
@pattern = <PAT>;
|
||||||
close PAT;
|
close PAT;
|
||||||
}
|
}
|
||||||
|
|
@ -256,7 +257,7 @@ sub safety_belt($$) {
|
||||||
}
|
}
|
||||||
unless ($opt_n) {
|
unless ($opt_n) {
|
||||||
move("$filename", "$rand")
|
move("$filename", "$rand")
|
||||||
or &cdie("move failed: $!\n");
|
or die("move failed: $!\n");
|
||||||
}
|
}
|
||||||
$source->[$filenr] = "$rand";
|
$source->[$filenr] = "$rand";
|
||||||
}
|
}
|
||||||
|
|
@ -284,10 +285,6 @@ sub paranoia($$) {
|
||||||
return 0;
|
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.
|
# place lockfile in dir, or exit if one exists.
|
||||||
sub lock {
|
sub lock {
|
||||||
if ( -e ".mvwrap") {
|
if ( -e ".mvwrap") {
|
||||||
|
|
@ -307,12 +304,12 @@ sub unlock {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# clean up lock & die
|
# clean up
|
||||||
sub cdie {
|
sub cleanup {
|
||||||
$message = shift;
|
|
||||||
&unlock;
|
&unlock;
|
||||||
unlink $temp_file or die "Couldn't remove temp file: $!\n";
|
if ( defined($temp_file) ) {
|
||||||
die "$message";
|
unlink $temp_file or die "Couldn't remove temp file: $!\n";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sub askyn($) {
|
sub askyn($) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue