summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorroot <root@nolonger-other.tetra.james.local>2020-10-26 22:55:18 +0000
committerfishsoupisgood <github@madingley.org>2020-10-26 23:04:50 +0000
commitb11ae5cf86000bfce35b6ec511014d8f6b04416e (patch)
treed4f4caf8ecb8b14e62858d46d781af6886eb1b73 /util.c
parent62bc1af6c6a1201db551e1ec523e757415464fd5 (diff)
downloadgalaxy_tools-b11ae5cf86000bfce35b6ec511014d8f6b04416e.tar.gz
galaxy_tools-b11ae5cf86000bfce35b6ec511014d8f6b04416e.tar.bz2
galaxy_tools-b11ae5cf86000bfce35b6ec511014d8f6b04416e.zip
add keypad
Diffstat (limited to 'util.c')
-rw-r--r--util.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/util.c b/util.c
index 95268b4..ab9afcc 100644
--- a/util.c
+++ b/util.c
@@ -45,6 +45,41 @@ int fd_can_read (int fd)
return 0;
}
+ssize_t read_with_timeout (int fd, void *_b, size_t len, unsigned timeout)
+{
+ struct timeval tv = {0};
+ unsigned char *b = _b;
+ fd_set rfds;
+ ssize_t red, ret = 0;
+
+ tv.tv_sec = timeout / 10;
+ tv.tv_usec = (timeout % 10) * 100000;
+
+ while (tv.tv_sec || tv.tv_usec) {
+ FD_ZERO (&rfds);
+ FD_SET (fd, &rfds);
+
+ if (select (fd + 1, &rfds, NULL, NULL, &tv) == 1) {
+
+ red = read (fd, &b[ret], 1);
+
+ if (red == 1)
+ ret++;
+
+ if (red <= 0) {
+ if (ret) return ret;
+
+ return red;
+ }
+
+ if (ret == len) return ret;
+
+ }
+ }
+
+ return ret;
+}
+
int fd_drain (int fd)
{
char c;