File: getifname.c

package info (click to toggle)
resolvconf-admin 0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, trixie
  • size: 128 kB
  • sloc: ansic: 337; sh: 41; makefile: 35
file content (26 lines) | stat: -rw-r--r-- 557 bytes parent folder | download
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;
}