lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 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 | |
| 9 | /* glibc 2.4 has no ETHER_FILE_NAME, host compile fails without this */ |
| 10 | #ifndef ETHER_FILE_NAME |
| 11 | #define ETHER_FILE_NAME "/etc/ethers" |
| 12 | #endif |
| 13 | |
| 14 | #define ETHER_LINE_LEN 256 |
| 15 | |
| 16 | /* This test requires /etc/ethers to exist |
| 17 | * and to have nonzero length |
| 18 | */ |
| 19 | |
| 20 | int main(void) |
| 21 | { |
| 22 | struct ether_addr addr; |
| 23 | char hostname[ETHER_LINE_LEN]; |
| 24 | int fd, i; |
| 25 | const char *ethers; |
| 26 | struct stat statb; |
| 27 | |
| 28 | if ((fd = open(ETHER_FILE_NAME, O_RDONLY)) == -1) { |
| 29 | perror ("Cannot open file"); |
| 30 | exit(1); |
| 31 | } |
| 32 | |
| 33 | if (fstat(fd, &statb)) { |
| 34 | perror("Stat failed"); |
| 35 | exit(1); |
| 36 | } |
| 37 | ethers = mmap(NULL, statb.st_size, PROT_READ, MAP_SHARED, fd, 0); |
| 38 | |
| 39 | if (ethers == MAP_FAILED) { |
| 40 | perror("File mapping failed"); |
| 41 | exit(1); |
| 42 | } |
| 43 | |
| 44 | ether_line(ethers, &addr, hostname); |
| 45 | |
| 46 | for (i = 0; i < 6; i++) { |
| 47 | printf("%02x", addr.ether_addr_octet[i]); |
| 48 | if (i < 5) |
| 49 | printf(":"); |
| 50 | } |
| 51 | printf(" %s\n", hostname); |
| 52 | |
| 53 | return 0; |
| 54 | } |