#!/usr/bin/env perl use warnings; use strict; use File::Spec; use Getopt::Std; use Fcntl; use constant BUF_SIZE => 4096; use lib '/home/myself/perl5/perls/perl-5.20.1/lib/site_perl/5.20.3/i686-linux/sys'; require 'syscall.ph'; my %opts; getopts( 'd:', \%opts ); die 'option -d is required' unless ( ( exists( $opts{d} ) ) and ( defined( $opts{d} ) ) ); sysopen( my $fd, $opts{d}, O_RDONLY | O_DIRECTORY ); while (1) { my $buf = "\0" x BUF_SIZE; my $read = syscall( &SYS_getdents, fileno($fd), $buf, BUF_SIZE ); if ( ( $read == -1 ) and ( $! != 0 ) ) { die "failed to syscal getdents: $!"; } last if ( $read == 0 ); while ( $read != 0 ) { my ( $ino, $off, $len, $name ) = unpack( "LLSZ*", $buf ); #print $name, "\n" if ( $ino != 0 ); unless ( ( $name eq '.' ) or ( $name eq '..' ) ) { my $path = File::Spec->catfile( $opts{d}, $name ); unlink $path or die "Cannot remove $path: $!"; } substr( $buf, 0, $len ) = ''; $read -= $len; } } close($fd);