summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMichael Stapelberg <michael@stapelberg.de>2014-12-07 15:14:02 +0100
committerMichael Stapelberg <michael@stapelberg.de>2014-12-07 15:17:31 +0100
commitb219f47f394e536198997578e4cce3c539b84b6d (patch)
tree91e7a6b489ed3dbbfc70a07b0ffa86a03512a4a4 /src
parent7bdfb5b72e730f2131dccd75f211aaa6d2236954 (diff)
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).
Diffstat (limited to 'src')
-rw-r--r--src/first_network_device.c39
1 files changed, 39 insertions, 0 deletions
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 <sys/stat.h>
+#include <stdlib.h>
+#include <ifaddrs.h>
+
+#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;
+}
+