]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - fd.c
interactive: Avoid select(2) in input_available().
[paraslash.git] / fd.c
diff --git a/fd.c b/fd.c
index 2f3ec997c11e796fc5ff8ada46c40a662e0aef13..63c0337b348e10705a92f53492d5ff4491ab4ac1 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -644,6 +644,33 @@ int para_munmap(void *start, size_t length)
        return -ERRNO_TO_PARA_ERROR(err);
 }
 
+static int xpoll(struct pollfd *fds, nfds_t nfds, int timeout)
+{
+       int ret;
+
+       do
+               ret = poll(fds, nfds, timeout);
+       while (ret < 0 && errno == EINTR);
+       return ret < 0? -ERRNO_TO_PARA_ERROR(errno) : ret;
+}
+
+/**
+ * Check a file descriptor for readability.
+ *
+ * \param fd The file descriptor.
+ *
+ * \return positive if fd is ready for reading, zero if it isn't, negative if
+ * an error occurred.
+ *
+ * \sa \ref write_ok().
+ */
+int read_ok(int fd)
+{
+       struct pollfd pfd = {.fd = fd, .events = POLLIN};
+       int ret = xpoll(&pfd, 1, 0);
+       return ret < 0? ret : pfd.revents & POLLIN;
+}
+
 /**
  * Check a file descriptor for writability.
  *
@@ -651,18 +678,14 @@ int para_munmap(void *start, size_t length)
  *
  * \return positive if fd is ready for writing, zero if it isn't, negative if
  * an error occurred.
+ *
+ * \sa \ref read_ok().
  */
 int write_ok(int fd)
 {
-       int ret;
        struct pollfd pfd = {.fd = fd, .events = POLLOUT};
-
-       do
-               ret = poll(&pfd, 1, 0);
-       while (ret < 0 && errno == EINTR);
-       if (ret < 0)
-               return -ERRNO_TO_PARA_ERROR(errno);
-       return pfd.revents & POLLOUT;
+       int ret = xpoll(&pfd, 1, 0);
+       return ret < 0? ret : pfd.revents & POLLOUT;
 }
 
 /**