Commit | Line | Data |
---|---|---|
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 | ||
8345f2b3 SK |
20 | char *argv0; |
21 | ||
22 | void | |
707b2f79 | 23 | print_usage() |
8345f2b3 SK |
24 | { |
25 | printf( | |
707b2f79 | 26 | "%s: [OPT ...] FIFO\n" |
8345f2b3 | 27 | "\n" |
707b2f79 | 28 | "FIFO = string # path to fifo file\n" |
8345f2b3 SK |
29 | "OPT = -i int # interval\n" |
30 | " | -f string # format string\n" | |
31 | " | -h # help message (i.e. what you're reading now :) )\n", | |
32 | argv0); | |
8345f2b3 SK |
33 | } |
34 | ||
35 | int | |
36 | main(int argc, char **argv) | |
37 | { | |
38 | argv0 = argv[0]; | |
39 | ||
40 | double opt_interval = 1.0; | |
41 | char *opt_fmt = "%a %b %d %H:%M:%S"; | |
42 | ||
43 | time_t t; | |
44 | struct timespec ti; | |
707b2f79 | 45 | char buf[MAX_LEN]; |
8345f2b3 SK |
46 | char c; |
47 | ||
707b2f79 SK |
48 | char *fifo_name; |
49 | int fifo_fd = -1; | |
50 | ||
51 | int n = 0; /* written */ | |
52 | int r = 0; /* remaining */ | |
53 | int i = 0; /* buffer position */ | |
54 | ||
55 | signal(SIGPIPE, SIG_IGN); /* Handling manually */ | |
56 | ||
57 | memset(buf, '\0', MAX_LEN); | |
8345f2b3 SK |
58 | while ((c = getopt(argc, argv, "f:i:h")) != -1) |
59 | switch (c) { | |
60 | case 'f': | |
61 | opt_fmt = calloc(strlen(optarg), sizeof(char)); | |
62 | strcpy(opt_fmt, optarg); | |
63 | break; | |
64 | case 'i': | |
65 | opt_interval = atof(optarg); | |
66 | break; | |
67 | case 'h': | |
707b2f79 SK |
68 | print_usage(); |
69 | return 0; | |
70 | case '?': | |
71 | if (optopt == 'f' || optopt == 'i') | |
72 | fprintf(stderr, | |
73 | "Option -%c requires an argument.\n", | |
74 | optopt); | |
75 | else if (isprint(optopt)) | |
76 | fprintf (stderr, | |
77 | "Unknown option `-%c'.\n", | |
78 | optopt); | |
79 | else | |
80 | fprintf(stderr, | |
81 | "Unknown option character `\\x%x'.\n", | |
82 | optopt); | |
83 | return 1; | |
8345f2b3 | 84 | default: |
707b2f79 | 85 | assert(0); |
8345f2b3 | 86 | } |
707b2f79 SK |
87 | fifo_name = argv[optind]; |
88 | debug("fifo_name: %s\n", fifo_name); | |
89 | if (!fifo_name) | |
90 | usage("No filename was provided\n"); | |
8345f2b3 SK |
91 | ti = timespec_of_float(opt_interval); |
92 | for (;;) { | |
707b2f79 SK |
93 | debug("openning \"%s\"\n", fifo_name); |
94 | fifo_fd = open(fifo_name, O_WRONLY); | |
95 | if (fifo_fd < 0) | |
96 | fatal("Failed to open FIFO file: \"%s\". Error: %s\n", | |
97 | fifo_name, | |
98 | strerror(errno)); | |
99 | debug("openned. fd: %d\n", fifo_fd); | |
8345f2b3 | 100 | t = time(NULL); |
707b2f79 SK |
101 | strftime(buf, MAX_LEN, opt_fmt, localtime(&t)); |
102 | r = strlen(buf); | |
103 | buf[r] = END_OF_MESSAGE; | |
104 | for (i = 0; (n = write(fifo_fd, buf + i++, 1)) && r; r--) | |
105 | ; | |
106 | if (n < 0) | |
107 | fatal("Failed to write to %s. Err num: %d, Err msg: %s\n", | |
108 | fifo_name, | |
109 | errno, | |
110 | strerror(errno)); | |
111 | if (close(fifo_fd) < 0) | |
112 | fatal("Failed to close %s. Err num: %d, Err msg: %s\n", | |
113 | fifo_name, | |
114 | errno, | |
115 | strerror(errno)); | |
116 | fifo_fd = -1; | |
117 | debug("closed. fd: %d\n", fifo_fd); | |
8345f2b3 SK |
118 | snooze(&ti); |
119 | } | |
120 | return EXIT_SUCCESS; | |
121 | } |