2 * This file is a ghastly hack because nobody can agree on
3 * gethostbyname_r()'s prototype.
5 * Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #define _SVID_SOURCE 1 /* Need this to get gethostbyname_r() */
35 #include "gethostbyname.h"
37 #if HAVE_GETHOSTBYNAME_R == 6
40 free_ghbnctx (struct ghbnctx *ctx)
44 if (ctx->hostbuf != NULL)
49 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
57 memset (ctx, 0, sizeof (struct ghbnctx));
58 ctx->hostbuf_len = 2048;
59 if ((ctx->hostbuf = (char *)malloc (ctx->hostbuf_len)) == NULL)
64 while ((err = gethostbyname_r (host,
65 &ctx->hostent, ctx->hostbuf, ctx->hostbuf_len,
66 &hp, &ctx->h_err)) == ERANGE)
68 ctx->hostbuf_len += 1024;
69 if ((tmp = (char *)realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
85 h_error_ctx (struct ghbnctx *ctx)
92 #elif HAVE_GETHOSTBYNAME_R == 5
95 free_ghbnctx (struct ghbnctx *ctx)
99 if (ctx->hostbuf != NULL)
104 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
109 assert (ctx != NULL);
111 memset (ctx, 0, sizeof (struct ghbnctx));
112 ctx->hostbuf_len = 2048;
113 if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
118 while ((hp = gethostbyname_r (host, &ctx->hostent,
119 ctx->hostbuf, ctx->hostbuf_len,
120 &ctx->h_err)) == NULL && errno == ERANGE)
122 ctx->hostbuf_len += 1024;
123 if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
134 h_error_ctx (struct ghbnctx *ctx)
136 assert (ctx != NULL);
141 #elif HAVE_GETHOSTBYNAME_R == 3
144 free_ghbnctx (struct ghbnctx *ctx)
146 assert (ctx != NULL);
148 /* FIXME: does this need to do anything? */
152 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
154 assert (ctx != NULL);
156 if (!gethostbyname_r (host, &ctx->hostent, &ctx->hostent_data))
158 ctx->h_err = h_errno; /* FIXME: is this correct? */
161 return &ctx->hostent;
165 h_error_ctx (struct ghbnctx *ctx)
167 assert (ctx != NULL);
175 free_ghbnctx (struct ghbnctx *ctx )
177 assert (ctx != NULL);
181 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
185 hp = gethostbyname (host);
187 ctx->h_err = h_errno;
192 h_error_ctx (struct ghbnctx *ctx)
194 assert (ctx != NULL);