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 Str $.name;
  20.  
  21. #`(
  22. submethod TWEAK() {
  23. $!ino = long.new(0);
  24. $!off = long.new(0);
  25. $!reclen = uint16.new(0);
  26. $!type = uint8.new(0);
  27. $!name := CArray[int8].new;
  28. $!name.[$_] = 0 for ^256;
  29. })
  30.  
  31. #`(method name() {
  32. say $!name.[0];
  33. my Buf $buf .= new;
  34. $buf[$_] = $!name[$_] for ^256;
  35. $buf.decode('utf8');
  36. })
  37. }
  38.  
  39. sub readdir(DIR) of Pointer is native(Str) { * }
  40.  
  41. sub readdir_r(DIR, Pointer[Dirent], Pointer[Pointer[Dirent]]) of int32 is native(Str) { * }
  42.  
  43. method read() of Dirent {
  44. my $ptr = nativecast(Pointer[Dirent], readdir(self));
  45. return $ptr.deref;
  46. }
  47.  
  48. method read_r() of Dirent {
  49. my $passd = CArray[Dirent].new;
  50. my $passr = CArray[Pointer[Dirent]].new;
  51.  
  52. $passd[0] = Dirent.new;
  53. $passr[0] = Pointer[Dirent].new;
  54. say "ret = ", readdir_r(self, nativecast(Pointer[Dirent], $passd), nativecast(Pointer[Pointer[Dirent]], $passr));
  55. say $passd[0];
  56. $passd[0];
  57. }
  58.  
  59. sub closedir(DIR) of int32 is native(Str) { * }
  60.  
  61. method close() of int {
  62. closedir(self);
  63. }
  64.  
  65. method error() {
  66. my $error := cglobal('libc.so.6', 'errno', int32);
  67. $error;
  68. }
  69.  
  70. submethod DESTORY() {
  71.  
  72. }
  73. }
  74.  
  75. my $current = DIR.new(".");
  76.  
  77. dd $current;
  78.  
  79. for ^3 {
  80. my $dirent = $current.read;
  81. say $current.error unless $dirent;
  82. say $dirent.name;
  83. }
  84.  
  85. $current.close;
  86.  
Runtime error #stdin #stdout #stderr 2.49s 183936KB
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 75