|
| 1 | +#define _POSIX_C_SOURCE 200809L |
| 2 | +#define TIMEFMT_DEFAULT TIMEFMT_ISO |
| 3 | +#define STR_LENGTH 32 |
| 4 | + |
| 5 | +#include <libgen.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <stdlib.h> |
| 8 | +#include <string.h> |
| 9 | +#include <time.h> |
| 10 | +#include <unistd.h> |
| 11 | + |
| 12 | +enum { |
| 13 | + TIMEFMT_ISO, |
| 14 | + TIMEFMT_HUMAN, |
| 15 | + NTIMEFMT |
| 16 | +}; |
| 17 | + |
| 18 | +const char *time_fmts[NTIMEFMT] = { |
| 19 | + "%FT%T%z", |
| 20 | + "%F %T %z" |
| 21 | +}; |
| 22 | + |
| 23 | +static void usage(const char *s) { |
| 24 | + fprintf(stderr, "usage: %s [-h] ...\n", s); |
| 25 | + exit(EXIT_FAILURE); |
| 26 | +} |
| 27 | + |
| 28 | +static void err_env(void) { |
| 29 | + fprintf(stderr, "error: could not get the value of the $HOME variable"); |
| 30 | + exit(EXIT_FAILURE); |
| 31 | +} |
| 32 | + |
| 33 | +static void err_file(const char *s) { |
| 34 | + fprintf(stderr, "error: configuration file not found in %s\n", s); |
| 35 | + exit(EXIT_FAILURE); |
| 36 | +} |
| 37 | + |
| 38 | +static void fparse(const char *s) { |
| 39 | + int hc = 0; |
| 40 | + char timestr[STR_LENGTH]; |
| 41 | + char *nl = NULL; |
| 42 | + char *sep = NULL; |
| 43 | + char *line = NULL; |
| 44 | + char *path = NULL; |
| 45 | + char *conf = "/.config"; |
| 46 | + char *home = getenv("XDG_CONFIG_HOME"); |
| 47 | + char *file = "/twc/tz.conf"; |
| 48 | + FILE *fp; |
| 49 | + |
| 50 | + if (home == NULL || *home == '\0') { |
| 51 | + hc = 1; |
| 52 | + home = getenv("HOME"); |
| 53 | + |
| 54 | + if (home == NULL || *home == '\0') { |
| 55 | + err_env(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + size_t sizet = strlen(home) + strlen(file); |
| 60 | + path = (char *) malloc(sizet); |
| 61 | + strcpy(path, home); |
| 62 | + |
| 63 | + if (hc == 1) { |
| 64 | + sizet += strlen(conf); |
| 65 | + path = (char *) realloc(path, sizet); |
| 66 | + strcat(path, conf); |
| 67 | + } |
| 68 | + |
| 69 | + strcat(path, file); |
| 70 | + fp = fopen(path, "r"); |
| 71 | + |
| 72 | + if (fp == NULL) { |
| 73 | + err_file(path); |
| 74 | + } |
| 75 | + |
| 76 | + size_t len = 0; |
| 77 | + ssize_t(read); |
| 78 | + time_t t; |
| 79 | + time(&t); |
| 80 | + |
| 81 | + while ((read = getline(&line, &len, fp)) != -1) { |
| 82 | + if (*line == '#' || *line == '\n') { |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + nl = strchr(line, '\n'); |
| 87 | + |
| 88 | + if (nl) { |
| 89 | + *nl = '\0'; |
| 90 | + } |
| 91 | + |
| 92 | + setenv("TZ", line, 1); |
| 93 | + strftime(timestr, sizeof(timestr), s, localtime(&t)); |
| 94 | + |
| 95 | + while ((sep = strchr(line, '_'))) { |
| 96 | + *sep = ' '; |
| 97 | + } |
| 98 | + |
| 99 | + printf("%-24s%s\n", line, timestr); |
| 100 | + } |
| 101 | + |
| 102 | + free(line); |
| 103 | + free(path); |
| 104 | + fclose(fp); |
| 105 | +} |
| 106 | + |
| 107 | +int main(int argc, char **argv) { |
| 108 | + int opt; |
| 109 | + const char *progname = basename(argv[0]); |
| 110 | + const char *timefmt = time_fmts[TIMEFMT_DEFAULT]; |
| 111 | + opterr = 0; |
| 112 | + |
| 113 | + while ((opt = getopt(argc, argv, "h")) != -1) { |
| 114 | + switch (opt) { |
| 115 | + case 'h': |
| 116 | + timefmt = time_fmts[TIMEFMT_HUMAN]; |
| 117 | + break; |
| 118 | + case '?': |
| 119 | + fprintf(stderr, "%s: invalid option -%c\n", progname, optopt); |
| 120 | + break; |
| 121 | + default: |
| 122 | + usage(progname); |
| 123 | + return(1); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + fparse(timefmt); |
| 128 | + return(0); |
| 129 | +} |
0 commit comments