diff options
author | Roland McGrath <roland@gnu.org> | 1992-10-19 20:56:52 +0000 |
---|---|---|
committer | Roland McGrath <roland@gnu.org> | 1992-10-19 20:56:52 +0000 |
commit | abb4e8e7914fbe5e50bff28ce53280ba2254dc81 (patch) | |
tree | 3c7f78ad98704d2deb77c89e04734aaf826652e9 /manual/examples | |
parent | 1600c1c5ccb5c4dc4df840d9a772d3ac541d8881 (diff) |
Initial revision
Diffstat (limited to 'manual/examples')
-rw-r--r-- | manual/examples/select.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/manual/examples/select.c b/manual/examples/select.c new file mode 100644 index 0000000000..743f30ef1e --- /dev/null +++ b/manual/examples/select.c @@ -0,0 +1,29 @@ +#include <stdio.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/time.h> + +int +input_timeout (int filedes, unsigned int seconds) +{ + fd_set set; + struct timeval timeout; + + /* Initialize the file descriptor set. */ + FD_ZERO (&set); + FD_SET (filedes, &set); + + /* Initialize the timeout data structure. */ + timeout.tv_sec = seconds; + timeout.tv_usec = 0; + + /* @code{select} returns 0 if timeout, 1 if input available, -1 if error. */ + return TEMP_FAILURE_RETRY (select (FD_SETSIZE, &set, NULL, NULL, &timeout)); +} + +int +main (void) +{ + fprintf (stderr, "select returned %d.\n", input_timeout (STDIN_FILENO, 5)); + return 0; +} |