+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"
}
}
-/* 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)
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(). */
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
/* 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, ...)
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>
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>
}
}
}
-# endif
+#endif
+
+#ifdef FCHDIR_REPLACEMENT
+ if (fd >= 0)
+ _gl_register_fd (fd, filename);
+#endif
return fd;
}
-#endif
-# 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.
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