| 1 | #!/usr/local/bin/perl -w | ||
| 2 | ### do not edit next line. Set by CS-RCS... http://www.componentsoftware.com/csrcs/uhome.htm | ||
| 3 | #REV=' @(#) $RCSfile: cp,v $ $Revision: 1.2 $ $Date: 2004/08/05 14:17:43 $ '; | ||
| 4 | require 5; | ||
| 5 | use strict; | ||
| 6 | use Cwd; | ||
| 7 | use File::Basename; | ||
| 8 | use File::Find; | ||
| 9 | use Getopt::Std; | ||
| 10 | |||
| 11 | ####### S U B R O U T I N E S ############### | ||
| 12 | sub insufficientArgs($); | ||
| 13 | sub printUsage(); | ||
| 14 | sub checkArgs(@); | ||
| 15 | sub findCopy; ## used by find() | ||
| 16 | sub copyFile($$); ## used by find() | ||
| 17 | |||
| 18 | $main::opt_f = undef; ## used and set by getopts() | ||
| 19 | $main::opt_i = undef; ## used and set by getopts() | ||
| 20 | $main::opt_p = undef; ## used and set by getopts() | ||
| 21 | $main::opt_v = undef; ## used and set by getopts() not standard, but may be helpful | ||
| 22 | ## especially if problems are found w/ routine. | ||
| 23 | |||
| 24 | ####### P R O C E S S - O P T I O N S #################################### | ||
| 25 | my $VERBOSE = 0; | ||
| 26 | my $PRESERVE = 0; | ||
| 27 | unless (getopts("fipv")) | ||
| 28 | { | ||
| 29 | &printUsage(); | ||
| 30 | exit 1; | ||
| 31 | } | ||
| 32 | if (defined $main::opt_f) { $main::opt_i = undef; } ## -f overrides -i | ||
| 33 | $VERBOSE = 1 if (defined $main::opt_v); | ||
| 34 | $PRESERVE = 1 if (defined $main::opt_p); | ||
| 35 | |||
| 36 | ####### | ||
| 37 | $cp::BINMODE = 1; ## default cp is binary mode | ||
| 38 | $cp::CWD = cwd(); ## where user invoked command | ||
| 39 | $cp::EXIT_STATUS = 0; ## this means everything worked | ||
| 40 | #$cp::PATH; ## used below ... mentioned here | ||
| 41 | #$cp::TARGET; ## used below | ||
| 42 | |||
| 43 | ####### O S S T U F F #################################################### | ||
| 44 | $cp::USES_DRIVE_LETTER = 0; ## the Windows 'C:\' type of thing... | ||
| 45 | if ($^O =~ /Win32/i) | ||
| 46 | { | ||
| 47 | $cp::USES_DRIVE_LETTER = 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | ## Do not use binary mode on some OS's ... | ||
| 51 | #if ( $^O =~ /????/i ) ## need some help here | ||
| 52 | #{ | ||
| 53 | # $cp::BINMODE = 0; | ||
| 54 | #} | ||
| 55 | |||
| 56 | ## default is that OS is case sensitive (file a.txt ne A.txt) | ||
| 57 | if ($^O =~ /(Win32|VMS|OS2)/i) ## OS is not case sensitive.. normalize path | ||
| 58 | { | ||
| 59 | $cp::CASE_SENSITIVE = 0; | ||
| 60 | $cp::CWD = uc $cp::CWD; | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | $cp::CASE_SENSITIVE = 1; | ||
| 65 | } | ||
| 66 | $cp::CWD =~ s|\\|/|g; ## normalize for compares | ||
| 67 | |||
| 68 | ####### C H E C K P A T H S & C L E A N U P I F N E E D E D ###### | ||
| 69 | my @paths = checkArgs(@ARGV); ## checks and cleans up args | ||
| 70 | if ($#paths < 1) ## need at least one source and a target | ||
| 71 | { | ||
| 72 | insufficientArgs($#paths); | ||
| 73 | &printUsage(); | ||
| 74 | exit 1; | ||
| 75 | } | ||
| 76 | |||
| 77 | ####### E S T A B L I S H T H E T A R G E T ############################ | ||
| 78 | my $target = pop @paths; ## last is target... | ||
| 79 | |||
| 80 | ####### M O V E S O U R C E S T O T A R G E T ######################## | ||
| 81 | my $path; | ||
| 82 | foreach $path (@paths) | ||
| 83 | { | ||
| 84 | my $current_target = $target; | ||
| 85 | |||
| 86 | if (-d $target) | ||
| 87 | { | ||
| 88 | my $base_path = basename($path); | ||
| 89 | $current_target = "$target/$base_path"; | ||
| 90 | } | ||
| 91 | |||
| 92 | if ($path eq $current_target) ## check again... compare may wk out after code above | ||
| 93 | { | ||
| 94 | print STDERR "cp: $path and target $current_target are identical ... skipping\n"; | ||
| 95 | $cp::EXIT_STATUS++; | ||
| 96 | next; | ||
| 97 | } | ||
| 98 | |||
| 99 | ## copy it ... | ||
| 100 | if (! -d $path) ## cp'ing a file | ||
| 101 | { | ||
| 102 | copyFile($path, $current_target); | ||
| 103 | } | ||
| 104 | else ## cp'ing a directory | ||
| 105 | { | ||
| 106 | print "cp $path $current_target\n" if $VERBOSE; | ||
| 107 | $cp::TARGET = $current_target; | ||
| 108 | $cp::PATH = $path; | ||
| 109 | &File::Find::find(\&findCopy, $path); ## copy files/dirs top -> down | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | exit $cp::EXIT_STATUS; | ||
| 114 | ################################################################################ | ||
| 115 | ####### E N D O F C P | ||
| 116 | ################################################################################ | ||
| 117 | |||
| 118 | ################################################################################ | ||
| 119 | ### This is called via find() to copy directory trees (top down) | ||
| 120 | sub findCopy | ||
| 121 | { | ||
| 122 | $_ = uc $_ if (! $cp::CASE_SENSITIVE); | ||
| 123 | my $dir_tail = cwd(); ## find() has "cd'ed" us here... | ||
| 124 | $dir_tail = uc $dir_tail if (! $cp::CASE_SENSITIVE); | ||
| 125 | $dir_tail =~ s|^$cp::PATH||; | ||
| 126 | $dir_tail = "$dir_tail/" unless $dir_tail eq ""; | ||
| 127 | if (-d $_) | ||
| 128 | { | ||
| 129 | $_ =~ s|\.$||; | ||
| 130 | $_ =~ s|\/$|| unless $_ eq '/'; | ||
| 131 | mkdir "$cp::TARGET/dir_tail$_", 0777; ## umask will modify | ||
| 132 | if (! -d "$cp::TARGET/$dir_tail$_") | ||
| 133 | { | ||
| 134 | print STDERR "mv: Unable to create dir $cp::TARGET/$dir_tail/$_\n"; | ||
| 135 | $cp::EXIT_STATUS++; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | else | ||
| 139 | { | ||
| 140 | if ((defined $main::opt_i) && (-e "$cp::TARGET/$dir_tail/$_")) | ||
| 141 | { | ||
| 142 | my $path2show = "$cp::TARGET/$dir_tail/$_"; | ||
| 143 | $path2show =~ s|^$cp::CWD|.|; | ||
| 144 | print STDERR "cp: overwrite $path2show (yes/no)? "; | ||
| 145 | my $response = <STDIN>; | ||
| 146 | return if ($response !~ /^y/i); | ||
| 147 | } | ||
| 148 | copyFile($_, "$cp::TARGET/$dir_tail/$_"); | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | ################################################################################ | ||
| 153 | ### This copies a single file | ||
| 154 | sub copyFile($$) | ||
| 155 | { ## source, target | ||
| 156 | my ($path, $target) = @_; | ||
| 157 | $path = uc $path if (! $cp::CASE_SENSITIVE); | ||
| 158 | $target = uc $target if (! $cp::CASE_SENSITIVE); | ||
| 159 | if ((defined $main::opt_i) && (-e $target)) ## used if '-i' option was given | ||
| 160 | { | ||
| 161 | my $path2show = $target; | ||
| 162 | $path2show =~ s|^$cp::CWD|.|; | ||
| 163 | print STDERR "cp: overwrite $path2show (yes/no)? "; | ||
| 164 | my $response = <STDIN>; | ||
| 165 | return if ($response !~ /^y/i); ## not this one | ||
| 166 | } | ||
| 167 | print "cp $path $target\n" if $VERBOSE; | ||
| 168 | |||
| 169 | open(PATH, "<$path") or die "Unable to read $path: $!"; | ||
| 170 | open(TARGET, ">$target") or die "Unable to create $target: $!"; | ||
| 171 | if ($cp::BINMODE) { binmode PATH; binmode TARGET; } | ||
| 172 | my $buffer; | ||
| 173 | while (read PATH, $buffer, 1024) { print TARGET $buffer; } | ||
| 174 | close PATH; | ||
| 175 | close TARGET; | ||
| 176 | |||
| 177 | if ($PRESERVE) ## preserve as many file attributes as possible... | ||
| 178 | { | ||
| 179 | my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat $path; | ||
| 180 | utime $atime, $mtime, ($target); | ||
| 181 | chown $uid, $gid, ($target); | ||
| 182 | my $oldMode = (07777 & $mode); ## from man -s 2 mknod | ||
| 183 | chmod $oldMode, $target; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | ################################################################################ | ||
| 188 | ## print Insufficient arguments error | ||
| 189 | sub insufficientArgs($) | ||
| 190 | { | ||
| 191 | my $arg_num = ($_[0] + 1); ## num to display | ||
| 192 | print STDERR "cp: Insufficient arguments ($arg_num)\n"; | ||
| 193 | $cp::EXIT_STATUS++; | ||
| 194 | } | ||
| 195 | |||
| 196 | ################################################################################ | ||
| 197 | sub printUsage() | ||
| 198 | { | ||
| 199 | print STDERR <<EOE | ||
| 200 | Usage: cp [-fivp] file1 file2 | ||
| 201 | cp [-fivp] file1... filex dir | ||
| 202 | cp [-fivp] dir1... dirx dir | ||
| 203 | cp [-fivp] dir1... dirx file1... filex dir | ||
| 204 | EOE | ||
| 205 | |||
| 206 | } | ||
| 207 | |||
| 208 | ################################################################################ | ||
| 209 | sub checkArgs(@) | ||
| 210 | { | ||
| 211 | my $target = $_[$#_]; | ||
| 212 | if ($#_ > 1) ### cp'ing > 1 thing target has to be an existing directory | ||
| 213 | { | ||
| 214 | if (! -e $target) ## has to be an existing directory... sorry it's over | ||
| 215 | { | ||
| 216 | print STDERR "cp: $target not found\n"; | ||
| 217 | print STDERR " exiting...\n"; | ||
| 218 | $cp::EXIT_STATUS++; | ||
| 219 | exit $cp::EXIT_STATUS | ||
| 220 | } | ||
| 221 | elsif (! -d $target) ## can only work if a directory... | ||
| 222 | { | ||
| 223 | print STDERR "cp: Target $target must be a directory when cp'ing > 1 thing\n"; | ||
| 224 | print STDERR " exiting...\n"; | ||
| 225 | printUsage(); | ||
| 226 | $cp::EXIT_STATUS++; | ||
| 227 | exit $cp::EXIT_STATUS | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | my $paths = ""; | ||
| 232 | my $Cwd = cwd(); | ||
| 233 | $Cwd = uc $Cwd if (! $cp::CASE_SENSITIVE); | ||
| 234 | $Cwd =~ s|[/\\]$|| unless $Cwd eq '/'; ## remove any possible trailing slash (DOS or UNIX style) | ||
| 235 | my $path; | ||
| 236 | my @paths = ""; | ||
| 237 | foreach $path (@_) ## check all paths passed in | ||
| 238 | { | ||
| 239 | $path =~ s|\\|/|g; ## normalize | ||
| 240 | ## if OS is not case sensitive then let's just set to uc and make life easy | ||
| 241 | $path = uc $path if (! $cp::CASE_SENSITIVE); | ||
| 242 | |||
| 243 | if ((! -e $path) && ($path ne $target)) | ||
| 244 | { | ||
| 245 | print STDERR "cp: can not access $path ... skipping\n"; | ||
| 246 | $cp::EXIT_STATUS++; | ||
| 247 | ## continue on... ignore this one | ||
| 248 | } | ||
| 249 | elsif (((! -d $path) && (! -r $path)) && ($path ne $target)) | ||
| 250 | { | ||
| 251 | print STDERR "cp: can not read $path ... skipping\n"; | ||
| 252 | $cp::EXIT_STATUS++; | ||
| 253 | ## continue on... ignore this one | ||
| 254 | } | ||
| 255 | else | ||
| 256 | { | ||
| 257 | $path =~ s|^\.\/||; ## no need for './' at the path start | ||
| 258 | if ($path =~ /^\.\.\//) ## change relative to full path | ||
| 259 | { ## ../../abc to /aaa/bbb/ccc/abc | ||
| 260 | my $path_front = $Cwd; | ||
| 261 | $path_front = uc $path if (! $cp::CASE_SENSITIVE); | ||
| 262 | while ($path =~ /^\.\.\//) | ||
| 263 | { | ||
| 264 | $path =~ s|^...||; | ||
| 265 | $path_front =~ s|(.*)\/[^\/]+|$1/|; | ||
| 266 | } | ||
| 267 | $path = $path_front.$path; | ||
| 268 | } | ||
| 269 | elsif (($path !~ /\//) && ($path !~ /^[a-z]:/i) && $cp::USES_DRIVE_LETTER) ## it's right here... | ||
| 270 | { | ||
| 271 | $path = "$Cwd/$path"; | ||
| 272 | } | ||
| 273 | elsif (($path !~ /\//) && (! $cp::USES_DRIVE_LETTER)) ## it's right here... | ||
| 274 | { | ||
| 275 | $path = "$Cwd/$path"; | ||
| 276 | } | ||
| 277 | |||
| 278 | while ($path =~ /\/\.\./) ## remove '/..' from middle or end of path | ||
| 279 | { | ||
| 280 | $path =~ s|[^\/]+\/\.\.||; | ||
| 281 | } | ||
| 282 | $path =~ s|\/\.?$|| unless $path eq '/'; ## remove '/' or '/.' from end of path | ||
| 283 | |||
| 284 | my $tmpPath; | ||
| 285 | my $path_cnt = 0; ## number used for splicing | ||
| 286 | foreach $tmpPath (@paths) | ||
| 287 | { | ||
| 288 | if ($path eq $tmpPath) | ||
| 289 | { | ||
| 290 | print STDERR "cp: $path found more than once on command line. Ignoring 1st occurance\n"; | ||
| 291 | $cp::EXIT_STATUS++; | ||
| 292 | splice @paths, $path_cnt, 1; | ||
| 293 | } | ||
| 294 | else | ||
| 295 | { | ||
| 296 | $path_cnt++; | ||
| 297 | } | ||
| 298 | } | ||
| 299 | push @paths, $path; | ||
| 300 | } | ||
| 301 | } | ||
| 302 | splice @paths, 0, 1; ## the "my @paths" initialization gave this.... | ||
| 303 | |||
| 304 | return @paths; ## checked, cleaned, and ready to go... | ||
| 305 | } | ||
| 306 | |||
| 307 | __END__ |