Xinqi Bao's Git

8c654350d42768050fde27fbee19f927df9eb591
[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 *load_avg(void);
55 static char *ram_free(void);
56 static char *ram_perc(void);
57 static char *ram_used(void);
58 static char *ram_total(void);
59 static char *run_command(const char *cmd);
60 static char *swap_free(void);
61 static char *swap_perc(void);
62 static char *swap_used(void);
63 static char *swap_total(void);
64 static char *temp(const char *file);
65 static char *uid(void);
66 static char *uptime(void);
67 static char *username(void);
68 static char *vol_perc(const char *card);
69 static char *wifi_perc(const char *iface);
70 static char *wifi_essid(const char *iface);
71 static char *kernel_release(void);
72 static void sighandler(const int signo);
73 static void usage(void);
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 load_avg(void)
319 {
320 double avgs[3];
321
322 if (getloadavg(avgs, 3) < 0) {
323 warnx("Failed to get the load avg");
324 return smprintf("%s", UNKNOWN_STR);
325 }
326
327 return smprintf("%.2f %.2f %.2f", avgs[0], avgs[1], avgs[2]);
328 }
329
330 static char *
331 ram_free(void)
332 {
333 long free;
334 FILE *fp;
335
336 fp = fopen("/proc/meminfo", "r");
337 if (fp == NULL) {
338 warn("Failed to open file /proc/meminfo");
339 return smprintf("%s", UNKNOWN_STR);
340 }
341 fscanf(fp, "MemFree: %ld kB\n", &free);
342 fclose(fp);
343
344 return smprintf("%f", (float)free / 1024 / 1024);
345 }
346
347 static char *
348 ram_perc(void)
349 {
350 long total, free, buffers, cached;
351 FILE *fp;
352
353 fp = fopen("/proc/meminfo", "r");
354 if (fp == NULL) {
355 warn("Failed to open file /proc/meminfo");
356 return smprintf("%s", UNKNOWN_STR);
357 }
358 fscanf(fp, "MemTotal: %ld kB\n", &total);
359 fscanf(fp, "MemFree: %ld kB\n", &free);
360 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
361 fscanf(fp, "Cached: %ld kB\n", &cached);
362 fclose(fp);
363
364 return smprintf("%d%%", 100 * ((total - free) - (buffers + cached)) / total);
365 }
366
367 static char *
368 ram_total(void)
369 {
370 long total;
371 FILE *fp;
372
373 fp = fopen("/proc/meminfo", "r");
374 if (fp == NULL) {
375 warn("Failed to open file /proc/meminfo");
376 return smprintf("%s", UNKNOWN_STR);
377 }
378 fscanf(fp, "MemTotal: %ld kB\n", &total);
379 fclose(fp);
380
381 return smprintf("%f", (float)total / 1024 / 1024);
382 }
383
384 static char *
385 ram_used(void)
386 {
387 long free, total, buffers, cached;
388 FILE *fp;
389
390 fp = fopen("/proc/meminfo", "r");
391 if (fp == NULL) {
392 warn("Failed to open file /proc/meminfo");
393 return smprintf("%s", UNKNOWN_STR);
394 }
395 fscanf(fp, "MemTotal: %ld kB\n", &total);
396 fscanf(fp, "MemFree: %ld kB\n", &free);
397 fscanf(fp, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers, &buffers);
398 fscanf(fp, "Cached: %ld kB\n", &cached);
399 fclose(fp);
400
401 return smprintf("%f", (float)(total - free - buffers - cached) / 1024 / 1024);
402 }
403
404 static char *
405 run_command(const char *cmd)
406 {
407 char *nlptr;
408 FILE *fp;
409 char buf[1024] = UNKNOWN_STR;
410
411 fp = popen(cmd, "r");
412 if (fp == NULL) {
413 warn("Failed to get command output for %s", cmd);
414 return smprintf("%s", UNKNOWN_STR);
415 }
416 fgets(buf, sizeof(buf), fp);
417 pclose(fp);
418 buf[strlen(buf)] = '\0';
419
420 if ((nlptr = strstr(buf, "\n")) != NULL) {
421 nlptr[0] = '\0';
422 }
423
424 return smprintf("%s", buf);
425 }
426
427 static char *
428 swap_free(void)
429 {
430 long total, free;
431 FILE *fp;
432 char buf[2048];
433 size_t bytes_read;
434 char *match;
435
436 fp = fopen("/proc/meminfo", "r");
437 if (fp == NULL) {
438 warn("Failed to open file /proc/meminfo");
439 return smprintf("%s", UNKNOWN_STR);
440 }
441
442 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
443 warn("swap_free: read error");
444 fclose(fp);
445 return smprintf("%s", UNKNOWN_STR);
446 }
447
448 buf[bytes_read] = '\0';
449 fclose(fp);
450
451 if ((match = strstr(buf, "SwapTotal")) == NULL) {
452 return smprintf("%s", UNKNOWN_STR);
453 }
454 sscanf(match, "SwapTotal: %ld kB\n", &total);
455
456 if ((match = strstr(buf, "SwapFree")) == NULL) {
457 return smprintf("%s", UNKNOWN_STR);
458 }
459 sscanf(match, "SwapFree: %ld kB\n", &free);
460
461 return smprintf("%f", (float)free / 1024 / 1024);
462 }
463
464 static char *
465 swap_perc(void)
466 {
467 long total, free, cached;
468 FILE *fp;
469 char buf[2048];
470 size_t bytes_read;
471 char *match;
472
473 fp = fopen("/proc/meminfo", "r");
474 if (fp == NULL) {
475 warn("Failed to open file /proc/meminfo");
476 return smprintf("%s", UNKNOWN_STR);
477 }
478
479 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
480 warn("swap_perc: read error");
481 fclose(fp);
482 return smprintf("%s", UNKNOWN_STR);
483 }
484
485 buf[bytes_read] = '\0';
486 fclose(fp);
487
488 if ((match = strstr(buf, "SwapTotal")) == NULL) {
489 return smprintf("%s", UNKNOWN_STR);
490 }
491 sscanf(match, "SwapTotal: %ld kB\n", &total);
492
493 if ((match = strstr(buf, "SwapCached")) == NULL) {
494 return smprintf("%s", UNKNOWN_STR);
495 }
496 sscanf(match, "SwapCached: %ld kB\n", &cached);
497
498 if ((match = strstr(buf, "SwapFree")) == NULL) {
499 return smprintf("%s", UNKNOWN_STR);
500 }
501 sscanf(match, "SwapFree: %ld kB\n", &free);
502
503 return smprintf("%d%%", 100 * (total - free - cached) / total);
504 }
505
506 static char *
507 swap_total(void)
508 {
509 long total;
510 FILE *fp;
511 char buf[2048];
512 size_t bytes_read;
513 char *match;
514
515 fp = fopen("/proc/meminfo", "r");
516 if (fp == NULL) {
517 warn("Failed to open file /proc/meminfo");
518 return smprintf("%s", UNKNOWN_STR);
519 }
520 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
521 warn("swap_total: read error");
522 fclose(fp);
523 return smprintf("%s", UNKNOWN_STR);
524 }
525
526 buf[bytes_read] = '\0';
527 fclose(fp);
528
529 if ((match = strstr(buf, "SwapTotal")) == NULL) {
530 return smprintf("%s", UNKNOWN_STR);
531 }
532 sscanf(match, "SwapTotal: %ld kB\n", &total);
533
534 return smprintf("%f", (float)total / 1024 / 1024);
535 }
536
537 static char *
538 swap_used(void)
539 {
540 long total, free, cached;
541 FILE *fp;
542 char buf[2048];
543 size_t bytes_read;
544 char *match;
545
546 fp = fopen("/proc/meminfo", "r");
547 if (fp == NULL) {
548 warn("Failed to open file /proc/meminfo");
549 return smprintf("%s", UNKNOWN_STR);
550 }
551 if ((bytes_read = fread(buf, sizeof(char), sizeof(buf), fp)) == 0) {
552 warn("swap_used: read error");
553 fclose(fp);
554 return smprintf("%s", UNKNOWN_STR);
555 }
556
557 buf[bytes_read] = '\0';
558 fclose(fp);
559
560 if ((match = strstr(buf, "SwapTotal")) == NULL) {
561 return smprintf("%s", UNKNOWN_STR);
562 }
563 sscanf(match, "SwapTotal: %ld kB\n", &total);
564
565 if ((match = strstr(buf, "SwapCached")) == NULL) {
566 return smprintf("%s", UNKNOWN_STR);
567 }
568 sscanf(match, "SwapCached: %ld kB\n", &cached);
569
570 if ((match = strstr(buf, "SwapFree")) == NULL) {
571 return smprintf("%s", UNKNOWN_STR);
572 }
573 sscanf(match, "SwapFree: %ld kB\n", &free);
574
575 return smprintf("%f", (float)(total - free - cached) / 1024 / 1024);
576 }
577
578 static char *
579 temp(const char *file)
580 {
581 int temp;
582 FILE *fp;
583
584 fp = fopen(file, "r");
585 if (fp == NULL) {
586 warn("Failed to open file %s", file);
587 return smprintf("%s", UNKNOWN_STR);
588 }
589 fscanf(fp, "%d", &temp);
590 fclose(fp);
591
592 return smprintf("%d°C", temp / 1000);
593 }
594
595 static char *
596 uptime(void)
597 {
598 struct sysinfo info;
599 int h = 0;
600 int m = 0;
601
602 sysinfo(&info);
603 h = info.uptime / 3600;
604 m = (info.uptime - h * 3600 ) / 60;
605
606 return smprintf("%dh %dm", h, m);
607 }
608
609 static char *
610 username(void)
611 {
612 uid_t uid = geteuid();
613 struct passwd *pw = getpwuid(uid);
614
615 if (pw == NULL) {
616 warn("Failed to get username");
617 return smprintf("%s", UNKNOWN_STR);
618 }
619
620 return smprintf("%s", pw->pw_name);
621 }
622
623 static char *
624 uid(void)
625 {
626 return smprintf("%d", geteuid());
627 }
628
629
630 static char *
631 vol_perc(const char *card)
632 {
633 int mute;
634 long int vol, max, min;
635 snd_mixer_t *handle;
636 snd_mixer_elem_t *elem;
637 snd_mixer_selem_id_t *s_elem;
638
639 snd_mixer_open(&handle, 0);
640 snd_mixer_attach(handle, card);
641 snd_mixer_selem_register(handle, NULL, NULL);
642 snd_mixer_load(handle);
643 snd_mixer_selem_id_malloc(&s_elem);
644 snd_mixer_selem_id_set_name(s_elem, "Master");
645 elem = snd_mixer_find_selem(handle, s_elem);
646
647 if (elem == NULL) {
648 snd_mixer_selem_id_free(s_elem);
649 snd_mixer_close(handle);
650 warn("Failed to get volume percentage for %s", card);
651 return smprintf("%s", UNKNOWN_STR);
652 }
653
654 snd_mixer_handle_events(handle);
655 snd_mixer_selem_get_playback_volume_range(elem, &min, &max);
656 snd_mixer_selem_get_playback_volume(elem, 0, &vol);
657 snd_mixer_selem_get_playback_switch(elem, 0, &mute);
658
659 snd_mixer_selem_id_free(s_elem);
660 snd_mixer_close(handle);
661
662 if (!mute)
663 return smprintf("mute");
664 else if (max == 0)
665 return smprintf("0%%");
666 else
667 return smprintf("%lu%%", ((uint_fast16_t)(vol * 100) / max));
668 }
669
670 static char *
671 wifi_perc(const char *iface)
672 {
673 int perc;
674 char buf[255];
675 char *datastart;
676 char status[5];
677 FILE *fp;
678
679 ccat(3, "/sys/class/net/", iface, "/operstate");
680 fp = fopen(concat, "r");
681 if (fp == NULL) {
682 warn("Failed to open file %s", concat);
683 return smprintf("%s", UNKNOWN_STR);
684 }
685 fgets(status, 5, fp);
686 fclose(fp);
687 if(strcmp(status, "up\n") != 0) {
688 return smprintf("%s", UNKNOWN_STR);
689 }
690
691 fp = fopen("/proc/net/wireless", "r");
692 if (fp == NULL) {
693 warn("Failed to open file /proc/net/wireless");
694 return smprintf("%s", UNKNOWN_STR);
695 }
696 ccat(2, iface, ":");
697 fgets(buf, sizeof(buf), fp);
698 fgets(buf, sizeof(buf), fp);
699 fgets(buf, sizeof(buf), fp);
700 fclose(fp);
701
702 datastart = strstr(buf, concat);
703 if (datastart != NULL) {
704 datastart = strstr(buf, ":");
705 sscanf(datastart + 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc);
706 }
707
708 return smprintf("%d%%", perc);
709 }
710
711 static char *
712 wifi_essid(const char *iface)
713 {
714 char id[IW_ESSID_MAX_SIZE+1];
715 int sockfd = socket(AF_INET, SOCK_DGRAM, 0);
716 struct iwreq wreq;
717
718 memset(&wreq, 0, sizeof(struct iwreq));
719 wreq.u.essid.length = IW_ESSID_MAX_SIZE+1;
720 sprintf(wreq.ifr_name, iface);
721 if (sockfd == -1) {
722 warn("Failed to get ESSID for interface %s", iface);
723 return smprintf("%s", UNKNOWN_STR);
724 }
725 wreq.u.essid.pointer = id;
726 if (ioctl(sockfd,SIOCGIWESSID, &wreq) == -1) {
727 warn("Failed to get ESSID for interface %s", iface);
728 return smprintf("%s", UNKNOWN_STR);
729 }
730
731 close(sockfd);
732
733 if (strcmp((char *)wreq.u.essid.pointer, "") == 0)
734 return smprintf("%s", UNKNOWN_STR);
735 else
736 return smprintf("%s", (char *)wreq.u.essid.pointer);
737 }
738
739 static char *
740 kernel_release(void)
741 {
742 struct utsname udata;
743 if (uname(&udata) < 0)
744 return smprintf("%s", UNKNOWN_STR);
745
746 return smprintf("%s", udata.release);
747 }
748
749 static void
750 sighandler(const int signo)
751 {
752 if (signo == SIGTERM || signo == SIGINT) {
753 done = 1;
754 }
755 }
756
757 static void
758 usage(void)
759 {
760 fprintf(stderr, "usage: %s [-dhov]\n", argv0);
761 exit(1);
762 }
763
764 int
765 main(int argc, char *argv[])
766 {
767 unsigned short int i;
768 char status_string[2048];
769 char *res, *element;
770 struct arg argument;
771 struct sigaction act;
772
773 ARGBEGIN {
774 case 'd':
775 dflag = 1;
776 break;
777 case 'o':
778 oflag = 1;
779 break;
780 case 'v':
781 printf("slstatus %s (C) 2016 slstatus engineers\n", VERSION);
782 return 0;
783 default:
784 usage();
785 } ARGEND
786
787 if (dflag && oflag) {
788 usage();
789 }
790 if (dflag && daemon(1, 1) < 0) {
791 err(1, "daemon");
792 }
793
794 memset(&act, 0, sizeof(act));
795 act.sa_handler = sighandler;
796 sigaction(SIGINT, &act, 0);
797 sigaction(SIGTERM, &act, 0);
798
799 if (!oflag) {
800 dpy = XOpenDisplay(NULL);
801 }
802
803 setlocale(LC_ALL, "");
804
805 while (!done) {
806 status_string[0] = '\0';
807
808 for (i = 0; i < sizeof(args) / sizeof(args[0]); ++i) {
809 argument = args[i];
810 if (argument.args == NULL) {
811 res = argument.func();
812 } else {
813 res = argument.func(argument.args);
814 }
815 element = smprintf(argument.fmt, res);
816 if (element == NULL) {
817 element = smprintf("%s", UNKNOWN_STR);
818 warnx("Failed to format output");
819 }
820 if (strlcat(status_string, element, sizeof(status_string)) >= sizeof(status_string))
821 warnx("Output too long");
822 free(res);
823 free(element);
824 }
825
826 if (!oflag) {
827 XStoreName(dpy, DefaultRootWindow(dpy), status_string);
828 XSync(dpy, False);
829 } else {
830 printf("%s\n", status_string);
831 }
832
833 /*
834 * subtract delay time spend in function
835 * calls from the actual global delay time
836 */
837 if ((UPDATE_INTERVAL - delay) <= 0) {
838 delay = 0;
839 continue;
840 } else {
841 sleep(UPDATE_INTERVAL - delay);
842 delay = 0;
843 }
844 }
845
846 if (!oflag) {
847 XStoreName(dpy, DefaultRootWindow(dpy), NULL);
848 XCloseDisplay(dpy);
849 }
850
851 return 0;
852 }