Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
3.4 KiB

  1. package ExtUtils::Command;
  2. use strict;
  3. # use AutoLoader;
  4. use Carp;
  5. use File::Copy;
  6. use File::Compare;
  7. use File::Basename;
  8. use File::Path qw(rmtree);
  9. require Exporter;
  10. use vars qw(@ISA @EXPORT $VERSION);
  11. @ISA = qw(Exporter);
  12. @EXPORT = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f);
  13. $VERSION = '1.01';
  14. =head1 NAME
  15. ExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
  16. =head1 SYNOPSIS
  17. perl -MExtUtils::Command -e cat files... > destination
  18. perl -MExtUtils::Command -e mv source... destination
  19. perl -MExtUtils::Command -e cp source... destination
  20. perl -MExtUtils::Command -e touch files...
  21. perl -MExtUtils::Command -e rm_f file...
  22. perl -MExtUtils::Command -e rm_rf directories...
  23. perl -MExtUtils::Command -e mkpath directories...
  24. perl -MExtUtils::Command -e eqtime source destination
  25. perl -MExtUtils::Command -e chmod mode files...
  26. perl -MExtUtils::Command -e test_f file
  27. =head1 DESCRIPTION
  28. The module is used in the Win32 port to replace common UNIX commands.
  29. Most commands are wrappers on generic modules File::Path and File::Basename.
  30. =over 4
  31. =cut
  32. sub expand_wildcards
  33. {
  34. @ARGV = map(/[\*\?]/ ? glob($_) : $_,@ARGV);
  35. }
  36. =item cat
  37. Concatenates all files mentioned on command line to STDOUT.
  38. =cut
  39. sub cat ()
  40. {
  41. expand_wildcards();
  42. print while (<>);
  43. }
  44. =item eqtime src dst
  45. Sets modified time of dst to that of src
  46. =cut
  47. sub eqtime
  48. {
  49. my ($src,$dst) = @ARGV;
  50. open(F,">$dst");
  51. close(F);
  52. utime((stat($src))[8,9],$dst);
  53. }
  54. =item rm_f files....
  55. Removes directories - recursively (even if readonly)
  56. =cut
  57. sub rm_rf
  58. {
  59. rmtree([grep -e $_,expand_wildcards()],0,0);
  60. }
  61. =item rm_f files....
  62. Removes files (even if readonly)
  63. =cut
  64. sub rm_f
  65. {
  66. foreach (expand_wildcards())
  67. {
  68. next unless -f $_;
  69. next if unlink($_);
  70. chmod(0777,$_);
  71. next if unlink($_);
  72. carp "Cannot delete $_:$!";
  73. }
  74. }
  75. =item touch files ...
  76. Makes files exist, with current timestamp
  77. =cut
  78. sub touch
  79. {
  80. expand_wildcards();
  81. my $t = time;
  82. while (@ARGV)
  83. {
  84. my $file = shift(@ARGV);
  85. open(FILE,">>$file") || die "Cannot write $file:$!";
  86. close(FILE);
  87. utime($t,$t,$file);
  88. }
  89. }
  90. =item mv source... destination
  91. Moves source to destination.
  92. Multiple sources are allowed if destination is an existing directory.
  93. =cut
  94. sub mv
  95. {
  96. my $dst = pop(@ARGV);
  97. expand_wildcards();
  98. croak("Too many arguments") if (@ARGV > 1 && ! -d $dst);
  99. while (@ARGV)
  100. {
  101. my $src = shift(@ARGV);
  102. move($src,$dst);
  103. }
  104. }
  105. =item cp source... destination
  106. Copies source to destination.
  107. Multiple sources are allowed if destination is an existing directory.
  108. =cut
  109. sub cp
  110. {
  111. my $dst = pop(@ARGV);
  112. expand_wildcards();
  113. croak("Too many arguments") if (@ARGV > 1 && ! -d $dst);
  114. while (@ARGV)
  115. {
  116. my $src = shift(@ARGV);
  117. copy($src,$dst);
  118. }
  119. }
  120. =item chmod mode files...
  121. Sets UNIX like permissions 'mode' on all the files.
  122. =cut
  123. sub chmod
  124. {
  125. my $mode = shift(@ARGV);
  126. chmod($mode,expand_wildcards()) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
  127. }
  128. =item mkpath directory...
  129. Creates directory, including any parent directories.
  130. =cut
  131. sub mkpath
  132. {
  133. File::Path::mkpath([expand_wildcards()],1,0777);
  134. }
  135. =item test_f file
  136. Tests if a file exists
  137. =cut
  138. sub test_f
  139. {
  140. exit !-f shift(@ARGV);
  141. }
  142. 1;
  143. __END__
  144. =back
  145. =head1 BUGS
  146. Should probably be Auto/Self loaded.
  147. =head1 SEE ALSO
  148. ExtUtils::MakeMaker, ExtUtils::MM_Unix, ExtUtils::MM_Win32
  149. =head1 AUTHOR
  150. Nick Ing-Simmons <F<[email protected]>>.
  151. =cut