commit dcbd58ec1f3bae1d5bbeccbdb309853564f3392b
parent 7190d24468f632afebb9b24a170c42261eaf5f8a
Author: Louis Burda <quent.burda@gmail.com>
Date: Sat, 9 Dec 2023 14:59:11 +0100
Switch from select() to poll()
Diffstat:
M | tquery.c | | | 35 | +++++++++++++++++------------------ |
1 file changed, 17 insertions(+), 18 deletions(-)
diff --git a/tquery.c b/tquery.c
@@ -1,7 +1,7 @@
#include <curses.h>
#include <sys/wait.h>
-#include <sys/select.h>
+#include <sys/poll.h>
#include <limits.h>
#include <signal.h>
#include <dirent.h>
@@ -472,8 +472,8 @@ parse(int argc, const char **argv)
int
main(int argc, const char **argv)
{
- int maxfd, rc;
- fd_set set;
+ struct pollfd fds[2];
+ int rc;
parse(argc, argv);
@@ -490,8 +490,6 @@ main(int argc, const char **argv)
timeout(0);
- update();
-
spawn(child_argv, "");
inputbuf = malloc(INPUT_BUFSIZ);
@@ -501,24 +499,25 @@ main(int argc, const char **argv)
if (!loadbuf) die("malloc loadbuf:");
inputlen = 0;
+ inputbuf[inputlen] = '\0';
+
+ update();
+
while (1) {
- maxfd = 0;
- FD_ZERO(&set);
- FD_SET(0, &set);
- if (child_pid) {
- FD_SET(child_fd, &set);
- if (child_fd > maxfd)
- maxfd = child_fd;
- }
- rc = select(maxfd+1, &set, NULL, NULL, NULL);
- if (rc <= 0 && errno == EINTR) {
+ fds[0].fd = 0;
+ fds[0].events = POLLIN;
+ fds[1].fd = child_fd;
+ fds[1].events = POLLIN;
+
+ rc = poll(fds, child_pid ? 2 : 1, -1);
+ if (rc < 0 && errno == EINTR) {
update();
continue;
- } else if (rc <= 0) die("select:");
+ } else if (rc < 0) die("select:");
- if (FD_ISSET(0, &set)) {
+ if (fds[0].revents & POLLIN) {
input();
- } else if (FD_ISSET(child_fd, &set)) {
+ } else if (fds[1].revents & POLLIN) {
load();
update();
}