Shift X2 status from legacy to archived
[khatus.git] / x5 / khatus_sensor_time.c
index 983ebdf..7e16aac 100644 (file)
@@ -1,7 +1,6 @@
 #include <assert.h>
 #include <ctype.h>
 #include <errno.h>
-#include <fcntl.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -9,52 +8,36 @@
 #include <time.h>
 #include <unistd.h>
 
-#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);}
 
 #define MAX_LEN 20
 #define END_OF_MESSAGE '\n'
 
-char *argv0;
+char *argv0 = NULL;
+
+double opt_interval = 1.0;
+char *opt_fmt = "%a %b %d %H:%M:%S";
 
 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",
            argv0);
 }
 
-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[MAX_LEN];
        char c;
 
-       char *fifo_name;
-       int   fifo_fd = -1;
-
-       int n = 0;  /* written */
-       int r = 0;  /* remaining */
-       int i = 0;  /* buffer position */
-
-       signal(SIGPIPE, SIG_IGN);  /* Handling manually */
-
-       memset(buf, '\0', MAX_LEN);
        while ((c = getopt(argc, argv, "f:i:h")) != -1)
                switch (c) {
                case 'f':
@@ -66,7 +49,7 @@ main(int argc, char **argv)
                        break;
                case 'h':
                        print_usage();
-                       return 0;
+                       exit(EXIT_SUCCESS);
                case '?':
                        if (optopt == 'f' || optopt == 'i')
                                fprintf(stderr,
@@ -80,42 +63,35 @@ main(int argc, char **argv)
                                fprintf(stderr,
                                        "Unknown option character `\\x%x'.\n",
                                        optopt);
-                       return 1;
+                       exit(EXIT_FAILURE);
                default:
                        assert(0);
                }
-       fifo_name = argv[optind];
-       debug("fifo_name: %s\n", fifo_name);
-       if (!fifo_name)
-               usage("No filename was provided\n");
-       ti = timespec_of_float(opt_interval);
+}
+
+int
+main(int argc, char **argv)
+{
+       argv0 = argv[0];
+
+       time_t t;
+       struct timespec ti;
+       char buf[MAX_LEN];
+
+       opt_parse(argc, argv);
+       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);
+       signal(SIGPIPE, SIG_IGN);
+
        for (;;) {
-               debug("openning \"%s\"\n", fifo_name);
-               fifo_fd = open(fifo_name, O_WRONLY);
-               if (fifo_fd < 0)
-                       fatal("Failed to open FIFO file: \"%s\". Error: %s\n",
-                           fifo_name,
-                           strerror(errno));
-               debug("openned. fd: %d\n", fifo_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(fifo_fd, buf + i++, 1)) && r; r--)
-                       ;
-               if (n < 0)
-                       fatal("Failed to write to %s. Err num: %d, Err msg: %s\n",
-                           fifo_name,
-                           errno,
-                           strerror(errno));
-               if (close(fifo_fd) < 0)
-                       fatal("Failed to close %s. Err num: %d, Err msg: %s\n",
-                           fifo_name,
-                           errno,
-                           strerror(errno));
-               fifo_fd = -1;
-               debug("closed. fd: %d\n", fifo_fd);
-               snooze(&ti);
+               puts(buf);
+               fflush(stdout);
+               khlib_sleep(&ti);
        }
        return EXIT_SUCCESS;
 }
This page took 0.031724 seconds and 4 git commands to generate.