tst-ethers-line.c 1022 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <netinet/ether.h>
  2. #include <stdio.h>
  3. #include <sys/mman.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <stdlib.h>
  8. /* glibc 2.4 has no ETHER_FILE_NAME, host compile fails without this */
  9. #ifndef ETHER_FILE_NAME
  10. #define ETHER_FILE_NAME "/etc/ethers"
  11. #endif
  12. #define ETHER_LINE_LEN 256
  13. /* This test requires /etc/ethers to exist
  14. * and to have nonzero length
  15. */
  16. int main(void)
  17. {
  18. struct ether_addr addr;
  19. char hostname[ETHER_LINE_LEN];
  20. int fd, i;
  21. const char *ethers;
  22. struct stat statb;
  23. if ((fd = open(ETHER_FILE_NAME, O_RDONLY)) == -1) {
  24. perror ("Cannot open file");
  25. exit(1);
  26. }
  27. if (fstat(fd, &statb)) {
  28. perror("Stat failed");
  29. exit(1);
  30. }
  31. ethers = mmap(NULL, statb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  32. if (ethers == MAP_FAILED) {
  33. perror("File mapping failed");
  34. exit(1);
  35. }
  36. ether_line(ethers, &addr, hostname);
  37. for (i = 0; i < 6; i++) {
  38. printf("%02x", addr.ether_addr_octet[i]);
  39. if (i < 5)
  40. printf(":");
  41. }
  42. printf(" %s\n", hostname);
  43. return 0;
  44. }