]> git.itanic.dy.fi Git - log-plotter/blobdiff - main.c
main: Add missing EOF check to serial read
[log-plotter] / main.c
diff --git a/main.c b/main.c
index b4a4c8fb635b2f813e23f4e6a43a403ae12abbdd..dd6c7c090a600705da207112849957a85a221990 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1,11 +1,14 @@
 #include <stdio.h>
 #include <unistd.h>
+#include <sys/epoll.h>
 
 #include "baud.h"
 
 int main(int argc, char *argv[])
 {
+       struct epoll_event ev;
        int fd, baud, ret;
+       int epoll_fd;
        char *device;
        char buf[256];
 
@@ -27,14 +30,32 @@ int main(int argc, char *argv[])
                return 1;
        }
 
+       epoll_fd = epoll_create(1);
+
+       ev.events = EPOLLIN;
+       ev.data.fd = fd;
+       if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) {
+               perror("epoll_ctl");
+               return 1;
+       }
+
        while (1) {
+               ret = epoll_wait(epoll_fd, &ev, 1, -1);
+               if (ret == 0)
+                       continue;
+
+               if (ret < 0) {
+                       perror("epoll");
+                       return 1;
+               }
+
                ret = read(fd, buf, sizeof(buf));
                if (read < 0) {
                        perror("read");
                        break;
                }
 
-               if (read == 0)
+               if (ret == 0)
                        break;
 
                ret = write(1, buf, ret);