Combine the two replacements of 'close'.
authorPaolo Bonzini <bonzini@gnu.org>
Thu, 9 Oct 2008 22:23:06 +0000 (00:23 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 9 Oct 2008 22:25:23 +0000 (00:25 +0200)
ChangeLog
lib/close.c
lib/sys_socket.in.h
lib/unistd.in.h
lib/winsock.c
m4/sys_socket_h.m4
m4/unistd_h.m4
modules/poll-tests
modules/select-tests
modules/sys_socket
modules/unistd

index 38a3083..6a1ffa4 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,6 +1,32 @@
 2008-10-09  Paolo Bonzini  <bonzini@gnu.org>
             Bruno Haible  <bruno@clisp.org>
 
+       Combine the two replacements of 'close'.
+       * lib/sys_socket.in.h (close): Define to a reminder to include <unistd.h>.
+       (_gl_close_fd_maybe_socket): New declaration.
+       (HAVE__GL_CLOSE_FD_MAYBE_SOCKET): New macro.
+       * lib/winsock.c (close): Remove undefinition.
+       (_gl_close_fd_maybe_socket): Renamed from rpl_close. Define only when
+       needed for the gnulib module 'close'.
+       * lib/unistd.in.h (close): If the gnulib module 'close' is not used,
+       define to an error symbol or to a warning, if suitable.
+       * lib/close.c: Include <sys/socket.h>.
+       (rpl_close): Invoke _gl_close_fd_maybe_socket when gnulib defines it.
+       * m4/sys_socket_h.m4 (gl_PREREQ_SYS_H_WINSOCK2): Set also
+       UNISTD_H_HAVE_WINSOCK2_H.
+       (gl_SYS_SOCKET_H_DEFAULTS): Require gl_UNISTD_H_DEFAULTS.
+       * m4/unistd_h.m4 (gl_UNISTD_H_DEFAULTS): Initialize
+       UNISTD_H_HAVE_WINSOCK2_H.
+       * modules/sys_socket (Files): Add m4/unistd_h.m4.
+       (configure.ac): Set a module indicator.
+       (Makefile.am): Substitute GNULIB_CLOSE.
+       * modules/unistd (Makefile.am): Substitute UNISTD_H_HAVE_WINSOCK2_H.
+       * modules/poll-tests (Depends-on): Add close.
+       * modules/select-tests (Depends-on): Likewise.
+
+2008-10-09  Paolo Bonzini  <bonzini@gnu.org>
+            Bruno Haible  <bruno@clisp.org>
+
        New module 'close'.
        * modules/close: New file.
        * lib/unistd.in.h (close): Move declaration out of the
index b0464c9..9b05066 100644 (file)
 /* Specification.  */
 #include <unistd.h>
 
+#if GNULIB_SYS_SOCKET
+# include <sys/socket.h>
+#endif
+
 
 /* Override close() to call into other gnulib modules.  */
 
@@ -26,7 +30,11 @@ int
 rpl_close (int fd)
 #undef close
 {
+#if HAVE__GL_CLOSE_FD_MAYBE_SOCKET
+  int retval = _gl_close_fd_maybe_socket (fd);
+#else
   int retval = close (fd);
+#endif
 
 #ifdef FCHDIR_REPLACEMENT
   if (retval >= 0)
index 13729c6..74cb37a 100644 (file)
@@ -138,10 +138,9 @@ rpl_fd_isset (int fd, fd_set * set)
 
 /* Wrap everything else to use libc file descriptors for sockets.  */
 
-# if @HAVE_WINSOCK2_H@
+# if @HAVE_WINSOCK2_H@ && !defined _GL_UNISTD_H
 #  undef close
-#  define close                        rpl_close
-extern int rpl_close(int);
+#  define close close_used_without_including_unistd_h
 # endif
 
 # if @GNULIB_SOCKET@
@@ -376,6 +375,12 @@ extern int rpl_setsockopt (int, int, int, const void *, int);
 #  define select               select_used_without_including_sys_select_h
 # endif
 
+# if @GNULIB_CLOSE@ && @HAVE_WINSOCK2_H@
+/* gnulib internal function.  */
+#  define HAVE__GL_CLOSE_FD_MAYBE_SOCKET 1
+extern int _gl_close_fd_maybe_socket (int fd);
+# endif
+
 # ifdef __cplusplus
 }
 # endif
index c7233b1..8b4facb 100644 (file)
@@ -82,6 +82,15 @@ extern int chown (const char *file, uid_t uid, gid_t gid);
 #  define close rpl_close
 extern int close (int);
 # endif
+#elif @UNISTD_H_HAVE_WINSOCK2_H@
+# undef close
+# define close close_used_without_requesting_gnulib_module_close
+#elif defined GNULIB_POSIXCHECK
+# undef close
+# define close(f) \
+    (GL_LINK_WARNING ("close does not portably work on sockets - " \
+                      "use gnulib module close for portability"), \
+     close (f))
 #endif
 
 
index 00609cf..feee00d 100644 (file)
@@ -25,7 +25,6 @@
 #include <io.h>
 #include <sys/socket.h>
 
-#undef close
 #undef socket
 #undef connect
 #undef accept
 # define SOCKET_TO_FD(fh)   (_open_osfhandle ((long) (fh), O_RDWR | O_BINARY))
 
 
-/* Wrappers for libc functions.  */
+/* Hook for gnulib module close.  */
 
+#if HAVE__GL_CLOSE_FD_MAYBE_SOCKET
 int
-rpl_close (int fd)
+_gl_close_fd_maybe_socket (int fd)
 {
   SOCKET sock = FD_TO_SOCKET (fd);
   WSANETWORKEVENTS ev;
@@ -67,6 +67,7 @@ rpl_close (int fd)
   else
     return _close (fd);
 }
+#endif
 
 
 /* Wrappers for WinSock functions.  */
index 1e38ee3..e61de7b 100644 (file)
@@ -1,4 +1,4 @@
-# sys_socket_h.m4 serial 8
+# sys_socket_h.m4 serial 9
 dnl Copyright (C) 2005-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,
@@ -71,6 +71,7 @@ AC_DEFUN([gl_HEADER_SYS_SOCKET],
 # Sets and substitutes HAVE_WINSOCK2_H.
 AC_DEFUN([gl_PREREQ_SYS_H_WINSOCK2],
 [
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
   AC_CHECK_HEADERS_ONCE([sys/socket.h])
   if test $ac_cv_header_sys_socket_h != yes; then
     dnl We cannot use AC_CHECK_HEADERS_ONCE here, because that would make
@@ -81,6 +82,7 @@ AC_DEFUN([gl_PREREQ_SYS_H_WINSOCK2],
   fi
   if test "$ac_cv_header_winsock2_h" = yes; then
     HAVE_WINSOCK2_H=1
+    UNISTD_H_HAVE_WINSOCK2_H=1
   else
     HAVE_WINSOCK2_H=0
   fi
@@ -97,6 +99,7 @@ AC_DEFUN([gl_SYS_SOCKET_MODULE_INDICATOR],
 
 AC_DEFUN([gl_SYS_SOCKET_H_DEFAULTS],
 [
+  AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) dnl for GNULIB_CLOSE
   GNULIB_SOCKET=0;      AC_SUBST([GNULIB_SOCKET])
   GNULIB_CONNECT=0;     AC_SUBST([GNULIB_CONNECT])
   GNULIB_ACCEPT=0;      AC_SUBST([GNULIB_ACCEPT])
index 21c400c..e9f7a7d 100644 (file)
@@ -69,4 +69,5 @@ AC_DEFUN([gl_UNISTD_H_DEFAULTS],
   REPLACE_LCHOWN=0;       AC_SUBST([REPLACE_LCHOWN])
   REPLACE_LSEEK=0;        AC_SUBST([REPLACE_LSEEK])
   REPLACE_WRITE=0;        AC_SUBST([REPLACE_WRITE])
+  UNISTD_H_HAVE_WINSOCK2_H=0; AC_SUBST([UNISTD_H_HAVE_WINSOCK2_H])
 ])
index 72241ae..403287c 100644 (file)
@@ -17,6 +17,7 @@ setsockopt
 listen
 connect
 accept
+close
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([unistd.h sys/wait.h])
index 1788348..80ee60c 100644 (file)
@@ -16,6 +16,7 @@ setsockopt
 listen
 connect
 accept
+close
 
 configure.ac:
 
index d2cf460..0d43570 100644 (file)
@@ -6,6 +6,7 @@ lib/sys_socket.in.h
 lib/winsock.c
 m4/sys_socket_h.m4
 m4/sockpfaf.m4
+m4/unistd_h.m4
 
 Depends-on:
 include_next
@@ -14,6 +15,7 @@ errno
 
 configure.ac:
 gl_HEADER_SYS_SOCKET
+gl_MODULE_INDICATOR([sys_socket])
 AC_PROG_MKDIR_P
 
 Makefile.am:
@@ -28,6 +30,7 @@ sys/socket.h: sys_socket.in.h
              -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
              -e 's|@''NEXT_SYS_SOCKET_H''@|$(NEXT_SYS_SOCKET_H)|g' \
              -e 's|@''HAVE_SYS_SOCKET_H''@|$(HAVE_SYS_SOCKET_H)|g' \
+             -e 's|@''GNULIB_CLOSE''@|$(GNULIB_CLOSE)|g' \
              -e 's|@''GNULIB_SOCKET''@|$(GNULIB_SOCKET)|g' \
              -e 's|@''GNULIB_CONNECT''@|$(GNULIB_CONNECT)|g' \
              -e 's|@''GNULIB_ACCEPT''@|$(GNULIB_ACCEPT)|g' \
index 95e10d9..820f33e 100644 (file)
@@ -60,6 +60,7 @@ unistd.h: unistd.in.h
              -e 's|@''REPLACE_LCHOWN''@|$(REPLACE_LCHOWN)|g' \
              -e 's|@''REPLACE_LSEEK''@|$(REPLACE_LSEEK)|g' \
              -e 's|@''REPLACE_WRITE''@|$(REPLACE_WRITE)|g' \
+             -e 's|@''UNISTD_H_HAVE_WINSOCK2_H''@|$(UNISTD_H_HAVE_WINSOCK2_H)|g' \
              -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
              < $(srcdir)/unistd.in.h; \
        } > $@-t