changeset 30299:74d49f46b574

Merge the two replacements for open() into a single one.
author Paolo Bonzini <bonzini@gnu.org>
date Thu, 09 Oct 2008 23:47:06 +0200
parents 583e3a91bdcf
children a7421631c863
files ChangeLog lib/fchdir.c lib/fcntl.in.h lib/open.c m4/fchdir.m4
diffstat 5 files changed, 42 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Thu Oct 09 10:35:37 2008 +0200
+++ b/ChangeLog	Thu Oct 09 23:47:06 2008 +0200
@@ -1,3 +1,15 @@
+2008-10-09  Paolo Bonzini  <bonzini@gnu.org>
+            Bruno Haible  <bruno@clisp.org>
+
+	* lib/fcntl.in.h (open): Simplify conditionals.
+	(_gl_register_fd): New declaration.
+	* lib/fchdir.c (rpl_open): Remove function.
+	* lib/open.c: When FCHDIR_REPLACEMENT is defined, compile the file
+	also.
+	(open): When FCHDIR_REPLACEMENT is defined, invoke _gl_register_fd.
+	* m4/fchdir.m4 (gl_FUNC_FCHDIR): When replacing fchdir, also replace
+	open.
+
 2008-10-09  Jim Meyering  <meyering@redhat.com>
 
 	GNUmakefile: use the more name-space-friendly "_version"
--- a/lib/fchdir.c	Thu Oct 09 10:35:37 2008 +0200
+++ b/lib/fchdir.c	Thu Oct 09 23:47:06 2008 +0200
@@ -105,7 +105,7 @@
     }
 }
 
-/* Override open() and close(), to keep track of the open file descriptors.  */
+/* Override close(), to keep track of the open file descriptors.  */
 
 int
 rpl_close (int fd)
@@ -118,39 +118,6 @@
   return retval;
 }
 
-int
-rpl_open (const char *filename, int flags, ...)
-#undef open
-{
-  mode_t mode;
-  int fd;
-  struct stat statbuf;
-
-  mode = 0;
-  if (flags & O_CREAT)
-    {
-      va_list arg;
-      va_start (arg, flags);
-
-      /* If mode_t is narrower than int, use the promoted type (int),
-	 not mode_t.  Use sizeof to guess whether mode_t is narrower;
-	 we don't know of any practical counterexamples.  */
-      mode = (sizeof (mode_t) < sizeof (int)
-	      ? va_arg (arg, int)
-	      : va_arg (arg, mode_t));
-
-      va_end (arg);
-    }
-#if defined GNULIB_OPEN && ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__)
-  if (strcmp (filename, "/dev/null") == 0)
-    filename = "NUL";
-#endif
-  fd = open (filename, flags, mode);
-  if (fd >= 0)
-    _gl_register_fd (fd, filename);
-  return fd;
-}
-
 /* Override opendir() and closedir(), to keep track of the open file
    descriptors.  Needed because there is a function dirfd().  */
 
--- a/lib/fcntl.in.h	Thu Oct 09 10:35:37 2008 +0200
+++ b/lib/fcntl.in.h	Thu Oct 09 23:47:06 2008 +0200
@@ -48,10 +48,17 @@
 extern "C" {
 #endif
 
-#if (@GNULIB_OPEN@ && @REPLACE_OPEN@) || defined FCHDIR_REPLACEMENT
-# undef open
-# define open rpl_open
+#if @GNULIB_OPEN@
+# if @REPLACE_OPEN@
+#  undef open
+#  define open rpl_open
 extern int open (const char *filename, int flags, ...);
+# endif
+#endif
+
+#ifdef FCHDIR_REPLACEMENT
+/* gnulib internal function.  */
+extern void _gl_register_fd (int fd, const char *filename);
 #endif
 
 #ifdef __cplusplus
--- a/lib/open.c	Thu Oct 09 10:35:37 2008 +0200
+++ b/lib/open.c	Thu Oct 09 23:47:06 2008 +0200
@@ -33,14 +33,11 @@
 /* Specification.  */
 #include <fcntl.h>
 
-/* If the fchdir replacement is used, open() is defined in fchdir.c.  */
-#ifndef FCHDIR_REPLACEMENT
-
-# include <errno.h>
-# include <stdarg.h>
-# include <string.h>
-# include <sys/types.h>
-# include <sys/stat.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 
 int
 open (const char *filename, int flags, ...)
@@ -64,12 +61,12 @@
       va_end (arg);
     }
 
-# if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
   if (strcmp (filename, "/dev/null") == 0)
     filename = "NUL";
-# endif
+#endif
 
-# if OPEN_TRAILING_SLASH_BUG
+#if OPEN_TRAILING_SLASH_BUG
   /* If the filename ends in a slash and one of O_CREAT, O_WRONLY, O_RDWR
      is specified, then fail.
      Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
@@ -100,11 +97,11 @@
 	  return -1;
 	}
     }
-# endif
+#endif
 
   fd = orig_open (filename, flags, mode);
 
-# if OPEN_TRAILING_SLASH_BUG
+#if OPEN_TRAILING_SLASH_BUG
   /* If the filename ends in a slash and fd does not refer to a directory,
      then fail.
      Rationale: POSIX <http://www.opengroup.org/susv3/basedefs/xbd_chap04.html>
@@ -132,8 +129,12 @@
 	    }
 	}
     }
-# endif
+#endif
+
+#ifdef FCHDIR_REPLACEMENT
+  if (fd >= 0)
+    _gl_register_fd (fd, filename);
+#endif
 
   return fd;
 }
-#endif
--- a/m4/fchdir.m4	Thu Oct 09 10:35:37 2008 +0200
+++ b/m4/fchdir.m4	Thu Oct 09 23:47:06 2008 +0200
@@ -1,5 +1,5 @@
-# fchdir.m4 serial 4
-dnl Copyright (C) 2006-2007 Free Software Foundation, Inc.
+# fchdir.m4 serial 5
+dnl Copyright (C) 2006-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,
 dnl with or without modifications, as long as this notice is preserved.
@@ -14,6 +14,7 @@
     gl_PREREQ_FCHDIR
     AC_DEFINE([FCHDIR_REPLACEMENT], 1,
       [Define if gnulib's fchdir() replacement is used.])
+    gl_REPLACE_OPEN
     gl_CHECK_NEXT_HEADERS([dirent.h])
     DIRENT_H='dirent.h'
   else