#!/usr/bin/perl6
# your code goes here
use NativeCall;
class DIR is repr( 'CPointer' ) {
sub opendir( Str) of Pointer is native( Str) { * }
method new( Str $dir) {
return nativecast( DIR, opendir( $dir) ) ;
}
class Dirent is repr( 'CStruct' ) {
has int64 $.ino ;
has int64 $.off ;
has uint16 $.reclen ;
has uint8 $.type ;
has CArray[ int8 ] $.name ;
submethod TWEAK( ) {
$! ino = long .new ( 0 ) ;
$! off = long .new ( 0 ) ;
$! reclen = uint16 .new ( 0 ) ;
$! type = uint8 .new ( 0 ) ;
$! name := CArray[ int8 ] .new ;
$! name[ $_] = 0 for ^ 256 ;
}
method name( ) {
say $! name[ 0 ] ;
#my Buf $buf .= new;
#$buf[$_] = $!name[$_] for ^256;
#$buf.decode('utf8');
}
}
sub readdir( DIR) of Pointer is native( Str) { * }
sub readdir_r( DIR, Dirent is rw, Pointer[ Dirent] is rw) of int32 is native( Str) { * }
method read( ) of Dirent {
my $ptr = nativecast( Pointer[ Dirent] , readdir( self) ) ;
return $ptr.deref ;
}
method read_r( ) of Dirent {
my $dirent = Dirent.new ;
my $res = Pointer[ Dirent] .new ;
say "ret = " , readdir_r( self, $dirent, $res) ;
say $res;
$dirent;
}
sub closedir( DIR) of int32 is native( Str) { * }
method close( ) of int {
closedir( self) ;
}
method error( ) {
my $error := cglobal( 'libc.so.6' , 'errno' , int32 ) ;
$error;
}
submethod DESTORY( ) {
}
}
my $current = DIR.new ( "." ) ;
dd $current;
for ^ 3 {
my $dirent = $current.read_r ;
dd $dirent;
say $dirent.name ;
}
$current.close ;
IyEvdXNyL2Jpbi9wZXJsNgojIHlvdXIgY29kZSBnb2VzIGhlcmUKCnVzZSBOYXRpdmVDYWxsOwoKY2xhc3MgRElSIGlzIHJlcHIoJ0NQb2ludGVyJykgewoKICAgIHN1YiBvcGVuZGlyKFN0cikgb2YgUG9pbnRlciBpcyBuYXRpdmUoU3RyKSB7ICogfQoKICAgIG1ldGhvZCBuZXcoU3RyICRkaXIpICB7CiAgICAgICAgcmV0dXJuIG5hdGl2ZWNhc3QoRElSLCBvcGVuZGlyKCRkaXIpKTsKICAgIH0KCiAgICBjbGFzcyBEaXJlbnQgaXMgcmVwcignQ1N0cnVjdCcpIHsKICAgICAgICBoYXMgaW50NjQgJC5pbm87CiAgICAgICAgaGFzIGludDY0ICQub2ZmOwogICAgICAgIGhhcyB1aW50MTYgJC5yZWNsZW47CiAgICAgICAgaGFzIHVpbnQ4ICQudHlwZTsKICAgICAgICBoYXMgQ0FycmF5W2ludDhdICQubmFtZTsKCiAgICAgICAgc3VibWV0aG9kIFRXRUFLKCkgewogICAgICAgICAgICAkIWlubyAgPSBsb25nLm5ldygwKTsKICAgICAgICAgICAgJCFvZmYgID0gbG9uZy5uZXcoMCk7CiAgICAgICAgICAgICQhcmVjbGVuID0gdWludDE2Lm5ldygwKTsKICAgICAgICAgICAgJCF0eXBlICAgPSB1aW50OC5uZXcoMCk7CiAgICAgICAgICAgICQhbmFtZSA6PSBDQXJyYXlbaW50OF0ubmV3OwogICAgICAgICAgICAkIW5hbWVbJF9dID0gMCBmb3IgXjI1NjsKICAgICAgICB9CgogICAgICAgIG1ldGhvZCBuYW1lKCkgewogICAgICAgICAgICBzYXkgJCFuYW1lWzBdOwogICAgICAgICAgICAjbXkgQnVmICRidWYgLj0gbmV3OwogICAgICAgICAgICAjJGJ1ZlskX10gPSAkIW5hbWVbJF9dIGZvciBeMjU2OwogICAgICAgICAgICAjJGJ1Zi5kZWNvZGUoJ3V0ZjgnKTsKICAgICAgICB9CiAgICB9CgogICAgc3ViIHJlYWRkaXIoRElSKSBvZiBQb2ludGVyIGlzIG5hdGl2ZShTdHIpIHsgKiB9CgogICAgc3ViIHJlYWRkaXJfcihESVIsIERpcmVudCBpcyBydywgUG9pbnRlcltEaXJlbnRdIGlzIHJ3KSBvZiBpbnQzMiBpcyBuYXRpdmUoU3RyKSB7ICogfQoKICAgIG1ldGhvZCByZWFkKCkgb2YgRGlyZW50IHsKICAgICAgICBteSAkcHRyID0gbmF0aXZlY2FzdChQb2ludGVyW0RpcmVudF0sIHJlYWRkaXIoc2VsZikpOwogICAgICAgIHJldHVybiAkcHRyLmRlcmVmOwogICAgfQoKICAgIG1ldGhvZCByZWFkX3IoKSBvZiBEaXJlbnQgewogICAgICAgIG15ICRkaXJlbnQgPSBEaXJlbnQubmV3OwogICAgICAgIG15ICRyZXMgPSBQb2ludGVyW0RpcmVudF0ubmV3OwogICAgICAgIHNheSAicmV0ID0gIiwgcmVhZGRpcl9yKHNlbGYsICRkaXJlbnQsICRyZXMpOwogICAgICAgIHNheSAkcmVzOwogICAgICAgICRkaXJlbnQ7CiAgICB9CgogICAgc3ViIGNsb3NlZGlyKERJUikgb2YgaW50MzIgaXMgbmF0aXZlKFN0cikgeyAqIH0KCiAgICBtZXRob2QgY2xvc2UoKSBvZiBpbnQgewogICAgICAgIGNsb3NlZGlyKHNlbGYpOwogICAgfQoKICAgIG1ldGhvZCBlcnJvcigpIHsKICAgICAgICBteSAkZXJyb3IgOj0gY2dsb2JhbCgnbGliYy5zby42JywgJ2Vycm5vJywgaW50MzIpOwogICAgICAgICRlcnJvcjsKICAgIH0KCiAgICBzdWJtZXRob2QgREVTVE9SWSgpIHsKCiAgICB9Cn0KCm15ICRjdXJyZW50ID0gRElSLm5ldygiLiIpOwoKZGQgJGN1cnJlbnQ7Cgpmb3IgXjMgewogICAgbXkgJGRpcmVudCA9ICRjdXJyZW50LnJlYWRfcjsKICAgIGRkICRkaXJlbnQ7CiAgICBzYXkgJGRpcmVudC5uYW1lOwp9CgokY3VycmVudC5jbG9zZTs=