struct hostent *
gethostname (char *host)
{
struct hostent hostbuf, *hp;
size_t hstbuflen;
char *tmphstbuf;
int res;
int herr;
hstbuflen = 1024;
/* Allocate buffer, remember to free it to avoid memory leakage. */
tmphstbuf = malloc (hstbuflen);
while ((res = gethostbyname_r (host, &hostbuf, tmphstbuf, hstbuflen,
&hp, &herr)) == ERANGE)
{
/* Enlarge the buffer. */
hstbuflen *= 2;
tmphstbuf = realloc (tmphstbuf, hstbuflen);
}
/* Check for errors. */
if (res || hp == NULL)
return NULL;
return hp;
}
ovaj sors sam nasao na www.gnu.org.
Buni me hostbuf, to je lokalna struktura, koja ne vazi izvan funkcije, a koristi se kao drugi parametar funkcije gethostbyname_r.
Cemu uopste sluzi onda ta struktura?
Ako se tu upisuju podaci, onda su ti podaci nedostupni izvan funkcije?
Mada, koliko ja znam, podaci se upisuju u dinamicki alociranoj memoriji na koju ukazuje tmphstbuf.
Pa, cemu onda sluzi ona hostbuf struktura?
Dakle, da rezimiram, ova fja gore ili
1) vraca hostent preko onog buffera (onda mi uopste nije jasna uloga one lokalne hostent strukture)
ili
2) uopste ne vraca hostent jer on vazi samo u funkciji (tj. neki idiot sa gnu.org je pisao ovaj example?)