Xinqi Bao's Git
1 /* See LICENSE file for copyright and license details. */
7 #include <linux/wireless.h>
16 #include <sys/ioctl.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>
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_perc(void);
41 static char *datetime(const char *fmt
);
42 static char *disk_free(const char *mnt
);
43 static char *disk_perc(const char *mnt
);
44 static char *disk_total(const char *mnt
);
45 static char *disk_used(const char *mnt
);
46 static char *entropy(void);
47 static char *gid(void);
48 static char *hostname(void);
49 static char *ip(const char *iface
);
50 static char *kernel_release(void);
51 static char *keyboard_indicators(void);
52 static char *load_avg(void);
53 static char *ram_free(void);
54 static char *ram_perc(void);
55 static char *ram_used(void);
56 static char *ram_total(void);
57 static char *run_command(const char *cmd
);
58 static char *swap_free(void);
59 static char *swap_perc(void);
60 static char *swap_used(void);
61 static char *swap_total(void);
62 static char *temp(const char *file
);
63 static char *uid(void);
64 static char *uptime(void);
65 static char *username(void);
66 static char *vol_perc(const char *card
);
67 static char *wifi_perc(const char *iface
);
68 static char *wifi_essid(const char *iface
);
69 static void sighandler(const int signo
);
70 static void usage(const int eval
);
73 static unsigned short int delay
= 0;
74 static unsigned short int done
;
75 static unsigned short int dflag
, oflag
, nflag
;
81 smprintf(const char *fmt
, ...)
88 len
= vsnprintf(NULL
, 0, fmt
, ap
);
97 vsnprintf(ret
, len
, fmt
, ap
);
104 battery_perc(const char *bat
)
110 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/capacity");
111 fp
= fopen(path
, "r");
113 warn("Failed to open file %s", path
);
114 return smprintf("%s", UNKNOWN_STR
);
116 fscanf(fp
, "%i", &perc
);
119 return smprintf("%d%%", perc
);
123 battery_power(const char *bat
)
129 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/power_now");
130 fp
= fopen(path
, "r");
132 warn("Failed to open file %s", path
);
133 return smprintf("%s", UNKNOWN_STR
);
135 fscanf(fp
, "%i", &watts
);
138 return smprintf("%d", (watts
+ 500000) / 1000000);
142 battery_state(const char *bat
)
148 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/status");
149 fp
= fopen(path
, "r");
151 warn("Failed to open file %s", path
);
152 return smprintf("%s", UNKNOWN_STR
);
154 fscanf(fp
, "%12s", state
);
157 if (strcmp(state
, "Charging") == 0) {
158 return smprintf("+");
159 } else if (strcmp(state
, "Discharging") == 0) {
160 return smprintf("-");
161 } else if (strcmp(state
, "Full") == 0) {
162 return smprintf("=");
163 } else if (strcmp(state
, "Unknown") == 0) {
164 return smprintf("/");
166 return smprintf("?");
174 long double a
[4], b
[4];
177 fp
= fopen("/proc/stat", "r");
179 warn("Failed to open file /proc/stat");
180 return smprintf("%s", UNKNOWN_STR
);
182 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
188 fp
= fopen("/proc/stat", "r");
190 warn("Failed to open file /proc/stat");
191 return smprintf("%s", UNKNOWN_STR
);
193 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
196 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]));
197 return smprintf("%d%%", perc
);
201 datetime(const char *fmt
)
207 if (strftime(str
, sizeof(str
), fmt
, localtime(&t
)) == 0) {
208 return smprintf("%s", UNKNOWN_STR
);
211 return smprintf("%s", str
);
215 disk_free(const char *mnt
)
219 if (statvfs(mnt
, &fs
) < 0) {
220 warn("Failed to get filesystem info");
221 return smprintf("%s", UNKNOWN_STR
);
224 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
228 disk_perc(const char *mnt
)
233 if (statvfs(mnt
, &fs
) < 0) {
234 warn("Failed to get filesystem info");
235 return smprintf("%s", UNKNOWN_STR
);
238 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
240 return smprintf("%d%%", perc
);
244 disk_total(const char *mnt
)
248 if (statvfs(mnt
, &fs
) < 0) {
249 warn("Failed to get filesystem info");
250 return smprintf("%s", UNKNOWN_STR
);
253 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
257 disk_used(const char *mnt
)
261 if (statvfs(mnt
, &fs
) < 0) {
262 warn("Failed to get filesystem info");
263 return smprintf("%s", UNKNOWN_STR
);
266 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
275 fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r");
277 warn("Failed to open file /proc/sys/kernel/random/entropy_avail");
278 return smprintf("%s", UNKNOWN_STR
);
280 fscanf(fp
, "%d", &num
);
283 return smprintf("%d", num
);
289 return smprintf("%d", getgid());
295 char buf
[HOST_NAME_MAX
];
297 if (gethostname(buf
, sizeof(buf
)) == -1) {
299 return smprintf("%s", UNKNOWN_STR
);
302 return smprintf("%s", buf
);
306 ip(const char *iface
)
308 struct ifaddrs
*ifaddr
, *ifa
;
310 char host
[NI_MAXHOST
];
312 if (getifaddrs(&ifaddr
) == -1) {
313 warn("Failed to get IP address for interface %s", iface
);
314 return smprintf("%s", UNKNOWN_STR
);
317 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
318 if (ifa
->ifa_addr
== NULL
) {
321 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
322 if ((strcmp(ifa
->ifa_name
, iface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
)) {
324 warnx("Failed to get IP address for interface %s", iface
);
325 return smprintf("%s", UNKNOWN_STR
);
327 return smprintf("%s", host
);
333 return smprintf("%s", UNKNOWN_STR
);
339 struct utsname udata
;
341 if (uname(&udata
) < 0) {
342 return smprintf(UNKNOWN_STR
);
345 return smprintf("%s", udata
.release
);
349 keyboard_indicators(void)
351 Display
*dpy
= XOpenDisplay(NULL
);
352 XKeyboardState state
;
353 XGetKeyboardControl(dpy
, &state
);
356 switch (state
.led_mask
) {
358 return smprintf("c");
361 return smprintf("n");
364 return smprintf("cn");
376 if (getloadavg(avgs
, 3) < 0) {
377 warnx("Failed to get the load avg");
378 return smprintf("%s", UNKNOWN_STR
);
381 return smprintf("%.2f %.2f %.2f", avgs
[0], avgs
[1], avgs
[2]);
390 fp
= fopen("/proc/meminfo", "r");
392 warn("Failed to open file /proc/meminfo");
393 return smprintf("%s", UNKNOWN_STR
);
395 fscanf(fp
, "MemFree: %ld kB\n", &free
);
398 return smprintf("%f", (float)free
/ 1024 / 1024);
404 long total
, free
, buffers
, cached
;
407 fp
= fopen("/proc/meminfo", "r");
409 warn("Failed to open file /proc/meminfo");
410 return smprintf("%s", UNKNOWN_STR
);
412 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
413 fscanf(fp
, "MemFree: %ld kB\n", &free
);
414 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
415 fscanf(fp
, "Cached: %ld kB\n", &cached
);
418 return smprintf("%d%%", 100 * ((total
- free
) - (buffers
+ cached
)) / total
);
427 fp
= fopen("/proc/meminfo", "r");
429 warn("Failed to open file /proc/meminfo");
430 return smprintf("%s", UNKNOWN_STR
);
432 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
435 return smprintf("%f", (float)total
/ 1024 / 1024);
441 long free
, total
, buffers
, cached
;
444 fp
= fopen("/proc/meminfo", "r");
446 warn("Failed to open file /proc/meminfo");
447 return smprintf("%s", UNKNOWN_STR
);
449 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
450 fscanf(fp
, "MemFree: %ld kB\n", &free
);
451 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
452 fscanf(fp
, "Cached: %ld kB\n", &cached
);
455 return smprintf("%f", (float)(total
- free
- buffers
- cached
) / 1024 / 1024);
459 run_command(const char *cmd
)
463 char buf
[1024] = UNKNOWN_STR
;
465 fp
= popen(cmd
, "r");
467 warn("Failed to get command output for %s", cmd
);
468 return smprintf("%s", UNKNOWN_STR
);
470 fgets(buf
, sizeof(buf
), fp
);
472 buf
[sizeof(buf
) - 1] = '\0';
474 if ((nlptr
= strrchr(buf
, '\n')) != NULL
) {
478 return smprintf("%s", buf
);
490 fp
= fopen("/proc/meminfo", "r");
492 warn("Failed to open file /proc/meminfo");
493 return smprintf("%s", UNKNOWN_STR
);
496 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
) - 1, fp
)) == 0) {
497 warn("swap_free: read error");
499 return smprintf("%s", UNKNOWN_STR
);
502 buf
[bytes_read
] = '\0';
505 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
506 return smprintf("%s", UNKNOWN_STR
);
508 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
510 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
511 return smprintf("%s", UNKNOWN_STR
);
513 sscanf(match
, "SwapFree: %ld kB\n", &free
);
515 return smprintf("%f", (float)free
/ 1024 / 1024);
521 long total
, free
, cached
;
527 fp
= fopen("/proc/meminfo", "r");
529 warn("Failed to open file /proc/meminfo");
530 return smprintf("%s", UNKNOWN_STR
);
533 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
) - 1, fp
)) == 0) {
534 warn("swap_perc: read error");
536 return smprintf("%s", UNKNOWN_STR
);
539 buf
[bytes_read
] = '\0';
542 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
543 return smprintf("%s", UNKNOWN_STR
);
545 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
547 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
548 return smprintf("%s", UNKNOWN_STR
);
550 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
552 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
553 return smprintf("%s", UNKNOWN_STR
);
555 sscanf(match
, "SwapFree: %ld kB\n", &free
);
557 return smprintf("%d%%", 100 * (total
- free
- cached
) / total
);
569 fp
= fopen("/proc/meminfo", "r");
571 warn("Failed to open file /proc/meminfo");
572 return smprintf("%s", UNKNOWN_STR
);
574 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
) - 1, fp
)) == 0) {
575 warn("swap_total: read error");
577 return smprintf("%s", UNKNOWN_STR
);
580 buf
[bytes_read
] = '\0';
583 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
584 return smprintf("%s", UNKNOWN_STR
);
586 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
588 return smprintf("%f", (float)total
/ 1024 / 1024);
594 long total
, free
, cached
;
600 fp
= fopen("/proc/meminfo", "r");
602 warn("Failed to open file /proc/meminfo");
603 return smprintf("%s", UNKNOWN_STR
);
605 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
) - 1, fp
)) == 0) {
606 warn("swap_used: read error");
608 return smprintf("%s", UNKNOWN_STR
);
611 buf
[bytes_read
] = '\0';
614 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
615 return smprintf("%s", UNKNOWN_STR
);
617 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
619 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
620 return smprintf("%s", UNKNOWN_STR
);
622 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
624 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
625 return smprintf("%s", UNKNOWN_STR
);
627 sscanf(match
, "SwapFree: %ld kB\n", &free
);
629 return smprintf("%f", (float)(total
- free
- cached
) / 1024 / 1024);
633 temp(const char *file
)
638 fp
= fopen(file
, "r");
640 warn("Failed to open file %s", file
);
641 return smprintf("%s", UNKNOWN_STR
);
643 fscanf(fp
, "%d", &temp
);
646 return smprintf("%d°C", temp
/ 1000);
657 h
= info
.uptime
/ 3600;
658 m
= (info
.uptime
- h
* 3600 ) / 60;
660 return smprintf("%dh %dm", h
, m
);
666 struct passwd
*pw
= getpwuid(geteuid());
669 warn("Failed to get username");
670 return smprintf("%s", UNKNOWN_STR
);
673 return smprintf("%s", pw
->pw_name
);
679 return smprintf("%d", geteuid());
684 vol_perc(const char *card
)
688 char *vnames
[] = SOUND_DEVICE_NAMES
;
690 afd
= open(card
, O_RDONLY
| O_NONBLOCK
);
692 warn("Cannot open %s", card
);
693 return smprintf(UNKNOWN_STR
);
696 if (ioctl(afd
, SOUND_MIXER_READ_DEVMASK
, &devmask
) == -1) {
697 warn("Cannot get volume for %s", card
);
699 return smprintf("%s", UNKNOWN_STR
);
701 for (i
= 0; i
< (sizeof(vnames
) / sizeof((vnames
[0]))); i
++) {
702 if (devmask
& (1 << i
) && !strcmp("vol", vnames
[i
])) {
703 if (ioctl(afd
, MIXER_READ(i
), &v
) == -1) {
704 warn("vol_perc: ioctl");
706 return smprintf("%s", UNKNOWN_STR
);
713 return smprintf("%d%%", v
& 0xff);
717 wifi_perc(const char *iface
)
726 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/net/", iface
, "/operstate");
727 fp
= fopen(path
, "r");
729 warn("Failed to open file %s", path
);
730 return smprintf("%s", UNKNOWN_STR
);
732 fgets(status
, 5, fp
);
734 if(strcmp(status
, "up\n") != 0) {
735 return smprintf("%s", UNKNOWN_STR
);
738 fp
= fopen("/proc/net/wireless", "r");
740 warn("Failed to open file /proc/net/wireless");
741 return smprintf("%s", UNKNOWN_STR
);
744 fgets(buf
, sizeof(buf
), fp
);
745 fgets(buf
, sizeof(buf
), fp
);
746 fgets(buf
, sizeof(buf
), fp
);
749 if ((datastart
= strstr(buf
, iface
)) == NULL
) {
750 return smprintf("%s", UNKNOWN_STR
);
752 datastart
= (datastart
+(strlen(iface
)+1));
753 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc
);
755 return smprintf("%d%%", perc
);
759 wifi_essid(const char *iface
)
761 char id
[IW_ESSID_MAX_SIZE
+1];
762 int sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
765 memset(&wreq
, 0, sizeof(struct iwreq
));
766 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
767 snprintf(wreq
.ifr_name
, sizeof(wreq
.ifr_name
), "%s", iface
);
770 warn("Failed to get ESSID for interface %s", iface
);
771 return smprintf("%s", UNKNOWN_STR
);
773 wreq
.u
.essid
.pointer
= id
;
774 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
775 warn("Failed to get ESSID for interface %s", iface
);
776 return smprintf("%s", UNKNOWN_STR
);
781 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0)
782 return smprintf("%s", UNKNOWN_STR
);
784 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
788 sighandler(const int signo
)
790 if (signo
== SIGTERM
|| signo
== SIGINT
) {
796 usage(const int eval
)
798 fprintf(stderr
, "usage: %s [-d] [-o] [-n] [-v] [-h]\n", argv0
);
803 main(int argc
, char *argv
[])
805 unsigned short int i
;
806 char status_string
[2048];
809 struct sigaction act
;
822 printf("slstatus (C) 2016-2017 slstatus engineers\n");
830 if ((dflag
&& oflag
) || (dflag
&& nflag
) || (oflag
&& nflag
)) {
833 if (dflag
&& daemon(1, 1) < 0) {
837 memset(&act
, 0, sizeof(act
));
838 act
.sa_handler
= sighandler
;
839 sigaction(SIGINT
, &act
, 0);
840 sigaction(SIGTERM
, &act
, 0);
843 dpy
= XOpenDisplay(NULL
);
846 setlocale(LC_ALL
, "");
849 status_string
[0] = '\0';
851 for (i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
853 if (argument
.args
== NULL
) {
854 res
= argument
.func();
856 res
= argument
.func(argument
.args
);
858 element
= smprintf(argument
.fmt
, res
);
859 if (element
== NULL
) {
860 element
= smprintf("%s", UNKNOWN_STR
);
861 warnx("Failed to format output");
863 strncat(status_string
, element
, sizeof(status_string
) - strlen(status_string
) - 1);
869 printf("%s\n", status_string
);
871 printf("%s\n", status_string
);
874 XStoreName(dpy
, DefaultRootWindow(dpy
), status_string
);
878 if ((UPDATE_INTERVAL
- delay
) <= 0) {
882 sleep(UPDATE_INTERVAL
- delay
);
888 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);