hostentp_ghba_r.c 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /*
  2. * hostentp_ghba_r.c:
  3. * Test program to see whether gethostbyaddr_r takes 7 arguments and returns
  4. * struct hostent*.
  5. */
  6. static const char rcsid[] = "$Id: hostentp_ghba_r.c,v 1.1 2002/11/04 12:27:35 chris Exp $";
  7. #include <sys/types.h>
  8. #include <errno.h>
  9. #include <netdb.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <netinet/in.h>
  13. int main(void) {
  14. struct in_addr localhost;
  15. struct hostent hostbuf, *hp;
  16. char *buf;
  17. int herr;
  18. size_t buflen = 1024;
  19. localhost.s_addr = htonl(INADDR_LOOPBACK);
  20. buf = malloc(buflen);
  21. while ((hp = gethostbyaddr_r((char*)&localhost, sizeof(struct in_addr),
  22. AF_INET, &hostbuf, buf, buflen, &herr))
  23. == NULL
  24. && errno == ERANGE)
  25. buf = (char*)realloc(buf, buflen *= 2);
  26. /* We assume that the loopback address can always be resolved if
  27. * gethostbyaddr_r is actually working. */
  28. if (hp == NULL) {
  29. fprintf(stderr, "errno = %d, herr = %d\n", errno, herr);
  30. return -1;
  31. } else
  32. return 0;
  33. }