]> Skullheadx's Git Forge - slstatus.git/commitdiff
add 'up' for whether a interface is up or down
authorsewn <sewn@disroot.org>
Fri, 15 Mar 2024 21:20:54 +0000 (00:20 +0300)
committerdrkhsh <me@drkhsh.at>
Wed, 30 Apr 2025 01:09:55 +0000 (03:09 +0200)
components/ip.c
config.def.h
slstatus.h

index 947654944f28c1e5cefcee0cad4bde0ae5902cf5..2cdad46466fe2cff868f53430186cb45b2bfddda 100644 (file)
@@ -1,6 +1,7 @@
 /* See LICENSE file for copyright and license details. */
 #include <ifaddrs.h>
 #include <netdb.h>
+#include <net/if.h>
 #include <stdio.h>
 #include <string.h>
 #if defined(__OpenBSD__)
@@ -59,3 +60,28 @@ ipv6(const char *interface)
 {
        return ip(interface, AF_INET6);
 }
+
+const char *
+up(const char *interface)
+{
+       struct ifaddrs *ifaddr, *ifa;
+
+       if (getifaddrs(&ifaddr) < 0) {
+               warn("getifaddrs:");
+               return NULL;
+       }
+
+       for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
+               if (!ifa->ifa_addr)
+                       continue;
+
+               if (!strcmp(ifa->ifa_name, interface)) {
+                       freeifaddrs(ifaddr);
+                       return ifa->ifa_flags & IFF_UP ? "up" : "down";
+               }
+       }
+
+       freeifaddrs(ifaddr);
+
+       return NULL;
+}
index d805331c85e550022efa66f03cf640016a3d61cc..100093e3ce1522131a043f0e853fc92408a4a467 100644 (file)
@@ -56,6 +56,7 @@ static const char unknown_str[] = "n/a";
  *                                                     thermal zone on FreeBSD
  *                                                     (tz0, tz1, etc.)
  * uid                 UID of current user             NULL
+ * up                  interface is running            interface name (eth0)
  * uptime              system uptime                   NULL
  * username            username of current user        NULL
  * vol_perc            OSS/ALSA volume in percent      mixer file (/dev/mixer)
index 8ef5874c05f5542a80fb1e9b0860b2516b333398..394281cb7cf52da030180b41d4345564102fcd77 100644 (file)
@@ -30,6 +30,7 @@ const char *hostname(const char *unused);
 /* ip */
 const char *ipv4(const char *interface);
 const char *ipv6(const char *interface);
+const char *up(const char *interface);
 
 /* kernel_release */
 const char *kernel_release(const char *unused);