- stderr = stderr;
- dpy = XOpenDisplay(NULL);
-
- memset(status_string, 0, sizeof(status_string));
- for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
- argument = args[i];
- if (argument.args == NULL)
- res = argument.func();
- else
- res = argument.func(argument.args);
- element = smprintf(argument.format, res);
- if (element == NULL) {
- element = smprintf(UNKNOWN_STR);
- fprintf(stderr, "Failed to format output.\n");
+ memset(&act, 0, sizeof(act));
+ act.sa_handler = sighandler;
+ sigaction(SIGINT, &act, 0);
+ sigaction(SIGTERM, &act, 0);
+
+ if (!oflag) {
+ dpy = XOpenDisplay(NULL);
+ }
+
+ setlocale(LC_ALL, "");
+
+ while (!done) {
+ status_string[0] = '\0';
+
+ for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
+ argument = args[i];
+ if (argument.args == NULL) {
+ res = argument.func();
+ } else {
+ res = argument.func(argument.args);
+ }
+ element = smprintf(argument.fmt, res);
+ if (element == NULL) {
+ element = smprintf("%s", UNKNOWN_STR);
+ warnx("Failed to format output");
+ }
+ strncat(status_string, element, sizeof(status_string) - strlen(status_string) - 1);
+ free(res);
+ free(element);
+ }
+
+ if (oflag) {
+ printf("%s\n", status_string);
+ } else if (nflag) {
+ printf("%s\n", status_string);
+ done = 1;
+ } else {
+ XStoreName(dpy, DefaultRootWindow(dpy), status_string);
+ XSync(dpy, False);
+ }
+
+ if ((UPDATE_INTERVAL - delay) <= 0) {
+ delay = 0;
+ continue;
+ } else {
+ sleep(UPDATE_INTERVAL - delay);
+ delay = 0;