added ip address function
authorAaron Marcher <info@nulltime.net>
Wed, 8 Jun 2016 07:42:32 +0000 (09:42 +0200)
committerAaron Marcher (drkhsh) <info@nulltime.net>
Wed, 8 Jun 2016 07:42:32 +0000 (09:42 +0200)
config.def.h
slstatus.c
slstatus.h

index 813bded504d1705b425fe21e12afbe180da2a146..b4b65ac40fc7bdaae98fc917e23538818b203f46 100644 (file)
@@ -17,6 +17,7 @@ static unsigned int update_interval = 1;
 - datetime (date and time) [argument: format]
 - disk_perc (disk usage in percent) [argument: mountpoint]
 - entropy (available entropy) [argument: NULL]
+- ip (ip address) [argument: interface]
 - ram_perc (ram usage in percent) [argument: NULL]
 - temp (temperature in degrees) [argument: temperature file]
 - vol_perc (alsa volume and mute status in percent) [argument: soundcard]
@@ -30,6 +31,5 @@ static const struct arg args[] = {
     { ram_perc,     "ram %3s | ",   NULL },
     { vol_perc,     "vol %4s | ",   "default" },
     { disk_perc,    "ssd %3s | ",   "/" },
-    { entropy,      "crypt %s | ",  NULL },
     { datetime,     "%s",           "%y-%m-%d %H:%M:%S" },
 };
index 357446810aaa70f2395767b9647b3ece2f571c89..9cd5ae2dcff60c69a7e07d1426ea73826d6683ab 100644 (file)
@@ -2,8 +2,11 @@
 
 /* global libraries */
 #include <alsa/asoundlib.h>
+#include <arpa/inet.h>
 #include <fcntl.h>
+#include <ifaddrs.h>
 #include <locale.h>
+#include <netdb.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -11,6 +14,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/statvfs.h>
+#include <sys/socket.h>
 #include <time.h>
 #include <unistd.h>
 #include <X11/Xlib.h>
@@ -204,6 +208,47 @@ entropy(const char *null)
     return smprintf("%d", entropy);
 }
 
+/* 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 +302,6 @@ temp(const char *file)
     return smprintf("%d°C", temperature / 1000);
 }
 
-
 /* alsa volume percentage */
 char *
 vol_perc(const char *soundcard)
index 12c03c761014e305e0e3d2b41b58427fe00e9e52..0d86f33b753f828183f30226316262f36a57f74d 100644 (file)
@@ -19,6 +19,7 @@ char *cpu_perc(const char *);
 char *datetime(const char *);
 char *disk_perc(const char *);
 char *entropy(const char*);
+char *ip(const char *);
 char *ram_perc(const char *);
 char *temp(const char *);
 char *vol_perc(const char *);