Maybe this will help, since it fixes the buffering of stdin which makes it kinda hard to see what's going on.
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/select.h>
#include <termios.h>
int main() {
int ch;
struct termios oldt, newt;
tcgetattr ( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
newt.c_cc[VMIN] = 0;
newt.c_cc[VTIME] = 0;
tcsetattr ( STDIN_FILENO, TCSANOW, &newt );
printf("Type stuff ");
fflush(stdout);
while ( ch != 'q' ) {
fd_set read_set;
FD_ZERO(&read_set);
FD_SET(0,&read_set);
struct timeval tmo = { 1, 0 }; // 1 second
if ( select(1,&read_set,NULL,NULL,&tmo) == 1 ) {
ch = getchar();
printf("Char=%c\n",ch);
fflush(stdout);
} else {
printf("timeout\n");
fflush(stdout);
}
}
tcsetattr ( STDIN_FILENO, TCSANOW, &oldt );
return 0;
}
$ ./a.out
Type stuff timeout
Char=h
Char=e
Char=l
Char=l
Char=o
Char=
timeout
Char=q