#include <stdio.h>
#include <unistd.h>
-#include <sys/epoll.h>
+#include <fcntl.h>
#include "options.h"
#include "baud.h"
+#include "debug.h"
+#include "data.h"
int main(int argc, char *argv[])
{
- struct epoll_event ev;
struct plotter_options options;
- int fd, baud, ret;
- int epoll_fd;
- char buf[256];
-
+ int fd, baud, ret = 0, out_fd = 0;
if (read_args(argc, argv, &options))
return 1;
return 1;
if (baud != options.baud_rate) {
- printf("Failed to set baudrate to %d, only got %d\n",
+ pr_err("Failed to set baudrate to %d, only got %d\n",
options.baud_rate, baud);
- close(fd);
- return 1;
+ ret = 1;
+ goto out;
}
- 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 (ret == 0)
- break;
-
- ret = write(1, buf, ret);
- if (read < 0) {
- perror("write");
- break;
+ if (options.output_path) {
+ pr_debug("Opening %s for writing the log file\n",
+ options.output_path);
+
+ out_fd = open(options.output_path,
+ O_CREAT | O_APPEND | O_WRONLY, 0664);
+ if (out_fd < 0) {
+ pr_err("Failed to open file %s for writing: %m\n",
+ options.output_path);
+ ret = 1;
+ goto out;
}
}
+ read_data(fd, out_fd);
+
+out:
close(fd);
- return 0;
+ return ret;
}