IO-Dirent-0.05/000755 000765 000024 00000000000 11624545575 013505 5ustar00scottstaff000000 000000 IO-Dirent-0.05/Changes000644 000765 000024 00000001362 10647472352 014776 0ustar00scottstaff000000 000000 Revision history for Perl extension IO::Dirent. Release 0.03 ---------------------------- revision 1.3 date: 2007/07/18 20:14:32; author: scott; state: Exp; lines: +9 -1 - update license ---------------------------- Release 0.02 (Dec 2 2002) ---------------------------- revision 1.2 date: 2007/04/09 17:10:09; author: scott; state: Exp; lines: +1 -1 - fix Makefile.PL to use for printf and stderr stream (Craig Barratt) - fix Makefile.PL to closedir(dir) instead of DIR (Craig Barratt) - version bump ---------------------------- revision 1.1 date: 2007/04/09 14:54:06; author: scott; state: Exp; - initial import of IO::Dirent 0.02 - new CVS repository ============================================================================= IO-Dirent-0.05/Dirent.pm000644 000765 000024 00000007601 11624543336 015265 0ustar00scottstaff000000 000000 package IO::Dirent; use strict; use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); require Exporter; require DynaLoader; @ISA = qw(Exporter DynaLoader); @EXPORT_OK = qw( DT_UNKNOWN DT_FIFO DT_CHR DT_DIR DT_BLK DT_REG DT_LNK DT_SOCK DT_WHT ); @EXPORT = qw( readdirent nextdirent ); %EXPORT_TAGS = ('ALL' => [@EXPORT, @EXPORT_OK]); $VERSION = '0.05'; use constant DT_UNKNOWN => 0; use constant DT_FIFO => 1; ## named pipe (fifo) use constant DT_CHR => 2; ## character special use constant DT_DIR => 4; ## directory use constant DT_BLK => 6; ## block special use constant DT_REG => 8; ## regular use constant DT_LNK => 10; ## symbolic link use constant DT_SOCK => 12; ## socket use constant DT_WHT => 14; ## whiteout bootstrap IO::Dirent $VERSION; 1; __END__ =head1 NAME IO::Dirent - Access to dirent structs returned by readdir =head1 SYNOPSIS use IO::Dirent; ## slurp-style opendir DIR, "/usr/local/foo"; my @entries = readdirent(DIR); closedir DIR; print $entries[0]->{name}, "\n"; print $entries[0]->{type}, "\n"; print $entries[0]->{inode}, "\n"; ## using the enumerator opendir DIR, "/etc"; while( my $entry = nextdirent(DIR) ) { print $entry->{name} . "\n"; } closedir DIR; =head1 DESCRIPTION B returns a list of hashrefs. Each hashref contains the name of the directory entry, its inode for the filesystem it resides on and its type (if available). If the file type or inode are not available, it won't be there! B returns the next dirent as a hashref, allowing you to iterate over directory entries one by one. This may be helpful in low-memory situations or where you have enormous directories. B exports the following symbols by default: readdirent nextdirent The following tags may be exported to your namespace: ALL which includes B, B and the following symbols: DT_UNKNOWN DT_FIFO DT_CHR DT_DIR DT_BLK DT_REG DT_LNK DT_SOCK DT_WHT These symbols can be used to test the file type returned by B in the following manner: for my $entry ( readdirent(DIR) ) { next unless $entry->{'type'} == DT_LNK; print $entry->{'name'} . " is a symbolic link.\n"; } For platforms that do not implement file type in its dirent struct, B will return a hashref with a single key/value of 'name' and the filename (effectively the same as readdir). This is subject to change, if I can implement some of the to do items below. =head1 CAVEATS This was written on FreeBSD and OS X which implement a robust (but somewhat non-standard) dirent struct and which includes a file type entry. I have plans to make this module more portable and useful by doing a stat on each directory entry to find the file type and inode number when the dirent.h does not implement it otherwise. Improvements and additional ports are welcome. =head1 TO DO =over 4 =item * For platforms that do not implement a dirent struct with file type, do a stat on the entry and populate the structure anyway. =item * Do some memory profiling (I'm not sure if I have any leaks or not). =back =head1 COPYRIGHT Copyright 2002, 2011 Scott Wiersdorf. This library is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License. =head1 AUTHOR Scott Wiersdorf, Escott@perlcode.orgE =head1 ACKNOWLEDGEMENTS Thanks to Nick Ing-Simmons for his help on the perl-xs mailing list. =head1 SEE ALSO dirent(5), L, L, L, L =head1 COPYRIGHT AND LICENSE Copyright (C) 2007 by Scott Wiersdorf This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.1 or, at your option, any later version of Perl 5 you may have available. =cut IO-Dirent-0.05/Dirent.xs000644 000765 000024 00000003247 11624543020 015273 0ustar00scottstaff000000 000000 #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include #include MODULE = IO::Dirent PACKAGE = IO::Dirent #ifdef DT_DIR #define USE_D_NAME #endif SV * readdirent(dirp) DIR *dirp; PROTOTYPE: * PPCODE: Direntry_t *dent; while ((dent = (Direntry_t *)readdir(dirp))) { HV *hdent; hdent = (HV *)sv_2mortal((SV *)newHV()); #ifdef DIRNAMLEN /* from perl's config.h */ hv_store(hdent, "name", 4, newSVpv(dent->d_name, dent->d_namlen), 0); #else hv_store(hdent, "name", 4, newSVpv(dent->d_name, 0), 0); #endif /* DIRNAMLEN */ #ifdef USE_D_NAME hv_store(hdent, "inode", 5, newSViv(dent->d_fileno), 0); hv_store(hdent, "type", 4, newSVnv(dent->d_type), 0); #endif XPUSHs(sv_2mortal(newRV((SV *) hdent))); } SV * nextdirent(dirp) DIR *dirp; PROTOTYPE: * CODE: Direntry_t *dent; HV *hdent; if( dent = (Direntry_t *)readdir(dirp) ) { hdent = (HV *)sv_2mortal((SV *)newHV()); #ifdef DIRNAMLEN /* from perl's config.h */ hv_store(hdent, "name", 4, newSVpv(dent->d_name, dent->d_namlen), 0); #else hv_store(hdent, "name", 4, newSVpv(dent->d_name, 0), 0); #endif /* DIRNAMLEN */ #ifdef USE_D_NAME hv_store(hdent, "inode", 5, newSViv(dent->d_fileno), 0); hv_store(hdent, "type", 4, newSVnv(dent->d_type), 0); #endif } else { XSRETURN_UNDEF; } RETVAL = newRV((SV *)hdent); OUTPUT: RETVAL IO-Dirent-0.05/JUNK000644 000765 000024 00000005104 07567016376 014202 0ustar00scottstaff000000 000000 /* this file contains experimental XS stuff I'm working on */ /* I'd like to be able to grab the dir handle coming in and get its fileno (IO::Dir::Dirfd has some sample code that might be useful). The reason I'd like to do that is so I can stat each entry in the dir (and get the file type) if DT_DIR is not implemented. */ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include #include MODULE = IO::Dirent PACKAGE = IO::Dirent #ifdef DT_DIR2 #define USE_D_NAME #endif SV * readdirent(dirh) SV *dirh = ST(0); PROTOTYPE: * PREINIT: IO *io; INIT: DIR *dirp = IoDIRP(sv_2io(dirh)); Direntry_t *dent; #ifndef USE_D_NAME int dirfp = fileno(dirfd(IoDIRP(sv_2io(dirh)))); int fdsave; /* cwd to return to */ struct stat sb; #endif PPCODE: #ifndef USE_D_NAME /* save an fd of our current working directory */ if( fdsave = open(".", O_RDONLY) < 0 ) { XSRETURN_UNDEF; } if( dirfp ) { fprintf(stderr, "ok!\n"); } else { fprintf(stderr, "not ok!\n"); } /* chdir to directory we're reading */ if( fchdir(dirfp) == 0 ) { fprintf(stderr, "Hi!\n"); #endif while ((dent = (Direntry_t *)readdir(dirp))) { HV *hdent; hdent = (HV *)sv_2mortal((SV *)newHV()); #ifdef DIRNAMLEN hv_store(hdent, "name", 4, newSVpv(dent->d_name, dent->d_namlen), 0); #else hv_store(hdent, "name", 4, newSVpv(dent->d_name, 0), 0); #endif /* DIRNAMLEN */ #ifdef USE_D_NAME hv_store(hdent, "type", 4, newSVnv(dent->d_type), 0); #else if( lstat(dent->d_name, &sb) == 0 ) { hv_store(hdent, "type", 4, newSVnv( ( ((sb.st_mode) & 0170000) >> 12) ), 0); } #endif XPUSHs(sv_2mortal(newRV((SV *) hdent))); /* test this w/o newRV */ } #ifndef USE_D_NAME fclose(fdsave); } else { switch(errno) { case EACCES: fprintf(stderr, "Access denied.\n"); break; case ENOTDIR: fprintf(stderr, "Directory does not exist.\n"); break; case EBADF: fprintf(stderr, "Not a valid file descriptor.\n"); break; default: fprintf(stderr, "Some other error.\n"); break; } } #endif IO-Dirent-0.05/Makefile.PL000644 000765 000024 00000002737 10606454024 015453 0ustar00scottstaff000000 000000 use strict; use Config qw(%Config); use ExtUtils::MakeMaker; my @extra = (); my @define = (); #@extra = (DEFINE => "@define") if @define = defines(); WriteMakefile( 'NAME' => 'IO::Dirent', 'VERSION_FROM' => 'Dirent.pm', # finds $VERSION @extra, 'dist' => {COMPRESS => 'gzip', SUFFIX => 'gz'}, ); sub defines { my %tests = (); my @results = (); $| = 1; open TEST, ">t.c" or die "$!"; print TEST <<_CODE_; close TEST; #include #include int main() { struct dirent *dent; DIR *dir = opendir("."); if( !dir ) return -1; while( dent = readdir(dir) ) { if( dent->d_name[0] == '.' && dent->d_name[1] == '.' && dent->d_name[2] == 0 ) { fprintf(stderr, "Type directory: %d\n", dent->d_type); } else { continue; } } closedir (dir); } _CODE_ push @results, ( compile_run() ? '-DHAS_D_TYPE' : () ); return @results; } sub compile_run { my $cc_cmd = "$Config{cc} $Config{ccflags} -I$Config{archlibexp}/CORE"; my $exe = "_t_phony$Config{_exe}"; if ($^O eq 'MSWin32') { $cc_cmd .= ' -DWIN32IO_IS_STDIO'; } else { $cc_cmd .= " -o $exe"; } my $rc = system("$cc_cmd $Config{ldflags} t.c $Config{libs} > /dev/null 2>&1"); if ($rc) { unlink("t.c", $exe, "t$Config{_o}"); return; } $rc = system("./$exe 2>&1 > /dev/null"); unlink("t.c", $exe, "t$Config{_o}"); return ( $rc ? 0 : 1 ); } IO-Dirent-0.05/MANIFEST000644 000765 000024 00000000234 10606471722 014624 0ustar00scottstaff000000 000000 Changes Dirent.pm Dirent.xs JUNK Makefile.PL MANIFEST README test.pl typemap META.yml Module meta-data (added by MakeMaker) IO-Dirent-0.05/META.yml000644 000765 000024 00000000516 11624545575 014760 0ustar00scottstaff000000 000000 --- #YAML:1.0 name: IO-Dirent version: 0.05 abstract: ~ license: ~ author: ~ generated_by: ExtUtils::MakeMaker version 6.42 distribution_type: module requires: meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.3.html version: 1.3 IO-Dirent-0.05/README000644 000765 000024 00000006210 11624545435 014357 0ustar00scottstaff000000 000000 NAME IO::Dirent - Access to dirent structs returned by readdir SYNOPSIS use IO::Dirent; opendir DIR, "/usr/local/foo"; my @entries = readdirent(DIR); closedir DIR; print $entries[0]->{name}, "\n"; print $entries[0]->{type}, "\n"; print $entries[0]->{inode}, "\n"; DESCRIPTION IO::Dirent exports the following symbols by default: readdirent nextdirent readdirent returns a list of hashrefs. Each hashref contains the name of the directory entry, its inode for the filesystem it resides on and its type (if available). If the file type or inode are not available, it won't be there! nextdirent is an enumerator for a directory handle. The following tags may be exported to your namespace: ALL which includes readdirent and the following symbols: DT_UNKNOWN DT_FIFO DT_CHR DT_DIR DT_BLK DT_REG DT_LNK DT_SOCK DT_WHT These symbols can be used to test the file type returned by readdirent in the following manner: for my $entry ( readdirent(DIR) ) { next unless $entry->{'type'} == DT_LNK; print $entry->{'name'} . " is a symbolic link.\n"; } Or by nextdirent in the same way: while( my $entry = nextdirent(DIR) ) { next unless $entry->{'type'} == DT_LNK; print $entry->{'name'} . " is a symbolic link.\n"; } For platforms that do not implement file type in its dirent struct, readdirent will return a hashref with a single key/value of 'name' and the filename (effectively the same as readdir). This is subject to change, if I can implement some of the to do items below. CAVEATS This was written on FreeBSD and OS X which implement a robust (but somewhat non-standard) dirent struct and which includes a file type entry. I have plans to make this module more portable and useful by doing a stat on each directory entry to find the file type and inode number when the dirent.h does not implement it otherwise. Improvements and additional ports are welcome. BUILDING On OS X and recent Apple hardware, the undocumented ARCHFLAGS environment variable was valuable; without it, Perl would attempt to build i386 and ppc versions (which didn't work on my hardware): ARCHFLAGS="-arch x86_64" perl Makefile.PL I'd be happy to know if anyone knows of a way to detect this and set it during Makefile.PL. TO DO * For platforms that do not implement a dirent struct with file type, do a stat on the entry and populate the structure anyway. * Do some memory profiling (I'm not sure if I have any leaks or not). COPYRIGHT Copyright 2002, 2011 Scott Wiersdorf. This library is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License. AUTHOR Scott Wiersdorf, ACKNOWLEDGEMENTS Thanks to Nick Ing-Simmons for his help on the perl-xs mailing list. SEE ALSO dirent(5), the perlxstut manpage, the perlxs manpage, the perlguts manpage, the perlapi manpage IO-Dirent-0.05/test.pl000644 000765 000024 00000001563 11624543114 015011 0ustar00scottstaff000000 000000 #!/usr/local/bin/perl -w use strict; use Test; BEGIN { plan tests => 5} use blib; use IO::Dirent qw(:ALL); ok(1); #open(FOO, "."); #use Devel::Peek; #Dump(*FOO); #close FOO; #print "=====================\n"; opendir DIR, "."; #Dump(*DIR); #print "=====================\n"; my @entries = readdirent(DIR); closedir DIR; for my $entry ( @entries ) { if( $entry->{'name'} eq 'blib' ) { skip( ! exists $entry->{'type'}, $entry->{'type'} == DT_DIR ); } if( $entry->{'name'} eq 'Dirent.pm' ) { skip( ! exists $entry->{'type'}, $entry->{'type'} == DT_REG ); } } opendir DIR, '.'; while( my $entry = nextdirent(DIR) ) { if( $entry->{'name'} eq 'blib' ) { skip( ! exists $entry->{'type'}, $entry->{'type'} == DT_DIR ); } if( $entry->{'name'} eq 'Dirent.pm' ) { skip( ! exists $entry->{'type'}, $entry->{'type'} == DT_REG ); } } closedir DIR; IO-Dirent-0.05/typemap000644 000765 000024 00000000065 07567014004 015075 0ustar00scottstaff000000 000000 DIR* T_DIR INPUT T_DIR $var = IoDIRP(sv_2io($arg))