From: Paolo Bonzini Date: Thu, 9 Oct 2008 21:47:06 +0000 (+0200) Subject: Merge the two replacements for open() into a single one. X-Git-Tag: v0.1~6874 X-Git-Url: http://erislabs.org.uk/gitweb/?a=commitdiff_plain;h=863234aa6007a9c337cf62def1c3593b5a14cb44;p=gnulib.git Merge the two replacements for open() into a single one. --- diff --git a/ChangeLog b/ChangeLog index 1f5d6bd04..070a66abc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2008-10-09 Paolo Bonzini + Bruno Haible + + * 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 GNUmakefile: use the more name-space-friendly "_version" diff --git a/lib/fchdir.c b/lib/fchdir.c index 7a30448fb..dccb4f19b 100644 --- a/lib/fchdir.c +++ b/lib/fchdir.c @@ -105,7 +105,7 @@ _gl_register_fd (int fd, const char *filename) } } -/* 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 @@ 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(). */ diff --git a/lib/fcntl.in.h b/lib/fcntl.in.h index 79b8f165f..761e85315 100644 --- a/lib/fcntl.in.h +++ b/lib/fcntl.in.h @@ -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 diff --git a/lib/open.c b/lib/open.c index 9a35f5b2a..13af27460 100644 --- a/lib/open.c +++ b/lib/open.c @@ -33,14 +33,11 @@ orig_open (const char *filename, int flags, mode_t mode) /* Specification. */ #include -/* If the fchdir replacement is used, open() is defined in fchdir.c. */ -#ifndef FCHDIR_REPLACEMENT - -# include -# include -# include -# include -# include +#include +#include +#include +#include +#include int open (const char *filename, int flags, ...) @@ -64,12 +61,12 @@ 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 @@ -100,11 +97,11 @@ open (const char *filename, int flags, ...) 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 @@ -132,8 +129,12 @@ open (const char *filename, int flags, ...) } } } -# endif +#endif + +#ifdef FCHDIR_REPLACEMENT + if (fd >= 0) + _gl_register_fd (fd, filename); +#endif return fd; } -#endif diff --git a/m4/fchdir.m4 b/m4/fchdir.m4 index fd170d294..37d3a55b9 100644 --- a/m4/fchdir.m4 +++ b/m4/fchdir.m4 @@ -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 @@ AC_DEFUN([gl_FUNC_FCHDIR], 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