summaryrefslogtreecommitdiffstats
path: root/util/copy-if-different.pl
blob: ec99e084b56a900e585d9f64299c27ff7a10eba9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/local/bin/perl

use strict;

use Fcntl;

# copy-if-different.pl

# Copy to the destination if the source is not the same as it.

my @filelist;

foreach my $arg (@ARGV) {
	$arg =~ s|\\|/|g;	# compensate for bug/feature in cygwin glob...
	foreach (glob $arg)
		{
		push @filelist, $_;
		}
}

my $fnum = @filelist;

if ($fnum <= 1)
	{
	die "Need at least two filenames";
	}

my $dest = pop @filelist;

if ($fnum > 2 && ! -d $dest)
	{
	die "Destination must be a directory";
	}

foreach (@filelist)
	{
        my $dfile;
	if (-d $dest)
		{
		$dfile = $_;
		$dfile =~ s|^.*[/\\]([^/\\]*)$|$1|;
		$dfile = "$dest/$dfile";
		}
	else
		{
		$dfile = $dest;
		}

	my $buf;
	if (-f $dfile)
		{
		sysopen(IN, $_, O_RDONLY|O_BINARY) || die "Can't Open $_";
		sysopen(OUT, $dfile, O_RDONLY|O_BINARY)
		  || die "Can't Open $dfile";
		while (sysread IN, $buf, 10240)
			{
			my $b2;
			goto copy if !sysread(OUT, $b2, 10240) || $buf ne $b2;
			}
		goto copy if sysread(OUT, $buf, 1);
		close(IN);
		close(OUT);
		print "NOT copying: $_ to $dfile\n";
		next;
		}
      copy:
	sysopen(IN, $_, O_RDONLY|O_BINARY) || die "Can't Open $_";
	sysopen(OUT, $dfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY)
					|| die "Can't Open $dfile";
	while (sysread IN, $buf, 10240)
		{
		syswrite(OUT, $buf, length($buf));
		}
	close(IN);
	close(OUT);
	print "Copying: $_ to $dfile\n";
	}