Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...