12 #include "khatus_lib_log.h"
13 #include "khatus_lib_time.h"
15 #define usage(...) {print_usage(); fprintf(stderr, "Error:\n " __VA_ARGS__); exit(EXIT_FAILURE);}
18 #define END_OF_MESSAGE '\n'
26 "%s: [OPT ...] FIFO\n"
28 "FIFO = string # path to fifo file\n"
29 "OPT = -i int # interval\n"
30 " | -f string # format string\n"
31 " | -h # help message (i.e. what you're reading now :) )\n",
36 main(int argc
, char **argv
)
40 double opt_interval
= 1.0;
41 char *opt_fmt
= "%a %b %d %H:%M:%S";
51 int n
= 0; /* written */
52 int r
= 0; /* remaining */
53 int i
= 0; /* buffer position */
55 signal(SIGPIPE
, SIG_IGN
); /* Handling manually */
57 memset(buf
, '\0', MAX_LEN
);
58 while ((c
= getopt(argc
, argv
, "f:i:h")) != -1)
61 opt_fmt
= calloc(strlen(optarg
), sizeof(char));
62 strcpy(opt_fmt
, optarg
);
65 opt_interval
= atof(optarg
);
71 if (optopt
== 'f' || optopt
== 'i')
73 "Option -%c requires an argument.\n",
75 else if (isprint(optopt
))
77 "Unknown option `-%c'.\n",
81 "Unknown option character `\\x%x'.\n",
87 fifo_name
= argv
[optind
];
88 debug("fifo_name: %s\n", fifo_name
);
90 usage("No filename was provided\n");
91 ti
= timespec_of_float(opt_interval
);
93 debug("openning \"%s\"\n", fifo_name
);
94 fifo_fd
= open(fifo_name
, O_WRONLY
);
96 fatal("Failed to open FIFO file: \"%s\". Error: %s\n",
99 debug("openned. fd: %d\n", fifo_fd
);
101 strftime(buf
, MAX_LEN
, opt_fmt
, localtime(&t
));
103 buf
[r
] = END_OF_MESSAGE
;
104 for (i
= 0; (n
= write(fifo_fd
, buf
+ i
++, 1)) && r
; r
--)
107 fatal("Failed to write to %s. Err num: %d, Err msg: %s\n",
111 if (close(fifo_fd
) < 0)
112 fatal("Failed to close %s. Err num: %d, Err msg: %s\n",
117 debug("closed. fd: %d\n", fifo_fd
);
This page took 0.062604 seconds and 4 git commands to generate.