Bruno Haible <[EMAIL PROTECTED]> writes: > Simon Josefsson wrote: >> 2008-01-16 Simon Josefsson <[EMAIL PROTECTED]> >> >> * lib/unistd.in.h: Include sys/socket.h, to get to winsock2.h, >> which declares gethostname on MinGW. >> >> * modules/unistd (Depends-on): Add sys_socket. > > Half OK. This patch does too much, IMO: > > - It wants to fix a problem on mingw, and ends up including <sys/socket.h> > on cygwin also, and you don't know if it will work. > > - It wants to fix a problem for packages that need gethostname, and ends > up forcing a sys/socket.h file and winsock2 related autoconf test into > every package. > > Especially <sys/socket.h> defines a lot of macros and types. For the sake > of namespace cleanliness, it's good to avoid including when possible. > > Hence I'm in favour of limiting the effect through #ifdefs, like the > appended patch.
That approach would be fine with me. > But this is still not complete: The doc/functions/gethostname.texi file > says that the problem is that mingw lacks a gethostname function. This is not the case now, and I'm not sure it ever was. Where did that information come from? For reference, the Windows documentation for gethostname is: http://msdn2.microsoft.com/en-us/library/ms738527.aspx It seems to be a rather old function. > But now you say that winsock2.h declares this function? Yes. > So presumably this function exists, and can return more meaningful > values than the empty string? To use this function, does a program > need to link to non-default libraries? It works for me here, by linking to -lws2_32 or -lwsock32: [EMAIL PROTECTED]:~$ cat foo.c #include <stdio.h> #include <winsock2.h> int main () { char buf[1024]; int rc; rc = gethostname (buf, 1024); printf ("rc %d\n", rc); printf ("buf %s\n", buf); return 0; } [EMAIL PROTECTED]:~$ i586-mingw32msvc-gcc -o foo.exe foo.c /tmp/cccHKrQ2.o:foo.c:(.text+0x29): undefined reference to [EMAIL PROTECTED]' collect2: ld returned 1 exit status [EMAIL PROTECTED]:~$ i586-mingw32msvc-gcc -o foo.exe foo.c -lws2_32 [EMAIL PROTECTED]:~$ ./foo.exe rc 0 buf mocca Wine exited with a successful status [EMAIL PROTECTED]:~$ i586-mingw32msvc-gcc -o foo.exe foo.c -lwsock32 [EMAIL PROTECTED]:~$ ./foo.exe rc 0 buf mocca Wine exited with a successful status [EMAIL PROTECTED]:~$ The MSDN documentation says it is in winsock2.h and ws2_32, so I think we should use those headers/libraries until we know a reason to prefer winsock.h and wsock32. > How does m4/gethostname.m4 need to be rewritten? I think it should do something of what getaddrinfo.m4 does, in particular: AC_REQUIRE([gl_HEADER_SYS_SOCKET])dnl for HAVE_SYS_SOCKET_H, HAVE_WINSOCK2_H AC_MSG_NOTICE([checking how to do getaddrinfo, freeaddrinfo and getnameinfo]) ... AC_CACHE_CHECK([for getaddrinfo], [gl_cv_func_getaddrinfo], [ AC_TRY_LINK([ #include <sys/types.h> #ifdef HAVE_SYS_SOCKET_H #include <sys/socket.h> #endif #ifdef HAVE_NETDB_H #include <netdb.h> #endif #include <stddef.h> ], [getaddrinfo("", "", NULL, NULL);], [gl_cv_func_getaddrinfo=yes], [gl_cv_func_getaddrinfo=no])]) if test $gl_cv_func_getaddrinfo = no; then AC_CACHE_CHECK(for getaddrinfo in ws2tcpip.h and -lws2_32, gl_cv_w32_getaddrinfo, [ gl_cv_w32_getaddrinfo=no am_save_LIBS="$LIBS" LIBS="$LIBS -lws2_32" AC_TRY_LINK([ #ifdef HAVE_WS2TCPIP_H #include <ws2tcpip.h> #endif #include <stddef.h> ], [getaddrinfo(NULL, NULL, NULL, NULL);], gl_cv_w32_getaddrinfo=yes) LIBS="$am_save_LIBS"]) if test "$gl_cv_w32_getaddrinfo" = "yes"; then LIBS="$LIBS -lws2_32" else AC_LIBOBJ(getaddrinfo) fi fi > And it is necessary to still override the function, in order to > WSAStartup before? No, this doesn't seem to be necessary, at least under Wine. But the MSDN documentation says there is an error code WSANOTINITIALISED which is returned if WSAStartup hasn't been invoked. I'm not whether we should trust the MSDN documentation or Wine behaviour more. I think we have identified two work items: 1) Modify gethostname module to look for gethostname in winsock2.h and -lws2_32, similar to what's in getaddrinfo.m4. This will make gethostname depend on sys/socket.h, just like getaddrinfo does. 2) Modify unistd module to make sure it includes sys/socket.h when @GNULIB_GETHOSTNAME@ is enabled. I think your patch mostly takes care of 2), so I'll see if I can propose a better gethostname.m4 test that actually detects gethostname on MinGW. /Simon