copy.pl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #! /usr/bin/env perl
  2. # Copyright 2005-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. use Fcntl;
  9. # copy.pl
  10. # Perl script 'copy' comment. On Windows the built in "copy" command also
  11. # copies timestamps: this messes up Makefile dependencies.
  12. my $stripcr = 0;
  13. my $arg;
  14. my @excludes = ();
  15. foreach $arg (@ARGV) {
  16. if ($arg eq "-stripcr")
  17. {
  18. $stripcr = 1;
  19. next;
  20. }
  21. if ($arg =~ /^-exclude_re=(.*)$/)
  22. {
  23. push @excludes, $1;
  24. next;
  25. }
  26. $arg =~ s|\\|/|g; # compensate for bug/feature in cygwin glob...
  27. $arg = qq("$arg") if ($arg =~ /\s/); # compensate for bug in 5.10...
  28. foreach my $f (glob $arg)
  29. {
  30. push @filelist, $f unless grep { $f =~ /$_/ } @excludes;
  31. }
  32. }
  33. $fnum = @filelist;
  34. if ($fnum <= 1)
  35. {
  36. die "Need at least two filenames";
  37. }
  38. $dest = pop @filelist;
  39. if ($fnum > 2 && ! -d $dest)
  40. {
  41. die "Destination must be a directory";
  42. }
  43. foreach (@filelist)
  44. {
  45. if (-d $dest)
  46. {
  47. $dfile = $_;
  48. $dfile =~ s|^.*[/\\]([^/\\]*)$|$1|;
  49. $dfile = "$dest/$dfile";
  50. }
  51. else
  52. {
  53. $dfile = $dest;
  54. }
  55. sysopen(IN, $_, O_RDONLY|O_BINARY) || die "Can't Open $_";
  56. sysopen(OUT, $dfile, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY)
  57. || die "Can't Open $dfile";
  58. while (sysread IN, $buf, 10240)
  59. {
  60. if ($stripcr)
  61. {
  62. $buf =~ tr/\015//d;
  63. }
  64. syswrite(OUT, $buf, length($buf));
  65. }
  66. close(IN);
  67. close(OUT);
  68. print "Copying: $_ to $dfile\n";
  69. }