Skip to content

Commit b2d6e8b

Browse files
committed
Initial commit
0 parents  commit b2d6e8b

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

.editorconfig

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
trim_trailing_whitespace = true
7+
8+
[*.c]
9+
indent_style = tab
10+
indent_size = 4

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (C) 2023 Neveda
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

twc.c

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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

Comments
 (0)