Skip to content

Commit

Permalink
Merge pull request #180 from mknos/cp-manysrc
Browse files Browse the repository at this point in the history
cp: copy many files at once
  • Loading branch information
briandfoy committed Jul 5, 2023
2 parents b384f7d + 9c4ae61 commit 036a746
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions bin/cp
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,46 @@ sub run {
$PerlPowerTools::cp::error_fh = $settings->{error_fh} if exists $settings->{error_fh};
$PerlPowerTools::cp::output_fh = $settings->{output_fh} if exists $settings->{output_fh};

my( $opts, $source, $destination ) = process_arguments(@args);
my( $opts, @files) = process_arguments(@args);
my $destination = pop @files;

return EX_USAGE unless defined $opts;
unless (@files) {
warn "$0: missing file operand\n";
return EX_USAGE;
}

my @unix_like = qw(darwin freebsd linux);
if( grep { $^O eq $_ } @unix_like and in_path('cp') ) {
# Although File::Copy seems like it should do the right thing,
# it doesn't.
my @command = 'cp';
push @command, map { "-$_" } grep { $opts->{$_} } qw(i f p v );
push @command, $source, $destination;
push @command, @files, $destination;

my $rc = system { $command[0] } @command;
return $rc;
}
else {
require File::Copy;
$destination = catfile( $destination, basename($source) ) if -d $destination;
print { output_fh() } "$source -> $destination\n" if $opts->{v};
if( -e $destination and $opts->{i} and ! $opts->{f} ) {
my $answer = prompt( "overwrite $destination? (y/n [n])", 'n' );
unless( $answer =~ m/\A\s*y/i ) {
print { error_fh() } "not overwritten";
return EX_FAILURE;
my $err = 0;
foreach my $source (@files) {
my $catdst = catfile( $destination, basename($source) ) if -d $destination;
print { output_fh() } "$source -> $catdst\n" if $opts->{v};
if( -e $catdst and $opts->{i} and ! $opts->{f} ) {
my $answer = prompt( "overwrite $catdst? (y/n [n])", 'n' );
unless( $answer =~ m/\A\s*y/i ) {
print { error_fh() } "not overwritten";
return EX_FAILURE;
}
}
$err = 1 if (File::Copy::copy($source, $catdst) == 0);
}
my $rc = File::Copy::copy( $source, $destination );
return $rc == 0 ? EX_FAILURE : EX_SUCCESS;
return $err ? EX_FAILURE : EX_SUCCESS;
}

return EX_FAILURE;
};
}

sub in_path {
my( $command ) = @_;
Expand Down Expand Up @@ -168,4 +177,3 @@ You may use this program under the terms of the Artistic License 2.0.
=cut


0 comments on commit 036a746

Please sign in to comment.