ad170efce4ce4c523b3de2e1a049124cb5f337aa
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'
22 double opt_interval
= 1.0;
23 char *opt_fmt
= "%a %b %d %H:%M:%S";
24 char *fifo_name
= NULL
;
30 "%s: [OPT ...] FIFO\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",
40 opt_parse(int argc
, char **argv
)
44 while ((c
= getopt(argc
, argv
, "f:i:h")) != -1)
47 opt_fmt
= calloc(strlen(optarg
), sizeof(char));
48 strcpy(opt_fmt
, optarg
);
51 opt_interval
= atof(optarg
);
57 if (optopt
== 'f' || optopt
== 'i')
59 "Option -%c requires an argument.\n",
61 else if (isprint(optopt
))
63 "Unknown option `-%c'.\n",
67 "Unknown option character `\\x%x'.\n",
73 fifo_name
= argv
[optind
];
74 debug("fifo_name: %s\n", fifo_name
);
76 usage("No filename was provided\n");
80 main(int argc
, char **argv
)
90 int n
= 0; /* written */
91 int r
= 0; /* remaining */
92 int i
= 0; /* buffer position */
94 opt_parse(argc
, argv
);
96 signal(SIGPIPE
, SIG_IGN
); /* Handling manually */
98 memset(buf
, '\0', MAX_LEN
);
99 ti
= timespec_of_float(opt_interval
);
101 debug("openning \"%s\"\n", fifo_name
);
102 fd
= open(fifo_name
, O_WRONLY
);
104 fatal("Failed to open FIFO file: \"%s\". Error: %s\n",
107 debug("openned. fd: %d\n", fd
);
109 strftime(buf
, MAX_LEN
, opt_fmt
, localtime(&t
));
111 buf
[r
] = END_OF_MESSAGE
;
112 for (i
= 0; (n
= write(fd
, buf
+ i
++, 1)) && r
; r
--)
115 fatal("Failed to write to %s. Err num: %d, Err msg: %s\n",
120 fatal("Failed to close %s. Err num: %d, Err msg: %s\n",
125 debug("closed. fd: %d\n", fd
);
This page took 0.09241 seconds and 3 git commands to generate.