Xinqi Bao's Git

Makefile fix and vol_perc fix for mute
[slstatus.git] / slstatus.c
1 /* See LICENSE file for copyright and license details. */
2
3 #include <err.h>
4 #include <fcntl.h>
5 #include <ifaddrs.h>
6 #include <limits.h>
7 #include <linux/wireless.h>
8 #include <locale.h>
9 #include <netdb.h>
10 #include <pwd.h>
11 #include <signal.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/ioctl.h>
17 #include <sys/stat.h>
18 #include <sys/statvfs.h>
19 #include <sys/socket.h>
20 #include <sys/soundcard.h>
21 #include <sys/sysinfo.h>
22 #include <sys/types.h>
23 #include <sys/utsname.h>
24 #include <time.h>
25 #include <unistd.h>
26 #include <X11/Xlib.h>
27
28 #include "arg.h"
29
30 struct arg {
31 char *(*func)();
32 const char *fmt;
33 const char *args;
34 };
35
36 static char *smprintf(const char *fmt, ...);
37 static char *battery_perc(const char *bat);
38 static char *battery_state(const char *bat);
39 static char *cpu_perc(void);
40 static char *datetime(const char *fmt);
41 static char *disk_free(const char *mnt);
42 static char *disk_perc(const char *mnt);
43 static char *disk_total(const char *mnt);
44 static char *disk_used(const char *mnt);
45 static char *entropy(void);
46 static char *gid(void);
47 static char *hostname(void);
48 static char *ip(const char *iface);
49 static char *kernel_release(void);
50 static char *load_avg(void);
51 static char *ram_free(void);
52 static char *ram_perc(void);
53 static char *ram_used(void);
54 static char *ram_total(void);
55 static char *run_command(const char *cmd);
56 static char *swap_free(void);
57 static char *swap_perc(void);
58 static char *swap_used(void);
59 static char *swap_total(void);
60 static char *temp(const char *file);
61 static char *uid(void);
62 static char *uptime(void);
63 static char *username(void);
64 static char *vol_perc(const char *card);
65 static char *wifi_perc(const char *iface);
66 static char *wifi_essid(const char *iface);
67 static void sighandler(const int signo);
68 static void usage(const int eval);
69
70 char *argv0;
71 static unsigned short int delay = 0;
72 static unsigned short int done;
73 static unsigned short int dflag, oflag;
74 static Display *dpy;
75
76 #include "config.h"
77
78 static char *
79 smprintf(const char *fmt, ...)
80 {
81 va_list ap;
82 char *ret;
83 int len;
84
85 va_start(ap, fmt);
86 len = vsnprintf(NULL, 0, fmt, ap);
87 va_end(ap);
88
89 ret = malloc(++len);
90 if (ret == NULL) {
91 err(1, "malloc");
92 }
93
94 va_start(ap, fmt);
95 vsnprintf(ret, len, fmt, ap);
96 va_end(ap);
97
98 return ret;
99 }
100
101 static char *
102 battery_perc(const char *bat)
103 {
104 int perc;
105 char path[PATH_MAX];
106 FILE *fp;
107
108 snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/capacity");
109 fp = fopen(path, "r");
110 if (fp == NULL) {
111 warn("Failed to open file %s", path);
112 return smprintf("%s", UNKNOWN_STR);
113 }
114 fscanf(fp, "%i", &perc);
115 fclose(fp);
116
117 return smprintf("%d%%", perc);
118 }
119
120 static char *
121 battery_state(const char *bat)
122 {
123 char path[PATH_MAX];
124 char state[12];
125 FILE *fp;
126
127 snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status");
128 fp = fopen(path, "r");
129 if (fp == NULL) {
130 warn("Failed to open file %s", path);
131 return smprintf("%s", UNKNOWN_STR);
132 }
133 fscanf(fp, "%12s", state);
134 fclose(fp);
135
136 if (strcmp(state, "Charging") == 0) {
137 return smprintf("+");
138 } else if (strcmp(state, "Discharging") == 0) {
139 return smprintf("-");
140 } else if (strcmp(state, "Full") == 0) {
141 return smprintf("=");
142 } else {
143 return smprintf("?");
144 }
145 }
146
147 static char *
148 cpu_perc(void)
149 {
150 int perc;
151 long double a[4], b[4];
152 FILE *fp;
153
154 fp = fopen("/proc/stat", "r");
155 if (fp == NULL) {
156 warn("Failed to open file /proc/stat");
157 return smprintf("%s", UNKNOWN_STR);
158 }
159 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &a[0], &a[1], &a[2], &a[3]);
160 fclose(fp);
161
162 delay++;
163 sleep(delay);
164
165 fp = fopen("/proc/stat", "r");
166 if (fp == NULL) {
167 warn("Failed to open file /proc/stat");
168 return smprintf("%s", UNKNOWN_STR);
169 }
170 fscanf(fp, "%*s %Lf %Lf %Lf %Lf", &b[0], &b[1], &b[2], &b[3]);
171 fclose(fp);
172
173 perc = 100 * ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
174 return smprintf("%d%%", perc);
175 }
176
177 static char *
178 datetime(const char *fmt)
179 {
180 time_t t;
181 char str[80];
182
183 t = time(NULL);
184 if (strftime(str, sizeof(str), fmt, localtime(&t)) == 0) {
185 return smprintf("%s", UNKNOWN_STR);
186 }
187
188 return smprintf("%s", str);
189 }
190
191 static char *
192 disk_free(const char *mnt)
193 {
194 struct statvfs fs;
195
196 if (statvfs(mnt, &fs) < 0) {
197 warn("Failed to get filesystem info");
198 return smprintf("%s", UNKNOWN_STR);
199 }
200
201 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_bfree / 1024 / 1024 / 1024);
202 }
203
204 static char *
205 disk_perc(const char *mnt)
206 {
207 int perc;
208 struct statvfs fs;
209
210 if (statvfs(mnt, &fs) < 0) {
211 warn("Failed to get filesystem info");
212 return smprintf("%s", UNKNOWN_STR);
213 }
214
215 perc = 100 * (1.0f - ((float)fs.f_bfree / (float)fs.f_blocks));
216
217 return smprintf("%d%%", perc);
218 }
219
220 static char *
221 disk_total(const char *mnt)
222 {
223 struct statvfs fs;
224
225 if (statvfs(mnt, &fs) < 0) {
226 warn("Failed to get filesystem info");
227 return smprintf("%s", UNKNOWN_STR);
228 }
229
230 return smprintf("%f", (float)fs.f_bsize * (float)fs.f_blocks / 1024 / 1024 / 1024);
231 }
232
233 static char *
234 disk_used(const char *mnt)
235 {
236 struct statvfs fs;
237
238 if (statvfs(mnt, &fs) < 0) {
239 warn("Failed to get filesystem info");
240 return smprintf("%s", UNKNOWN_STR);
241 }
242
243 return smprintf("%f", (float)fs.f_bsize * ((float)fs.f_blocks - (float)fs.f_bfree) / 1024 / 1024 / 1024);
244 }
245
246 static char *
247 entropy(void)
248 {
249 int num;
250 FILE *fp;
251
252 fp= fopen("/proc/sys/kernel/random/entropy_avail", "r");
253 if (fp == NULL) {
254 warn("Failed to open file /proc/sys/kernel/random/entropy_avail");
255 return smprintf("%s", UNKNOWN_STR);
256 }
257 fscanf(fp, "%d", &num);
258 fclose(fp);
259
260 return smprintf("%d", num);
261 }
262
263 static char *
264 gid(void)
265 {
266 return smprintf("%d", getgid());
267 }
268
269 static char *
270 hostname(void)
271 {
272 char buf[HOST_NAME_MAX];
273
274 if (gethostname(buf, sizeof(buf)) == -1) {
275 warn("hostname");
276 return smprintf("%s", UNKNOWN_STR);
277 }
278
279 return smprintf("%s", buf);
280 }
281
282 static char *
283 ip(const char *iface)
284 {
285 struct ifaddrs *ifaddr, *ifa;
286 int s;
287 char host[NI_MAXHOST];
288
289 if (getifaddrs(&ifaddr) == -1) {
290 warn("Failed to get IP address for interface %s", iface);
291 return smprintf("%s", UNKNOWN_STR);
292 }
293
294 for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
295 if (ifa->ifa_addr == NULL) {
296 continue;
297 }
298 s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
299 if ((strcmp(ifa->ifa_name, iface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) {
300 if (s != 0) {
301 warnx("Failed to get IP address for interface %s", iface);
302 return smprintf("%s", UNKNOWN_STR);
303 }
304 return smprintf("%s", host);
305 }
306 }
307
308 freeifaddrs(ifaddr);
309
310 return smprintf("%s", UNKNOWN_STR);
311 }
312
313 static char *
314 kernel_release(void)
315 {
316 struct utsname udata;
317
318 if (uname(&udata) < 0) {
319 return smprintf(UNKNOWN_STR);
320 }
321
322 return smprintf("%s", udata.release);
323 }
324
325 static char *
326 load_avg(void)
327 {
328 double avgs[3];
329
330 if (getloadavg(avgs, 3) < 0) {
331 warnx("Failed to get the load avg");
332 return smprintf("%s", UNKNOWN_STR);
333 }
334
335 return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
336 }
337
338 static char *
339 ram_free(void)
340 {
341 long free;
342 FILE *fp;
343
344 fp = fopen("/proc/meminfo", "r");
345 if (fp == NULL) {
346 warn("Failed to open file /proc/meminfo");
347 return smprintf("%s", UNKNOWN_STR);
348 }
349 fscanf(fp, "MemFree: %ld kB\n", &free);
350 fclose(fp);
351
352 return smprintf("%f", (float)free / 1024 / 1024);
353 }
354
355 static char *
356 ram_perc(void)
357 {
358 long total, free, buffers, cached;
359 FILE *fp;
360
361 fp = fopen("/proc/meminfo", "r");
362 if (fp == NULL) {
363 warn("Failed to open file /proc/meminfo");
364 return smprintf("%s", UNKNOWN_STR);
365 }
366 fscanf(fp, "MemTotal: %ld kB\n", &total);
367 fscanf(fp, "MemFree: %ld kB\n", &free);
368 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
369 fscanf(fp, "Cached: %ld kB\n", &cached);
370 fclose(fp);
371
372 return smprintf("%d%%", 100 * ((total - free) - (buffers + cached)) / total);
373 }
374
375 static char *
376 ram_total(void)
377 {
378 long total;
379 FILE *fp;
380
381 fp = fopen("/proc/meminfo", "r");
382 if (fp == NULL) {
383 warn("Failed to open file /proc/meminfo");
384 return smprintf("%s", UNKNOWN_STR);
385 }
386 fscanf(fp, "MemTotal: %ld kB\n", &total);
387 fclose(fp);
388
389 return smprintf("%f", (float)total / 1024 / 1024);
390 }
391
392 static char *
393 ram_used(void)
394 {
395 long free, total, buffers, cached;
396 FILE *fp;
397
398 fp = fopen("/proc/meminfo", "r");
399 if (fp == NULL) {
400 warn("Failed to open file /proc/meminfo");
401 return smprintf("%s", UNKNOWN_STR);
402 }
403 fscanf(fp, "MemTotal: %ld kB\n", &total);
404 fscanf(fp, "MemFree: %ld kB\n", &free);
405 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
406 fscanf(fp, "Cached: %ld kB\n", &cached);
407 fclose(fp);
408
409 return smprintf("%f", (float)(total - free - buffers - cached) / 1024 / 1024);
410 }
411
412 static char *
413 run_command(const char *cmd)
414 {
415 char *nlptr;
416 FILE *fp;
417 char buf[1024] = UNKNOWN_STR;
418
419 fp = popen(cmd, "r");
420 if (fp == NULL) {
421 warn("Failed to get command output for %s", cmd);
422 return smprintf("%s", UNKNOWN_STR);
423 }
424 fgets(buf, sizeof(buf), fp);
425 pclose(fp);
426 buf[sizeof(buf)] = '\0';
427
428 if ((nlptr = strstr(buf, "\n")) != NULL) {
429 nlptr[0] = '\0';
430 }
431
432 return smprintf("%s", buf);
433 }
434
435 static char *
436 swap_free(void)
437 {
438 long total, free;
439 FILE *fp;
440 char buf[2048];
441 size_t bytes_read;
442 char *match;
443
444 fp = fopen("/proc/meminfo", "r");
445 if (fp == NULL) {
446 warn("Failed to open file /proc/meminfo");
447 return smprintf("%s", UNKNOWN_STR);
448 }
449
450 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
451 warn("swap_free: read error");
452 fclose(fp);
453 return smprintf("%s", UNKNOWN_STR);
454 }
455
456 buf[bytes_read] = '\0';
457 fclose(fp);
458
459 if ((match = strstr(buf, "SwapTotal")) == NULL) {
460 return smprintf("%s", UNKNOWN_STR);
461 }
462 sscanf(match, "SwapTotal: %ld kB\n", &total);
463
464 if ((match = strstr(buf, "SwapFree")) == NULL) {
465 return smprintf("%s", UNKNOWN_STR);
466 }
467 sscanf(match, "SwapFree: %ld kB\n", &free);
468
469 return smprintf("%f", (float)free / 1024 / 1024);
470 }
471
472 static char *
473 swap_perc(void)
474 {
475 long total, free, cached;
476 FILE *fp;
477 char buf[2048];
478 size_t bytes_read;
479 char *match;
480
481 fp = fopen("/proc/meminfo", "r");
482 if (fp == NULL) {
483 warn("Failed to open file /proc/meminfo");
484 return smprintf("%s", UNKNOWN_STR);
485 }
486
487 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
488 warn("swap_perc: read error");
489 fclose(fp);
490 return smprintf("%s", UNKNOWN_STR);
491 }
492
493 buf[bytes_read] = '\0';
494 fclose(fp);
495
496 if ((match = strstr(buf, "SwapTotal")) == NULL) {
497 return smprintf("%s", UNKNOWN_STR);
498 }
499 sscanf(match, "SwapTotal: %ld kB\n", &total);
500
501 if ((match = strstr(buf, "SwapCached")) == NULL) {
502 return smprintf("%s", UNKNOWN_STR);
503 }
504 sscanf(match, "SwapCached: %ld kB\n", &cached);
505
506 if ((match = strstr(buf, "SwapFree")) == NULL) {
507 return smprintf("%s", UNKNOWN_STR);
508 }
509 sscanf(match, "SwapFree: %ld kB\n", &free);
510
511 return smprintf("%d%%", 100 * (total - free - cached) / total);
512 }
513
514 static char *
515 swap_total(void)
516 {
517 long total;
518 FILE *fp;
519 char buf[2048];
520 size_t bytes_read;
521 char *match;
522
523 fp = fopen("/proc/meminfo", "r");
524 if (fp == NULL) {
525 warn("Failed to open file /proc/meminfo");
526 return smprintf("%s", UNKNOWN_STR);
527 }
528 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
529 warn("swap_total: read error");
530 fclose(fp);
531 return smprintf("%s", UNKNOWN_STR);
532 }
533
534 buf[bytes_read] = '\0';
535 fclose(fp);
536
537 if ((match = strstr(buf, "SwapTotal")) == NULL) {
538 return smprintf("%s", UNKNOWN_STR);
539 }
540 sscanf(match, "SwapTotal: %ld kB\n", &total);
541
542 return smprintf("%f", (float)total / 1024 / 1024);
543 }
544
545 static char *
546 swap_used(void)
547 {
548 long total, free, cached;
549 FILE *fp;
550 char buf[2048];
551 size_t bytes_read;
552 char *match;
553
554 fp = fopen("/proc/meminfo", "r");
555 if (fp == NULL) {
556 warn("Failed to open file /proc/meminfo");
557 return smprintf("%s", UNKNOWN_STR);
558 }
559 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
560 warn("swap_used: read error");
561 fclose(fp);
562 return smprintf("%s", UNKNOWN_STR);
563 }
564
565 buf[bytes_read] = '\0';
566 fclose(fp);
567
568 if ((match = strstr(buf, "SwapTotal")) == NULL) {
569 return smprintf("%s", UNKNOWN_STR);
570 }
571 sscanf(match, "SwapTotal: %ld kB\n", &total);
572
573 if ((match = strstr(buf, "SwapCached")) == NULL) {
574 return smprintf("%s", UNKNOWN_STR);
575 }
576 sscanf(match, "SwapCached: %ld kB\n", &cached);
577
578 if ((match = strstr(buf, "SwapFree")) == NULL) {
579 return smprintf("%s", UNKNOWN_STR);
580 }
581 sscanf(match, "SwapFree: %ld kB\n", &free);
582
583 return smprintf("%f", (float)(total - free - cached) / 1024 / 1024);
584 }
585
586 static char *
587 temp(const char *file)
588 {
589 int temp;
590 FILE *fp;
591
592 fp = fopen(file, "r");
593 if (fp == NULL) {
594 warn("Failed to open file %s", file);
595 return smprintf("%s", UNKNOWN_STR);
596 }
597 fscanf(fp, "%d", &temp);
598 fclose(fp);
599
600 return smprintf("%d°C", temp / 1000);
601 }
602
603 static char *
604 uptime(void)
605 {
606 struct sysinfo info;
607 int h = 0;
608 int m = 0;
609
610 sysinfo(&info);
611 h = info.uptime / 3600;
612 m = (info.uptime - h * 3600 ) / 60;
613
614 return smprintf("%dh %dm", h, m);
615 }
616
617 static char *
618 username(void)
619 {
620 uid_t uid = geteuid();
621 struct passwd *pw = getpwuid(uid);
622
623 if (pw == NULL) {
624 warn("Failed to get username");
625 return smprintf("%s", UNKNOWN_STR);
626 }
627
628 return smprintf("%s", pw->pw_name);
629 }
630
631 static char *
632 uid(void)
633 {
634 return smprintf("%d", geteuid());
635 }
636
637
638 static char *
639 vol_perc(const char *card)
640 {
641 unsigned int i;
642 int v, afd, devmask;
643 char *vnames[] = SOUND_DEVICE_NAMES;
644
645 afd = open(card, O_RDONLY);
646 if (afd < 0) {
647 warn("Cannot open %s", card);
648 return smprintf(UNKNOWN_STR);
649 }
650
651 ioctl(afd, MIXER_READ(SOUND_MIXER_DEVMASK), &devmask);
652 for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) {
653 if (devmask & (1 << i)) {
654 if (!strcmp("vol", vnames[i])) {
655 ioctl(afd, MIXER_READ(i), &v);
656 }
657 }
658 }
659
660 close(afd);
661
662 return smprintf("%d%%", v & 0xff);
663 }
664
665 static char *
666 wifi_perc(const char *iface)
667 {
668 int perc;
669 char buf[255];
670 char *datastart;
671 char path[PATH_MAX];
672 char status[5];
673 FILE *fp;
674
675 snprintf(path, sizeof(path), "%s%s%s", "/sys/class/net/", iface, "/operstate");
676 fp = fopen(path, "r");
677 if (fp == NULL) {
678 warn("Failed to open file %s", path);
679 return smprintf("%s", UNKNOWN_STR);
680 }
681 fgets(status, 5, fp);
682 fclose(fp);
683 if(strcmp(status, "up\n") != 0) {
684 return smprintf("%s", UNKNOWN_STR);
685 }
686
687 fp = fopen("/proc/net/wireless", "r");
688 if (fp == NULL) {
689 warn("Failed to open file /proc/net/wireless");
690 return smprintf("%s", UNKNOWN_STR);
691 }
692
693 fgets(buf, sizeof(buf), fp);
694 fgets(buf, sizeof(buf), fp);
695 fgets(buf, sizeof(buf), fp);
696 fclose(fp);
697
698 if ((datastart = strstr(buf, iface)) == NULL) {
699 return smprintf("%s", UNKNOWN_STR);
700 }
701 datastart = (datastart+(strlen(iface)+1));
702 sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc);
703
704 return smprintf("%d%%", perc);
705 }
706
707 static char *
708 wifi_essid(const char *iface)
709 {
710 char id[IW_ESSID_MAX_SIZE+1];
711 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
712 struct iwreq wreq;
713
714 memset(&wreq, 0, sizeof(struct iwreq));
715 wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
716 snprintf(wreq.ifr_name, sizeof(wreq.ifr_name), "%s", iface);
717
718 if (sockfd == -1) {
719 warn("Failed to get ESSID for interface %s", iface);
720 return smprintf("%s", UNKNOWN_STR);
721 }
722 wreq.u.essid.pointer = id;
723 if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
724 warn("Failed to get ESSID for interface %s", iface);
725 return smprintf("%s", UNKNOWN_STR);
726 }
727
728 close(sockfd);
729
730 if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
731 return smprintf("%s", UNKNOWN_STR);
732 else
733 return smprintf("%s", (char *)wreq.u.essid.pointer);
734 }
735
736 static void
737 sighandler(const int signo)
738 {
739 if (signo == SIGTERM || signo == SIGINT) {
740 done = 1;
741 }
742 }
743
744 static void
745 usage(const int eval)
746 {
747 fprintf(stderr, "usage: %s [-d] [-o] [-v] [-h]\n", argv0);
748 exit(eval);
749 }
750
751 int
752 main(int argc, char *argv[])
753 {
754 unsigned short int i;
755 char status_string[2048];
756 char *res, *element;
757 struct arg argument;
758 struct sigaction act;
759
760 ARGBEGIN {
761 case 'd':
762 dflag = 1;
763 break;
764 case 'o':
765 oflag = 1;
766 break;
767 case 'v':
768 printf("slstatus (C) 2016-2017 slstatus engineers\n");
769 return 0;
770 case 'h':
771 usage(0);
772 default:
773 usage(1);
774 } ARGEND
775
776 if (dflag && oflag) {
777 usage(1);
778 }
779 if (dflag && daemon(1, 1) < 0) {
780 err(1, "daemon");
781 }
782
783 memset(&act, 0, sizeof(act));
784 act.sa_handler = sighandler;
785 sigaction(SIGINT, &act, 0);
786 sigaction(SIGTERM, &act, 0);
787
788 if (!oflag) {
789 dpy = XOpenDisplay(NULL);
790 }
791
792 setlocale(LC_ALL, "");
793
794 while (!done) {
795 status_string[0] = '\0';
796
797 for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
798 argument = args[i];
799 if (argument.args == NULL) {
800 res = argument.func();
801 } else {
802 res = argument.func(argument.args);
803 }
804 element = smprintf(argument.fmt, res);
805 if (element == NULL) {
806 element = smprintf("%s", UNKNOWN_STR);
807 warnx("Failed to format output");
808 }
809 strncat(status_string, element, sizeof(status_string) - strlen(status_string) - 1);
810 free(res);
811 free(element);
812 }
813
814 if (!oflag) {
815 XStoreName(dpy, DefaultRootWindow(dpy), status_string);
816 XSync(dpy, False);
817 } else {
818 printf("%s\n", status_string);
819 }
820
821 if ((UPDATE_INTERVAL - delay) <= 0) {
822 delay = 0;
823 continue;
824 } else {
825 sleep(UPDATE_INTERVAL - delay);
826 delay = 0;
827 }
828 }
829
830 if (!oflag) {
831 XStoreName(dpy, DefaultRootWindow(dpy), NULL);
832 XCloseDisplay(dpy);
833 }
834
835 return 0;
836 }