Xinqi Bao's Git

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