// this illustrates the use of select() to "poll" files to see if they are
// ready, instead of using signal(), SIGIO, and user-level interrupts.
// In this example, I didn't bother putting the terminal in non-blocking
// mode, or turning off the echo, because that just confuses the issue.
// So this sample behaves strangely, you still have to press ENTER before
// input is seen.
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
void main(void)
{
while (1)
{
fd_set files; // an fd_set is a list of files we are interested in
FD_ZERO(&files); // empty the fd_set, then add the ones we care about:
FD_SET(0, &files); // 0 for stdin, multiple file ids may be set
timeval timeout = { 0, 0 }; // seconds then microseconds
int n=select(1, // biggest file number set, plus one
&files, // fd_set for input files
NULL, // fd_set for output files
NULL, // fd_set for internet sockets gone peculiar
&timeout);
// result n is number of ready file numbers, fd_sets are modified
// so only ready ones are still set
if (n>0 && FD_ISSET(0, &files))
{ int c=getchar();
if (c>0) printf("You typed '%c'\n", c); }
else
{ sleep(3);
printf(".");
fflush(stdout); }
}
}