gethostbyname2.c 479 B

12345678910111213141516171819202122232425
  1. #define _GNU_SOURCE
  2. #include <sys/socket.h>
  3. #include <netdb.h>
  4. #include <errno.h>
  5. #include <stdlib.h>
  6. struct hostent *gethostbyname2(const char *name, int af)
  7. {
  8. static struct hostent *h;
  9. size_t size = 63;
  10. struct hostent *res;
  11. int err;
  12. do {
  13. free(h);
  14. h = malloc(size+=size+1);
  15. if (!h) {
  16. h_errno = NO_RECOVERY;
  17. return 0;
  18. }
  19. err = gethostbyname2_r(name, af, h,
  20. (void *)(h+1), size-sizeof *h, &res, &h_errno);
  21. } while (err == ERANGE);
  22. return err ? 0 : h;
  23. }