update NEWS.stable
[gnulib.git] / m4 / rename.m4
1 # serial 25
2
3 # Copyright (C) 2001, 2003, 2005-2006, 2009-2012 Free Software Foundation, Inc.
4 # This file is free software; the Free Software Foundation
5 # gives unlimited permission to copy and/or distribute it,
6 # with or without modifications, as long as this notice is preserved.
7
8 dnl From Volker Borchert.
9 dnl Determine whether rename works for source file names with a trailing slash.
10 dnl The rename from SunOS 4.1.1_U1 doesn't.
11 dnl
12 dnl If it doesn't, then define RENAME_TRAILING_SLASH_BUG and arrange
13 dnl to compile the wrapper function.
14 dnl
15
16 AC_DEFUN([gl_FUNC_RENAME],
17 [
18   AC_REQUIRE([AC_CANONICAL_HOST])
19   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
20   AC_CHECK_FUNCS_ONCE([lstat])
21
22   dnl Solaris 10, AIX 7.1 mistakenly allow rename("file","name/").
23   dnl NetBSD 1.6 mistakenly forbids rename("dir","name/").
24   dnl FreeBSD 7.2 mistakenly allows rename("file","link-to-file/").
25   dnl The Solaris bug can be worked around without stripping
26   dnl trailing slash, while the NetBSD bug requires stripping;
27   dnl the two conditions can be distinguished by whether hard
28   dnl links are also broken.
29   AC_CACHE_CHECK([whether rename honors trailing slash on destination],
30     [gl_cv_func_rename_slash_dst_works],
31     [rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk
32     touch conftest.f && touch conftest.f1 && mkdir conftest.d1 ||
33       AC_MSG_ERROR([cannot create temporary files])
34     # Assume that if we have lstat, we can also check symlinks.
35     if test $ac_cv_func_lstat = yes; then
36       ln -s conftest.f conftest.lnk
37     fi
38     AC_RUN_IFELSE(
39       [AC_LANG_PROGRAM([[
40 #        include <stdio.h>
41 #        include <stdlib.h>
42          ]],
43          [[int result = 0;
44            if (rename ("conftest.f1", "conftest.f2/") == 0)
45              result |= 1;
46            if (rename ("conftest.d1", "conftest.d2/") != 0)
47              result |= 2;
48 #if HAVE_LSTAT
49            if (rename ("conftest.f", "conftest.lnk/") == 0)
50              result |= 4;
51 #endif
52            return result;
53          ]])],
54       [gl_cv_func_rename_slash_dst_works=yes],
55       [gl_cv_func_rename_slash_dst_works=no],
56       dnl When crosscompiling, assume rename is broken.
57       [gl_cv_func_rename_slash_dst_works="guessing no"])
58     rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk
59   ])
60   if test "x$gl_cv_func_rename_slash_dst_works" != xyes; then
61     REPLACE_RENAME=1
62     AC_DEFINE([RENAME_TRAILING_SLASH_DEST_BUG], [1],
63       [Define if rename does not correctly handle slashes on the destination
64        argument, such as on Solaris 10 or NetBSD 1.6.])
65   fi
66
67   dnl SunOS 4.1.1_U1 mistakenly forbids rename("dir/","name").
68   dnl Solaris 9 mistakenly allows rename("file/","name").
69   dnl FreeBSD 7.2 mistakenly allows rename("link-to-file/","name").
70   dnl These bugs require stripping trailing slash to avoid corrupting
71   dnl symlinks with a trailing slash.
72   AC_CACHE_CHECK([whether rename honors trailing slash on source],
73     [gl_cv_func_rename_slash_src_works],
74     [rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
75     touch conftest.f && touch conftest.f1 && mkdir conftest.d1 ||
76       AC_MSG_ERROR([cannot create temporary files])
77     # Assume that if we have lstat, we can also check symlinks.
78     if test $ac_cv_func_lstat = yes; then
79       ln -s conftest.f conftest.lnk
80     fi
81     AC_RUN_IFELSE(
82       [AC_LANG_PROGRAM([[
83 #        include <stdio.h>
84 #        include <stdlib.h>
85          ]],
86          [[int result = 0;
87            if (rename ("conftest.f1/", "conftest.d3") == 0)
88              result |= 1;
89            if (rename ("conftest.d1/", "conftest.d2") != 0)
90              result |= 2;
91 #if HAVE_LSTAT
92            if (rename ("conftest.lnk/", "conftest.f") == 0)
93              result |= 4;
94 #endif
95            return result;
96          ]])],
97       [gl_cv_func_rename_slash_src_works=yes],
98       [gl_cv_func_rename_slash_src_works=no],
99       dnl When crosscompiling, assume rename is broken.
100       [gl_cv_func_rename_slash_src_works="guessing no"])
101     rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
102   ])
103   if test "x$gl_cv_func_rename_slash_src_works" != xyes; then
104     REPLACE_RENAME=1
105     AC_DEFINE([RENAME_TRAILING_SLASH_SOURCE_BUG], [1],
106       [Define if rename does not correctly handle slashes on the source
107        argument, such as on Solaris 9 or cygwin 1.5.])
108   fi
109
110   dnl NetBSD 1.6 and cygwin 1.5.x mistakenly reduce hard link count
111   dnl on rename("h1","h2").
112   dnl This bug requires stat'ting targets prior to attempting rename.
113   AC_CHECK_FUNCS_ONCE([link])
114   AC_CACHE_CHECK([whether rename manages hard links correctly],
115     [gl_cv_func_rename_link_works],
116     [if test $ac_cv_func_link = yes; then
117        rm -rf conftest.f conftest.f1
118        if touch conftest.f && ln conftest.f conftest.f1 &&
119            set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
120          AC_RUN_IFELSE(
121            [AC_LANG_PROGRAM([[
122 #             include <stdio.h>
123 #             include <stdlib.h>
124 #             include <unistd.h>
125               ]],
126               [[int result = 0;
127                 if (rename ("conftest.f", "conftest.f1"))
128                   result |= 1;
129                 if (unlink ("conftest.f1"))
130                   result |= 2;
131                 if (rename ("conftest.f", "conftest.f"))
132                   result |= 4;
133                 if (rename ("conftest.f1", "conftest.f1") == 0)
134                   result |= 8;
135                 return result;
136               ]])],
137            [gl_cv_func_rename_link_works=yes],
138            [gl_cv_func_rename_link_works=no],
139            dnl When crosscompiling, assume rename is broken.
140            [gl_cv_func_rename_link_works="guessing no"])
141        else
142          gl_cv_func_rename_link_works="guessing no"
143        fi
144        rm -rf conftest.f conftest.f1
145      else
146        gl_cv_func_rename_link_works=yes
147      fi
148     ])
149   if test "x$gl_cv_func_rename_link_works" != xyes; then
150     REPLACE_RENAME=1
151     AC_DEFINE([RENAME_HARD_LINK_BUG], [1],
152       [Define if rename fails to leave hard links alone, as on NetBSD 1.6
153        or Cygwin 1.5.])
154   fi
155
156   dnl Cygwin 1.5.x mistakenly allows rename("dir","file").
157   dnl mingw mistakenly forbids rename("dir1","dir2").
158   dnl These bugs require stripping trailing slash to avoid corrupting
159   dnl symlinks with a trailing slash.
160   AC_CACHE_CHECK([whether rename manages existing destinations correctly],
161     [gl_cv_func_rename_dest_works],
162     [rm -rf conftest.f conftest.d1 conftest.d2
163     touch conftest.f && mkdir conftest.d1 conftest.d2 ||
164       AC_MSG_ERROR([cannot create temporary files])
165     AC_RUN_IFELSE(
166       [AC_LANG_PROGRAM([[
167 #        include <stdio.h>
168 #        include <stdlib.h>
169          ]],
170          [[int result = 0;
171            if (rename ("conftest.d1", "conftest.d2") != 0)
172              result |= 1;
173            if (rename ("conftest.d2", "conftest.f") == 0)
174              result |= 2;
175            return result;
176          ]])],
177       [gl_cv_func_rename_dest_works=yes],
178       [gl_cv_func_rename_dest_works=no],
179       dnl When crosscompiling, assume rename is broken.
180       [gl_cv_func_rename_dest_works="guessing no"])
181     rm -rf conftest.f conftest.d1 conftest.d2
182   ])
183   if test "x$gl_cv_func_rename_dest_works" != xyes; then
184     REPLACE_RENAME=1
185     AC_DEFINE([RENAME_DEST_EXISTS_BUG], [1],
186       [Define if rename does not work when the destination file exists,
187        as on Cygwin 1.5 or Windows.])
188   fi
189 ])