-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathrmdir
executable file
·115 lines (77 loc) · 2.25 KB
/
rmdir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/perl
=begin metadata
Name: rmdir
Description: remove directories
Author: Abigail, [email protected]
License: perl
=end metadata
=cut
use strict;
use File::Basename qw(basename);
use File::Spec;
use Getopt::Std qw(getopts);
use constant EX_SUCCESS => 0;
use constant EX_FAILURE => 1;
my ($VERSION) = '1.3';
my $Program = basename($0);
my $rc = EX_SUCCESS;
my %opt;
if (!getopts('p', \%opt) || scalar(@ARGV) == 0) {
warn "usage: $Program [-p] directory ...\n";
exit EX_FAILURE;
}
foreach my $directory (@ARGV) {
next unless remove($directory);
if ($opt{'p'}) {
my @parts = File::Spec->splitdir($directory);
my @seq = 0 .. (scalar(@parts) - 2);
for (reverse @seq) {
my $d = File::Spec->catfile(@parts[0 .. $_]);
next if length($d) == 0; # absolute path
remove($d);
}
}
}
exit $rc;
sub remove {
my $dir = shift;
unless (rmdir $dir) {
warn "$Program: failed to remove '$dir': $!\n";
$rc = EX_FAILURE;
return 0;
}
return 1;
}
__END__
=pod
=head1 NAME
rmdir - remove directories
=head1 SYNOPSIS
rmdir [-p] directory ...
=head1 DESCRIPTION
I<rmdir> removes the directories which are given as argument, if
they are empty. Trying to remove a non-empty directory is regarded
as an error.
=head2 OPTIONS
I<rmdir> accepts the following options:
=over 4
=item -p
Make I<rmdir> treat the arguments as path names, of which all non-empty
components will be removed. Leftmost components will be removed first.
=back
=head1 ENVIRONMENT
The working of I<rmdir> is not influenced by any environment variables.
=head1 BUGS
I<rmdir> does not have any known bugs.
=head1 STANDARDS
This I<rmdir> implementation is compatible with the B<OpenBSD> implementation,
which is expected to be compatible with the B<IEEE Std1003.2-1992>
(aka B<POSIX.2>) standard.
=head1 AUTHOR
The Perl implementation of I<rmdir> was written by Abigail, I<[email protected]>.
=head1 COPYRIGHT and LICENSE
This program is copyright by Abigail 1999.
This program is free and open software. You may use, copy, modify, distribute
and sell this program (and any modified variants) in any way you wish,
provided you do not restrict others to do the same.
=cut