hostent.c 740 B

12345678910111213141516171819202122232425262728293031323334353637
  1. #include "unp.h"
  2. int
  3. main(int argc, char **argv)
  4. {
  5. char *ptr, **pptr;
  6. char str[INET_ADDRSTRLEN];
  7. struct hostent *hptr;
  8. while (--argc > 0) {
  9. ptr = *++argv;
  10. if ( (hptr = gethostbyname(ptr)) == NULL) {
  11. err_msg("gethostbyname error for host: %s: %s",
  12. ptr, hstrerror(h_errno));
  13. continue;
  14. }
  15. printf("official hostname: %s\n", hptr->h_name);
  16. for (pptr = hptr->h_aliases; *pptr != NULL; pptr++)
  17. printf("\talias: %s\n", *pptr);
  18. switch (hptr->h_addrtype) {
  19. case AF_INET:
  20. pptr = hptr->h_addr_list;
  21. for ( ; *pptr != NULL; pptr++)
  22. printf("\taddress: %s\n",
  23. Inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
  24. break;
  25. default:
  26. err_ret("unknown address type");
  27. break;
  28. }
  29. }
  30. exit(0);
  31. }