Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
3 #include <alsa/asoundlib.h>
8 #include <linux/wireless.h>
17 #include <sys/ioctl.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>
31 #include "extern/arg.h"
32 #include "extern/strlcat.h"
33 #include "extern/concat.h"
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);
77 static unsigned short int delay
= 0;
78 static unsigned short int done
;
79 static unsigned short int dflag
, oflag
;
85 smprintf(const char *fmt
, ...)
92 len
= vsnprintf(NULL
, 0, fmt
, ap
);
101 vsnprintf(ret
, len
, fmt
, ap
);
108 battery_perc(const char *bat
)
113 ccat(3, "/sys/class/power_supply/", bat
, "/capacity");
114 fp
= fopen(concat
, "r");
116 warn("Failed to open file %s", concat
);
117 return smprintf("%s", UNKNOWN_STR
);
119 fscanf(fp
, "%i", &perc
);
122 return smprintf("%d%%", perc
);
126 battery_state(const char *bat
)
131 ccat(3, "/sys/class/power_supply/", bat
, "/status");
132 fp
= fopen(concat
, "r");
134 warn("Failed to open file %s", concat
);
135 return smprintf("%s", UNKNOWN_STR
);
137 fscanf(fp
, "%12s", state
);
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("=");
147 return smprintf("?");
155 long double a
[4], b
[4];
158 fp
= fopen("/proc/stat", "r");
160 warn("Failed to open file /proc/stat");
161 return smprintf("%s", UNKNOWN_STR
);
163 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
169 fp
= fopen("/proc/stat", "r");
171 warn("Failed to open file /proc/stat");
172 return smprintf("%s", UNKNOWN_STR
);
174 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
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
);
182 datetime(const char *fmt
)
188 if (strftime(str
, sizeof(str
), fmt
, localtime(&t
)) == 0) {
189 return smprintf("%s", UNKNOWN_STR
);
192 return smprintf("%s", str
);
196 disk_free(const char *mnt
)
200 if (statvfs(mnt
, &fs
) < 0) {
201 warn("Failed to get filesystem info");
202 return smprintf("%s", UNKNOWN_STR
);
205 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
209 disk_perc(const char *mnt
)
214 if (statvfs(mnt
, &fs
) < 0) {
215 warn("Failed to get filesystem info");
216 return smprintf("%s", UNKNOWN_STR
);
219 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
221 return smprintf("%d%%", perc
);
225 disk_total(const char *mnt
)
229 if (statvfs(mnt
, &fs
) < 0) {
230 warn("Failed to get filesystem info");
231 return smprintf("%s", UNKNOWN_STR
);
234 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
238 disk_used(const char *mnt
)
242 if (statvfs(mnt
, &fs
) < 0) {
243 warn("Failed to get filesystem info");
244 return smprintf("%s", UNKNOWN_STR
);
247 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
256 fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r");
258 warn("Failed to open file /proc/sys/kernel/random/entropy_avail");
259 return smprintf("%s", UNKNOWN_STR
);
261 fscanf(fp
, "%d", &num
);
264 return smprintf("%d", num
);
270 return smprintf("%d", getgid());
276 char buf
[HOST_NAME_MAX
];
278 if (gethostname(buf
, sizeof(buf
)) == -1) {
280 return smprintf("%s", UNKNOWN_STR
);
283 return smprintf("%s", buf
);
287 ip(const char *iface
)
289 struct ifaddrs
*ifaddr
, *ifa
;
291 char host
[NI_MAXHOST
];
293 if (getifaddrs(&ifaddr
) == -1) {
294 warn("Failed to get IP address for interface %s", iface
);
295 return smprintf("%s", UNKNOWN_STR
);
298 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
299 if (ifa
->ifa_addr
== NULL
) {
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
)) {
305 warnx("Failed to get IP address for interface %s", iface
);
306 return smprintf("%s", UNKNOWN_STR
);
308 return smprintf("%s", host
);
314 return smprintf("%s", UNKNOWN_STR
);
320 struct utsname udata
;
322 if (uname(&udata
) < 0) {
323 return smprintf(UNKNOWN_STR
);
326 return smprintf("%s", udata
.release
);
334 if (getloadavg(avgs
, 3) < 0) {
335 warnx("Failed to get the load avg");
336 return smprintf("%s", UNKNOWN_STR
);
339 return smprintf("%.2f %.2f %.2f", avgs
[0], avgs
[1], avgs
[2]);
348 fp
= fopen("/proc/meminfo", "r");
350 warn("Failed to open file /proc/meminfo");
351 return smprintf("%s", UNKNOWN_STR
);
353 fscanf(fp
, "MemFree: %ld kB\n", &free
);
356 return smprintf("%f", (float)free
/ 1024 / 1024);
362 long total
, free
, buffers
, cached
;
365 fp
= fopen("/proc/meminfo", "r");
367 warn("Failed to open file /proc/meminfo");
368 return smprintf("%s", UNKNOWN_STR
);
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
);
376 return smprintf("%d%%", 100 * ((total
- free
) - (buffers
+ cached
)) / total
);
385 fp
= fopen("/proc/meminfo", "r");
387 warn("Failed to open file /proc/meminfo");
388 return smprintf("%s", UNKNOWN_STR
);
390 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
393 return smprintf("%f", (float)total
/ 1024 / 1024);
399 long free
, total
, buffers
, cached
;
402 fp
= fopen("/proc/meminfo", "r");
404 warn("Failed to open file /proc/meminfo");
405 return smprintf("%s", UNKNOWN_STR
);
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
);
413 return smprintf("%f", (float)(total
- free
- buffers
- cached
) / 1024 / 1024);
417 run_command(const char *cmd
)
421 char buf
[1024] = UNKNOWN_STR
;
423 fp
= popen(cmd
, "r");
425 warn("Failed to get command output for %s", cmd
);
426 return smprintf("%s", UNKNOWN_STR
);
428 fgets(buf
, sizeof(buf
), fp
);
430 buf
[strlen(buf
)] = '\0';
432 if ((nlptr
= strstr(buf
, "\n")) != NULL
) {
436 return smprintf("%s", buf
);
448 fp
= fopen("/proc/meminfo", "r");
450 warn("Failed to open file /proc/meminfo");
451 return smprintf("%s", UNKNOWN_STR
);
454 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
455 warn("swap_free: read error");
457 return smprintf("%s", UNKNOWN_STR
);
460 buf
[bytes_read
] = '\0';
463 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
464 return smprintf("%s", UNKNOWN_STR
);
466 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
468 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
469 return smprintf("%s", UNKNOWN_STR
);
471 sscanf(match
, "SwapFree: %ld kB\n", &free
);
473 return smprintf("%f", (float)free
/ 1024 / 1024);
479 long total
, free
, cached
;
485 fp
= fopen("/proc/meminfo", "r");
487 warn("Failed to open file /proc/meminfo");
488 return smprintf("%s", UNKNOWN_STR
);
491 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
492 warn("swap_perc: read error");
494 return smprintf("%s", UNKNOWN_STR
);
497 buf
[bytes_read
] = '\0';
500 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
501 return smprintf("%s", UNKNOWN_STR
);
503 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
505 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
506 return smprintf("%s", UNKNOWN_STR
);
508 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
510 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
511 return smprintf("%s", UNKNOWN_STR
);
513 sscanf(match
, "SwapFree: %ld kB\n", &free
);
515 return smprintf("%d%%", 100 * (total
- free
- cached
) / total
);
527 fp
= fopen("/proc/meminfo", "r");
529 warn("Failed to open file /proc/meminfo");
530 return smprintf("%s", UNKNOWN_STR
);
532 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
533 warn("swap_total: read error");
535 return smprintf("%s", UNKNOWN_STR
);
538 buf
[bytes_read
] = '\0';
541 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
542 return smprintf("%s", UNKNOWN_STR
);
544 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
546 return smprintf("%f", (float)total
/ 1024 / 1024);
552 long total
, free
, cached
;
558 fp
= fopen("/proc/meminfo", "r");
560 warn("Failed to open file /proc/meminfo");
561 return smprintf("%s", UNKNOWN_STR
);
563 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
564 warn("swap_used: read error");
566 return smprintf("%s", UNKNOWN_STR
);
569 buf
[bytes_read
] = '\0';
572 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
573 return smprintf("%s", UNKNOWN_STR
);
575 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
577 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
578 return smprintf("%s", UNKNOWN_STR
);
580 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
582 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
583 return smprintf("%s", UNKNOWN_STR
);
585 sscanf(match
, "SwapFree: %ld kB\n", &free
);
587 return smprintf("%f", (float)(total
- free
- cached
) / 1024 / 1024);
591 temp(const char *file
)
596 fp
= fopen(file
, "r");
598 warn("Failed to open file %s", file
);
599 return smprintf("%s", UNKNOWN_STR
);
601 fscanf(fp
, "%d", &temp
);
604 return smprintf("%d°C", temp
/ 1000);
615 h
= info
.uptime
/ 3600;
616 m
= (info
.uptime
- h
* 3600 ) / 60;
618 return smprintf("%dh %dm", h
, m
);
624 uid_t uid
= geteuid();
625 struct passwd
*pw
= getpwuid(uid
);
628 warn("Failed to get username");
629 return smprintf("%s", UNKNOWN_STR
);
632 return smprintf("%s", pw
->pw_name
);
638 return smprintf("%d", geteuid());
643 vol_perc(const char *card
)
646 long int vol
, max
, min
;
648 snd_mixer_elem_t
*elem
;
649 snd_mixer_selem_id_t
*s_elem
;
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
);
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
);
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
);
671 snd_mixer_selem_id_free(s_elem
);
672 snd_mixer_close(handle
);
675 return smprintf("mute");
677 return smprintf("0%%");
679 return smprintf("%lu%%", ((uint_fast16_t)(vol
* 100) / max
));
683 wifi_perc(const char *iface
)
691 ccat(3, "/sys/class/net/", iface
, "/operstate");
692 fp
= fopen(concat
, "r");
694 warn("Failed to open file %s", concat
);
695 return smprintf("%s", UNKNOWN_STR
);
697 fgets(status
, 5, fp
);
699 if(strcmp(status
, "up\n") != 0) {
700 return smprintf("%s", UNKNOWN_STR
);
703 fp
= fopen("/proc/net/wireless", "r");
705 warn("Failed to open file /proc/net/wireless");
706 return smprintf("%s", UNKNOWN_STR
);
709 fgets(buf
, sizeof(buf
), fp
);
710 fgets(buf
, sizeof(buf
), fp
);
711 fgets(buf
, sizeof(buf
), fp
);
714 if ((datastart
= strstr(buf
, concat
)) == NULL
) {
715 return smprintf("%s", UNKNOWN_STR
);
717 datastart
= (datastart
+(strlen(iface
)+1));
718 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc
);
720 return smprintf("%d%%", perc
);
724 wifi_essid(const char *iface
)
726 char id
[IW_ESSID_MAX_SIZE
+1];
727 int sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
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
);
735 warn("Failed to get ESSID for interface %s", iface
);
736 return smprintf("%s", UNKNOWN_STR
);
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
);
746 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0)
747 return smprintf("%s", UNKNOWN_STR
);
749 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
753 sighandler(const int signo
)
755 if (signo
== SIGTERM
|| signo
== SIGINT
) {
763 fprintf(stderr
, "usage: %s [-d] [-o] [-v] [-h]\n", argv0
);
768 main(int argc
, char *argv
[])
770 unsigned short int i
;
771 char status_string
[2048];
774 struct sigaction act
;
784 printf("slstatus %s (C) 2016 slstatus engineers\n", VERSION
);
792 if (dflag
&& oflag
) {
795 if (dflag
&& daemon(1, 1) < 0) {
799 memset(&act
, 0, sizeof(act
));
800 act
.sa_handler
= sighandler
;
801 sigaction(SIGINT
, &act
, 0);
802 sigaction(SIGTERM
, &act
, 0);
805 dpy
= XOpenDisplay(NULL
);
808 setlocale(LC_ALL
, "");
811 status_string
[0] = '\0';
813 for (i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
815 if (argument
.args
== NULL
) {
816 res
= argument
.func();
818 res
= argument
.func(argument
.args
);
820 element
= smprintf(argument
.fmt
, res
);
821 if (element
== NULL
) {
822 element
= smprintf("%s", UNKNOWN_STR
);
823 warnx("Failed to format output");
825 if (strlcat(status_string
, element
, sizeof(status_string
)) >= sizeof(status_string
)) {
826 warnx("Output too long");
833 XStoreName(dpy
, DefaultRootWindow(dpy
), status_string
);
836 printf("%s\n", status_string
);
839 if ((UPDATE_INTERVAL
- delay
) <= 0) {
843 sleep(UPDATE_INTERVAL
- delay
);
849 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);