#!/usr/bin/perl use strict; usage() if ($#ARGV < 1); if (-d $ARGV[$#ARGV]) { for (my $i = 0; $i < $#ARGV; $i++) { my $src = $ARGV[$i]; $src =~ s{.*/}{}; cpf ($ARGV[$i], $ARGV[$#ARGV] . '/' . $src); } } else { usage() if ($#ARGV != 1); cpf ($ARGV[0], $ARGV[1]); } sub usage() { die <<"ENDUSAGE"; This is cpf, "copy-forward" utility. Usage is similar to "cp", but doesn't overwrite recent-enough copies. Written by Karel Kubat, http://www.kubat.nl. Use at your own risk and for your own pleasure. cpf srcfile dstfile - copy src to dst, if it's more recent cpf file file file dstdir - copy files to dstdir, if they are more recent ENDUSAGE } sub cpf($$) { my $src = shift; my $dst = shift; $src =~ s{//}{/}g; $dst =~ s{//}{/}g; die ("cpf: No such file $src\n") unless (-f $src); if (-f $dst and (stat($src))[9] <= (stat($dst))[9]) { print ("cpf: $src skipped\n"); return; } print ("cpf: $src -> $dst\n"); open (my $if, $src) or die ("cpf: cannot read $src: $!\n"); binmode($if); open (my $of, ">$dst") or die ("cpf: cannot write $dst: $!\n"); binmode($of); my ($buf, $n); while ($n = sysread($if, $buf, 10240)) { die ("cpf: incomplete write to $dst\n") if (syswrite($of, $buf, $n) != $n); } close($if); close($of); chmod((stat($src))[2], $dst); }