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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
|
/* See LICENSE file for copyright and license details. */
#include <sys/ioctl.h>
#include <sys/timerfd.h>
#include <errno.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include "arg.h"
#define DX 16
#define DY 12
#define DC 16
#include "mongo_0.h"
#include "mongo_1.h"
#include "mongo_2.h"
#include "mongo_3.h"
#include "mongo_4.h"
#include "mongo_5.h"
#include "mongo_6.h"
#include "mongo_7.h"
#include "mongo_8.h"
#include "mongo_9.h"
#include "mongo_c.h"
static const char **mongo_ds[] = {
mongo_0, mongo_1, mongo_2, mongo_3, mongo_4,
mongo_5, mongo_6, mongo_7, mongo_8, mongo_9
};
static volatile sig_atomic_t caught_sigterm = 0;
static volatile sig_atomic_t caught_sigwinch = 1;
char *argv0;
static void
usage(void)
{
fprintf(stderr, "usage: %s\n", argv0);
exit(1);
}
static void
sigterm(int signo)
{
caught_sigterm = 1;
(void) signo;
}
static void
sigwinch(int signo)
{
caught_sigwinch = 1;
(void) signo;
}
static void
print_time(const char ***str, size_t y, size_t x)
{
size_t r, c;
fprintf(stdout, "\033[%zu;1H\033[1J", y + 1);
for (r = 0; r < DY; r++) {
fprintf(stdout, "\033[%zu;%zuH\033[1K", y + r + 1, x + 1);
for (c = 0; str[c]; c++)
fprintf(stdout, "%s", str[c][r]);
fprintf(stdout, "\033[0K");
}
fprintf(stdout, "\033[0J");
fflush(stdout);
}
int
main(int argc, char *argv[])
{
time_t now_;
struct tm *now;
const char **digits[9];
size_t x = 0, y = 0;
struct winsize winsize;
int small = 0;
int fd = -1;
struct itimerspec itimerspec;
uint64_t _overrun;
ARGBEGIN {
default:
usage();
} ARGEND;
if (argc)
usage();
fprintf(stdout, "\033[?1049h\033[?25l");
if (clock_gettime(CLOCK_REALTIME, &itimerspec.it_value))
goto fail;
itimerspec.it_interval.tv_sec = 1;
itimerspec.it_interval.tv_nsec = 0;
itimerspec.it_value.tv_sec += 1;
itimerspec.it_value.tv_nsec = 0;
fd = timerfd_create(CLOCK_REALTIME, 0);
if (fd < 0)
goto fail;
if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &itimerspec, NULL))
goto fail;
signal(SIGTERM, sigterm);
signal(SIGQUIT, sigterm);
signal(SIGINT, sigterm);
signal(SIGWINCH, sigwinch);
siginterrupt(SIGTERM, 1);
siginterrupt(SIGQUIT, 1);
siginterrupt(SIGINT, 1);
siginterrupt(SIGWINCH, 1);
while (!caught_sigterm) {
if (caught_sigwinch) {
if (ioctl(STDOUT_FILENO, (unsigned long)TIOCGWINSZ, &winsize) < 0) {
if (errno == EINTR)
continue;
goto fail;
}
caught_sigwinch = 0;
y = winsize.ws_row;
x = winsize.ws_col;
if (y < DY) small = 2;
else if (x < 4 * DX + DC) small = 2;
else if (x < 6 * DX + 2 * DC) small = 1;
else small = 0;
y -= DY;
x -= 4 * DX + DC;
if (!small)
x -= 2 * DX + DC;
y /= 2;
x /= 2;
}
if (small == 2) {
fprintf(stdout, "\033[H\033[2J%s\n", "Screen is too small");
fflush(stdout);
pause();
continue;
}
now_ = time(NULL);
if (now_ == -1)
goto fail;
now = localtime(&now_);
if (now == NULL)
goto fail;
digits[0] = mongo_ds[now->tm_hour / 10];
digits[1] = mongo_ds[now->tm_hour % 10];
digits[2] = mongo_c;
digits[3] = mongo_ds[now->tm_min / 10];
digits[4] = mongo_ds[now->tm_min % 10];
digits[5] = small ? NULL : mongo_c;
digits[6] = mongo_ds[now->tm_sec / 10];
digits[7] = mongo_ds[now->tm_sec % 10];
digits[8] = NULL;
print_time(digits, y, x);
if (read(fd, &_overrun, sizeof(_overrun)) < 0)
if (errno != EINTR)
goto fail;
}
fprintf(stdout, "\033[?25h\n\033[?1049l");
fflush(stdout);
close(fd);
return 0;
fail:
perror(argc ? *argv : "mongoclock");
fprintf(stdout, "\033[?25h\n\033[?1049l");
fflush(stdout);
if (fd >= 0)
close(fd);
return 1;
}
|