X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=x5%2Fkhatus_sensor_time.c;h=84e747a5ea139c1e9be6c35b367df0697758ac1c;hb=55883d9a0ab2ca4956bda685c219557f8bd940f1;hp=f156f455d40ab4db874c3a5100418f3434ece3d2;hpb=8345f2b388e0986829ceeae50ecf186d5d540a9c;p=khatus.git diff --git a/x5/khatus_sensor_time.c b/x5/khatus_sensor_time.c index f156f45..84e747a 100644 --- a/x5/khatus_sensor_time.c +++ b/x5/khatus_sensor_time.c @@ -1,5 +1,8 @@ +#include +#include #include -#include +#include +#include #include #include #include @@ -7,37 +10,38 @@ #include #include "khatus_lib_log.h" +#include "khatus_lib_sensor.h" #include "khatus_lib_time.h" -char *argv0; +#define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);} + +#define MAX_LEN 20 +#define END_OF_MESSAGE '\n' + +char *argv0 = NULL; + +double opt_interval = 1.0; +char *opt_fmt = "%a %b %d %H:%M:%S"; +char *fifo_name = NULL; void -usage() +print_usage() { printf( - "%s: [OPT ...]\n" + "%s: [OPT ...] FIFO\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", argv0); - fatal("usage\n"); } -int -main(int argc, char **argv) +void +opt_parse(int argc, char **argv) { - argv0 = argv[0]; - - double opt_interval = 1.0; - char *opt_fmt = "%a %b %d %H:%M:%S"; - - time_t t; - struct timespec ti; - char buf[128]; char c; - memset(buf, '\0', 128); while ((c = getopt(argc, argv, "f:i:h")) != -1) switch (c) { case 'f': @@ -48,18 +52,55 @@ main(int argc, char **argv) opt_interval = atof(optarg); break; case 'h': - usage(); - break; + print_usage(); + exit(EXIT_SUCCESS); + case '?': + if (optopt == 'f' || optopt == 'i') + fprintf(stderr, + "Option -%c requires an argument.\n", + optopt); + else if (isprint(optopt)) + fprintf (stderr, + "Unknown option `-%c'.\n", + optopt); + else + fprintf(stderr, + "Unknown option character `\\x%x'.\n", + optopt); + exit(EXIT_FAILURE); default: - usage(); + assert(0); } + fifo_name = argv[optind]; + debug("fifo_name: %s\n", fifo_name); + if (!fifo_name) + usage("No filename was provided\n"); +} + +int +read_time(char *buf) +{ + time_t t; + + t = time(NULL); + strftime(buf, MAX_LEN, opt_fmt, localtime(&t)); + return strlen(buf); +} + +int +main(int argc, char **argv) +{ + argv0 = argv[0]; + + struct timespec ti; + char buf[MAX_LEN]; + + opt_parse(argc, argv); + + signal(SIGPIPE, SIG_IGN); /* Handled in loop */ + + memset(buf, '\0', MAX_LEN); ti = timespec_of_float(opt_interval); - for (;;) { - t = time(NULL); - strftime(buf, sizeof(buf), opt_fmt, localtime(&t)); - puts(buf); - fflush(stdout); - snooze(&ti); - } + loop(&ti, fifo_name, buf, read_time); return EXIT_SUCCESS; }