Xinqi Bao's Git
35bdcd6372002008f8731dde2bc5364d7cdc327b
1 /* See LICENSE file for copyright and license details. */
16 const char *(*func
)();
29 terminate(const int signo
)
37 difftimespec(struct timespec
*res
, struct timespec
*a
, struct timespec
*b
)
39 res
->tv_sec
= a
->tv_sec
- b
->tv_sec
- (a
->tv_nsec
< b
->tv_nsec
);
40 res
->tv_nsec
= a
->tv_nsec
- b
->tv_nsec
+
41 (a
->tv_nsec
< b
->tv_nsec
) * 1000000000;
47 fprintf(stderr
, "usage: %s [-s]\n", argv0
);
52 main(int argc
, char *argv
[])
55 struct timespec start
, current
, diff
, intspec
, wait
;
73 setlocale(LC_ALL
, "");
75 memset(&act
, 0, sizeof(act
));
76 act
.sa_handler
= terminate
;
77 sigaction(SIGINT
, &act
, NULL
);
78 sigaction(SIGTERM
, &act
, NULL
);
80 if (!sflag
&& !(dpy
= XOpenDisplay(NULL
))) {
81 fprintf(stderr
, "Cannot open display");
86 clock_gettime(CLOCK_MONOTONIC
, &start
);
89 for (i
= len
= 0; i
< LEN(args
); i
++) {
90 const char * res
= args
[i
].func(args
[i
].args
);
91 res
= (res
== NULL
) ? unknown_str
: res
;
92 if ((ret
= snprintf(status
+ len
, sizeof(status
) - len
,
93 args
[i
].fmt
, res
)) < 0) {
94 fprintf(stderr
, "snprintf: %s\n",
97 } else if ((size_t)ret
>= sizeof(status
) - len
) {
98 fprintf(stderr
, "snprintf: Output truncated\n");
105 printf("%s\n", status
);
108 XStoreName(dpy
, DefaultRootWindow(dpy
), status
);
113 clock_gettime(CLOCK_MONOTONIC
, ¤t
);
114 difftimespec(&diff
, ¤t
, &start
);
116 intspec
.tv_sec
= interval
/ 1000;
117 intspec
.tv_nsec
= (interval
% 1000) * 1000000;
118 difftimespec(&wait
, &intspec
, &diff
);
120 if (wait
.tv_sec
>= 0) {
121 nanosleep(&wait
, NULL
);
127 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);