summaryrefslogtreecommitdiffstats
path: root/lib/libncurses/lib_twait.c
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>1994-10-07 08:58:58 +0000
committerache <ache@FreeBSD.org>1994-10-07 08:58:58 +0000
commita80c0624fbd8bd1c784b0b5b7a0fd20b09d317b9 (patch)
tree4a94ca97fb2fc2fdc1fcdd522a66e39c6e763138 /lib/libncurses/lib_twait.c
downloadFreeBSD-src-a80c0624fbd8bd1c784b0b5b7a0fd20b09d317b9.zip
FreeBSD-src-a80c0624fbd8bd1c784b0b5b7a0fd20b09d317b9.tar.gz
Moved from ports with several enhancements
Diffstat (limited to 'lib/libncurses/lib_twait.c')
-rw-r--r--lib/libncurses/lib_twait.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/libncurses/lib_twait.c b/lib/libncurses/lib_twait.c
new file mode 100644
index 0000000..01e7ffa
--- /dev/null
+++ b/lib/libncurses/lib_twait.c
@@ -0,0 +1,59 @@
+/* This work is copyrighted. See COPYRIGHT.OLD & COPYRIGHT.NEW for *
+* details. If they are missing then this copy is in violation of *
+* the copyright conditions. */
+
+/*
+** lib_twait.c
+**
+** The routine timed_wait().
+**
+*/
+
+#include <string.h>
+#include <sys/time.h>
+#include <unistd.h>
+#include "curses.priv.h"
+
+int timed_wait(int fd, int wait, int *timeleft)
+{
+int result;
+struct timeval timeout;
+static fd_set set;
+#ifndef GOOD_SELECT
+struct timeval starttime, returntime;
+
+ gettimeofday(&starttime, NULL);
+#endif
+
+ FD_ZERO(&set);
+ FD_SET(fd, &set);
+
+ /* the units of wait are milliseconds */
+ timeout.tv_sec = wait / 1000;
+ timeout.tv_usec = (wait % 1000) * 1000;
+
+ T(("start twait: sec = %d, usec = %d", timeout.tv_sec, timeout.tv_usec));
+
+ result = select(fd+1, &set, NULL, NULL, &timeout);
+
+#ifndef GOOD_SELECT
+ gettimeofday(&returntime, NULL);
+ timeout.tv_sec -= (returntime.tv_sec - starttime.tv_sec);
+ timeout.tv_usec -= (returntime.tv_usec - starttime.tv_usec);
+ if (timeout.tv_usec < 0 && timeout.tv_sec > 0) {
+ timeout.tv_sec--;
+ timeout.tv_usec += 1000000;
+ }
+ if (timeout.tv_sec < 0)
+ timeout.tv_sec = timeout.tv_usec = 0;
+#endif
+
+ /* return approximate time left on the timeout, in milliseconds */
+ if (timeleft)
+ *timeleft = (timeout.tv_sec * 1000) + (timeout.tv_usec / 1000);
+
+ T(("end twait: returned %d, sec = %d, usec = %d (%d msec)",
+ result, timeout.tv_sec, timeout.tv_usec, *timeleft));
+
+ return(result);
+}
OpenPOWER on IntegriCloud