Xinqi Bao's Git

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