xref: /freebsd-src/crypto/openssl/util/copy.pl (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert#! /usr/bin/env perl
2*e0c4386eSCy Schubert# Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert#
4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License").  You may not use
5*e0c4386eSCy Schubert# this file except in compliance with the License.  You can obtain a copy
6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubertuse Fcntl;
11*e0c4386eSCy Schubert
12*e0c4386eSCy Schubert
13*e0c4386eSCy Schubert# copy.pl
14*e0c4386eSCy Schubert
15*e0c4386eSCy Schubert# Perl script 'copy' comment. On Windows the built in "copy" command also
16*e0c4386eSCy Schubert# copies timestamps: this messes up Makefile dependencies.
17*e0c4386eSCy Schubert
18*e0c4386eSCy Schubertmy $stripcr = 0;
19*e0c4386eSCy Schubert
20*e0c4386eSCy Schubertmy $arg;
21*e0c4386eSCy Schubertmy @excludes = ();
22*e0c4386eSCy Schubert
23*e0c4386eSCy Schubertforeach $arg (@ARGV) {
24*e0c4386eSCy Schubert	if ($arg eq "-stripcr")
25*e0c4386eSCy Schubert		{
26*e0c4386eSCy Schubert		$stripcr = 1;
27*e0c4386eSCy Schubert		next;
28*e0c4386eSCy Schubert		}
29*e0c4386eSCy Schubert	if ($arg =~ /^-exclude_re=(.*)$/)
30*e0c4386eSCy Schubert		{
31*e0c4386eSCy Schubert		push @excludes, $1;
32*e0c4386eSCy Schubert		next;
33*e0c4386eSCy Schubert		}
34*e0c4386eSCy Schubert	$arg =~ s|\\|/|g;	# compensate for bug/feature in cygwin glob...
35*e0c4386eSCy Schubert	$arg = qq("$arg") if ($arg =~ /\s/);	# compensate for bug in 5.10...
36*e0c4386eSCy Schubert	foreach my $f (glob $arg)
37*e0c4386eSCy Schubert		{
38*e0c4386eSCy Schubert		push @filelist, $f unless grep { $f =~ /$_/ } @excludes;
39*e0c4386eSCy Schubert		}
40*e0c4386eSCy Schubert}
41*e0c4386eSCy Schubert
42*e0c4386eSCy Schubert$fnum = @filelist;
43*e0c4386eSCy Schubert
44*e0c4386eSCy Schubertif ($fnum <= 1)
45*e0c4386eSCy Schubert	{
46*e0c4386eSCy Schubert	die "Need at least two filenames";
47*e0c4386eSCy Schubert	}
48*e0c4386eSCy Schubert
49*e0c4386eSCy Schubert$dest = pop @filelist;
50*e0c4386eSCy Schubert
51*e0c4386eSCy Schubertif ($fnum > 2 && ! -d $dest)
52*e0c4386eSCy Schubert	{
53*e0c4386eSCy Schubert	die "Destination must be a directory";
54*e0c4386eSCy Schubert	}
55*e0c4386eSCy Schubert
56*e0c4386eSCy Schubertforeach (@filelist)
57*e0c4386eSCy Schubert	{
58*e0c4386eSCy Schubert	if (-d $dest)
59*e0c4386eSCy Schubert		{
60*e0c4386eSCy Schubert		$dfile = $_;
61*e0c4386eSCy Schubert		$dfile =~ s|^.*[/\\]([^/\\]*)$|$1|;
62*e0c4386eSCy Schubert		$dfile = "$dest/$dfile";
63*e0c4386eSCy Schubert		}
64*e0c4386eSCy Schubert	else
65*e0c4386eSCy Schubert		{
66*e0c4386eSCy Schubert		$dfile = $dest;
67*e0c4386eSCy Schubert		}
68*e0c4386eSCy Schubert	sysopen(IN, $_, O_RDONLY|O_BINARY) || die "Can't Open $_";
69*e0c4386eSCy Schubert	sysopen(OUT, $dfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY)
70*e0c4386eSCy Schubert					|| die "Can't Open $dfile";
71*e0c4386eSCy Schubert	while (sysread IN, $buf, 10240)
72*e0c4386eSCy Schubert		{
73*e0c4386eSCy Schubert		if ($stripcr)
74*e0c4386eSCy Schubert			{
75*e0c4386eSCy Schubert			$buf =~ tr/\015//d;
76*e0c4386eSCy Schubert			}
77*e0c4386eSCy Schubert		syswrite(OUT, $buf, length($buf));
78*e0c4386eSCy Schubert		}
79*e0c4386eSCy Schubert	close(IN);
80*e0c4386eSCy Schubert	close(OUT);
81*e0c4386eSCy Schubert	print "Copying: $_ to $dfile\n";
82*e0c4386eSCy Schubert	}
83*e0c4386eSCy Schubert
84*e0c4386eSCy Schubert
85