about summary refs log tree commit diff
path: root/manual/examples/select.c
diff options
context:
space:
mode:
authorRoland McGrath <roland@gnu.org>1995-02-18 01:27:10 +0000
committerRoland McGrath <roland@gnu.org>1995-02-18 01:27:10 +0000
commit28f540f45bbacd939bfd07f213bcad2bf730b1bf (patch)
tree15f07c4c43d635959c6afee96bde71fb1b3614ee /manual/examples/select.c
downloadglibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.tar.gz
glibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.tar.xz
glibc-28f540f45bbacd939bfd07f213bcad2bf730b1bf.zip
initial import
Diffstat (limited to 'manual/examples/select.c')
-rw-r--r--manual/examples/select.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/manual/examples/select.c b/manual/examples/select.c
new file mode 100644
index 0000000000..def2cd6f9f
--- /dev/null
+++ b/manual/examples/select.c
@@ -0,0 +1,40 @@
+/*@group*/
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/time.h>
+/*@end group*/
+
+/*@group*/
+int 
+input_timeout (int filedes, unsigned int seconds)
+{
+  fd_set set;
+  struct timeval timeout;
+/*@end group*/
+
+  /* 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;
+
+/*@group*/
+  /* @code{select} returns 0 if timeout, 1 if input available, -1 if error. */
+  return TEMP_FAILURE_RETRY (select (FD_SETSIZE,
+				     &set, NULL, NULL,
+				     &timeout));
+}
+/*@end group*/
+
+/*@group*/
+int
+main (void)
+{
+  fprintf (stderr, "select returned %d.\n",
+	   input_timeout (STDIN_FILENO, 5));
+  return 0;
+}
+/*@end group*/