From 9636425fa0a941f6f92e906b0e578b9869b3d043 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 6 Oct 2013 18:26:14 +0300 Subject: [PATCH] main: Open ouput file when one is requested Write the read data into a given logfile and not only into stdout. Signed-off-by: Timo Kokkonen --- main.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/main.c b/main.c index 065d9dc..9ac44f9 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,9 @@ #include #include +#include #include +#include +#include #include "options.h" #include "baud.h" @@ -10,7 +13,7 @@ int main(int argc, char *argv[]) { struct epoll_event ev; struct plotter_options options; - int fd, baud, ret; + int fd, baud, ret, out_fd = STDOUT_FILENO; int epoll_fd; char buf[256]; @@ -26,8 +29,22 @@ int main(int argc, char *argv[]) if (baud != options.baud_rate) { pr_err("Failed to set baudrate to %d, only got %d\n", options.baud_rate, baud); - close(fd); - return 1; + ret = 1; + goto out; + } + + 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; + } } epoll_fd = epoll_create(1); @@ -67,6 +84,7 @@ int main(int argc, char *argv[]) } } +out: close(fd); - return 0; + return ret; } -- 2.44.0