Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

c - changing pseudo tty echo mode from the master side

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
", ptsname(ptyfd));

    write(ptyfd, "C", 1);
    rc=read(ptyfd, readbuf, 1);
    die_on_error(rc, "read");
    printf("read returned %c
",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 ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...