Xinqi Bao's Git
9bd9e81188e51626647ca6aff0b6ea9a4c3bc52e
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 if (printf("%s\n", status
) < 0) {
109 if (XStoreName(dpy
, DefaultRootWindow(dpy
), status
) < 0) {
110 die("XStoreName: Allocation failed");
120 if (clock_gettime(CLOCK_MONOTONIC
, ¤t
) < 0) {
121 die("clock_gettime:");
123 difftimespec(&diff
, ¤t
, &start
);
125 intspec
.tv_sec
= interval
/ 1000;
126 intspec
.tv_nsec
= (interval
% 1000) * 1000000;
127 difftimespec(&wait
, &intspec
, &diff
);
129 if (wait
.tv_sec
>= 0) {
130 if (nanosleep(&wait
, NULL
) < 0 &&
139 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);
140 if (XCloseDisplay(dpy
) < 0) {
141 die("XCloseDisplay: Failed to close display");