从主方改变伪tty回声模式

时间:2022-08-24 21:02:44

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 :

在linux上,我在master方面打开一个伪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;   
}

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.

当我运行这个程序时,我希望read调用阻塞,但它会立即返回并且readbuf内容为C.如何更改此行为?当从属方未打开时,我希望写在主方的字符要么消失要么被丢弃以便以后由从属方读取。

Is changing the master side attributes the right way to do it ?

正在改变主方属性的正确方法吗?

2 个解决方案

#1


I thought the master side was not a tty, but apparently it is, so you can call things like tcgettattr and tcsetattr, and suppress the echo.

我认为主方不是一个tty,但显然它是,所以你可以调用像tcgettattr和tcsetattr这样的东西,并抑制回声。

#2


You can use the blocking getch() call. Also getch() will not echo the content.

您可以使用阻塞getch()调用。 getch()也不会回显内容。

#1


I thought the master side was not a tty, but apparently it is, so you can call things like tcgettattr and tcsetattr, and suppress the echo.

我认为主方不是一个tty,但显然它是,所以你可以调用像tcgettattr和tcsetattr这样的东西,并抑制回声。

#2


You can use the blocking getch() call. Also getch() will not echo the content.

您可以使用阻塞getch()调用。 getch()也不会回显内容。