Nonblocking stdin on telnet

Peter Dufault dufault at
Wed Jul 7 00:08:22 UTC 2010

On Jul 6, 2010, at 7:13 , Andrei Chichak wrote:

> I need to do a non-blocking read on stdin in my telnet process. I have done the following, but the getchar blocks.

This works for me (recent code head, MPC55XX BSP), I just double-checked.  It looks a lot like what you're doing (we must have copied the same code):

app_stop(FILE *in)
    char c;
    int r;
    struct termios oldt, newt;
    int oldf;
    int fd = fileno(in);

    tcgetattr(fd, &oldt);
    newt = oldt;
    newt.c_lflag &= ~ICANON;
    newt.c_cc[VMIN] = 0;
    newt.c_cc[VTIME] = 0;
    tcsetattr(fd, TCSANOW, &newt); 
    oldf = fcntl(fd, F_GETFL, 0);
    fcntl(fd, F_SETFL, oldf | O_NONBLOCK);
    r = 0;  
    if (read(fd, &c, 1) == 1) {
        ungetc(c, in);
        r = 1;
    tcsetattr(fd, TCSANOW, &oldt);
    fcntl(fd, F_SETFL, oldf);
    return r;

This is polling some currents until I enter a character.  In a shell that I opened in telnet I see:

[/] # current 
 -0.008  0.003 -0.004 -0.031 -0.010 -0.018          
[/] # z

It kept showing the currents until I entered "z" without a CR or LF and then it echoed the "z".

Peter Dufault
HD Associates, Inc.      Software and System Engineering

More information about the users mailing list