[hackers] [slstatus][PATCH] add 'up' for whether a interface is up or down

From: sewn <sewn_AT_disroot.org>
Date: Sat, 16 Mar 2024 00:20:54 +0300

---
 components/ip.c | 26 ++++++++++++++++++++++++++
 config.def.h    |  1 +
 slstatus.h      |  1 +
 3 files changed, 28 insertions(+)
diff --git a/components/ip.c b/components/ip.c
index 9476549..2cdad46 100644
--- a/components/ip.c
+++ b/components/ip.c
_AT_@ -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__)
_AT_@ -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;
+}
diff --git a/config.def.h b/config.def.h
index d805331..100093e 100644
--- a/config.def.h
+++ b/config.def.h
_AT_@ -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)
diff --git a/slstatus.h b/slstatus.h
index 8ef5874..394281c 100644
--- a/slstatus.h
+++ b/slstatus.h
_AT_@ -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);
-- 
2.44.0
Received on Fri Mar 15 2024 - 22:20:54 CET

This archive was generated by hypermail 2.3.0 : Fri Mar 15 2024 - 22:24:43 CET