Xinqi Bao's Git
6deb68cf82c9a0e8860335c08ed0d244a0155ac5
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("=");
143 } else if (strcmp(state
, "Unknown") == 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
);
329 keyboard_indicators(void)
331 Display
*dpy
= XOpenDisplay(NULL
);
332 XKeyboardState state
;
333 XGetKeyboardControl(dpy
, &state
);
336 switch (state
.led_mask
) {
338 return smprintf("c");
341 return smprintf("n");
344 return smprintf("cn");
356 if (getloadavg(avgs
, 3) < 0) {
357 warnx("Failed to get the load avg");
358 return smprintf("%s", UNKNOWN_STR
);
361 return smprintf("%.2f %.2f %.2f", avgs
[0], avgs
[1], avgs
[2]);
370 fp
= fopen("/proc/meminfo", "r");
372 warn("Failed to open file /proc/meminfo");
373 return smprintf("%s", UNKNOWN_STR
);
375 fscanf(fp
, "MemFree: %ld kB\n", &free
);
378 return smprintf("%f", (float)free
/ 1024 / 1024);
384 long total
, free
, buffers
, cached
;
387 fp
= fopen("/proc/meminfo", "r");
389 warn("Failed to open file /proc/meminfo");
390 return smprintf("%s", UNKNOWN_STR
);
392 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
393 fscanf(fp
, "MemFree: %ld kB\n", &free
);
394 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
395 fscanf(fp
, "Cached: %ld kB\n", &cached
);
398 return smprintf("%d%%", 100 * ((total
- free
) - (buffers
+ cached
)) / total
);
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
);
415 return smprintf("%f", (float)total
/ 1024 / 1024);
421 long free
, total
, buffers
, cached
;
424 fp
= fopen("/proc/meminfo", "r");
426 warn("Failed to open file /proc/meminfo");
427 return smprintf("%s", UNKNOWN_STR
);
429 fscanf(fp
, "MemTotal: %ld kB\n", &total
);
430 fscanf(fp
, "MemFree: %ld kB\n", &free
);
431 fscanf(fp
, "MemAvailable: %ld kB\nBuffers: %ld kB\n", &buffers
, &buffers
);
432 fscanf(fp
, "Cached: %ld kB\n", &cached
);
435 return smprintf("%f", (float)(total
- free
- buffers
- cached
) / 1024 / 1024);
439 run_command(const char *cmd
)
443 char buf
[1024] = UNKNOWN_STR
;
445 fp
= popen(cmd
, "r");
447 warn("Failed to get command output for %s", cmd
);
448 return smprintf("%s", UNKNOWN_STR
);
450 fgets(buf
, sizeof(buf
) - 1, fp
);
452 buf
[sizeof(buf
) - 1] = '\0';
454 if ((nlptr
= strstr(buf
, "\n")) != NULL
) {
458 return smprintf("%s", buf
);
470 fp
= fopen("/proc/meminfo", "r");
472 warn("Failed to open file /proc/meminfo");
473 return smprintf("%s", UNKNOWN_STR
);
476 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
) - 1, fp
)) == 0) {
477 warn("swap_free: read error");
479 return smprintf("%s", UNKNOWN_STR
);
482 buf
[bytes_read
] = '\0';
485 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
486 return smprintf("%s", UNKNOWN_STR
);
488 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
490 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
491 return smprintf("%s", UNKNOWN_STR
);
493 sscanf(match
, "SwapFree: %ld kB\n", &free
);
495 return smprintf("%f", (float)free
/ 1024 / 1024);
501 long total
, free
, cached
;
507 fp
= fopen("/proc/meminfo", "r");
509 warn("Failed to open file /proc/meminfo");
510 return smprintf("%s", UNKNOWN_STR
);
513 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
) - 1, fp
)) == 0) {
514 warn("swap_perc: read error");
516 return smprintf("%s", UNKNOWN_STR
);
519 buf
[bytes_read
] = '\0';
522 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
523 return smprintf("%s", UNKNOWN_STR
);
525 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
527 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
528 return smprintf("%s", UNKNOWN_STR
);
530 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
532 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
533 return smprintf("%s", UNKNOWN_STR
);
535 sscanf(match
, "SwapFree: %ld kB\n", &free
);
537 return smprintf("%d%%", 100 * (total
- free
- cached
) / total
);
549 fp
= fopen("/proc/meminfo", "r");
551 warn("Failed to open file /proc/meminfo");
552 return smprintf("%s", UNKNOWN_STR
);
554 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
) - 1, fp
)) == 0) {
555 warn("swap_total: read error");
557 return smprintf("%s", UNKNOWN_STR
);
560 buf
[bytes_read
] = '\0';
563 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
564 return smprintf("%s", UNKNOWN_STR
);
566 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
568 return smprintf("%f", (float)total
/ 1024 / 1024);
574 long total
, free
, cached
;
580 fp
= fopen("/proc/meminfo", "r");
582 warn("Failed to open file /proc/meminfo");
583 return smprintf("%s", UNKNOWN_STR
);
585 if ((bytes_read
= fread(buf
, sizeof(char), sizeof(buf
) - 1, fp
)) == 0) {
586 warn("swap_used: read error");
588 return smprintf("%s", UNKNOWN_STR
);
591 buf
[bytes_read
] = '\0';
594 if ((match
= strstr(buf
, "SwapTotal")) == NULL
) {
595 return smprintf("%s", UNKNOWN_STR
);
597 sscanf(match
, "SwapTotal: %ld kB\n", &total
);
599 if ((match
= strstr(buf
, "SwapCached")) == NULL
) {
600 return smprintf("%s", UNKNOWN_STR
);
602 sscanf(match
, "SwapCached: %ld kB\n", &cached
);
604 if ((match
= strstr(buf
, "SwapFree")) == NULL
) {
605 return smprintf("%s", UNKNOWN_STR
);
607 sscanf(match
, "SwapFree: %ld kB\n", &free
);
609 return smprintf("%f", (float)(total
- free
- cached
) / 1024 / 1024);
613 temp(const char *file
)
618 fp
= fopen(file
, "r");
620 warn("Failed to open file %s", file
);
621 return smprintf("%s", UNKNOWN_STR
);
623 fscanf(fp
, "%d", &temp
);
626 return smprintf("%d°C", temp
/ 1000);
637 h
= info
.uptime
/ 3600;
638 m
= (info
.uptime
- h
* 3600 ) / 60;
640 return smprintf("%dh %dm", h
, m
);
646 uid_t uid
= geteuid();
647 struct passwd
*pw
= getpwuid(uid
);
650 warn("Failed to get username");
651 return smprintf("%s", UNKNOWN_STR
);
654 return smprintf("%s", pw
->pw_name
);
660 return smprintf("%d", geteuid());
665 vol_perc(const char *card
)
669 char *vnames
[] = SOUND_DEVICE_NAMES
;
671 afd
= open(card
, O_RDONLY
);
673 warn("Cannot open %s", card
);
674 return smprintf(UNKNOWN_STR
);
677 ioctl(afd
, MIXER_READ(SOUND_MIXER_DEVMASK
), &devmask
);
678 for (i
= 0; i
< (sizeof(vnames
) / sizeof((vnames
[0]))); i
++) {
679 if (devmask
& (1 << i
)) {
680 if (!strcmp("vol", vnames
[i
])) {
681 ioctl(afd
, MIXER_READ(i
), &v
);
688 return smprintf("%d%%", v
& 0xff);
692 wifi_perc(const char *iface
)
701 snprintf(path
, sizeof(path
), "%s%s%s", "/sys/class/net/", iface
, "/operstate");
702 fp
= fopen(path
, "r");
704 warn("Failed to open file %s", path
);
705 return smprintf("%s", UNKNOWN_STR
);
707 fgets(status
, 5, fp
);
709 if(strcmp(status
, "up\n") != 0) {
710 return smprintf("%s", UNKNOWN_STR
);
713 fp
= fopen("/proc/net/wireless", "r");
715 warn("Failed to open file /proc/net/wireless");
716 return smprintf("%s", UNKNOWN_STR
);
719 fgets(buf
, sizeof(buf
), fp
);
720 fgets(buf
, sizeof(buf
), fp
);
721 fgets(buf
, sizeof(buf
), fp
);
724 if ((datastart
= strstr(buf
, iface
)) == NULL
) {
725 return smprintf("%s", UNKNOWN_STR
);
727 datastart
= (datastart
+(strlen(iface
)+1));
728 sscanf(datastart
+ 1, " %*d %d %*d %*d %*d %*d %*d %*d %*d %*d", &perc
);
730 return smprintf("%d%%", perc
);
734 wifi_essid(const char *iface
)
736 char id
[IW_ESSID_MAX_SIZE
+1];
737 int sockfd
= socket(AF_INET
, SOCK_DGRAM
, 0);
740 memset(&wreq
, 0, sizeof(struct iwreq
));
741 wreq
.u
.essid
.length
= IW_ESSID_MAX_SIZE
+1;
742 snprintf(wreq
.ifr_name
, sizeof(wreq
.ifr_name
), "%s", iface
);
745 warn("Failed to get ESSID for interface %s", iface
);
746 return smprintf("%s", UNKNOWN_STR
);
748 wreq
.u
.essid
.pointer
= id
;
749 if (ioctl(sockfd
,SIOCGIWESSID
, &wreq
) == -1) {
750 warn("Failed to get ESSID for interface %s", iface
);
751 return smprintf("%s", UNKNOWN_STR
);
756 if (strcmp((char *)wreq
.u
.essid
.pointer
, "") == 0)
757 return smprintf("%s", UNKNOWN_STR
);
759 return smprintf("%s", (char *)wreq
.u
.essid
.pointer
);
763 sighandler(const int signo
)
765 if (signo
== SIGTERM
|| signo
== SIGINT
) {
771 usage(const int eval
)
773 fprintf(stderr
, "usage: %s [-d] [-o] [-v] [-h]\n", argv0
);
778 main(int argc
, char *argv
[])
780 unsigned short int i
;
781 char status_string
[2048];
784 struct sigaction act
;
794 printf("slstatus (C) 2016-2017 slstatus engineers\n");
802 if (dflag
&& oflag
) {
805 if (dflag
&& daemon(1, 1) < 0) {
809 memset(&act
, 0, sizeof(act
));
810 act
.sa_handler
= sighandler
;
811 sigaction(SIGINT
, &act
, 0);
812 sigaction(SIGTERM
, &act
, 0);
815 dpy
= XOpenDisplay(NULL
);
818 setlocale(LC_ALL
, "");
821 status_string
[0] = '\0';
823 for (i
= 0; i
< sizeof(args
) / sizeof(args
[0]); ++i
) {
825 if (argument
.args
== NULL
) {
826 res
= argument
.func();
828 res
= argument
.func(argument
.args
);
830 element
= smprintf(argument
.fmt
, res
);
831 if (element
== NULL
) {
832 element
= smprintf("%s", UNKNOWN_STR
);
833 warnx("Failed to format output");
835 strncat(status_string
, element
, sizeof(status_string
) - strlen(status_string
) - 1);
841 XStoreName(dpy
, DefaultRootWindow(dpy
), status_string
);
844 printf("%s\n", status_string
);
847 if ((UPDATE_INTERVAL
- delay
) <= 0) {
851 sleep(UPDATE_INTERVAL
- delay
);
857 XStoreName(dpy
, DefaultRootWindow(dpy
), NULL
);