Track /etc/gitconfig
[msysgit/mtrensch.git] / lib / perl5 / 5.8.8 / ExtUtils / Command.pm
blobecd7813bb312ed6b6e91ce08731acefe43feb49f
1 package ExtUtils::Command;
3 use 5.00503;
4 use strict;
5 use Carp;
6 use File::Copy;
7 use File::Compare;
8 use File::Basename;
9 use File::Path qw(rmtree);
10 require Exporter;
11 use vars qw(@ISA @EXPORT @EXPORT_OK $VERSION);
12 @ISA = qw(Exporter);
13 @EXPORT = qw(cp rm_f rm_rf mv cat eqtime mkpath touch test_f chmod
14 dos2unix);
15 $VERSION = '1.09';
17 my $Is_VMS = $^O eq 'VMS';
19 =head1 NAME
21 ExtUtils::Command - utilities to replace common UNIX commands in Makefiles etc.
23 =head1 SYNOPSIS
25 perl -MExtUtils::Command -e cat files... > destination
26 perl -MExtUtils::Command -e mv source... destination
27 perl -MExtUtils::Command -e cp source... destination
28 perl -MExtUtils::Command -e touch files...
29 perl -MExtUtils::Command -e rm_f files...
30 perl -MExtUtils::Command -e rm_rf directories...
31 perl -MExtUtils::Command -e mkpath directories...
32 perl -MExtUtils::Command -e eqtime source destination
33 perl -MExtUtils::Command -e test_f file
34 perl -MExtUtils::Command -e chmod mode files...
35 ...
37 =head1 DESCRIPTION
39 The module is used to replace common UNIX commands. In all cases the
40 functions work from @ARGV rather than taking arguments. This makes
41 them easier to deal with in Makefiles.
43 perl -MExtUtils::Command -e some_command some files to work on
45 I<NOT>
47 perl -MExtUtils::Command -e 'some_command qw(some files to work on)'
49 For that use L<Shell::Command>.
51 Filenames with * and ? will be glob expanded.
53 =over 4
55 =cut
57 # VMS uses % instead of ? to mean "one character"
58 my $wild_regex = $Is_VMS ? '*%' : '*?';
59 sub expand_wildcards
61 @ARGV = map(/[$wild_regex]/o ? glob($_) : $_,@ARGV);
65 =item cat
67 cat file ...
69 Concatenates all files mentioned on command line to STDOUT.
71 =cut
73 sub cat ()
75 expand_wildcards();
76 print while (<>);
79 =item eqtime
81 eqtime source destination
83 Sets modified time of destination to that of source.
85 =cut
87 sub eqtime
89 my ($src,$dst) = @ARGV;
90 local @ARGV = ($dst); touch(); # in case $dst doesn't exist
91 utime((stat($src))[8,9],$dst);
94 =item rm_rf
96 rm_rf files or directories ...
98 Removes files and directories - recursively (even if readonly)
100 =cut
102 sub rm_rf
104 expand_wildcards();
105 rmtree([grep -e $_,@ARGV],0,0);
108 =item rm_f
110 rm_f file ...
112 Removes files (even if readonly)
114 =cut
116 sub rm_f {
117 expand_wildcards();
119 foreach my $file (@ARGV) {
120 next unless -f $file;
122 next if _unlink($file);
124 chmod(0777, $file);
126 next if _unlink($file);
128 carp "Cannot delete $file: $!";
132 sub _unlink {
133 my $files_unlinked = 0;
134 foreach my $file (@_) {
135 my $delete_count = 0;
136 $delete_count++ while unlink $file;
137 $files_unlinked++ if $delete_count;
139 return $files_unlinked;
143 =item touch
145 touch file ...
147 Makes files exist, with current timestamp
149 =cut
151 sub touch {
152 my $t = time;
153 expand_wildcards();
154 foreach my $file (@ARGV) {
155 open(FILE,">>$file") || die "Cannot write $file:$!";
156 close(FILE);
157 utime($t,$t,$file);
161 =item mv
163 mv source_file destination_file
164 mv source_file source_file destination_dir
166 Moves source to destination. Multiple sources are allowed if
167 destination is an existing directory.
169 Returns true if all moves succeeded, false otherwise.
171 =cut
173 sub mv {
174 expand_wildcards();
175 my @src = @ARGV;
176 my $dst = pop @src;
178 croak("Too many arguments") if (@src > 1 && ! -d $dst);
180 my $nok = 0;
181 foreach my $src (@src) {
182 $nok ||= !move($src,$dst);
184 return !$nok;
187 =item cp
189 cp source_file destination_file
190 cp source_file source_file destination_dir
192 Copies sources to the destination. Multiple sources are allowed if
193 destination is an existing directory.
195 Returns true if all copies succeeded, false otherwise.
197 =cut
199 sub cp {
200 expand_wildcards();
201 my @src = @ARGV;
202 my $dst = pop @src;
204 croak("Too many arguments") if (@src > 1 && ! -d $dst);
206 my $nok = 0;
207 foreach my $src (@src) {
208 $nok ||= !copy($src,$dst);
210 return $nok;
213 =item chmod
215 chmod mode files ...
217 Sets UNIX like permissions 'mode' on all the files. e.g. 0666
219 =cut
221 sub chmod {
222 local @ARGV = @ARGV;
223 my $mode = shift(@ARGV);
224 expand_wildcards();
226 if( $Is_VMS ) {
227 foreach my $idx (0..$#ARGV) {
228 my $path = $ARGV[$idx];
229 next unless -d $path;
231 # chmod 0777, [.foo.bar] doesn't work on VMS, you have to do
232 # chmod 0777, [.foo]bar.dir
233 my @dirs = File::Spec->splitdir( $path );
234 $dirs[-1] .= '.dir';
235 $path = File::Spec->catfile(@dirs);
237 $ARGV[$idx] = $path;
241 chmod(oct $mode,@ARGV) || die "Cannot chmod ".join(' ',$mode,@ARGV).":$!";
244 =item mkpath
246 mkpath directory ...
248 Creates directories, including any parent directories.
250 =cut
252 sub mkpath
254 expand_wildcards();
255 File::Path::mkpath([@ARGV],0,0777);
258 =item test_f
260 test_f file
262 Tests if a file exists
264 =cut
266 sub test_f
268 exit !-f $ARGV[0];
271 =item dos2unix
273 dos2unix files or dirs ...
275 Converts DOS and OS/2 linefeeds to Unix style recursively.
277 =cut
279 sub dos2unix {
280 require File::Find;
281 File::Find::find(sub {
282 return if -d;
283 return unless -w _;
284 return unless -r _;
285 return if -B _;
287 local $\;
289 my $orig = $_;
290 my $temp = '.dos2unix_tmp';
291 open ORIG, $_ or do { warn "dos2unix can't open $_: $!"; return };
292 open TEMP, ">$temp" or
293 do { warn "dos2unix can't create .dos2unix_tmp: $!"; return };
294 while (my $line = <ORIG>) {
295 $line =~ s/\015\012/\012/g;
296 print TEMP $line;
298 close ORIG;
299 close TEMP;
300 rename $temp, $orig;
302 }, @ARGV);
305 =back
307 =head1 SEE ALSO
309 Shell::Command which is these same functions but take arguments normally.
312 =head1 AUTHOR
314 Nick Ing-Simmons C<ni-s@cpan.org>
316 Currently maintained by Michael G Schwern C<schwern@pobox.com>.
318 =cut