From c5c46dcc8e875485a2c5d27cce106491fb41400e Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Mon, 7 Sep 2009 11:27:05 -0600 Subject: [PATCH] rename: modernize replacement * modules/rename (Depends-on): Add stdio. (configure.ac): Declare witness. * m4/rename.m4 (gl_FUNC_RENAME): Ensure dependency order, and let stdio take care of replacement. * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Add new defaults. * modules/stdio (Makefile.am): Substitute them. * lib/stdio.in.h (rename): Declare replacement. * lib/rename.c (includes): Allow cross-compilation to non-windows machines. * doc/posix-functions/rename.texi (rename): Improve documentation. Signed-off-by: Eric Blake --- ChangeLog | 13 ++++++++++++ doc/posix-functions/rename.texi | 10 ++++++--- lib/rename.c | 47 +++++++++++++++++++++++++---------------- lib/stdio.in.h | 14 ++++++++++++ m4/rename.m4 | 8 +++---- m4/stdio_h.m4 | 2 ++ modules/rename | 2 ++ modules/stdio | 2 ++ 8 files changed, 73 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index e3af6853e..0b9e3a07d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,18 @@ 2009-09-07 Eric Blake + rename: modernize replacement + * modules/rename (Depends-on): Add stdio. + (configure.ac): Declare witness. + * m4/rename.m4 (gl_FUNC_RENAME): Ensure dependency order, and let + stdio take care of replacement. + * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Add new defaults. + * modules/stdio (Makefile.am): Substitute them. + * lib/stdio.in.h (rename): Declare replacement. + * lib/rename.c (includes): Allow cross-compilation to non-windows + machines. + * doc/posix-functions/rename.texi (rename): Improve + documentation. + stdio: sort witness names * modules/stdio (Makefile.am): Sort replacements. * m4/stdio_h.m4 (gl_STDIO_H_DEFAULTS): Likewise. diff --git a/doc/posix-functions/rename.texi b/doc/posix-functions/rename.texi index aa7287a4e..84a03ab75 100644 --- a/doc/posix-functions/rename.texi +++ b/doc/posix-functions/rename.texi @@ -9,9 +9,9 @@ Gnulib module: rename Portability problems fixed by Gnulib: @itemize @item -This function does not work when the source file name ends in a slash on -some platforms: -SunOS 4.1. +This function does not handle trailing slashes correctly on +some platforms (the full rules for trailing slashes are complex): +SunOS 4.1, mingw. @item This function will not replace an existing destination on some platforms: @@ -23,4 +23,8 @@ Portability problems not fixed by Gnulib: This function will not replace a destination that is currently opened by any process: mingw. +@item +This function mistakenly allows names ending in @samp{.} or @samp{..} +on some platforms: +Cygwin 1.5.x. @end itemize diff --git a/lib/rename.c b/lib/rename.c index b27fd9100..5cd4dee9b 100644 --- a/lib/rename.c +++ b/lib/rename.c @@ -3,7 +3,8 @@ a trailing slash. On mingw, rename fails when the destination exists. - Copyright (C) 2001, 2002, 2003, 2005, 2006, 2009 Free Software Foundation, Inc. + Copyright (C) 2001, 2002, 2003, 2005, 2006, 2009 Free Software + Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -21,16 +22,20 @@ /* written by Volker Borchert */ #include + +#include + #undef rename -#if RENAME_DEST_EXISTS_BUG -/* This replacement must come first, otherwise when cross - * compiling to Windows we will guess that it has the trailing - * slash bug and entirely miss this one. */ -#include +#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ +/* The mingw rename has problems with trailing slashes; it also + requires use of native Windows calls to allow atomic renames over + existing files. */ + +# include -#define WIN32_LEAN_AND_MEAN -#include +# define WIN32_LEAN_AND_MEAN +# include /* Rename the file SRC to DST. This replacement is necessary on Windows, on which the system rename function will not replace @@ -114,10 +119,10 @@ rpl_rename (char const *src, char const *dst) errno = EPERM; /* ? */ break; -#ifndef ERROR_FILE_TOO_LARGE +# ifndef ERROR_FILE_TOO_LARGE /* This value is documented but not defined in all versions of windows.h. */ -#define ERROR_FILE_TOO_LARGE 223 -#endif +# define ERROR_FILE_TOO_LARGE 223 +# endif case ERROR_FILE_TOO_LARGE: errno = EFBIG; break; @@ -129,13 +134,18 @@ rpl_rename (char const *src, char const *dst) return -1; } -#elif RENAME_TRAILING_SLASH_BUG -#include -#include -#include -#include "dirname.h" -#include "xalloc.h" +#else /* ! W32 platform */ + +# if RENAME_DEST_EXISTS_BUG +# error Please report your platform and this message to bug-gnulib@gnu.org. +# elif RENAME_TRAILING_SLASH_BUG +# include +# include +# include + +# include "dirname.h" +# include "xalloc.h" /* Rename the file SRC to DST, removing any trailing slashes from SRC. Needed for SunOS 4.1.1_U1. */ @@ -162,4 +172,5 @@ rpl_rename (char const *src, char const *dst) return ret_val; } -#endif /* RENAME_TRAILING_SLASH_BUG */ +# endif /* RENAME_TRAILING_SLASH_BUG */ +#endif /* ! W32 platform */ diff --git a/lib/stdio.in.h b/lib/stdio.in.h index 495bacaf0..27cd30551 100644 --- a/lib/stdio.in.h +++ b/lib/stdio.in.h @@ -416,6 +416,20 @@ extern int putchar (int c); extern int puts (const char *string); #endif +#if @GNULIB_RENAME@ +# if @REPLACE_RENAME@ +# undef rename +# define rename rpl_rename +extern int rename (const char *old, const char *new); +# endif +#elif defined GNULIB_POSIXCHECK +# undef rename +# define rename(o,n) \ + (GL_LINK_WARNING ("rename is buggy on some platforms - " \ + "use gnulib module rename for more portability"), \ + rename (o, n)) +#endif + #if @GNULIB_SNPRINTF@ # if @REPLACE_SNPRINTF@ # define snprintf rpl_snprintf diff --git a/m4/rename.m4 b/m4/rename.m4 index 0592fcb22..2e43a8759 100644 --- a/m4/rename.m4 +++ b/m4/rename.m4 @@ -1,4 +1,4 @@ -# serial 14 +# serial 15 # Copyright (C) 2001, 2003, 2005, 2006, 2009 Free Software Foundation, Inc. # This file is free software; the Free Software Foundation @@ -16,7 +16,8 @@ dnl AC_DEFUN([gl_FUNC_RENAME], [ AC_REQUIRE([AC_CANONICAL_HOST]) - AC_CACHE_CHECK([whether rename is broken with a trailing slash], + AC_REQUIRE([gl_STDIO_H_DEFAULTS]) + AC_CACHE_CHECK([whether rename is broken with a trailing slash], gl_cv_func_rename_trailing_slash_bug, [ rm -rf conftest.d1 conftest.d2 @@ -49,8 +50,7 @@ AC_DEFUN([gl_FUNC_RENAME], if test $gl_cv_func_rename_trailing_slash_bug = yes || test $gl_cv_func_rename_dest_exists_bug = yes; then AC_LIBOBJ([rename]) - AC_DEFINE([rename], [rpl_rename], - [Define to rpl_rename if the replacement function should be used.]) + REPLACE_RENAME=1 if test $gl_cv_func_rename_trailing_slash_bug = yes; then AC_DEFINE([RENAME_TRAILING_SLASH_BUG], [1], [Define if rename does not work for source file names with a trailing diff --git a/m4/stdio_h.m4 b/m4/stdio_h.m4 index 8314f58b3..ac5e20aba 100644 --- a/m4/stdio_h.m4 +++ b/m4/stdio_h.m4 @@ -67,6 +67,7 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], GNULIB_PUTC=0; AC_SUBST([GNULIB_PUTC]) GNULIB_PUTCHAR=0; AC_SUBST([GNULIB_PUTCHAR]) GNULIB_PUTS=0; AC_SUBST([GNULIB_PUTS]) + GNULIB_RENAME=0; AC_SUBST([GNULIB_RENAME]) GNULIB_SNPRINTF=0; AC_SUBST([GNULIB_SNPRINTF]) GNULIB_SPRINTF_POSIX=0; AC_SUBST([GNULIB_SPRINTF_POSIX]) GNULIB_STDIO_H_SIGPIPE=0; AC_SUBST([GNULIB_STDIO_H_SIGPIPE]) @@ -106,6 +107,7 @@ AC_DEFUN([gl_STDIO_H_DEFAULTS], REPLACE_PERROR=0; AC_SUBST([REPLACE_PERROR]) REPLACE_POPEN=0; AC_SUBST([REPLACE_POPEN]) REPLACE_PRINTF=0; AC_SUBST([REPLACE_PRINTF]) + REPLACE_RENAME=0; AC_SUBST([REPLACE_RENAME]) REPLACE_SNPRINTF=0; AC_SUBST([REPLACE_SNPRINTF]) REPLACE_SPRINTF=0; AC_SUBST([REPLACE_SPRINTF]) REPLACE_STDIO_WRITE_FUNCS=0; AC_SUBST([REPLACE_STDIO_WRITE_FUNCS]) diff --git a/modules/rename b/modules/rename index 9eaea933a..8d2a96861 100644 --- a/modules/rename +++ b/modules/rename @@ -8,9 +8,11 @@ m4/rename.m4 Depends-on: xalloc dirname +stdio configure.ac: gl_FUNC_RENAME +gl_STDIO_MODULE_INDICATOR([rename]) Makefile.am: diff --git a/modules/stdio b/modules/stdio index 9f95fd337..eb5826935 100644 --- a/modules/stdio +++ b/modules/stdio @@ -52,6 +52,7 @@ stdio.h: stdio.in.h -e 's|@''GNULIB_PUTC''@|$(GNULIB_PUTC)|g' \ -e 's|@''GNULIB_PUTCHAR''@|$(GNULIB_PUTCHAR)|g' \ -e 's|@''GNULIB_PUTS''@|$(GNULIB_PUTS)|g' \ + -e 's|@''GNULIB_RENAME''@|$(GNULIB_RENAME)|g' \ -e 's|@''GNULIB_SNPRINTF''@|$(GNULIB_SNPRINTF)|g' \ -e 's|@''GNULIB_SPRINTF_POSIX''@|$(GNULIB_SPRINTF_POSIX)|g' \ -e 's|@''GNULIB_STDIO_H_SIGPIPE''@|$(GNULIB_STDIO_H_SIGPIPE)|g' \ @@ -88,6 +89,7 @@ stdio.h: stdio.in.h -e 's|@''REPLACE_PERROR''@|$(REPLACE_PERROR)|g' \ -e 's|@''REPLACE_POPEN''@|$(REPLACE_POPEN)|g' \ -e 's|@''REPLACE_PRINTF''@|$(REPLACE_PRINTF)|g' \ + -e 's|@''REPLACE_RENAME''@|$(REPLACE_RENAME)|g' \ -e 's|@''REPLACE_SNPRINTF''@|$(REPLACE_SNPRINTF)|g' \ -e 's|@''REPLACE_SPRINTF''@|$(REPLACE_SPRINTF)|g' \ -e 's|@''REPLACE_STDIO_WRITE_FUNCS''@|$(REPLACE_STDIO_WRITE_FUNCS)|g' \ -- 2.11.0