| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | =begin metadata | ||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | Name: cp | ||||||||||||||||||||||||||||||||||||||||||
| 6 | Description: copy files and/or directories | ||||||||||||||||||||||||||||||||||||||||||
| 7 | Author: brian d foy, bdfoy@cpan.org | ||||||||||||||||||||||||||||||||||||||||||
| 8 | License: artistic2 | ||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =end metadata | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | package PerlPowerTools::cp; | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use Config; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use ExtUtils::MakeMaker qw(prompt); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use File::Spec::Functions qw(catfile); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 2 | ▶ | exit( run( {}, @ARGV) ) unless caller; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | BEGIN { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 2 | $PerlPowerTools::cp::error_fh = \*STDERR; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 2 | $PerlPowerTools::cp::output_fh = \*STDOUT; | |||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub error_fh { $PerlPowerTools::cp::error_fh } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
error_fh: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | sub output_fh { $PerlPowerTools::cp::output_fh } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
output_fh: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 2 | ▶ | use constant EX_SUCCESS => 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | use constant EX_FAILURE => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | ▶ | use constant EX_USAGE => 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=18 SCAR=48.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 2 | my( $settings, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 39 | 2 | ▶ | $settings = {} unless defined $settings; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | $PerlPowerTools::cp::error_fh = $settings->{error_fh} if exists $settings->{error_fh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | ▶ | $PerlPowerTools::cp::output_fh = $settings->{output_fh} if exists $settings->{output_fh}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | 2 | my( $opts, @files) = process_arguments(@args); | |||||||||||||||||||||||||||||||||||||||||
| 45 | 2 | my $destination = pop @files; | |||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | ▶ | return EX_USAGE unless defined $opts; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 2 | ▶ | unless (@files) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | warn "$0: missing file operand\n"; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | usage(); | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return EX_USAGE; | |||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 2 | my @unix_like = qw(darwin freebsd linux); | |||||||||||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | if( grep { $^O eq $_ } @unix_like and in_path('cp') ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
grep {$^O eq $_;} @unix_like and in_path("cp")
MC/DC0%0/2
grep {$^O eq $_;} @unix_like and in_path("cp")
grep {$^O eq $_;} @unix_like
in_path("cp")
Truth table33%1/3
A and B
A= grep {$^O eq $_;} @unix_likeB= in_path("cp")
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | # Although File::Copy seems like it should do the right thing, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | # it doesn't. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 2 | my @command = 'cp'; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | push @command, map { "-$_" } grep { $opts->{$_} } qw(i f p v ); | |||||||||||||||||||||||||||||||||||||||||
| 60 | 2 | push @command, '--', @files, $destination; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 2 | my $rc = system { $command[0] } @command; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | return $rc >> 8; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | require File::Copy; | |||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | my $err = 0; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | foreach my $source (@files) { | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if (-d $source) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | print { error_fh() } "$0: '$source' is a directory (not copied)\n"; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | $err = 1; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | my $catdst = $destination; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if( -d $destination ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $catdst = catfile( $destination, basename($source) ) | |||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | print { output_fh() } "$source -> $catdst\n" if $opts->{v}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if( -e $catdst and $opts->{i} and ! $opts->{f} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
-e $catdst and $opts->{"i"} and not $opts->{"f"}
-e $catdst and $opts->{"i"}
MC/DC0%0/3
-e $catdst and $opts->{"i"} and not $opts->{"f"}
-e $catdst
$opts->{"i"}
not $opts->{"f"}
Truth table0%0/4
A and B and not(C)
A= -e $catdstB= $opts->{"i"}C= not $opts->{"f"}
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | my $answer = prompt( "overwrite $catdst? (y/n [n])", 'n' ); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | next unless $answer =~ m/\A\s*y/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if (File::Copy::copy($source, $catdst) == 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | print { error_fh() } "$0: $source -> $catdst: copy failed: $!\n"; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $err = 1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | return $err ? EX_FAILURE : EX_SUCCESS; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | return EX_FAILURE; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | sub in_path { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in_path: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 2 | my( $command ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 97 | 2 | foreach my $dir ( split /$Config{path_sep}/, $ENV{PATH} ) { | |||||||||||||||||||||||||||||||||||||||||
| 98 | 16 | my $path = catfile( $dir, $command ); | |||||||||||||||||||||||||||||||||||||||||
| 99 | 16 | ▶ | return 1 if -x $path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | 2 | ▶ | sub process_arguments { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process_arguments: called CC=2 SCAR=7.5
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 2 | my @args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 2 | my %opts; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 2 | require Getopt::Long; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 2 | Getopt::Long::Configure('bundling'); | |||||||||||||||||||||||||||||||||||||||||
| 110 | Getopt::Long::GetOptionsFromArray( | ||||||||||||||||||||||||||||||||||||||||||
| 111 | \@args, | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 'f' => \$opts{'f'}, | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 'i' => \$opts{'i'}, | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 'n' => \$opts{'n'}, | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 'p' => \$opts{'p'}, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 'v' => \$opts{'v'}, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | ▶ | ) or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | usage(); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 120 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 2 | return ( \%opts, @args ) | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | require Pod::Usage; | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | Pod::Usage::pod2usage({ -exitval => 'NOEXIT', -verbose => 0 }); | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | cp - copy files and/or directories | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | % cp [-fipv] source_file target_file | ||||||||||||||||||||||||||||||||||||||||||
| 142 | % cp [-fipv] source... target_dir | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | The cp utility copies the source files/directories to the target. If the | ||||||||||||||||||||||||||||||||||||||||||
| 147 | target is a file you may only specify one file as the source. cp will not | ||||||||||||||||||||||||||||||||||||||||||
| 148 | copy a file onto itself. | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | =head2 OPTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =item * B<-f> - force copy if possible (DEFAULT) | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =item * B<-i> - prompt for confirmation whenever the copy would overwrite an existing target. | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =item * B<-p> - preserve source file attributes (like modDate) as much as possible onto the target. | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | =item * B<-v> - verbose. Echo "cp source target" before copy is done. | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | Specifying both B<-f> and B<-i> options is not considered an | ||||||||||||||||||||||||||||||||||||||||||
| 165 | error. The B<-f> option will override the B<-i> option. | ||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | B<cp> has no known bugs, but be aware that the current copy mode | ||||||||||||||||||||||||||||||||||||||||||
| 170 | is binary mode. | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | =head1 EXIT STATUS | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =item * 0 - All sources were copied successfully. | ||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | =item * 1 - There was error | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | brian d foy, E<lt>schumacks@att.netE<gt> | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | =head1 COPYRIGHT and LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||
| 188 | Copyright © 2023 brian d foy. All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | You may use this program under the terms of the Artistic License 2.0. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||