Implement the data expiry check
[khatus.git] / x5 / khatus_sensor_time.c
1 #include <assert.h>
2 #include <ctype.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <signal.h>
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
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
20 char *argv0 = NULL;
21
22 double opt_interval = 1.0;
23 char *opt_fmt = "%a %b %d %H:%M:%S";
24 char *fifo_name = NULL;
25
26 void
27 print_usage()
28 {
29 printf(
30 "%s: [OPT ...] FIFO\n"
31 "\n"
32 "FIFO = string # path to fifo file\n"
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);
37 }
38
39 void
40 opt_parse(int argc, char **argv)
41 {
42 char c;
43
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':
54 print_usage();
55 exit(EXIT_SUCCESS);
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);
69 exit(EXIT_FAILURE);
70 default:
71 assert(0);
72 }
73 fifo_name = argv[optind];
74 debug("fifo_name: %s\n", fifo_name);
75 if (!fifo_name)
76 usage("No filename was provided\n");
77 }
78
79 int
80 main(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);
99 ti = timespec_of_float(opt_interval);
100 for (;;) {
101 debug("openning \"%s\"\n", fifo_name);
102 fd = open(fifo_name, O_WRONLY);
103 if (fd < 0)
104 fatal("Failed to open FIFO file: \"%s\". Error: %s\n",
105 fifo_name,
106 strerror(errno));
107 debug("openned. fd: %d\n", fd);
108 t = time(NULL);
109 strftime(buf, MAX_LEN, opt_fmt, localtime(&t));
110 r = strlen(buf);
111 buf[r] = END_OF_MESSAGE;
112 for (i = 0; (n = write(fd, buf + i++, 1)) && r; r--)
113 ;
114 if (n < 0)
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 }
127 if (close(fd) < 0)
128 fatal("Failed to close %s. Err num: %d, Err msg: %s\n",
129 fifo_name,
130 errno,
131 strerror(errno));
132 fd = -1;
133 debug("closed. fd: %d\n", fd);
134 snooze(&ti);
135 }
136 return EXIT_SUCCESS;
137 }
This page took 0.082586 seconds and 4 git commands to generate.