# New Ticket Created by  Kay-Uwe Huell 
# Please include the string:  [perl #40299]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=40299 >


Hi parrot-team,

needed a readdir() function, which I have implemented in os.pmc (for 
POSIX compliant systems). Here is the patch.

Regards,

Kiwi

diffstat:
  src/pmc/os.pmc |   48 +++++++++++++++++++++++++++++++++++++++++++++++-
  t/pmc/os.t     |   20 +++++++++++++++++++-
  2 files changed, 66 insertions(+), 2 deletions(-)
Index: src/pmc/os.pmc
===================================================================
--- src/pmc/os.pmc	(Revision 14421)
+++ src/pmc/os.pmc	(Arbeitskopie)
@@ -23,6 +23,10 @@
 #include <direct.h>
 #endif
 
+#ifndef WIN32
+#include <dirent.h>
+#endif
+
 #include "parrot/parrot.h"
 
 /* XXX Check if we need to deallocate strerror strings */
@@ -433,10 +437,52 @@
 #endif
     }
 
-}
 
 /*
 
+=item C<PMC* readdir(STRING* path)>
+
+reads entries from a directory.
+
+=cut
+
+*/
+    METHOD PMC* readdir(STRING* path) {
+#ifndef WIN32
+        DIR *dir ;
+        struct dirent *dirent;
+        PMC *array;
+        char *cpath = string_to_cstring(interpreter, path);
+        STRING *retval ;
+
+        dir = opendir(cpath);
+        string_cstring_free(cpath);
+        if (dir == NULL)
+        {
+            char *errmsg = strerror(errno);
+            real_exception(interpreter, NULL, E_SystemError, errmsg);
+        }
+
+        array = pmc_new(interpreter, enum_class_ResizableStringArray);
+        while ((dirent = readdir(dir)) != NULL)
+        {
+            retval = string_from_cstring(interpreter, dirent->d_name, 0) ;
+            VTABLE_push_string(interpreter, array, retval);
+        }
+
+        closedir(dir);
+
+        return array ;
+#else
+        real_exception(interpreter, NULL, E_NotImplementedError,
+                "Win32 is not POSIX. Need win32 developer!");
+#endif
+    }
+
+
+}
+/*
+
 =back
 
 =head1 SEE ALS0
Index: t/pmc/os.t
===================================================================
--- t/pmc/os.t	(Revision 14421)
+++ t/pmc/os.t	(Arbeitskopie)
@@ -6,7 +6,7 @@
 use warnings;
 use lib qw( . lib ../lib ../../lib );
 use Test::More;
-use Parrot::Test tests => 13;
+use Parrot::Test tests => 14;
 use Parrot::Config;
 use Cwd;
 use File::Spec;
@@ -193,6 +193,24 @@
 
 }
 
+# test readdir
+unless ($MSWin32)
+{
+   opendir IN, '.' ;
+   my @entries = readdir IN ;
+   closedir IN ;
+   my $entries = join(' ', @entries)."\n" ;
+   pir_output_is(<<'CODE', $entries, 'Test OS.readdir');
+.sub main :main
+    $P1 = new .OS
+    $P2 = $P1.readdir('.')
+    
+    $S0 = join ' ', $P2
+    print $S0
+    print "\n"
+.end
+CODE
+}
 
 # test lstat
 

Reply via email to