tst-ethers-line.c 949 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. int main(void)
  14. {
  15. struct ether_addr addr;
  16. char hostname[ETHER_LINE_LEN];
  17. int fd, i;
  18. const char *ethers;
  19. struct stat statb;
  20. if ((fd = open(ETHER_FILE_NAME, O_RDONLY)) == -1) {
  21. perror ("Cannot open file");
  22. exit(1);
  23. }
  24. if (fstat(fd, &statb)) {
  25. perror("Stat failed");
  26. exit(1);
  27. }
  28. ethers = mmap(NULL, statb.st_size, PROT_READ, MAP_SHARED, fd, 0);
  29. if (ethers == MAP_FAILED) {
  30. perror("File mapping failed");
  31. exit(1);
  32. }
  33. ether_line(ethers, &addr, hostname);
  34. for (i = 0; i < 6; i++) {
  35. printf("%02x", addr.ether_addr_octet[i]);
  36. if (i < 5)
  37. printf(":");
  38. }
  39. printf(" %s\n", hostname);
  40. return 0;
  41. }