有没有办法在 erlang 中打开终端设备文件?
我在 Solaris 上,我正在尝试以下操作:
Erlang (BEAM) 模拟器版本 5.6 [源代码] [64 位] [异步线程:0] [内核轮询:假]
/xlcabuser1/xlc/abp/arunmu/Dolphin/ebin
Eshell V5.6(使用 ^G 中止)
1>文件:打开(“/dev/pts/2”,[写])。
{错误,eisdir}
2> file:open("/dev/null",[write]).
{好的,}
3>
如上所示,erlang文件驱动打开空文件没有问题,但不打开终端设备文件!!
无法得出结论,因为文件驱动程序能够打开一个空文件。
有没有其他方法可以打开终端设备文件?
谢谢
最佳答案
更新 :我能够使用端口解决下面描述的限制。例如,这里有一个示例程序,它将“hello world”打印到 /dev/stdout
:
-module(test).
-export([main/1]).
main(X) ->
P = open_port({spawn, "/bin/cat >/dev/stdout"}, [out]),
P ! {self(), {command, "hello world"}}.
这有点不方便,因为端口不像常规文件那样工作,但至少它是完成工作的一种方式。
在
efile_openfile()
(在 erts/emulator/drivers/unix/unix_efile.c
中)有以下代码: if (stat(name, &statbuf) >= 0 && !ISREG(statbuf)) {
#if !defined(VXWORKS) && !defined(OSE)
/*
* For UNIX only, here is some ugly code to allow
* /dev/null to be opened as a file.
*
* Assumption: The i-node number for /dev/null cannot be zero.
*/
static ino_t dev_null_ino = 0;
if (dev_null_ino == 0) {
struct stat nullstatbuf;
if (stat("/dev/null", &nullstatbuf) >= 0) {
dev_null_ino = nullstatbuf.st_ino;
}
}
if (!(dev_null_ino && statbuf.st_ino == dev_null_ino)) {
#endif
errno = EISDIR;
return check_error(-1, errInfo);
#if !defined(VXWORKS) && !defined(OSE)
}
#endif
}
此代码(令人困惑)返回
EISDIR
如果文件不是常规文件(这是 ISREG(statbuf)
检查),则错误,除非文件特别是 /dev/null
. file(3)
文件指出: eisdir :
The named file is not a regular file. It may be a directory, a
fifo, or a device.
所以它实际上是记录在案的。我不确定为什么会存在这种限制——也许它与性能有关,因为设备驱动程序可能会比普通文件阻塞更多的时间。
关于file - 使用erlang打开设备文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4517393/