diff options
author | sewn <sewn@disroot.org> | 2024-03-16 00:20:54 +0300 |
---|---|---|
committer | drkhsh <me@drkhsh.at> | 2025-04-30 03:09:55 +0200 |
commit | af508f0b4cf88b1921c59fdf5dd1a98c08223fa5 (patch) | |
tree | 56b37faf5e15573e3b9b2a11e233bbd62df3cf5f | |
parent | b6267f7d0bedf5789206adf21f33cfe1a872289c (diff) |
add 'up' for whether a interface is up or down
-rw-r--r-- | components/ip.c | 26 | ||||
-rw-r--r-- | config.def.h | 1 | ||||
-rw-r--r-- | slstatus.h | 1 |
3 files changed, 28 insertions, 0 deletions
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 <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; +} 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) @@ -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); |