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_state(const char *bat
);
39 static char *cpu_perc(void);
40 static char *datetime(const char *fmt
);
41 static char *disk_free(const char *mnt
);
42 static char *disk_perc(const char *mnt
);
43 static char *disk_total(const char *mnt
);
44 static char *disk_used(const char *mnt
);
45 static char *entropy(void);
46 static char *gid(void);
47 static char *hostname(void);
48 static char *ip(const char *iface
);
49 static char *kernel_release(void);
50 static char *keyboard_indicators(void);
51 static char *load_avg(void);
52 static char *ram_free(void);
53 static char *ram_perc(void);
54 static char *ram_used(void);
55 static char *ram_total(void);
56 static char *run_command(const char *cmd
);
57 static char *swap_free(void);
58 static char *swap_perc(void);
59 static char *swap_used(void);
60 static char *swap_total(void);
61 static char *temp(const char *file
);
62 static char *uid(void);
63 static char *uptime(void);
64 static char *username(void);
65 static char *vol_perc(const char *card
);
66 static char *wifi_perc(const char *iface
);
67 static char *wifi_essid(const char *iface
);
68 static void sighandler(const int signo
);
69 static void usage(const int eval
);
72 static unsigned short int delay
= 0;
73 static unsigned short int done
;
74 static unsigned short int dflag
, oflag
;
80 smprintf(const char *fmt
, ...)
87 len
= vsnprintf(NULL
, 0, fmt
, ap
);
96 vsnprintf(ret
, len
, fmt
, ap
);
103 battery_perc(const char *bat
)
109 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/capacity");
110 fp
= fopen(path
, "r");
112 warn("Failed to open file %s", path
);
113 return smprintf("%s", UNKNOWN_STR
);
115 fscanf(fp
, "%i", &perc
);
118 return smprintf("%d%%", perc
);
122 battery_state(const char *bat
)
128 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/power_supply/", bat
, "/status");
129 fp
= fopen(path
, "r");
131 warn("Failed to open file %s", path
);
132 return smprintf("%s", UNKNOWN_STR
);
134 fscanf(fp
, "%12s", state
);
137 if (strcmp(state
, "Charging") == 0) {
138 return smprintf("+");
139 } else if (strcmp(state
, "Discharging") == 0) {
140 return smprintf("-");
141 } else if (strcmp(state
, "Full") == 0) {
142 return smprintf("=");
144 return smprintf("?");
152 long double a
[4], b
[4];
155 fp
= fopen("/proc/stat", "r");
157 warn("Failed to open file /proc/stat");
158 return smprintf("%s", UNKNOWN_STR
);
160 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &a
[0], &a
[1], &a
[2], &a
[3]);
166 fp
= fopen("/proc/stat", "r");
168 warn("Failed to open file /proc/stat");
169 return smprintf("%s", UNKNOWN_STR
);
171 fscanf(fp
, "%*s %Lf %Lf %Lf %Lf", &b
[0], &b
[1], &b
[2], &b
[3]);
174 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]));
175 return smprintf("%d%%", perc
);
179 datetime(const char *fmt
)
185 if (strftime(str
, sizeof(str
), fmt
, localtime(&t
)) == 0) {
186 return smprintf("%s", UNKNOWN_STR
);
189 return smprintf("%s", str
);
193 disk_free(const char *mnt
)
197 if (statvfs(mnt
, &fs
) < 0) {
198 warn("Failed to get filesystem info");
199 return smprintf("%s", UNKNOWN_STR
);
202 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_bfree
/ 1024 / 1024 / 1024);
206 disk_perc(const char *mnt
)
211 if (statvfs(mnt
, &fs
) < 0) {
212 warn("Failed to get filesystem info");
213 return smprintf("%s", UNKNOWN_STR
);
216 perc
= 100 * (1.0f
- ((float)fs
.f_bfree
/ (float)fs
.f_blocks
));
218 return smprintf("%d%%", perc
);
222 disk_total(const char *mnt
)
226 if (statvfs(mnt
, &fs
) < 0) {
227 warn("Failed to get filesystem info");
228 return smprintf("%s", UNKNOWN_STR
);
231 return smprintf("%f", (float)fs
.f_bsize
* (float)fs
.f_blocks
/ 1024 / 1024 / 1024);
235 disk_used(const char *mnt
)
239 if (statvfs(mnt
, &fs
) < 0) {
240 warn("Failed to get filesystem info");
241 return smprintf("%s", UNKNOWN_STR
);
244 return smprintf("%f", (float)fs
.f_bsize
* ((float)fs
.f_blocks
- (float)fs
.f_bfree
) / 1024 / 1024 / 1024);
253 fp
= fopen("/proc/sys/kernel/random/entropy_avail", "r");
255 warn("Failed to open file /proc/sys/kernel/random/entropy_avail");
256 return smprintf("%s", UNKNOWN_STR
);
258 fscanf(fp
, "%d", &num
);
261 return smprintf("%d", num
);
267 return smprintf("%d", getgid());
273 char buf
[HOST_NAME_MAX
];
275 if (gethostname(buf
, sizeof(buf
)) == -1) {
277 return smprintf("%s", UNKNOWN_STR
);
280 return smprintf("%s", buf
);
284 ip(const char *iface
)
286 struct ifaddrs
*ifaddr
, *ifa
;
288 char host
[NI_MAXHOST
];
290 if (getifaddrs(&ifaddr
) == -1) {
291 warn("Failed to get IP address for interface %s", iface
);
292 return smprintf("%s", UNKNOWN_STR
);
295 for (ifa
= ifaddr
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
296 if (ifa
->ifa_addr
== NULL
) {
299 s
= getnameinfo(ifa
->ifa_addr
, sizeof(struct sockaddr_in
), host
, NI_MAXHOST
, NULL
, 0, NI_NUMERICHOST
);
300 if ((strcmp(ifa
->ifa_name
, iface
) == 0) && (ifa
->ifa_addr
->sa_family
== AF_INET
)) {
302 warnx("Failed to get IP address for interface %s", iface
);
303 return smprintf("%s", UNKNOWN_STR
);
305 return smprintf("%s", host
);
311 return smprintf("%s", UNKNOWN_STR
);
317 struct utsname udata
;
319 if (uname(&udata
) < 0) {
320 return smprintf(UNKNOWN_STR
);
323 return smprintf("%s", udata
.release
);
327 keyboard_indicators(void)
329 Display
*dpy
= XOpenDisplay(NULL
);
330 XKeyboardState state
;
331 XGetKeyboardControl(dpy
, &state
);
334 switch (state
.led_mask
) {
336 return smprintf("c");
339 return smprintf("n");
342 return smprintf("cn");
354 if (getloadavg(avgs
, 3) < 0) {
355 warnx("Failed to get the load avg");
356 return smprintf("%s", UNKNOWN_STR
);
359 return smprintf("%.2f %.2f %.2f", avgs
[0], avgs
[1], avgs
[2]);
368 fp
= fopen("/proc/meminfo", "r");
370 warn("Failed to open file /proc/meminfo");
371 return smprintf("%s", UNKNOWN_STR
);
373 fscanf(fp
, "MemFree: %ld kB\n", &free
);
376 return smprintf("%f", (float)free
/ 1024 / 1024);
382 long total
, free
, buffers
, cached
;
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
);
391 fscanf(fp
, "MemFree: %ld kB\n", &free
);
392 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
393 fscanf(fp
, "Cached: %ld kB\n", &cached
);
396 return smprintf("%d%%", 100 * ((total
- free
) - (buffers
+ cached
)) / total
);
405 fp
= fopen("/proc/meminfo", "r");
407 warn("Failed to open file /proc/meminfo");
408 return smprintf("%s", UNKNOWN_STR
);
410 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
413 return smprintf("%f", (float)total
/ 1024 / 1024);
419 long free
, total
, buffers
, cached
;
422 fp
= fopen("/proc/meminfo", "r");
424 warn("Failed to open file /proc/meminfo");
425 return smprintf("%s", UNKNOWN_STR
);
427 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
428 fscanf(fp
, "MemFree: %ld kB\n", &free
);
429 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
430 fscanf(fp
, "Cached: %ld kB\n", &cached
);
433 return smprintf("%f", (float)(total
- free
- buffers
- cached
) / 1024 / 1024);
437 run_command(const char *cmd
)
441 char buf
[1024] = UNKNOWN_STR
;
443 fp
= popen(cmd
, "r");
445 warn("Failed to get command output for %s", cmd
);
446 return smprintf("%s", UNKNOWN_STR
);
448 fgets(buf
, sizeof(buf
), fp
);
450 buf
[sizeof(buf
)] = '\0';
452 if ((nlptr
= strstr(buf
, "\n")) != NULL
) {
456 return smprintf("%s", buf
);
468 fp
= fopen("/proc/meminfo", "r");
470 warn("Failed to open file /proc/meminfo");
471 return smprintf("%s", UNKNOWN_STR
);
474 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
475 warn("swap_free: read error");
477 return smprintf("%s", UNKNOWN_STR
);
480 buf
[bytes_read
] = '\0';
483 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
484 return smprintf("%s", UNKNOWN_STR
);
486 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
488 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
489 return smprintf("%s", UNKNOWN_STR
);
491 sscanf(match
, "SwapFree: %ld kB\n", &free
);
493 return smprintf("%f", (float)free
/ 1024 / 1024);
499 long total
, free
, cached
;
505 fp
= fopen("/proc/meminfo", "r");
507 warn("Failed to open file /proc/meminfo");
508 return smprintf("%s", UNKNOWN_STR
);
511 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
512 warn("swap_perc: read error");
514 return smprintf("%s", UNKNOWN_STR
);
517 buf
[bytes_read
] = '\0';
520 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
521 return smprintf("%s", UNKNOWN_STR
);
523 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
525 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
526 return smprintf("%s", UNKNOWN_STR
);
528 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
530 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
531 return smprintf("%s", UNKNOWN_STR
);
533 sscanf(match
, "SwapFree: %ld kB\n", &free
);
535 return smprintf("%d%%", 100 * (total
- free
- cached
) / total
);
547 fp
= fopen("/proc/meminfo", "r");
549 warn("Failed to open file /proc/meminfo");
550 return smprintf("%s", UNKNOWN_STR
);
552 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
553 warn("swap_total: read error");
555 return smprintf("%s", UNKNOWN_STR
);
558 buf
[bytes_read
] = '\0';
561 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
562 return smprintf("%s", UNKNOWN_STR
);
564 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
566 return smprintf("%f", (float)total
/ 1024 / 1024);
572 long total
, free
, cached
;
578 fp
= fopen("/proc/meminfo", "r");
580 warn("Failed to open file /proc/meminfo");
581 return smprintf("%s", UNKNOWN_STR
);
583 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
584 warn("swap_used: read error");
586 return smprintf("%s", UNKNOWN_STR
);
589 buf
[bytes_read
] = '\0';
592 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
593 return smprintf("%s", UNKNOWN_STR
);
595 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
597 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
598 return smprintf("%s", UNKNOWN_STR
);
600 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
602 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
603 return smprintf("%s", UNKNOWN_STR
);
605 sscanf(match
, "SwapFree: %ld kB\n", &free
);
607 return smprintf("%f", (float)(total
- free
- cached
) / 1024 / 1024);
611 temp(const char *file
)
616 fp
= fopen(file
, "r");
618 warn("Failed to open file %s", file
);
619 return smprintf("%s", UNKNOWN_STR
);
621 fscanf(fp
, "%d", &temp
);
624 return smprintf("%d°C", temp
/ 1000);
635 h
= info
.uptime
/ 3600;
636 m
= (info
.uptime
- h
* 3600 ) / 60;
638 return smprintf("%dh %dm", h
, m
);
644 uid_t uid
= geteuid();
645 struct passwd
*pw
= getpwuid(uid
);
648 warn("Failed to get username");
649 return smprintf("%s", UNKNOWN_STR
);
652 return smprintf("%s", pw
->pw_name
);
658 return smprintf("%d", geteuid());
663 vol_perc(const char *card
)
667 char *vnames
[] = SOUND_DEVICE_NAMES
;
669 afd
= open(card
, O_RDONLY
);
671 warn("Cannot open %s", card
);
672 return smprintf(UNKNOWN_STR
);
675 ioctl(afd
, MIXER_READ(SOUND_MIXER_DEVMASK
), &devmask
);
676 for (i
= 0; i
< (sizeof(vnames
) / sizeof((vnames
[0]))); i
++) {
677 if (devmask
& (1 << i
)) {
678 if (!strcmp("vol", vnames
[i
])) {
679 ioctl(afd
, MIXER_READ(i
), &v
);
686 return smprintf("%d%%", v
& 0xff);
690 wifi_perc(const char *iface
)
699 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/net/", iface
, "/operstate");
700 fp
= fopen(path
, "r");
702 warn("Failed to open file %s", path
);
703 return smprintf("%s", UNKNOWN_STR
);
705 fgets(status
, 5, fp
);
707 if(strcmp(status
, "up\n") != 0) {
708 return smprintf("%s", UNKNOWN_STR
);
711 fp
= fopen("/proc/net/wireless", "r");
713 warn("Failed to open file /proc/net/wireless");
714 return smprintf("%s", UNKNOWN_STR
);
717 fgets(buf
, sizeof(buf
), fp
);
718 fgets(buf
, sizeof(buf
), fp
);
719 fgets(buf
, sizeof(buf
), fp
);
722 if ((datastart
= strstr(buf
, iface
)) == NULL
) {
723 return smprintf("%s", UNKNOWN_STR
);
725 datastart
= (datastart
+(strlen(iface
)+1));
726 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc
);
728 return smprintf("%d%%", perc
);
732 wifi_essid(const char *iface
)
734 char id
[IW_ESSID_MAX_SIZE
+1];
735 int sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
738 memset(&wreq
, 0, sizeof(struct iwreq
));
739 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
740 snprintf(wreq
.ifr_name
, sizeof(wreq
.ifr_name
), "%s", iface
);
743 warn("Failed to get ESSID for interface %s", iface
);
744 return smprintf("%s", UNKNOWN_STR
);
746 wreq
.u
.essid
.pointer
= id
;
747 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
748 warn("Failed to get ESSID for interface %s", iface
);
749 return smprintf("%s", UNKNOWN_STR
);
754 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0)
755 return smprintf("%s", UNKNOWN_STR
);
757 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
761 sighandler(const int signo
)
763 if (signo
== SIGTERM
|| signo
== SIGINT
) {
769 usage(const int eval
)
771 fprintf(stderr
, "usage: %s [-d] [-o] [-v] [-h]\n", argv0
);
776 main(int argc
, char *argv
[])
778 unsigned short int i
;
779 char status_string
[2048];
782 struct sigaction act
;
792 printf("slstatus (C) 2016-2017 slstatus engineers\n");
800 if (dflag
&& oflag
) {
803 if (dflag
&& daemon(1, 1) < 0) {
807 memset(&act
, 0, sizeof(act
));
808 act
.sa_handler
= sighandler
;
809 sigaction(SIGINT
, &act
, 0);
810 sigaction(SIGTERM
, &act
, 0);
813 dpy
= XOpenDisplay(NULL
);
816 setlocale(LC_ALL
, "");
819 status_string
[0] = '\0';
821 for (i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
823 if (argument
.args
== NULL
) {
824 res
= argument
.func();
826 res
= argument
.func(argument
.args
);
828 element
= smprintf(argument
.fmt
, res
);
829 if (element
== NULL
) {
830 element
= smprintf("%s", UNKNOWN_STR
);
831 warnx("Failed to format output");
833 strncat(status_string
, element
, sizeof(status_string
) - strlen(status_string
) - 1);
839 XStoreName(dpy
, DefaultRootWindow(dpy
), status_string
);
842 printf("%s\n", status_string
);
845 if ((UPDATE_INTERVAL
- delay
) <= 0) {
849 sleep(UPDATE_INTERVAL
- delay
);
855 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);