UPS: apcupsd clean sources
[tomato.git] / release / src / router / apcupsd / src / gapcmon / gethostname.c
blobbec09fcd01215012bf5f82d05a34ec88c116c64f
1 #include <netdb.h>
2 #include <errno.h>
3 #include <stdlib.h>
5 #ifdef HAVE_FUNC_GETHOSTBYNAME_R_6
6 struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char **tmphstbuf,size_t *hstbuflen)
8 struct hostent *hp;
9 int herr,res;
11 if (*hstbuflen == 0)
13 *hstbuflen = 1024;
14 *tmphstbuf = (char *)malloc (*hstbuflen);
17 while (( res =
18 gethostbyname_r(host,hostbuf,*tmphstbuf,*hstbuflen,&hp,&herr))
19 && (errno == ERANGE))
21 /* Enlarge the buffer. */
22 *hstbuflen *= 2;
23 *tmphstbuf = (char *)realloc (*tmphstbuf,*hstbuflen);
25 if (res)
26 return NULL;
27 return hp;
29 #endif
30 #ifdef HAVE_FUNC_GETHOSTBYNAME_R_5
31 struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char **tmphstbuf,size_t *hstbuflen)
33 struct hostent *hp;
34 int herr;
36 if (*hstbuflen == 0)
38 *hstbuflen = 1024;
39 *tmphstbuf = (char *)malloc (*hstbuflen);
42 while ((NULL == ( hp =
43 gethostbyname_r(host,hostbuf,*tmphstbuf,*hstbuflen,&herr)))
44 && (errno == ERANGE))
46 /* Enlarge the buffer. */
47 *hstbuflen *= 2;
48 *tmphstbuf = (char *)realloc (*tmphstbuf,*hstbuflen);
50 return hp;
52 #endif
53 #ifdef HAVE_FUNC_GETHOSTBYNAME_R_3
54 struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char **tmphstbuf,size_t *hstbuflen)
56 if (*hstbuflen == 0)
58 *hstbuflen = sizeof(struct hostent_data);
59 *tmphstbuf = (char *)malloc (*hstbuflen);
61 else if (*hstbuflen < sizeof(struct hostent_data))
63 *hstbuflen = sizeof(struct hostent_data);
64 *tmphstbuf = (char *)realloc(*tmphstbuf, *hstbuflen);
66 memset((void *)(*tmphstbuf),0,*hstbuflen);
68 if (0 != gethostbyname_r(host,hostbuf,(struct hostent_data *)*tmphstbuf))
69 return NULL;
70 return hostbuf;
72 #endif
73 #ifdef HAVE_FUNC_GETHOSTBYNAME_R_0
74 #warning WARNING! Your system does not have a thread-safe DNS resolver (gethostbyname_r)!
75 #warning WARNING! Name service lookups may be corrupted. Consider switching to
76 #warning WARNING! a more thread-friendly platform.
77 struct hostent * gethostname_re (const char *host,struct hostent *hostbuf,char **tmphstbuf,size_t *hstbuflen)
79 return gethostbyname(host);
81 #endif