Xinqi Bao's Git
d29436db7ee58575f1d2e3fbd649940bf400e452
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
);
333 switch (state
.led_mask
) {
335 return smprintf("c");
338 return smprintf("n");
341 return smprintf("cn");
355 if (getloadavg(avgs
, 3) < 0) {
356 warnx("Failed to get the load avg");
357 return smprintf("%s", UNKNOWN_STR
);
360 return smprintf("%.2f %.2f %.2f", avgs
[0], avgs
[1], avgs
[2]);
369 fp
= fopen("/proc/meminfo", "r");
371 warn("Failed to open file /proc/meminfo");
372 return smprintf("%s", UNKNOWN_STR
);
374 fscanf(fp
, "MemFree: %ld kB\n", &free
);
377 return smprintf("%f", (float)free
/ 1024 / 1024);
383 long total
, free
, buffers
, cached
;
386 fp
= fopen("/proc/meminfo", "r");
388 warn("Failed to open file /proc/meminfo");
389 return smprintf("%s", UNKNOWN_STR
);
391 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
392 fscanf(fp
, "MemFree: %ld kB\n", &free
);
393 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
394 fscanf(fp
, "Cached: %ld kB\n", &cached
);
397 return smprintf("%d%%", 100 * ((total
- free
) - (buffers
+ cached
)) / total
);
406 fp
= fopen("/proc/meminfo", "r");
408 warn("Failed to open file /proc/meminfo");
409 return smprintf("%s", UNKNOWN_STR
);
411 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
414 return smprintf("%f", (float)total
/ 1024 / 1024);
420 long free
, total
, buffers
, cached
;
423 fp
= fopen("/proc/meminfo", "r");
425 warn("Failed to open file /proc/meminfo");
426 return smprintf("%s", UNKNOWN_STR
);
428 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
429 fscanf(fp
, "MemFree: %ld kB\n", &free
);
430 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
431 fscanf(fp
, "Cached: %ld kB\n", &cached
);
434 return smprintf("%f", (float)(total
- free
- buffers
- cached
) / 1024 / 1024);
438 run_command(const char *cmd
)
442 char buf
[1024] = UNKNOWN_STR
;
444 fp
= popen(cmd
, "r");
446 warn("Failed to get command output for %s", cmd
);
447 return smprintf("%s", UNKNOWN_STR
);
449 fgets(buf
, sizeof(buf
), fp
);
451 buf
[sizeof(buf
)] = '\0';
453 if ((nlptr
= strstr(buf
, "\n")) != NULL
) {
457 return smprintf("%s", buf
);
469 fp
= fopen("/proc/meminfo", "r");
471 warn("Failed to open file /proc/meminfo");
472 return smprintf("%s", UNKNOWN_STR
);
475 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
476 warn("swap_free: read error");
478 return smprintf("%s", UNKNOWN_STR
);
481 buf
[bytes_read
] = '\0';
484 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
485 return smprintf("%s", UNKNOWN_STR
);
487 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
489 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
490 return smprintf("%s", UNKNOWN_STR
);
492 sscanf(match
, "SwapFree: %ld kB\n", &free
);
494 return smprintf("%f", (float)free
/ 1024 / 1024);
500 long total
, free
, cached
;
506 fp
= fopen("/proc/meminfo", "r");
508 warn("Failed to open file /proc/meminfo");
509 return smprintf("%s", UNKNOWN_STR
);
512 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
513 warn("swap_perc: read error");
515 return smprintf("%s", UNKNOWN_STR
);
518 buf
[bytes_read
] = '\0';
521 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
522 return smprintf("%s", UNKNOWN_STR
);
524 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
526 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
527 return smprintf("%s", UNKNOWN_STR
);
529 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
531 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
532 return smprintf("%s", UNKNOWN_STR
);
534 sscanf(match
, "SwapFree: %ld kB\n", &free
);
536 return smprintf("%d%%", 100 * (total
- free
- cached
) / total
);
548 fp
= fopen("/proc/meminfo", "r");
550 warn("Failed to open file /proc/meminfo");
551 return smprintf("%s", UNKNOWN_STR
);
553 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
554 warn("swap_total: read error");
556 return smprintf("%s", UNKNOWN_STR
);
559 buf
[bytes_read
] = '\0';
562 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
563 return smprintf("%s", UNKNOWN_STR
);
565 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
567 return smprintf("%f", (float)total
/ 1024 / 1024);
573 long total
, free
, cached
;
579 fp
= fopen("/proc/meminfo", "r");
581 warn("Failed to open file /proc/meminfo");
582 return smprintf("%s", UNKNOWN_STR
);
584 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
), fp
)) == 0) {
585 warn("swap_used: read error");
587 return smprintf("%s", UNKNOWN_STR
);
590 buf
[bytes_read
] = '\0';
593 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
594 return smprintf("%s", UNKNOWN_STR
);
596 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
598 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
599 return smprintf("%s", UNKNOWN_STR
);
601 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
603 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
604 return smprintf("%s", UNKNOWN_STR
);
606 sscanf(match
, "SwapFree: %ld kB\n", &free
);
608 return smprintf("%f", (float)(total
- free
- cached
) / 1024 / 1024);
612 temp(const char *file
)
617 fp
= fopen(file
, "r");
619 warn("Failed to open file %s", file
);
620 return smprintf("%s", UNKNOWN_STR
);
622 fscanf(fp
, "%d", &temp
);
625 return smprintf("%d°C", temp
/ 1000);
636 h
= info
.uptime
/ 3600;
637 m
= (info
.uptime
- h
* 3600 ) / 60;
639 return smprintf("%dh %dm", h
, m
);
645 uid_t uid
= geteuid();
646 struct passwd
*pw
= getpwuid(uid
);
649 warn("Failed to get username");
650 return smprintf("%s", UNKNOWN_STR
);
653 return smprintf("%s", pw
->pw_name
);
659 return smprintf("%d", geteuid());
664 vol_perc(const char *card
)
668 char *vnames
[] = SOUND_DEVICE_NAMES
;
670 afd
= open(card
, O_RDONLY
);
672 warn("Cannot open %s", card
);
673 return smprintf(UNKNOWN_STR
);
676 ioctl(afd
, MIXER_READ(SOUND_MIXER_DEVMASK
), &devmask
);
677 for (i
= 0; i
< (sizeof(vnames
) / sizeof((vnames
[0]))); i
++) {
678 if (devmask
& (1 << i
)) {
679 if (!strcmp("vol", vnames
[i
])) {
680 ioctl(afd
, MIXER_READ(i
), &v
);
687 return smprintf("%d%%", v
& 0xff);
691 wifi_perc(const char *iface
)
700 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/net/", iface
, "/operstate");
701 fp
= fopen(path
, "r");
703 warn("Failed to open file %s", path
);
704 return smprintf("%s", UNKNOWN_STR
);
706 fgets(status
, 5, fp
);
708 if(strcmp(status
, "up\n") != 0) {
709 return smprintf("%s", UNKNOWN_STR
);
712 fp
= fopen("/proc/net/wireless", "r");
714 warn("Failed to open file /proc/net/wireless");
715 return smprintf("%s", UNKNOWN_STR
);
718 fgets(buf
, sizeof(buf
), fp
);
719 fgets(buf
, sizeof(buf
), fp
);
720 fgets(buf
, sizeof(buf
), fp
);
723 if ((datastart
= strstr(buf
, iface
)) == NULL
) {
724 return smprintf("%s", UNKNOWN_STR
);
726 datastart
= (datastart
+(strlen(iface
)+1));
727 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc
);
729 return smprintf("%d%%", perc
);
733 wifi_essid(const char *iface
)
735 char id
[IW_ESSID_MAX_SIZE
+1];
736 int sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
739 memset(&wreq
, 0, sizeof(struct iwreq
));
740 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
741 snprintf(wreq
.ifr_name
, sizeof(wreq
.ifr_name
), "%s", iface
);
744 warn("Failed to get ESSID for interface %s", iface
);
745 return smprintf("%s", UNKNOWN_STR
);
747 wreq
.u
.essid
.pointer
= id
;
748 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
749 warn("Failed to get ESSID for interface %s", iface
);
750 return smprintf("%s", UNKNOWN_STR
);
755 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0)
756 return smprintf("%s", UNKNOWN_STR
);
758 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
762 sighandler(const int signo
)
764 if (signo
== SIGTERM
|| signo
== SIGINT
) {
770 usage(const int eval
)
772 fprintf(stderr
, "usage: %s [-d] [-o] [-v] [-h]\n", argv0
);
777 main(int argc
, char *argv
[])
779 unsigned short int i
;
780 char status_string
[2048];
783 struct sigaction act
;
793 printf("slstatus (C) 2016-2017 slstatus engineers\n");
801 if (dflag
&& oflag
) {
804 if (dflag
&& daemon(1, 1) < 0) {
808 memset(&act
, 0, sizeof(act
));
809 act
.sa_handler
= sighandler
;
810 sigaction(SIGINT
, &act
, 0);
811 sigaction(SIGTERM
, &act
, 0);
814 dpy
= XOpenDisplay(NULL
);
817 setlocale(LC_ALL
, "");
820 status_string
[0] = '\0';
822 for (i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
824 if (argument
.args
== NULL
) {
825 res
= argument
.func();
827 res
= argument
.func(argument
.args
);
829 element
= smprintf(argument
.fmt
, res
);
830 if (element
== NULL
) {
831 element
= smprintf("%s", UNKNOWN_STR
);
832 warnx("Failed to format output");
834 strncat(status_string
, element
, sizeof(status_string
) - strlen(status_string
) - 1);
840 XStoreName(dpy
, DefaultRootWindow(dpy
), status_string
);
843 printf("%s\n", status_string
);
846 if ((UPDATE_INTERVAL
- delay
) <= 0) {
850 sleep(UPDATE_INTERVAL
- delay
);
856 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);