copy.pl revision 194206
1160814Ssimon#!/usr/local/bin/perl
2160814Ssimon
3160814Ssimonuse Fcntl;
4160814Ssimon
5160814Ssimon
6160814Ssimon# copy.pl
7160814Ssimon
8160814Ssimon# Perl script 'copy' comment. On Windows the built in "copy" command also
9160814Ssimon# copies timestamps: this messes up Makefile dependencies.
10160814Ssimon
11194206Ssimonmy $stripcr = 0;
12194206Ssimon
13160814Ssimonmy $arg;
14160814Ssimon
15160814Ssimonforeach $arg (@ARGV) {
16194206Ssimon	if ($arg eq "-stripcr")
17194206Ssimon		{
18194206Ssimon		$stripcr = 1;
19194206Ssimon		next;
20194206Ssimon		}
21160814Ssimon	$arg =~ s|\\|/|g;	# compensate for bug/feature in cygwin glob...
22160814Ssimon	foreach (glob $arg)
23160814Ssimon		{
24160814Ssimon		push @filelist, $_;
25160814Ssimon		}
26160814Ssimon}
27160814Ssimon
28160814Ssimon$fnum = @filelist;
29160814Ssimon
30160814Ssimonif ($fnum <= 1)
31160814Ssimon	{
32160814Ssimon	die "Need at least two filenames";
33160814Ssimon	}
34160814Ssimon
35160814Ssimon$dest = pop @filelist;
36160814Ssimon
37160814Ssimonif ($fnum > 2 && ! -d $dest)
38160814Ssimon	{
39160814Ssimon	die "Destination must be a directory";
40160814Ssimon	}
41160814Ssimon
42160814Ssimonforeach (@filelist)
43160814Ssimon	{
44160814Ssimon	if (-d $dest)
45160814Ssimon		{
46160814Ssimon		$dfile = $_;
47160814Ssimon		$dfile =~ s|^.*[/\\]([^/\\]*)$|$1|;
48160814Ssimon		$dfile = "$dest/$dfile";
49160814Ssimon		}
50160814Ssimon	else
51160814Ssimon		{
52160814Ssimon		$dfile = $dest;
53160814Ssimon		}
54160814Ssimon	sysopen(IN, $_, O_RDONLY|O_BINARY) || die "Can't Open $_";
55160814Ssimon	sysopen(OUT, $dfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY)
56160814Ssimon					|| die "Can't Open $dfile";
57160814Ssimon	while (sysread IN, $buf, 10240)
58160814Ssimon		{
59194206Ssimon		if ($stripcr)
60194206Ssimon			{
61194206Ssimon			$buf =~ tr/\015//d;
62194206Ssimon			}
63160814Ssimon		syswrite(OUT, $buf, length($buf));
64160814Ssimon		}
65160814Ssimon	close(IN);
66160814Ssimon	close(OUT);
67160814Ssimon	print "Copying: $_ to $dfile\n";
68160814Ssimon	}
69160814Ssimon
70160814Ssimon
71