Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
15 const char *(*func
)();
27 terminate(const int signo
)
35 difftimespec(struct timespec
*res
, struct timespec
*a
, struct timespec
*b
)
37 res
->tv_sec
= a
->tv_sec
- b
->tv_sec
- (a
->tv_nsec
< b
->tv_nsec
);
38 res
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
+
39 (a
->tv_nsec
< b
->tv_nsec
) * 1000000000;
45 die("usage: %s [-so]", argv0
);
49 main(int argc
, char *argv
[])
52 struct timespec start
, current
, diff
, intspec
, wait
;
54 int sflag
, oflag
, ret
;
74 memset(&act
, 0, sizeof(act
));
75 act
.sa_handler
= terminate
;
76 sigaction(SIGINT
, &act
, NULL
);
77 sigaction(SIGTERM
, &act
, NULL
);
83 if (!sflag
&& !(dpy
= XOpenDisplay(NULL
))) {
84 die("XOpenDisplay: Failed to open display");
88 if (clock_gettime(CLOCK_MONOTONIC
, &start
) < 0) {
89 die("clock_gettime:");
93 for (i
= len
= 0; i
< LEN(args
); i
++) {
94 if (!(res
= args
[i
].func(args
[i
].args
))) {
97 if ((ret
= esnprintf(status
+ len
, sizeof(status
) - len
,
98 args
[i
].fmt
, res
)) < 0) {
105 printf("%s\n", status
);
107 if (XStoreName(dpy
, DefaultRootWindow(dpy
), status
) < 0) {
108 die("XStoreName: Allocation failed");
118 if (clock_gettime(CLOCK_MONOTONIC
, ¤t
) < 0) {
119 die("clock_gettime:");
121 difftimespec(&diff
, ¤t
, &start
);
123 intspec
.tv_sec
= interval
/ 1000;
124 intspec
.tv_nsec
= (interval
% 1000) * 1000000;
125 difftimespec(&wait
, &intspec
, &diff
);
127 if (wait
.tv_sec
>= 0) {
128 if (nanosleep(&wait
, NULL
) < 0 &&
137 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);
138 if (XCloseDisplay(dpy
) < 0) {
139 die("XCloseDisplay: Failed to close display");