gethostbyaddr.c 463 字节
Newer Older
R
Rich Felker 已提交
1 2 3
#define _GNU_SOURCE

#include <netdb.h>
4 5
#include <errno.h>
#include <stdlib.h>
R
Rich Felker 已提交
6 7 8

struct hostent *gethostbyaddr(const void *a, socklen_t l, int af)
{
9 10
	static struct hostent *h;
	size_t size = 63;
R
Rich Felker 已提交
11
	struct hostent *res;
12 13 14 15 16 17 18 19 20 21 22 23
	int err;
	do {
		free(h);
		h = malloc(size+=size+1);
		if (!h) {
			h_errno = NO_RECOVERY;
			return 0;
		}
		err = gethostbyaddr_r(a, l, af, h,
			(void *)(h+1), size-sizeof *h, &res, &h_errno);
	} while (err == ERANGE);
	return err ? 0 : h;
R
Rich Felker 已提交
24
}