From b219f47f394e536198997578e4cce3c539b84b6d Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 7 Dec 2014 15:14:02 +0100 Subject: support the special interface name _first_ for ethernet/wireless Since we have deterministic device names in Linux, these strings are a much better default in the i3status config than "eth0" and "wlan0" (what we used before). --- src/first_network_device.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/first_network_device.c (limited to 'src') diff --git a/src/first_network_device.c b/src/first_network_device.c new file mode 100644 index 0000000..c160027 --- /dev/null +++ b/src/first_network_device.c @@ -0,0 +1,39 @@ +// vim:ts=8:expandtab +#include +#include +#include + +#include "i3status.h" + +const char *first_eth_interface(const net_type_t type) { + static char *interface = NULL; + struct ifaddrs *ifaddr, *addrp; + struct stat stbuf; + static char path[1024]; + + getifaddrs(&ifaddr); + + if (ifaddr == NULL) + return NULL; + + free(interface); + interface = NULL; + for (addrp = ifaddr; + addrp != NULL; + addrp = addrp->ifa_next) { + if (strncasecmp("lo", addrp->ifa_name, strlen("lo")) == 0) + continue; + // Skip this interface if it is a wireless interface. + snprintf(path, sizeof(path), "/sys/class/net/%s/wireless", addrp->ifa_name); + const bool is_wireless = (stat(path, &stbuf) == 0); + if (( is_wireless && type == NET_TYPE_ETHERNET) || + (!is_wireless && type == NET_TYPE_WIRELESS)) + continue; + interface = strdup(addrp->ifa_name); + break; + } + + freeifaddrs(ifaddr); + return interface; +} + -- cgit v1.2.3