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