fork download
  1. #!/usr/bin/perl6
  2. # your code goes here
  3.  
  4. use NativeCall;
  5.  
  6. class DIR is repr('CPointer') {
  7.  
  8. sub opendir(Str) of Pointer is native(Str) { * }
  9.  
  10. method new(Str $dir) {
  11. return nativecast(DIR, opendir($dir));
  12. }
  13.  
  14. class Dirent is repr('CStruct') {
  15. has int64 $.ino;
  16. has int64 $.off;
  17. has uint16 $.reclen;
  18. has uint8 $.type;
  19. has CArray[int8] $.name;
  20.  
  21. submethod TWEAK() {
  22. $!ino = long.new(0);
  23. $!off = long.new(0);
  24. $!reclen = uint16.new(0);
  25. $!type = uint8.new(0);
  26. $!name := CArray[int8].new;
  27. $!name[$_] = 0 for ^256;
  28. }
  29.  
  30. method name() {
  31. say $!name[0];
  32. #my Buf $buf .= new;
  33. #$buf[$_] = $!name[$_] for ^256;
  34. #$buf.decode('utf8');
  35. }
  36. }
  37.  
  38. sub readdir(DIR) of Pointer is native(Str) { * }
  39.  
  40. sub readdir_r(DIR, Dirent is rw, Pointer[Dirent] is rw) of int32 is native(Str) { * }
  41.  
  42. method read() of Dirent {
  43. my $ptr = nativecast(Pointer[Dirent], readdir(self));
  44. return $ptr.deref;
  45. }
  46.  
  47. method read_r() of Dirent {
  48. my $dirent = Dirent.new;
  49. my $res = Pointer[Dirent].new;
  50. say "ret = ", readdir_r(self, $dirent, $res);
  51. say $res;
  52. $dirent;
  53. }
  54.  
  55. sub closedir(DIR) of int32 is native(Str) { * }
  56.  
  57. method close() of int {
  58. closedir(self);
  59. }
  60.  
  61. method error() {
  62. my $error := cglobal('libc.so.6', 'errno', int32);
  63. $error;
  64. }
  65.  
  66. submethod DESTORY() {
  67.  
  68. }
  69. }
  70.  
  71. my $current = DIR.new(".");
  72.  
  73. dd $current;
  74.  
  75. for ^3 {
  76. my $dirent = $current.read_r;
  77. dd $dirent;
  78. say $dirent.name;
  79. }
  80.  
  81. $current.close;
Runtime error #stdin #stdout #stderr 2.52s 182720KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
lock requires a concrete object with REPR ReentrantMutex
  in method setup at /usr/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 line 295
  in method CALL-ME at /usr/share/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 line 322
  in method new at prog.pl line 10
  in block <unit> at prog.pl line 71