stdout-clock: add option -p for prefix
This commit is contained in:
1 parent
d0a863dcdf
commit
871b91b7c3
1 file changed
+18
-5
@@ -1,27 +1,40 @@
|
||||
#include <X11/Xlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
void show(struct tm *tm)
|
||||
void show(char *prefix, struct tm *tm)
|
||||
{
|
||||
char s[6];
|
||||
strftime(s, sizeof(s), "%H:%M", tm);
|
||||
printf("%s\n", s);
|
||||
printf("%s%s\n", prefix, s);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int h, m, init = 60;
|
||||
int main(int argc, char **argv) {
|
||||
int h, m, init = 60, opt;
|
||||
time_t t;
|
||||
struct tm *tm;
|
||||
char *prefix = "";
|
||||
|
||||
while ((opt = getopt(argc, argv, "p:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'p':
|
||||
prefix = optarg;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "Usage: %s [-p PREFIX]\n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
t = time(NULL);
|
||||
tm = localtime(&t);
|
||||
if (tm->tm_hour != h || tm->tm_min != m || init > 0) {
|
||||
show(tm);
|
||||
show(prefix, tm);
|
||||
h = tm->tm_hour;
|
||||
m = tm->tm_min;
|
||||
init = (init > 0) ? init - 1 : 0;
|
||||
|
||||
Reference in new issue
Block a user