]>
Commit | Line | Data |
---|---|---|
654d227a BL |
1 | #!/usr/local/bin/perl |
2 | ||
3 | use strict; | |
4 | ||
5 | use Fcntl; | |
6 | ||
654d227a BL |
7 | # copy-if-different.pl |
8 | ||
9 | # Copy to the destination if the source is not the same as it. | |
10 | ||
654d227a BL |
11 | my @filelist; |
12 | ||
13 | foreach my $arg (@ARGV) { | |
14 | $arg =~ s|\\|/|g; # compensate for bug/feature in cygwin glob... | |
15 | foreach (glob $arg) | |
16 | { | |
17 | push @filelist, $_; | |
18 | } | |
19 | } | |
20 | ||
21 | my $fnum = @filelist; | |
22 | ||
23 | if ($fnum <= 1) | |
24 | { | |
25 | die "Need at least two filenames"; | |
26 | } | |
27 | ||
28 | my $dest = pop @filelist; | |
29 | ||
30 | if ($fnum > 2 && ! -d $dest) | |
31 | { | |
32 | die "Destination must be a directory"; | |
33 | } | |
34 | ||
35 | foreach (@filelist) | |
36 | { | |
37 | my $dfile; | |
38 | if (-d $dest) | |
39 | { | |
40 | $dfile = $_; | |
41 | $dfile =~ s|^.*[/\\]([^/\\]*)$|$1|; | |
42 | $dfile = "$dest/$dfile"; | |
43 | } | |
44 | else | |
45 | { | |
46 | $dfile = $dest; | |
47 | } | |
48 | ||
49 | my $buf; | |
50 | if (-f $dfile) | |
51 | { | |
52 | sysopen(IN, $_, O_RDONLY|O_BINARY) || die "Can't Open $_"; | |
53 | sysopen(OUT, $dfile, O_RDONLY|O_BINARY) | |
54 | || die "Can't Open $dfile"; | |
55 | while (sysread IN, $buf, 10240) | |
56 | { | |
57 | my $b2; | |
58 | goto copy if !sysread(OUT, $b2, 10240) || $buf ne $b2; | |
59 | } | |
60 | goto copy if sysread(OUT, $buf, 1); | |
61 | close(IN); | |
62 | close(OUT); | |
63 | print "NOT copying: $_ to $dfile\n"; | |
64 | next; | |
65 | } | |
66 | copy: | |
67 | sysopen(IN, $_, O_RDONLY|O_BINARY) || die "Can't Open $_"; | |
68 | sysopen(OUT, $dfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY) | |
69 | || die "Can't Open $dfile"; | |
70 | while (sysread IN, $buf, 10240) | |
71 | { | |
72 | syswrite(OUT, $buf, length($buf)); | |
73 | } | |
74 | close(IN); | |
75 | close(OUT); | |
76 | print "Copying: $_ to $dfile\n"; | |
77 | } | |
78 |