From dd66a62cc2d76ab463e1167824ea4ca5fd4ef2ae Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Tue, 1 Sep 2009 09:18:16 -0600 Subject: [PATCH] fchdir: use more consistent macro convention * lib/fcntl.in.h (_gl_register_fd): Move declaration to unistd. * lib/sys_stat.in.h (rpl_fstat): Declare via make-time REPLACE_FCHDIR, rather than relying on config.h macros. * lib/unistd.in.h (fchdir): Move all fchdir internal declarations inside a single make-time REPLACE_FCHDIR block, rather than using the config.h FCHDIR_REPLACEMENT. * m4/fchdir.m4 (gl_FUNC_FCHDIR): REPLACE_FCHDIR was already AC_SUBST'd, also AC_DEFINE it. Don't define FCHDIR_REPLACEMENT. Manage fstat replacement. * m4/sys_stat_h.m4 (gl_SYS_STAT_H_DEFAULTS): Pick up REPLACE_FCHDIR. * modules/sys_stat (Files): Add m4/unistd_h.m4. (Makefile.am): Substitute REPLACE_FCHDIR. * lib/close.c (rpl_close): Use REPLACE_FCHDIR, not FCHDIR_REPLACEMENT. * lib/dup-safer.c (dup_safer): Likewise. * lib/dup2.c (rpl_dup2): Likewise. * lib/dup3.c (rpl_dup3): Likewise. * lib/open.c (rpl_open): Likewise. Signed-off-by: Eric Blake --- ChangeLog | 21 +++++++++++++++++++++ lib/close.c | 2 +- lib/dup-safer.c | 2 +- lib/dup2.c | 4 ++-- lib/dup3.c | 6 +++--- lib/fcntl.in.h | 5 ----- lib/open.c | 5 +++-- lib/sys_stat.in.h | 2 +- lib/unistd.in.h | 13 +++++-------- m4/fchdir.m4 | 6 ++++-- m4/sys_stat_h.m4 | 5 +++-- modules/sys_stat | 2 ++ 12 files changed, 46 insertions(+), 27 deletions(-) diff --git a/ChangeLog b/ChangeLog index 6a458876e..3205ba8d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,26 @@ 2009-09-02 Eric Blake + fchdir: use more consistent macro convention + * lib/fcntl.in.h (_gl_register_fd): Move declaration to unistd. + * lib/sys_stat.in.h (rpl_fstat): Declare via make-time + REPLACE_FCHDIR, rather than relying on config.h macros. + * lib/unistd.in.h (fchdir): Move all fchdir internal declarations + inside a single make-time REPLACE_FCHDIR block, rather than using + the config.h FCHDIR_REPLACEMENT. + * m4/fchdir.m4 (gl_FUNC_FCHDIR): REPLACE_FCHDIR was already + AC_SUBST'd, also AC_DEFINE it. Don't define FCHDIR_REPLACEMENT. + Manage fstat replacement. + * m4/sys_stat_h.m4 (gl_SYS_STAT_H_DEFAULTS): Pick up + REPLACE_FCHDIR. + * modules/sys_stat (Files): Add m4/unistd_h.m4. + (Makefile.am): Substitute REPLACE_FCHDIR. + * lib/close.c (rpl_close): Use REPLACE_FCHDIR, not + FCHDIR_REPLACEMENT. + * lib/dup-safer.c (dup_safer): Likewise. + * lib/dup2.c (rpl_dup2): Likewise. + * lib/dup3.c (rpl_dup3): Likewise. + * lib/open.c (rpl_open): Likewise. + fchdir: simplify error handling, and support dup3 * modules/fchdir (Depends-on): Use strdup-posix, not strdup. Add stdbool, malloc-posix, realloc-posix. diff --git a/lib/close.c b/lib/close.c index 0e56dcb27..5278f24bb 100644 --- a/lib/close.c +++ b/lib/close.c @@ -33,7 +33,7 @@ rpl_close (int fd) int retval = close (fd); #endif -#ifdef FCHDIR_REPLACEMENT +#if REPLACE_FCHDIR if (retval >= 0) _gl_unregister_fd (fd); #endif diff --git a/lib/dup-safer.c b/lib/dup-safer.c index 7d9b2be34..22b96babf 100644 --- a/lib/dup-safer.c +++ b/lib/dup-safer.c @@ -32,7 +32,7 @@ int dup_safer (int fd) { -#if defined F_DUPFD && !defined FCHDIR_REPLACEMENT +#if defined F_DUPFD && !REPLACE_FCHDIR return fcntl (fd, F_DUPFD, STDERR_FILENO + 1); #else /* fd_safer calls us back, but eventually the recursion unwinds and diff --git a/lib/dup2.c b/lib/dup2.c index a513e5bfe..140af1b9f 100644 --- a/lib/dup2.c +++ b/lib/dup2.c @@ -70,7 +70,7 @@ rpl_dup2 (int fd, int desired_fd) /* Correct a cygwin 1.5.x errno value. */ else if (result == -1 && errno == EMFILE) errno = EBADF; -#ifdef FCHDIR_REPLACEMENT +#if REPLACE_FCHDIR if (fd != desired_fd && result == desired_fd) result = _gl_register_dup (fd, desired_fd); #endif @@ -111,7 +111,7 @@ dup2 (int fd, int desired_fd) # else result = dupfd (fd, desired_fd); # endif -#ifdef FCHDIR_REPLACEMENT +#if REPLACE_FCHDIR if (0 <= result) result = _gl_register_dup (fd, desired_fd); #endif diff --git a/lib/dup3.c b/lib/dup3.c index 3d6f940cd..879a9074f 100644 --- a/lib/dup3.c +++ b/lib/dup3.c @@ -63,7 +63,7 @@ dup3 (int oldfd, int newfd, int flags) if (!(result < 0 && errno == ENOSYS)) { have_dup3_really = 1; -#ifdef FCHDIR_REPLACEMENT +#if REPLACE_FCHDIR if (0 <= result) result = _gl_register_dup (oldfd, newfd); #endif @@ -184,7 +184,7 @@ dup3 (int oldfd, int newfd, int flags) errno = saved_errno; } -#ifdef FCHDIR_REPLACEMENT +#if REPLACE_FCHDIR if (result == newfd) result = _gl_register_dup (oldfd, newfd); #endif @@ -226,7 +226,7 @@ dup3 (int oldfd, int newfd, int flags) setmode (newfd, O_TEXT); #endif -#ifdef FCHDIR_REPLACEMENT +#if REPLACE_FCHDIR newfd = _gl_register_dup (oldfd, newfd); #endif return newfd; diff --git a/lib/fcntl.in.h b/lib/fcntl.in.h index 959be44ea..5c63afd59 100644 --- a/lib/fcntl.in.h +++ b/lib/fcntl.in.h @@ -58,11 +58,6 @@ extern int open (const char *filename, int flags, ...); # endif #endif -#ifdef FCHDIR_REPLACEMENT -/* gnulib internal function. */ -extern int _gl_register_fd (int fd, const char *filename); -#endif - #ifdef __cplusplus } #endif diff --git a/lib/open.c b/lib/open.c index 02dd12d0b..08ecaff44 100644 --- a/lib/open.c +++ b/lib/open.c @@ -38,6 +38,7 @@ orig_open (const char *filename, int flags, mode_t mode) #include #include #include +#include #ifndef REPLACE_OPEN_DIRECTORY # define REPLACE_OPEN_DIRECTORY 0 @@ -102,7 +103,7 @@ open (const char *filename, int flags, ...) fd = orig_open (filename, flags, mode); -#ifdef FCHDIR_REPLACEMENT +#if REPLACE_FCHDIR /* Implementing fchdir and fdopendir requires the ability to open a directory file descriptor. If open doesn't support that (as on mingw), we use a dummy file that behaves the same as directories @@ -155,7 +156,7 @@ open (const char *filename, int flags, ...) } #endif -#ifdef FCHDIR_REPLACEMENT +#if REPLACE_FCHDIR if (!REPLACE_OPEN_DIRECTORY && 0 <= fd) fd = _gl_register_fd (fd, filename); #endif diff --git a/lib/sys_stat.in.h b/lib/sys_stat.in.h index 9d511da6f..b0341681f 100644 --- a/lib/sys_stat.in.h +++ b/lib/sys_stat.in.h @@ -302,7 +302,7 @@ extern int rpl_lstat (const char *name, struct stat *buf); lstat (p, b)) #endif -#if defined FCHDIR_REPLACEMENT && REPLACE_OPEN_DIRECTORY +#if @REPLACE_FCHDIR@ # define fstat rpl_fstat extern int fstat (int fd, struct stat *buf); #endif diff --git a/lib/unistd.in.h b/lib/unistd.in.h index f0b5cc4b5..3a748a672 100644 --- a/lib/unistd.in.h +++ b/lib/unistd.in.h @@ -248,6 +248,11 @@ extern int fchdir (int /*fd*/); # define dup rpl_dup extern int dup (int); +/* Gnulib internal hooks needed to maintain the fchdir metadata. */ +extern int _gl_register_fd (int fd, const char *filename); +extern void _gl_unregister_fd (int fd); +extern int _gl_register_dup (int oldfd, int newfd); + # endif #elif defined GNULIB_POSIXCHECK # undef fchdir @@ -615,14 +620,6 @@ extern ssize_t write (int fd, const void *buf, size_t count); #endif -#ifdef FCHDIR_REPLACEMENT -/* gnulib internal function. */ -extern void _gl_unregister_fd (int fd); -/* gnulib internal function. */ -extern int _gl_register_dup (int oldfd, int newfd); -#endif - - #ifdef __cplusplus } #endif diff --git a/m4/fchdir.m4 b/m4/fchdir.m4 index bcaf056c7..6597be06c 100644 --- a/m4/fchdir.m4 +++ b/m4/fchdir.m4 @@ -8,13 +8,15 @@ AC_DEFUN([gl_FUNC_FCHDIR], [ AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) AC_REQUIRE([gl_DIRENT_H_DEFAULTS]) + AC_REQUIRE([gl_SYS_STAT_H_DEFAULTS]) AC_CHECK_FUNCS_ONCE([fchdir]) if test $ac_cv_func_fchdir = no; then REPLACE_FCHDIR=1 AC_LIBOBJ([fchdir]) gl_PREREQ_FCHDIR - AC_DEFINE([FCHDIR_REPLACEMENT], [1], - [Define if gnulib's fchdir() replacement is used.]) + AC_DEFINE([REPLACE_FCHDIR], [1], + [Define to 1 if gnulib's fchdir() replacement is used.]) + REPLACE_FSTAT=1 gl_REPLACE_OPEN gl_REPLACE_CLOSE gl_REPLACE_DUP2 diff --git a/m4/sys_stat_h.m4 b/m4/sys_stat_h.m4 index 20c82edf9..5113e5538 100644 --- a/m4/sys_stat_h.m4 +++ b/m4/sys_stat_h.m4 @@ -1,5 +1,5 @@ -# sys_stat_h.m4 serial 10 -*- Autoconf -*- -dnl Copyright (C) 2006-2008 Free Software Foundation, Inc. +# sys_stat_h.m4 serial 11 -*- Autoconf -*- +dnl Copyright (C) 2006-2009 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. @@ -50,6 +50,7 @@ AC_DEFUN([gl_SYS_STAT_MODULE_INDICATOR], AC_DEFUN([gl_SYS_STAT_H_DEFAULTS], [ + AC_REQUIRE([gl_UNISTD_H_DEFAULTS]) dnl for REPLACE_FCHDIR GNULIB_LCHMOD=0; AC_SUBST([GNULIB_LCHMOD]) GNULIB_LSTAT=0; AC_SUBST([GNULIB_LSTAT]) dnl Assume proper GNU behavior unless another module says otherwise. diff --git a/modules/sys_stat b/modules/sys_stat index e4fba73ab..40f1c1880 100644 --- a/modules/sys_stat +++ b/modules/sys_stat @@ -4,6 +4,7 @@ A for systems with missing declarations. Files: lib/sys_stat.in.h m4/sys_stat_h.m4 +m4/unistd_h.m4 Depends-on: include_next @@ -31,6 +32,7 @@ sys/stat.h: sys_stat.in.h -e 's|@''HAVE_LSTAT''@|$(HAVE_LSTAT)|g' \ -e 's|@''REPLACE_LSTAT''@|$(REPLACE_LSTAT)|g' \ -e 's|@''REPLACE_MKDIR''@|$(REPLACE_MKDIR)|g' \ + -e 's|@''REPLACE_FCHDIR''@|$(REPLACE_FCHDIR)|g' \ -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \ < $(srcdir)/sys_stat.in.h; \ } > $@-t && \ -- 2.11.0