changeset 14856:c593fd7d2ab9

pipe2: Remove dependency on 'nonblocking' module. * lib/pipe2.c: Include verify.h. Include nonblocking.h only if O_NONBLOCK is defined by gnulib. (pipe2) [WIN32]: If O_NONBLOCK is not defined by gnulib, verify that it is zero. * modules/pipe2 (Depends-on): Add verify. Remove nonblocking. * tests/test-pipe2.c: Include nonblocking.h only if O_NONBLOCK is defined by gnulib. (get_nonblocking_flag): New function. (main): Test O_NONBLOCK flag only if it is nonzero.
author Bruno Haible <bruno@clisp.org>
date Fri, 03 Jun 2011 00:10:00 +0200
parents d08e35db9bba
children cef4d5bc6b72
files ChangeLog doc/glibc-functions/pipe2.texi lib/pipe2.c modules/pipe2 tests/test-pipe2.c
diffstat 5 files changed, 53 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2011-06-02  Bruno Haible  <bruno@clisp.org>
+
+	pipe2: Remove dependency on 'nonblocking' module.
+	* lib/pipe2.c: Include verify.h. Include nonblocking.h only if
+	O_NONBLOCK is defined by gnulib.
+	(pipe2) [WIN32]: If O_NONBLOCK is not defined by gnulib, verify that it
+	is zero.
+	* modules/pipe2 (Depends-on): Add verify. Remove nonblocking.
+	* tests/test-pipe2.c: Include nonblocking.h only if O_NONBLOCK is
+	defined by gnulib.
+	(get_nonblocking_flag): New function.
+	(main): Test O_NONBLOCK flag only if it is nonzero.
+	* doc/glibc-functions/pipe2.texi: Mention the 'nonblocking' module.
+
 2011-06-03  Jim Meyering  <meyering@redhat.com>
 
 	maint: three new prohibit-header-without-use rules
--- a/doc/glibc-functions/pipe2.texi
+++ b/doc/glibc-functions/pipe2.texi
@@ -15,3 +15,6 @@
 Portability problems not fixed by Gnulib:
 @itemize
 @end itemize
+
+Note: This function portably supports the @code{O_NONBLOCK} flag only if the
+gnulib module @code{nonblocking} is also used.
--- a/lib/pipe2.c
+++ b/lib/pipe2.c
@@ -24,7 +24,11 @@
 #include <fcntl.h>
 
 #include "binary-io.h"
-#include "nonblocking.h"
+#include "verify.h"
+
+#if GNULIB_defined_O_NONBLOCK
+# include "nonblocking.h"
+#endif
 
 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
 /* Native Woe32 API.  */
@@ -69,12 +73,19 @@
   if (_pipe (fd, 4096, flags & ~O_NONBLOCK) < 0)
     return -1;
 
+  /* O_NONBLOCK handling.
+     On native Windows platforms, O_NONBLOCK is defined by gnulib.  Use the
+     functions defined by the gnulib module 'nonblocking'.  */
+# if GNULIB_defined_O_NONBLOCK
   if (flags & O_NONBLOCK)
     {
       if (set_nonblocking_flag (fd[0], true) != 0
           || set_nonblocking_flag (fd[1], true) != 0)
         goto fail;
     }
+# else
+  verify (O_NONBLOCK == 0);
+# endif
 
   return 0;
 
@@ -88,6 +99,8 @@
      says that initially, the O_NONBLOCK and FD_CLOEXEC flags are cleared on
      both fd[0] and fd[1].  */
 
+  /* O_NONBLOCK handling.
+     On Unix platforms, O_NONBLOCK is defined by the system.  Use fcntl().  */
   if (flags & O_NONBLOCK)
     {
       int fcntl_flags;
--- a/modules/pipe2
+++ b/modules/pipe2
@@ -10,7 +10,7 @@
 fcntl-h
 binary-io
 extensions
-nonblocking
+verify
 
 configure.ac:
 gl_FUNC_PIPE2
--- a/tests/test-pipe2.c
+++ b/tests/test-pipe2.c
@@ -33,7 +33,9 @@
 
 #include "binary-io.h"
 #include "macros.h"
-#include "nonblocking.h"
+#if GNULIB_NONBLOCKING
+# include "nonblocking.h"
+#endif
 
 /* Return true if FD is open.  */
 static bool
@@ -68,13 +70,30 @@
 #endif
 }
 
+#if ! GNULIB_NONBLOCKING
+static int
+get_nonblocking_flag (int fd)
+{
+# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+  return 0;
+# else
+#  ifndef F_GETFL
+#   error Please port fcntl to your platform
+#  endif
+  int flags;
+  ASSERT ((flags = fcntl (fd, F_GETFL)) >= 0);
+  return (flags & O_NONBLOCK) != 0;
+# endif
+}
+#endif
+
 int
 main ()
 {
   int use_nonblocking;
   int use_cloexec;
 
-  for (use_nonblocking = 0; use_nonblocking <= 1; use_nonblocking++)
+  for (use_nonblocking = 0; use_nonblocking <= !!O_NONBLOCK; use_nonblocking++)
     for (use_cloexec = 0; use_cloexec <= !!O_CLOEXEC; use_cloexec++)
       {
         int o_flags;