9

我有一个简单的测试文件,如下所示:

use v6.c;
use NativeCall;

sub fcntl(int32, int32 --> int32) is native { * }
sub close(int32 --> int32) is native { * }

my $fd := fcntl($*OUT.native-descriptor, 0);
say $fd;
close($fd);

返回的文件描述符是-1,这不是我想要的。但是当我在 REPL 中运行相同的代码时,我得到了我正在寻找的东西:

> use NativeCall
Nil
> sub fcntl(int32, int32 --> int32) is native { * }
sub fcntl (int32 $, int32 $ --> int32) { #`(Sub+{Callable[int32]}+{NativeCall::Native[Sub+{Callable[int32]},Str]}|17126514527616) ... }
> sub close(int32 --> int32) is native { * }
sub close (int32 $ --> int32) { #`(Sub+{Callable[int32]}+{NativeCall::Native[Sub+{Callable[int32]},Str]}|17126514527904) ... }
> my $fd := fcntl($*OUT.native-descriptor, 0)
15
> say $fd
15
> close($fd)
0

为什么不像fcntl在 REPL 中那样在文件中创建新的文件描述符?

编辑:我正在运行 OpenBSD 6.2,Rakudo 2018.02

4

1 回答 1

8

F_DUPFD 标志与 fcntl(为 0)一起使用时,OpenBSD 要求将原始文件描述符的状态标志也传递给新的。所以这将起作用:

use v6.c;
use NativeCall;

constant F_DUPFD = 0;
constant F_GETFD = 1;

sub fcntl(int32, int32, int32 --> int32) is native { * }
sub close(int32 --> int32) is native { * }

sub MAIN() {
    my $out-fd := $*OUT.native-descriptor;
    my $flags  := fcntl($out-fd, F_GETFD, 0);
    my $fd     := fcntl($out-fd, F_DUPFD, $flags);
    say $fd; # 15
    close($fd);
}
于 2018-03-30T15:55:15.590 回答