Xinqi Bao's Git
7ff323fc74053c397028efc81ff91eaaa18b88e0
1 /* See LICENSE file for copyright and license details. */
15 const char *(*func
)();
28 terminate(const int signo
)
36 difftimespec(struct timespec
*res
, struct timespec
*a
, struct timespec
*b
)
38 res
->tv_sec
= a
->tv_sec
- b
->tv_sec
- (a
->tv_nsec
< b
->tv_nsec
);
39 res
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
+
40 (a
->tv_nsec
< b
->tv_nsec
) * 1000000000;
46 fprintf(stderr
, "usage: %s [-s]\n", argv0
);
51 main(int argc
, char *argv
[])
54 struct timespec start
, current
, diff
, intspec
, wait
;
73 memset(&act
, 0, sizeof(act
));
74 act
.sa_handler
= terminate
;
75 sigaction(SIGINT
, &act
, NULL
);
76 sigaction(SIGTERM
, &act
, NULL
);
82 if (!sflag
&& !(dpy
= XOpenDisplay(NULL
))) {
83 fprintf(stderr
, "XOpenDisplay: Failed to open display\n");
88 if (clock_gettime(CLOCK_MONOTONIC
, &start
) < 0) {
89 fprintf(stderr
, "clock_gettime: %s\n", strerror(errno
));
94 for (i
= len
= 0; i
< LEN(args
); i
++) {
95 if (!(res
= args
[i
].func(args
[i
].args
))) {
98 if ((ret
= snprintf(status
+ len
, sizeof(status
) - len
,
99 args
[i
].fmt
, res
)) < 0) {
100 fprintf(stderr
, "snprintf: %s\n",
103 } else if ((size_t)ret
>= sizeof(status
) - len
) {
104 fprintf(stderr
, "snprintf: Output truncated\n");
111 printf("%s\n", status
);
113 if (XStoreName(dpy
, DefaultRootWindow(dpy
), status
) < 0) {
115 "XStoreName: Allocation failed\n");
122 if (clock_gettime(CLOCK_MONOTONIC
, ¤t
) < 0) {
123 fprintf(stderr
, "clock_gettime: %s\n",
127 difftimespec(&diff
, ¤t
, &start
);
129 intspec
.tv_sec
= interval
/ 1000;
130 intspec
.tv_nsec
= (interval
% 1000) * 1000000;
131 difftimespec(&wait
, &intspec
, &diff
);
133 if (wait
.tv_sec
>= 0) {
134 if (nanosleep(&wait
, NULL
) < 0 &&
136 fprintf(stderr
, "nanosleep: %s\n",
145 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);
146 if (XCloseDisplay(dpy
) < 0) {
148 "XCloseDisplay: Failed to close display\n");