Bruno Haible <[EMAIL PROTECTED]> writes: >> Maybe the same approach cannot be used for gethostname: ... >> >> I'm not sure. It seems pretty clear that MINGW_LIBS and GetModuleHandle >> are two approaches that can solve this problem. > > And with GetComputerNameEx, there is no link requirement at all. > > I propose this, together with some WINVER tricks to make it work.
Looks fine, please push it, although I don't see any WINVER tricks? /Simon > > --- lib/gethostname.c.orig 2008-10-23 13:45:02.000000000 +0200 > +++ lib/gethostname.c 2008-10-23 13:44:03.000000000 +0200 > @@ -22,8 +22,10 @@ > /* Specification. */ > #include <unistd.h> > > -#ifdef HAVE_UNAME > +#if HAVE_UNAME > # include <sys/utsname.h> > +#elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ > +# include <windows.h> > #endif > > #include <string.h> > @@ -37,7 +39,7 @@ > int > gethostname (char *name, size_t len) > { > -#ifdef HAVE_UNAME > +#if HAVE_UNAME > struct utsname uts; > > if (uname (&uts) == -1) > @@ -49,6 +51,14 @@ > len = sizeof (uts.nodename); > } > strncpy (name, uts.nodename, len); > +#elif (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ > + /* GetComputerName is not the right thing. gethostname from <winsock2.h> > + would be right, but requires linking with -lws2_32. So we use > + GetComputerNameEx. */ > + DWORD size = (len <= (DWORD)~0 ? len : (DWORD)~0); > + > + if (!GetComputerNameEx (ComputerNameDnsHostname, name, &size)) > + return -1; > #else > strcpy (name, ""); /* Hardcode your system name if you want. */ > #endif