X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/67f61b839e21c05f787ec8edf68946e0c1aed077..2e72b212d3019a23accfd9d001bcec5d3ee5ab46:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 1db9af8..5bbf58a 100644 --- a/slstatus.c +++ b/slstatus.c @@ -2,8 +2,11 @@ /* global libraries */ #include +#include #include +#include #include +#include #include #include #include @@ -11,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -45,7 +49,7 @@ smprintf(const char *fmt, ...) /* battery percentage */ char * -get_battery(const char *battery) +battery_perc(const char *battery) { int now, full, perc; char batterynowfile[64] = ""; @@ -95,32 +99,9 @@ get_battery(const char *battery) return smprintf("%d%%", perc); } -/* cpu temperature */ -char * -get_cpu_temperature(const char *file) -{ - int temperature; - FILE *fp; - - /* open temperature file */ - if (!(fp = fopen(file, "r"))) { - fprintf(stderr, "Could not open temperature file.\n"); - return smprintf("n/a"); - } - - /* extract temperature */ - fscanf(fp, "%d", &temperature); - - /* close temperature file */ - fclose(fp); - - /* return temperature in degrees */ - return smprintf("%d°C", temperature / 1000); -} - /* cpu percentage */ char * -get_cpu_usage(const char *null) +cpu_perc(const char *null) { int perc; long double a[4], b[4]; @@ -162,7 +143,7 @@ get_cpu_usage(const char *null) /* date and time */ char * -get_datetime(const char *timeformat) +datetime(const char *timeformat) { time_t tm; size_t bufsize = 64; @@ -186,7 +167,7 @@ get_datetime(const char *timeformat) /* disk usage percentage */ char * -get_diskusage(const char *mountpoint) +disk_perc(const char *mountpoint) { int perc = 0; struct statvfs fs; @@ -204,9 +185,96 @@ get_diskusage(const char *mountpoint) return smprintf("%d%%", perc); } +/* entropy available */ +char * +entropy(const char *null) +{ + int entropy = 0; + FILE *fp; + + /* open entropy file */ + if (!(fp = fopen("/proc/sys/kernel/random/entropy_avail", "r"))) { + fprintf(stderr, "Could not open entropy file.\n"); + return smprintf("n/a"); + } + + /* extract entropy */ + fscanf(fp, "%d", &entropy); + + /* close entropy file */ + fclose(fp); + + /* return entropy */ + return smprintf("%d", entropy); +} + +/* hostname */ +char * +hostname(const char *null) +{ + char *hostname = ""; + FILE *fp; + + /* open hostname file */ + if (!(fp = fopen("/proc/sys/kernel/hostname", "r"))) { + fprintf(stderr, "Could not open hostname file.\n"); + return smprintf("n/a"); + } + + /* extract hostname */ + fscanf(fp, "%s", hostname); + + /* close hostname file */ + fclose(fp); + + /* return entropy */ + return smprintf("%s", hostname); +} + +/* ip address */ +char * +ip(const char *interface) +{ + struct ifaddrs *ifaddr, *ifa; + int s; + char host[NI_MAXHOST]; + + /* check if getting ip address works */ + if (getifaddrs(&ifaddr) == -1) + { + fprintf(stderr, "Error getting IP address."); + return smprintf("n/a"); + } + + /* get the ip address */ + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) + { + if (ifa->ifa_addr == NULL) + continue; + + s = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); + + if ((strcmp(ifa->ifa_name, interface) == 0) && (ifa->ifa_addr->sa_family == AF_INET)) + { + if (s != 0) + { + fprintf(stderr, "Error getting IP address."); + return smprintf("n/a"); + } + return smprintf("%s", host); + } + } + + /* free the address */ + freeifaddrs(ifaddr); + + /* return n/a if nothing works */ + return smprintf("n/a"); +} + /* ram percentage */ char * -get_ram_usage(const char *null) +ram_perc(const char *null) { int perc; long total, free, buffers, cached; @@ -234,9 +302,32 @@ get_ram_usage(const char *null) return smprintf("%d%%", perc); } +/* temperature */ +char * +temp(const char *file) +{ + int temperature; + FILE *fp; + + /* open temperature file */ + if (!(fp = fopen(file, "r"))) { + fprintf(stderr, "Could not open temperature file.\n"); + return smprintf("n/a"); + } + + /* extract temperature */ + fscanf(fp, "%d", &temperature); + + /* close temperature file */ + fclose(fp); + + /* return temperature in degrees */ + return smprintf("%d°C", temperature / 1000); +} + /* alsa volume percentage */ char * -get_volume(const char *soundcard) +vol_perc(const char *soundcard) { int mute = 0; long vol = 0, max = 0, min = 0; @@ -274,7 +365,7 @@ get_volume(const char *soundcard) /* wifi percentage */ char * -get_wifi_signal(const char *wificard) +wifi_perc(const char *wificard) { int bufsize = 255; int strength;