44e /* Gets the host name for a given address */ #include #include #include #include #include main(int argc, char **argv) { struct hostent h_ent, *hp; extern int h_errno; char addrs[50], *p; char addr[4]; if (argc < 2) { printf("usage: gethostname \n"); exit(1); } strcpy(addrs, argv[1]); if ((p = strtok(addrs, ".")) == NULL) goto err_exit; addr[0] = (char)atoi(p); if ((p = strtok(NULL, ".")) == NULL) goto err_exit; addr[1] = (char)atoi(p); if ((p = strtok(NULL, ".")) == NULL) goto err_exit; addr[2] = (char)atoi(p); if ((p = strtok(NULL, "\n")) == NULL) goto err_exit; addr[3] = (char)atoi(p); if ((hp = gethostbyaddr(addr, 4, AF_INET)) == NULL) { printf("gethostbyaddr error, h_errno %d\n", h_errno); exit(1); } h_ent = *hp; if (strchr(h_ent.h_name, '.')) h_ent.h_name[(int)strchr(h_ent.h_name, '.') - (int)h_ent.h_name] = 0; printf("%s", h_ent.h_name); exit(0); err_exit: printf("Invalid address.\n"); exit(1); } . 0