tst-ethers-line.c 807 B

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