rework langinfo code for ABI compat and for use by time code
[musl] / src / network / gethostbyname2.c
1 #define _GNU_SOURCE
2
3 #include <sys/socket.h>
4 #include <netdb.h>
5 #include <string.h>
6 #include <netinet/in.h>
7 #include <errno.h>
8 #include <stdlib.h>
9
10 struct hostent *gethostbyname2(const char *name, int af)
11 {
12         static struct hostent *h;
13         size_t size = 63;
14         struct hostent *res;
15         int err;
16         do {
17                 free(h);
18                 h = malloc(size+=size+1);
19                 if (!h) {
20                         h_errno = NO_RECOVERY;
21                         return 0;
22                 }
23                 err = gethostbyname2_r(name, af, h,
24                         (void *)(h+1), size-sizeof *h, &res, &h_errno);
25         } while (err == ERANGE);
26         return err ? 0 : h;
27 }