X-Git-Url: https://git.xandkar.net/?p=khatus.git;a=blobdiff_plain;f=x5%2Fkhatus_sensor_time.c;h=7e16aac892452d13e8915d650d186d2e6d15e240;hp=869134c01f4bd0e39a7dd04d0608c1ab56b4030d;hb=HEAD;hpb=80e23f5474b94b36f66c0406425f2214d89eaf6e diff --git a/x5/khatus_sensor_time.c b/x5/khatus_sensor_time.c index 869134c..7e16aac 100644 --- a/x5/khatus_sensor_time.c +++ b/x5/khatus_sensor_time.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -9,8 +8,8 @@ #include #include -#include "khatus_lib_log.h" -#include "khatus_lib_time.h" +#include "khlib_log.h" +#include "khlib_time.h" #define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);} @@ -21,15 +20,13 @@ char *argv0 = NULL; double opt_interval = 1.0; char *opt_fmt = "%a %b %d %H:%M:%S"; -char *fifo_name = NULL; void print_usage() { printf( - "%s: [OPT ...] FIFO\n" + "%s: [OPT ...]\n" "\n" - "FIFO = string # path to fifo file\n" "OPT = -i int # interval\n" " | -f string # format string\n" " | -h # help message (i.e. what you're reading now :) )\n", @@ -70,10 +67,6 @@ opt_parse(int argc, char **argv) default: assert(0); } - fifo_name = argv[optind]; - debug("fifo_name: %s\n", fifo_name); - if (!fifo_name) - usage("No filename was provided\n"); } int @@ -85,53 +78,20 @@ main(int argc, char **argv) struct timespec ti; char buf[MAX_LEN]; - int fd = -1; - - int n = 0; /* written */ - int r = 0; /* remaining */ - int i = 0; /* buffer position */ - opt_parse(argc, argv); - - signal(SIGPIPE, SIG_IGN); /* Handling manually */ - + ti = khlib_timespec_of_float(opt_interval); + khlib_debug("opt_fmt: \"%s\"\n", opt_fmt); + khlib_debug("opt_interval: %f\n", opt_interval); + khlib_debug("ti: {tv_sec = %ld, tv_nsec = %ld}\n",ti.tv_sec,ti.tv_nsec); memset(buf, '\0', MAX_LEN); - ti = timespec_of_float(opt_interval); + signal(SIGPIPE, SIG_IGN); + for (;;) { - debug("openning \"%s\"\n", fifo_name); - fd = open(fifo_name, O_WRONLY); - if (fd < 0) - fatal("Failed to open FIFO file: \"%s\". Error: %s\n", - fifo_name, - strerror(errno)); - debug("openned. fd: %d\n", fd); t = time(NULL); strftime(buf, MAX_LEN, opt_fmt, localtime(&t)); - r = strlen(buf); - buf[r] = END_OF_MESSAGE; - for (i = 0; (n = write(fd, buf + i++, 1)) && r; r--) - ; - if (n < 0) - switch (errno) { - case EPIPE: - error("Broken pipe. Msg buf: %s\n", buf); - break; - default: - fatal( - "Failed to write to %s. " - "Err num: %d, Err msg: %s\n", - fifo_name, - errno, - strerror(errno)); - } - if (close(fd) < 0) - fatal("Failed to close %s. Err num: %d, Err msg: %s\n", - fifo_name, - errno, - strerror(errno)); - fd = -1; - debug("closed. fd: %d\n", fd); - snooze(&ti); + puts(buf); + fflush(stdout); + khlib_sleep(&ti); } return EXIT_SUCCESS; }