从主端更改伪 tty 回显模式
在 Linux 上,我在主控端打开一个伪 tty。 虽然从属端没有客户端,但伪 tty 似乎正在回显我写给他的所有内容,这不是我所期望的。 考虑下面的代码:
int main(int argc, char * argv[])
{
int ptyfd;
int rc; /* return code */
char readbuf[3];
ptyfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
die_on_error(ptyfd, "open ptmx");
/* unlock and print slave name */
rc = unlockpt(ptyfd);
die_on_error(rc, "unlockpt");
printf("Slave pts name : %s\n", ptsname(ptyfd));
write(ptyfd, "C", 1);
rc=read(ptyfd, readbuf, 1);
die_on_error(rc, "read");
printf("read returned %c\n",readbuf[0]);
return 0;
}
当我运行这个程序时,我希望 read 调用被阻止,但它立即返回并且 readbuf 内容是 C。我怎样才能改变这种行为? 当从属端未打开时,我希望主端写入的字符消失或被 fifoed 以便稍后由从属端读取。
改变主属性是正确的方法吗?
On linux, I am opening a pseudo tty on the master side. While there is no client on the slave side, the pseudo tty seems to be echoing everything I am writing to him, which is not what I am expecting.
Consider the folowing code :
int main(int argc, char * argv[])
{
int ptyfd;
int rc; /* return code */
char readbuf[3];
ptyfd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
die_on_error(ptyfd, "open ptmx");
/* unlock and print slave name */
rc = unlockpt(ptyfd);
die_on_error(rc, "unlockpt");
printf("Slave pts name : %s\n", ptsname(ptyfd));
write(ptyfd, "C", 1);
rc=read(ptyfd, readbuf, 1);
die_on_error(rc, "read");
printf("read returned %c\n",readbuf[0]);
return 0;
}
When I run this program, I would expect the read call to block, but instead it immediately returns and the readbuf content is C. How can I change this behaviour ? When the slave side is not opened, I would like the character written on the master side to either vanish or be fifoed for later reading by the slave side.
Is changing the master side attributes the right way to do it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我以为主端不是 tty,但显然它是,所以你可以调用诸如 tcgettattr 和 tcsetattr 之类的东西,并抑制回声。
I thought the master side was not a tty, but apparently it is, so you can call things like
tcgettattr
andtcsetattr
, and suppress the echo.旧的答案都没有提供正确的 C 代码,所以这里是:
None of the older answers provided the correct C code, so here it is:
您可以使用阻塞
getch()
调用。 此外,getch()
不会回显内容。You can use the blocking
getch()
call. Alsogetch()
will not echo the content.