The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/local/bin/perl -w

#
# $Id: rmdir,v 1.2 2004/08/05 14:17:44 cwest Exp $
#
# $Log: rmdir,v $
# Revision 1.2  2004/08/05 14:17:44  cwest
# cleanup, new version number on website
#
# Revision 1.1  2004/07/23 20:10:16  cwest
# initial import
#
# Revision 1.1  1999/02/28 13:32:45  abigail
# Initial revision
#
#

use strict;
use Getopt::Std;

my ($VERSION) = '$Revision: 1.2 $' =~ /([.\d]+)/;

my $warnings = 0;

# Print a usuage message on a unknown option.
# Requires my patch to Getopt::Std of 25 Feb 1999.
$SIG {__WARN__} = sub {
    require File::Basename;
    $0 = File::Basename::basename ($0);
    if (substr ($_ [0], 0, 14) eq "Unknown option" ||
        substr ($_ [0], 0,  5) eq "Usage") {
        warn <<EOF;
$0 (Perl bin utils) $VERSION
$0 [-p] directory [directories ...]
EOF
        exit 1;
    }
    else {
        $warnings = 1;
        warn "$0: @_";
    }
};

$SIG {__DIE__} = sub {
    require File::Basename;
    $0 = File::Basename::basename ($0);
    die "$0: @_";
};

# Get the options.
getopts ('p', \my %options);

warn "Unknown option" unless @ARGV;   # This will actually die.

my $parent = exists $options {p};

foreach my $directory (@ARGV) {

    rmdir $directory or do {
        warn "$!\n";
        next;
    };

    if ($parent) {
        require File::Basename;
        # Errors are ignored once in recursion.
        1 while ($directory = File::Basename::dirname ($directory)) &&
                 $directory && rmdir $directory;
    }
}

    
exit $warnings;

__END__

=pod

=head1 NAME

rmdir -- remove directories

=head1 SYNOPSIS

rmdir [-p] directory [directories ...]

=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<head> 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 REVISION HISTORY

    $Log: rmdir,v $
    Revision 1.2  2004/08/05 14:17:44  cwest
    cleanup, new version number on website

    Revision 1.1  2004/07/23 20:10:16  cwest
    initial import

    Revision 1.1  1999/02/28 13:32:45  abigail
    Initial revision

=head1 AUTHOR

The Perl implementation of I<rmdir> was written by Abigail, I<abigail@fnx.com>.

=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