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