Ben Pfaff wrote: > Fix gethostname on mingw. > * lib/unistd.in.h: Bypass bad winsock2 prototype for gethostname. > Prevent calling gethostname on mingw without using the gethostname > module. > * gethostname.m4: Invoke gl_PREREQ_SYS_H_WINSOCK2 if it is > available. > * doc/posix-functions/gethostname.texi: Update.
Nearly good. I improved two things: - "#include <unistd.h>" would make the non-POSIX functions accept(), ..., socket(), select() visible. - When the module 'gethostname' is requested and the module 'sys_socket' is not, then the use of #include <unistd.h> #include <winsock2.h> would lead to a clash of different prototypes of gethostname(). In other words, every time you have an m4_ifdef() of a macro defined in another gnulib module, there are chances that it's a bug. and applied this: 2008-10-25 Ben Pfaff <[EMAIL PROTECTED]> Bruno Haible <[EMAIL PROTECTED]> * lib/unistd.in.h: Include <winsock2.h>. (socket, connect,accept, bind, getpeername, getsockname, getsockopt, listen, recv, send, recvfrom, sendto, setsockopt, shutdown, select): Provide dummy declarations. (gethostname): Override. * lib/sys_socket.in.h (gethostname): Provide dummy declaration. * m4/gethostname.m4 (gl_FUNC_GETHOSTNAME): Invoke gl_PREREQ_SYS_H_WINSOCK2. * modules/gethostname (Files): Add m4/sys_socket_h.m4. * doc/posix-functions/gethostname.texi: More details. --- lib/sys_socket.in.h.orig 2008-10-25 23:00:21.000000000 +0200 +++ lib/sys_socket.in.h 2008-10-25 22:57:53.000000000 +0200 @@ -145,6 +145,11 @@ # define close close_used_without_including_unistd_h # endif +# if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H +# undef gethostname +# define gethostname gethostname_used_without_including_unistd_h +# endif + # if @GNULIB_SOCKET@ # if @HAVE_WINSOCK2_H@ # undef socket --- lib/unistd.in.h.orig 2008-10-25 23:00:21.000000000 +0200 +++ lib/unistd.in.h 2008-10-25 22:57:53.000000000 +0200 @@ -42,6 +42,47 @@ # include <sys/types.h> #endif +#if @GNULIB_GETHOSTNAME@ +/* Get all possible declarations of gethostname(). */ +# if @UNISTD_H_HAVE_WINSOCK2_H@ +# include <winsock2.h> +# if !defined _GL_SYS_SOCKET_H +# undef socket +# define socket socket_used_without_including_sys_socket_h +# undef connect +# define connect connect_used_without_including_sys_socket_h +# undef accept +# define accept accept_used_without_including_sys_socket_h +# undef bind +# define bind bind_used_without_including_sys_socket_h +# undef getpeername +# define getpeername getpeername_used_without_including_sys_socket_h +# undef getsockname +# define getsockname getsockname_used_without_including_sys_socket_h +# undef getsockopt +# define getsockopt getsockopt_used_without_including_sys_socket_h +# undef listen +# define listen listen_used_without_including_sys_socket_h +# undef recv +# define recv recv_used_without_including_sys_socket_h +# undef send +# define send send_used_without_including_sys_socket_h +# undef recvfrom +# define recvfrom recvfrom_used_without_including_sys_socket_h +# undef sendto +# define sendto sendto_used_without_including_sys_socket_h +# undef setsockopt +# define setsockopt setsockopt_used_without_including_sys_socket_h +# undef shutdown +# define shutdown shutdown_used_without_including_sys_socket_h +# endif +# if !defined _GL_SYS_SELECT_H +# undef select +# define select select_used_without_including_sys_select_h +# endif +# endif +#endif + /* The definition of GL_LINK_WARNING is copied here. */ @@ -284,9 +325,16 @@ Null terminate it if the name is shorter than LEN. If the host name is longer than LEN, set errno = EINVAL and return -1. Return 0 if successful, otherwise set errno and return -1. */ -# if [EMAIL PROTECTED]@ +# if @UNISTD_H_HAVE_WINSOCK2_H@ +# undef gethostname +# define gethostname rpl_gethostname +# endif +# if @UNISTD_H_HAVE_WINSOCK2_H@ || [EMAIL PROTECTED]@ extern int gethostname(char *name, size_t len); # endif +#elif @UNISTD_H_HAVE_WINSOCK2_H@ +# undef gethostname +# define gethostname gethostname_used_without_requesting_gnulib_module_gethostname #elif defined GNULIB_POSIXCHECK # undef gethostname # define gethostname(n,l) \ --- m4/gethostname.m4.orig 2008-10-25 23:00:21.000000000 +0200 +++ m4/gethostname.m4 2008-10-25 22:57:53.000000000 +0200 @@ -1,4 +1,4 @@ -# gethostname.m4 serial 3 +# gethostname.m4 serial 4 dnl Copyright (C) 2002, 2008 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -7,6 +7,7 @@ AC_DEFUN([gl_FUNC_GETHOSTNAME], [ AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) + gl_PREREQ_SYS_H_WINSOCK2 AC_REPLACE_FUNCS(gethostname) if test $ac_cv_func_gethostname = no; then HAVE_GETHOSTNAME=0 --- modules/gethostname.orig 2008-10-25 23:00:21.000000000 +0200 +++ modules/gethostname 2008-10-25 22:57:53.000000000 +0200 @@ -4,6 +4,7 @@ Files: lib/gethostname.c m4/gethostname.m4 +m4/sys_socket_h.m4 Depends-on: unistd --- doc/posix-functions/gethostname.texi.orig 2008-10-25 23:00:21.000000000 +0200 +++ doc/posix-functions/gethostname.texi 2008-10-25 22:57:53.000000000 +0200 @@ -9,8 +9,8 @@ Portability problems fixed by Gnulib: @itemize @item -This function is missing on some platforms: -mingw. +On mingw, this function has a prototype that differs from that +specified by POSIX, and it is defined only in the ws2_32 library. @end itemize Portability problems not fixed by Gnulib: