| 1 | #include <errno.h> |
| 2 | #include <stdarg.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | #include <time.h> |
| 7 | #include <unistd.h> |
| 8 | |
| 9 | #include "khatus_lib_log.h" |
| 10 | #include "khatus_lib_time.h" |
| 11 | |
| 12 | char *argv0; |
| 13 | |
| 14 | void |
| 15 | usage() |
| 16 | { |
| 17 | printf( |
| 18 | "%s: [OPT ...]\n" |
| 19 | "\n" |
| 20 | "OPT = -i int # interval\n" |
| 21 | " | -f string # format string\n" |
| 22 | " | -h # help message (i.e. what you're reading now :) )\n", |
| 23 | argv0); |
| 24 | fatal("usage\n"); |
| 25 | } |
| 26 | |
| 27 | int |
| 28 | main(int argc, char **argv) |
| 29 | { |
| 30 | argv0 = argv[0]; |
| 31 | |
| 32 | double opt_interval = 1.0; |
| 33 | char *opt_fmt = "%a %b %d %H:%M:%S"; |
| 34 | |
| 35 | time_t t; |
| 36 | struct timespec ti; |
| 37 | char buf[128]; |
| 38 | char c; |
| 39 | |
| 40 | memset(buf, '\0', 128); |
| 41 | while ((c = getopt(argc, argv, "f:i:h")) != -1) |
| 42 | switch (c) { |
| 43 | case 'f': |
| 44 | opt_fmt = calloc(strlen(optarg), sizeof(char)); |
| 45 | strcpy(opt_fmt, optarg); |
| 46 | break; |
| 47 | case 'i': |
| 48 | opt_interval = atof(optarg); |
| 49 | break; |
| 50 | case 'h': |
| 51 | usage(); |
| 52 | break; |
| 53 | default: |
| 54 | usage(); |
| 55 | } |
| 56 | ti = timespec_of_float(opt_interval); |
| 57 | for (;;) { |
| 58 | t = time(NULL); |
| 59 | strftime(buf, sizeof(buf), opt_fmt, localtime(&t)); |
| 60 | puts(buf); |
| 61 | fflush(stdout); |
| 62 | snooze(&ti); |
| 63 | } |
| 64 | return EXIT_SUCCESS; |
| 65 | } |