]> git.sur5r.net Git - i3/i3status/blob - src/first_network_device.c
Add contrib/net-speed
[i3/i3status] / src / first_network_device.c
1 // vim:ts=8:expandtab
2 #include <sys/stat.h>
3 #include <stdlib.h>
4 #include <ifaddrs.h>
5
6 #include "i3status.h"
7
8 const char *first_eth_interface(const net_type_t type) {
9         static char *interface = NULL;
10         struct ifaddrs *ifaddr, *addrp;
11         struct stat stbuf;
12         static char path[1024];
13
14         getifaddrs(&ifaddr);
15
16         if (ifaddr == NULL)
17                 return NULL;
18
19         free(interface);
20         interface = NULL;
21         for (addrp = ifaddr;
22              addrp != NULL;
23              addrp = addrp->ifa_next) {
24                 if (strncasecmp("lo", addrp->ifa_name, strlen("lo")) == 0)
25                         continue;
26                 // Skip this interface if it is a wireless interface.
27                 snprintf(path, sizeof(path), "/sys/class/net/%s/wireless", addrp->ifa_name);
28                 const bool is_wireless = (stat(path, &stbuf) == 0);
29                 if (( is_wireless && type == NET_TYPE_ETHERNET) ||
30                     (!is_wireless && type == NET_TYPE_WIRELESS))
31                         continue;
32                 interface = strdup(addrp->ifa_name);
33                 break;
34         }
35
36         freeifaddrs(ifaddr);
37         return interface;
38 }
39