tst-ethers-line.c 1.1 KB

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