From af508f0b4cf88b1921c59fdf5dd1a98c08223fa5 Mon Sep 17 00:00:00 2001 From: sewn Date: Sat, 16 Mar 2024 00:20:54 +0300 Subject: [PATCH] add 'up' for whether a interface is up or down --- 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 @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include #include +#include #include #include #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; +} diff --git a/config.def.h b/config.def.h index d805331..100093e 100644 --- a/config.def.h +++ b/config.def.h @@ -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 @@ -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);