Implement the data expiry check
[khatus.git] / x5 / khatus_sensor_time.c
CommitLineData
707b2f79
SK
1#include <assert.h>
2#include <ctype.h>
8345f2b3 3#include <errno.h>
707b2f79
SK
4#include <fcntl.h>
5#include <signal.h>
8345f2b3
SK
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <time.h>
10#include <unistd.h>
11
12#include "khatus_lib_log.h"
13#include "khatus_lib_time.h"
14
707b2f79
SK
15#define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);}
16
17#define MAX_LEN 20
18#define END_OF_MESSAGE '\n'
19
f1eff928
SK
20char *argv0 = NULL;
21
22double opt_interval = 1.0;
23char *opt_fmt = "%a %b %d %H:%M:%S";
24char *fifo_name = NULL;
8345f2b3
SK
25
26void
707b2f79 27print_usage()
8345f2b3
SK
28{
29 printf(
707b2f79 30 "%s: [OPT ...] FIFO\n"
8345f2b3 31 "\n"
707b2f79 32 "FIFO = string # path to fifo file\n"
8345f2b3
SK
33 "OPT = -i int # interval\n"
34 " | -f string # format string\n"
35 " | -h # help message (i.e. what you're reading now :) )\n",
36 argv0);
8345f2b3
SK
37}
38
f1eff928
SK
39void
40opt_parse(int argc, char **argv)
8345f2b3 41{
8345f2b3
SK
42 char c;
43
8345f2b3
SK
44 while ((c = getopt(argc, argv, "f:i:h")) != -1)
45 switch (c) {
46 case 'f':
47 opt_fmt = calloc(strlen(optarg), sizeof(char));
48 strcpy(opt_fmt, optarg);
49 break;
50 case 'i':
51 opt_interval = atof(optarg);
52 break;
53 case 'h':
707b2f79 54 print_usage();
f1eff928 55 exit(EXIT_SUCCESS);
707b2f79
SK
56 case '?':
57 if (optopt == 'f' || optopt == 'i')
58 fprintf(stderr,
59 "Option -%c requires an argument.\n",
60 optopt);
61 else if (isprint(optopt))
62 fprintf (stderr,
63 "Unknown option `-%c'.\n",
64 optopt);
65 else
66 fprintf(stderr,
67 "Unknown option character `\\x%x'.\n",
68 optopt);
f1eff928 69 exit(EXIT_FAILURE);
8345f2b3 70 default:
707b2f79 71 assert(0);
8345f2b3 72 }
707b2f79
SK
73 fifo_name = argv[optind];
74 debug("fifo_name: %s\n", fifo_name);
75 if (!fifo_name)
76 usage("No filename was provided\n");
f1eff928
SK
77}
78
79int
80main(int argc, char **argv)
81{
82 argv0 = argv[0];
83
84 time_t t;
85 struct timespec ti;
86 char buf[MAX_LEN];
87
88 int fd = -1;
89
90 int n = 0; /* written */
91 int r = 0; /* remaining */
92 int i = 0; /* buffer position */
93
94 opt_parse(argc, argv);
95
96 signal(SIGPIPE, SIG_IGN); /* Handling manually */
97
98 memset(buf, '\0', MAX_LEN);
8345f2b3
SK
99 ti = timespec_of_float(opt_interval);
100 for (;;) {
707b2f79 101 debug("openning \"%s\"\n", fifo_name);
f1eff928
SK
102 fd = open(fifo_name, O_WRONLY);
103 if (fd < 0)
707b2f79
SK
104 fatal("Failed to open FIFO file: \"%s\". Error: %s\n",
105 fifo_name,
106 strerror(errno));
f1eff928 107 debug("openned. fd: %d\n", fd);
8345f2b3 108 t = time(NULL);
707b2f79
SK
109 strftime(buf, MAX_LEN, opt_fmt, localtime(&t));
110 r = strlen(buf);
111 buf[r] = END_OF_MESSAGE;
f1eff928 112 for (i = 0; (n = write(fd, buf + i++, 1)) && r; r--)
707b2f79
SK
113 ;
114 if (n < 0)
80e23f54
SK
115 switch (errno) {
116 case EPIPE:
117 error("Broken pipe. Msg buf: %s\n", buf);
118 break;
119 default:
120 fatal(
121 "Failed to write to %s. "
122 "Err num: %d, Err msg: %s\n",
123 fifo_name,
124 errno,
125 strerror(errno));
126 }
f1eff928 127 if (close(fd) < 0)
707b2f79
SK
128 fatal("Failed to close %s. Err num: %d, Err msg: %s\n",
129 fifo_name,
130 errno,
131 strerror(errno));
f1eff928
SK
132 fd = -1;
133 debug("closed. fd: %d\n", fd);
8345f2b3
SK
134 snooze(&ti);
135 }
136 return EXIT_SUCCESS;
137}
This page took 0.037593 seconds and 4 git commands to generate.