Initial revision
authorroland <roland>
Mon, 19 Oct 1992 20:56:52 +0000 (20:56 +0000)
committerroland <roland>
Mon, 19 Oct 1992 20:56:52 +0000 (20:56 +0000)
manual/examples/select.c [new file with mode: 0644]

diff --git a/manual/examples/select.c b/manual/examples/select.c
new file mode 100644 (file)
index 0000000..743f30e
--- /dev/null
@@ -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;
+}