forked from Neved4/twc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwc.c
169 lines (134 loc) · 3.1 KB
/
twc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <time.h>
#include <unistd.h>
#define STR_LENGTH 32
enum {
TIMEFMT_ISO, TIMEFMT_HUMAN, NTIMEFMT
};
const char *time_fmts[NTIMEFMT] = {
"%FT%T%z", "%F %T %z"
};
const char *conf = "twc/tz.conf";
static void usage(const char *s) {
fprintf(stderr,
"usage: %s [-h] [-f path] [-s format] [-t timezone] ...\n\n"
"Options:\n"
" -f path\n"
" Read config from path (default \"$HOME/.config/twc/tz.conf\")\n"
" -h Print in human-readable format\n"
" -s format\n"
" Set desired time format (e.g. \"%%Y-%%m-%%d\")\n"
" -t timezone\n"
" Set a specific timezone (e.g. \"Asia/Tokyo\")\n", s);
exit(EXIT_FAILURE);
}
static void err(const char *msg) {
fprintf(stderr, "error: %s\n", msg);
exit(EXIT_FAILURE);
}
static char *confpath(char *fpath) {
const char *env;
if (!fpath) {
char *npath = NULL;
if ((env = getenv("XDG_CONFIG_HOME"))) {
asprintf(&npath, "%s/%s", env, conf);
} else if ((env = getenv("HOME"))) {
asprintf(&npath, "%s/.config/%s", env, conf);
} else {
err("could not get value of $HOME or $XDG_CONFIG_HOME");
}
return npath;
}
return fpath;
}
static size_t calcwidth(FILE *fp) {
size_t max_width = 0;
char *line = NULL;
size_t len = 0;
while (getline(&line, &len, fp) != -1) {
if (*line == '#' || *line == '\n')
continue;
char *nl = strchr(line, '\n');
if (nl)
*nl = '\0';
size_t width = strlen(line);
if (width > max_width)
max_width = width;
}
free(line);
return max_width + 1;
}
static void tztime(const char *tz, const char *fmt, time_t t, size_t max_width) {
char timestr[STR_LENGTH];
setenv("TZ", tz, 1);
if (strftime(timestr, sizeof(timestr), fmt, localtime(&t)) == 0) {
fprintf(stderr, "error: failed to format time for timezone: %s\n", tz);
return;
}
printf("%-*s %s\n", (int)max_width, tz, timestr);
}
static void parsetz(const char *fmt, const char *s, const char *fpath) {
FILE *fp = fopen(fpath, "r");
if (!fp) {
time_t t;
time(&t);
tztime("UTC", s, t, 0);
return;
}
size_t max_width = calcwidth(fp);
rewind(fp);
char *line = NULL;
size_t len = 0;
time_t t;
time(&t);
while (getline(&line, &len, fp) != -1) {
if (*line == '#' || *line == '\n')
continue;
char *nl = strchr(line, '\n');
if (nl) *nl = '\0';
tztime(line, s, t, max_width);
}
free(line);
fclose(fp);
}
static void fparse(const char *s, char *fpath, const char *fmt) {
if (fmt) {
time_t t;
time(&t);
tztime(fmt, s, t, 0);
return;
}
fpath = confpath(fpath);
parsetz(fmt, s, fpath);
}
int main(int argc, char **argv) {
int opt;
char *fpath = NULL;
const char *progname = basename(argv[0]),
*timefmt = time_fmts[TIMEFMT_ISO], *fmt = NULL,
*tz_env = getenv("TZ");
if (tz_env)
fmt = tz_env;
while ((opt = getopt(argc, argv, "hf:s:t:")) != -1) {
switch (opt) {
case 'h':
timefmt = time_fmts[TIMEFMT_HUMAN];
break;
case 'f':
fpath = optarg;
break;
case 's':
timefmt = optarg;
break;
case 't':
fmt = optarg;
break;
default:
usage(progname);
}
}
fparse(timefmt, fpath, fmt);
}