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>
30 #include "extern/arg.h"
31 #include "extern/strlcat.h"
32 #include "extern/concat.h"
40 static char *smprintf(const char *fmt
, ...);
41 static char *battery_perc(const char *bat
);
42 static char *battery_state(const char *bat
);
43 static char *cpu_perc(void);
44 static char *datetime(const char *fmt
);
45 static char *disk_free(const char *mnt
);
46 static char *disk_perc(const char *mnt
);
47 static char *disk_total(const char *mnt
);
48 static char *disk_used(const char *mnt
);
49 static char *entropy(void);
50 static char *gid(void);
51 static char *hostname(void);
52 static char *ip(const char *iface
);
53 static char *kernel_release(void);
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 void sighandler(const int signo
);
72 static void usage(int);
76 static unsigned short int delay
= 0;
77 static unsigned short int done
;
78 static unsigned short int dflag
, oflag
;
84 smprintf(const char *fmt
, ...)
91 len
= vsnprintf(NULL
, 0, fmt
, ap
);
100 vsnprintf(ret
, len
, fmt
, ap
);
107 battery_perc(const char *bat
)
112 ccat(3, "/sys/class/power_supply/", bat
, "/capacity");
113 fp
= fopen(concat
, "r");
115 warn("Failed to open file %s", concat
);
116 return smprintf("%s", UNKNOWN_STR
);
118 fscanf(fp
, "%i", &perc
);
121 return smprintf("%d%%", perc
);
125 battery_state(const char *bat
)
130 ccat(3, "/sys/class/power_supply/", bat
, "/status");
131 fp
= fopen(concat
, "r");
133 warn("Failed to open file %s", concat
);
134 return smprintf("%s", UNKNOWN_STR
);
136 fscanf(fp
, "%12s", state
);
139 if (strcmp(state
, "Charging") == 0) {
140 return smprintf("+");
141 } else if (strcmp(state
, "Discharging") == 0) {
142 return smprintf("-");
143 } else if (strcmp(state
, "Full") == 0) {
144 return smprintf("=");
146 return smprintf("?");
154 long double a
[4], b
[4];
157 fp
= fopen("/proc/stat", "r");
159 warn("Failed to open file /proc/stat");
160 return smprintf("%s", UNKNOWN_STR
);
162 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
168 fp
= fopen("/proc/stat", "r");
170 warn("Failed to open file /proc/stat");
171 return smprintf("%s", UNKNOWN_STR
);
173 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
176 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]));
177 return smprintf("%d%%", perc
);
181 datetime(const char *fmt
)
187 if (strftime(str
, sizeof(str
), fmt
, localtime(&t
)) == 0) {
188 return smprintf("%s", UNKNOWN_STR
);
191 return smprintf("%s", str
);
195 disk_free(const char *mnt
)
199 if (statvfs(mnt
, &fs
) < 0) {
200 warn("Failed to get filesystem info");
201 return smprintf("%s", UNKNOWN_STR
);
204 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
208 disk_perc(const char *mnt
)
213 if (statvfs(mnt
, &fs
) < 0) {
214 warn("Failed to get filesystem info");
215 return smprintf("%s", UNKNOWN_STR
);
218 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
220 return smprintf("%d%%", perc
);
224 disk_total(const char *mnt
)
228 if (statvfs(mnt
, &fs
) < 0) {
229 warn("Failed to get filesystem info");
230 return smprintf("%s", UNKNOWN_STR
);
233 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
237 disk_used(const char *mnt
)
241 if (statvfs(mnt
, &fs
) < 0) {
242 warn("Failed to get filesystem info");
243 return smprintf("%s", UNKNOWN_STR
);
246 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
255 fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r");
257 warn("Failed to open file /proc/sys/kernel/random/entropy_avail");
258 return smprintf("%s", UNKNOWN_STR
);
260 fscanf(fp
, "%d", &num
);
263 return smprintf("%d", num
);
269 return smprintf("%d", getgid());
275 char buf
[HOST_NAME_MAX
];
277 if (gethostname(buf
, sizeof(buf
)) == -1) {
279 return smprintf("%s", UNKNOWN_STR
);
282 return smprintf("%s", buf
);
286 ip(const char *iface
)
288 struct ifaddrs
*ifaddr
, *ifa
;
290 char host
[NI_MAXHOST
];
292 if (getifaddrs(&ifaddr
) == -1) {
293 warn("Failed to get IP address for interface %s", iface
);
294 return smprintf("%s", UNKNOWN_STR
);
297 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
298 if (ifa
->ifa_addr
== NULL
) {
301 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
302 if ((strcmp(ifa
->ifa_name
, iface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
)) {
304 warnx("Failed to get IP address for interface %s", iface
);
305 return smprintf("%s", UNKNOWN_STR
);
307 return smprintf("%s", host
);
313 return smprintf("%s", UNKNOWN_STR
);
319 struct utsname udata
;
321 if (uname(&udata
) < 0) {
322 return smprintf(UNKNOWN_STR
);
325 return smprintf("%s", udata
.release
);
333 if (getloadavg(avgs
, 3) < 0) {
334 warnx("Failed to get the load avg");
335 return smprintf("%s", UNKNOWN_STR
);
338 return smprintf("%.2f %.2f %.2f", avgs
[0], avgs
[1], avgs
[2]);
347 fp
= fopen("/proc/meminfo", "r");
349 warn("Failed to open file /proc/meminfo");
350 return smprintf("%s", UNKNOWN_STR
);
352 fscanf(fp
, "MemFree: %ld kB\n", &free
);
355 return smprintf("%f", (float)free
/ 1024 / 1024);
361 long total
, free
, buffers
, cached
;
364 fp
= fopen("/proc/meminfo", "r");
366 warn("Failed to open file /proc/meminfo");
367 return smprintf("%s", UNKNOWN_STR
);
369 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
370 fscanf(fp
, "MemFree: %ld kB\n", &free
);
371 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
372 fscanf(fp
, "Cached: %ld kB\n", &cached
);
375 return smprintf("%d%%", 100 * ((total
- free
) - (buffers
+ cached
)) / total
);
384 fp
= fopen("/proc/meminfo", "r");
386 warn("Failed to open file /proc/meminfo");
387 return smprintf("%s", UNKNOWN_STR
);
389 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
392 return smprintf("%f", (float)total
/ 1024 / 1024);
398 long free
, total
, buffers
, cached
;
401 fp
= fopen("/proc/meminfo", "r");
403 warn("Failed to open file /proc/meminfo");
404 return smprintf("%s", UNKNOWN_STR
);
406 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
407 fscanf(fp
, "MemFree: %ld kB\n", &free
);
408 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
409 fscanf(fp
, "Cached: %ld kB\n", &cached
);
412 return smprintf("%f", (float)(total
- free
- buffers
- cached
) / 1024 / 1024);
416 run_command(const char *cmd
)
420 char buf
[1024] = UNKNOWN_STR
;
422 fp
= popen(cmd
, "r");
424 warn("Failed to get command output for %s", cmd
);
425 return smprintf("%s", UNKNOWN_STR
);
427 fgets(buf
, sizeof(buf
), fp
);
429 buf
[strlen(buf
)] = '\0';
431 if ((nlptr
= strstr(buf
, "\n")) != NULL
) {
435 return smprintf("%s", buf
);
447 fp
= fopen("/proc/meminfo", "r");
449 warn("Failed to open file /proc/meminfo");
450 return smprintf("%s", UNKNOWN_STR
);
453 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
454 warn("swap_free: read error");
456 return smprintf("%s", UNKNOWN_STR
);
459 buf
[bytes_read
] = '\0';
462 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
463 return smprintf("%s", UNKNOWN_STR
);
465 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
467 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
468 return smprintf("%s", UNKNOWN_STR
);
470 sscanf(match
, "SwapFree: %ld kB\n", &free
);
472 return smprintf("%f", (float)free
/ 1024 / 1024);
478 long total
, free
, cached
;
484 fp
= fopen("/proc/meminfo", "r");
486 warn("Failed to open file /proc/meminfo");
487 return smprintf("%s", UNKNOWN_STR
);
490 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
491 warn("swap_perc: read error");
493 return smprintf("%s", UNKNOWN_STR
);
496 buf
[bytes_read
] = '\0';
499 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
500 return smprintf("%s", UNKNOWN_STR
);
502 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
504 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
505 return smprintf("%s", UNKNOWN_STR
);
507 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
509 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
510 return smprintf("%s", UNKNOWN_STR
);
512 sscanf(match
, "SwapFree: %ld kB\n", &free
);
514 return smprintf("%d%%", 100 * (total
- free
- cached
) / total
);
526 fp
= fopen("/proc/meminfo", "r");
528 warn("Failed to open file /proc/meminfo");
529 return smprintf("%s", UNKNOWN_STR
);
531 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
532 warn("swap_total: read error");
534 return smprintf("%s", UNKNOWN_STR
);
537 buf
[bytes_read
] = '\0';
540 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
541 return smprintf("%s", UNKNOWN_STR
);
543 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
545 return smprintf("%f", (float)total
/ 1024 / 1024);
551 long total
, free
, cached
;
557 fp
= fopen("/proc/meminfo", "r");
559 warn("Failed to open file /proc/meminfo");
560 return smprintf("%s", UNKNOWN_STR
);
562 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
563 warn("swap_used: read error");
565 return smprintf("%s", UNKNOWN_STR
);
568 buf
[bytes_read
] = '\0';
571 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
572 return smprintf("%s", UNKNOWN_STR
);
574 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
576 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
577 return smprintf("%s", UNKNOWN_STR
);
579 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
581 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
582 return smprintf("%s", UNKNOWN_STR
);
584 sscanf(match
, "SwapFree: %ld kB\n", &free
);
586 return smprintf("%f", (float)(total
- free
- cached
) / 1024 / 1024);
590 temp(const char *file
)
595 fp
= fopen(file
, "r");
597 warn("Failed to open file %s", file
);
598 return smprintf("%s", UNKNOWN_STR
);
600 fscanf(fp
, "%d", &temp
);
603 return smprintf("%d°C", temp
/ 1000);
614 h
= info
.uptime
/ 3600;
615 m
= (info
.uptime
- h
* 3600 ) / 60;
617 return smprintf("%dh %dm", h
, m
);
623 uid_t uid
= geteuid();
624 struct passwd
*pw
= getpwuid(uid
);
627 warn("Failed to get username");
628 return smprintf("%s", UNKNOWN_STR
);
631 return smprintf("%s", pw
->pw_name
);
637 return smprintf("%d", geteuid());
642 vol_perc(const char *card
)
645 long int vol
, max
, min
;
647 snd_mixer_elem_t
*elem
;
648 snd_mixer_selem_id_t
*s_elem
;
650 snd_mixer_open(&handle
, 0);
651 snd_mixer_attach(handle
, card
);
652 snd_mixer_selem_register(handle
, NULL
, NULL
);
653 snd_mixer_load(handle
);
654 snd_mixer_selem_id_malloc(&s_elem
);
655 snd_mixer_selem_id_set_name(s_elem
, "Master");
656 elem
= snd_mixer_find_selem(handle
, s_elem
);
659 snd_mixer_selem_id_free(s_elem
);
660 snd_mixer_close(handle
);
661 warn("Failed to get volume percentage for %s", card
);
662 return smprintf("%s", UNKNOWN_STR
);
665 snd_mixer_handle_events(handle
);
666 snd_mixer_selem_get_playback_volume_range(elem
, &min
, &max
);
667 snd_mixer_selem_get_playback_volume(elem
, 0, &vol
);
668 snd_mixer_selem_get_playback_switch(elem
, 0, &mute
);
670 snd_mixer_selem_id_free(s_elem
);
671 snd_mixer_close(handle
);
674 return smprintf("mute");
676 return smprintf("0%%");
678 return smprintf("%lu%%", ((uint_fast16_t)(vol
* 100) / max
));
682 wifi_perc(const char *iface
)
690 ccat(3, "/sys/class/net/", iface
, "/operstate");
691 fp
= fopen(concat
, "r");
693 warn("Failed to open file %s", concat
);
694 return smprintf("%s", UNKNOWN_STR
);
696 fgets(status
, 5, fp
);
698 if(strcmp(status
, "up\n") != 0) {
699 return smprintf("%s", UNKNOWN_STR
);
702 fp
= fopen("/proc/net/wireless", "r");
704 warn("Failed to open file /proc/net/wireless");
705 return smprintf("%s", UNKNOWN_STR
);
708 fgets(buf
, sizeof(buf
), fp
);
709 fgets(buf
, sizeof(buf
), fp
);
710 fgets(buf
, sizeof(buf
), fp
);
713 if ((datastart
= strstr(buf
, concat
)) == NULL
) {
714 return smprintf("%s", UNKNOWN_STR
);
716 datastart
= (datastart
+(strlen(iface
)+1));
717 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc
);
719 return smprintf("%d%%", perc
);
723 wifi_essid(const char *iface
)
725 char id
[IW_ESSID_MAX_SIZE
+1];
726 int sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
729 memset(&wreq
, 0, sizeof(struct iwreq
));
730 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
731 snprintf(wreq
.ifr_name
, sizeof(wreq
.ifr_name
), "%s", iface
);
734 warn("Failed to get ESSID for interface %s", iface
);
735 return smprintf("%s", UNKNOWN_STR
);
737 wreq
.u
.essid
.pointer
= id
;
738 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
739 warn("Failed to get ESSID for interface %s", iface
);
740 return smprintf("%s", UNKNOWN_STR
);
745 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0)
746 return smprintf("%s", UNKNOWN_STR
);
748 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
752 sighandler(const int signo
)
754 if (signo
== SIGTERM
|| signo
== SIGINT
) {
762 fprintf(stderr
, "usage: %s [-d] [-o] [-v] [-h]\n", argv0
);
767 main(int argc
, char *argv
[])
769 unsigned short int i
;
770 char status_string
[2048];
773 struct sigaction act
;
783 printf("slstatus %s (C) 2016 slstatus engineers\n", VERSION
);
791 if (dflag
&& oflag
) {
794 if (dflag
&& daemon(1, 1) < 0) {
798 memset(&act
, 0, sizeof(act
));
799 act
.sa_handler
= sighandler
;
800 sigaction(SIGINT
, &act
, 0);
801 sigaction(SIGTERM
, &act
, 0);
804 dpy
= XOpenDisplay(NULL
);
807 setlocale(LC_ALL
, "");
810 status_string
[0] = '\0';
812 for (i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
814 if (argument
.args
== NULL
) {
815 res
= argument
.func();
817 res
= argument
.func(argument
.args
);
819 element
= smprintf(argument
.fmt
, res
);
820 if (element
== NULL
) {
821 element
= smprintf("%s", UNKNOWN_STR
);
822 warnx("Failed to format output");
824 if (strlcat(status_string
, element
, sizeof(status_string
)) >= sizeof(status_string
)) {
825 warnx("Output too long");
832 XStoreName(dpy
, DefaultRootWindow(dpy
), status_string
);
835 printf("%s\n", status_string
);
838 if ((UPDATE_INTERVAL
- delay
) <= 0) {
842 sleep(UPDATE_INTERVAL
- delay
);
848 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);