OK. It makes the code more concise, too, and now the Windows and GNU/Linux code is much closer.
Code:
char *getip(char *s, char *u, int u_len)
{
#ifdef _WIN32_
HOSTENT *hostentptr;
IN_ADDR addr; /* IP address */
#endif /* _WIN32_ */
#ifdef _LINUX_
struct hostent *hostentptr;
struct in_addr *addr;/* IP address */
#endif /* _LINUX_ */
/* get HOSTENT data */
hostentptr = gethostbyname(s);
if ( hostentptr == NULL )
{ fprintf(stderr, "getip: %s: Unresolvable hostname; returning hostname\n", s);
strncpy(u, s, u_len);
return(u);
}
/* extract main IP address */
#ifdef _WIN32_
addr.S_un.S_addr = *(ULONG *)hostentptr->h_addr_list[0];
#endif /* _WIN32_ */
#ifdef _LINUX_
addr = (struct in_addr *)hostentptr->h_addr_list[0];
#endif /* _LINUX_ */
strncpy(u, inet_ntoa(*addr), u_len);
return(u);
}