X-Git-Url: https://git.xinqibao.xyz/slstatus.git/blobdiff_plain/5a943fa59cab43f4a2cf6c133c4ef5676d6358b1..c7f3a407f4f8d539fe68cbf7dde76876cbcbccaa:/slstatus.c diff --git a/slstatus.c b/slstatus.c index 3574468..404ead6 100644 --- a/slstatus.c +++ b/slstatus.c @@ -2,8 +2,12 @@ /* global libraries */ #include +#include #include +#include +#include #include +#include #include #include #include @@ -11,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -204,6 +209,70 @@ entropy(const char *null) return smprintf("%d", entropy); } +/* hostname */ +char * +hostname(const char *null) +{ + char hostname[HOST_NAME_MAX]; + 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\n", 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 * ram_perc(const char *null) @@ -257,7 +326,6 @@ temp(const char *file) return smprintf("%d°C", temperature / 1000); } - /* alsa volume percentage */ char * vol_perc(const char *soundcard)