changing pseudo tty echo mode from the master side

拜拜、爱过 提交于 2020-07-16 04:39:10

问题


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 ?


回答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.




回答2:


None of the older answers provided the correct C code, so here it is:

struct termios tmios;
tcgetattr(ptfd, &tmios);
tmios.c_lflag &= ~(ECHO);
tcsetattr(ptfd, TCSANOW, &tmios);



回答3:


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



来源:https://stackoverflow.com/questions/482818/changing-pseudo-tty-echo-mode-from-the-master-side

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!