1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <sys/types.h>
#include <ifaddrs.h>
#include <stdio.h>
/* print the name of the first discovered network interface on stdout */
int
main(int argc, const char **argv)
{
struct ifaddrs *ifa, *cur;
if (getifaddrs(&ifa)) {
perror("Failed to getifaddrs()");
return 1;
}
for (cur = ifa; cur; cur = cur->ifa_next) {
if (cur->ifa_name && cur->ifa_name[0] != 0) {
printf("%s\n", cur->ifa_name);
freeifaddrs(ifa);
return 0;
}
}
freeifaddrs(ifa);
perror("no interfaces found via getifaddrs()");
return 2;
}
|