git-merge-changelog: Simplify installation instructions.
[gnulib.git] / m4 / rename.m4
blobe9be4b55d73dfbef28f8f67c161e25657e5506ad
1 # rename.m4
2 # serial 37
3 dnl Copyright (C) 2001, 2003, 2005-2006, 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
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, arrange to compile the wrapper function and define
13 dnl macros for use by that function.
14 dnl
16 AC_DEFUN([gl_FUNC_RENAME],
18   AC_REQUIRE([AC_CANONICAL_HOST])
19   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
20   AC_CHECK_FUNCS_ONCE([lstat])
22   dnl Solaris 11.3, 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       [case "$host_os" in
58                             # Guess yes on Linux systems
59                             # and on systems that emulate the Linux system calls.
60          linux* | midipix*) gl_cv_func_rename_slash_dst_works="guessing yes" ;;
61                             # Guess yes on glibc systems.
62          *-gnu*)            gl_cv_func_rename_slash_dst_works="guessing yes" ;;
63                             # Guess no on native Windows.
64          mingw* | windows*) gl_cv_func_rename_slash_dst_works="guessing no" ;;
65                             # If we don't know, obey --enable-cross-guesses.
66          *)                 gl_cv_func_rename_slash_dst_works="$gl_cross_guess_normal" ;;
67        esac
68       ])
69     rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk
70   ])
71   case "$gl_cv_func_rename_slash_dst_works" in
72     *yes) ;;
73     *)
74       REPLACE_RENAME=1
75       AC_DEFINE([RENAME_TRAILING_SLASH_DEST_BUG], [1],
76         [Define if rename does not correctly handle slashes on the destination
77          argument, such as on Solaris 11 or NetBSD 1.6.])
78       ;;
79   esac
81   dnl SunOS 4.1.1_U1 mistakenly forbids rename("dir/","name").
82   dnl Solaris 9 mistakenly allows rename("file/","name").
83   dnl FreeBSD 7.2 mistakenly allows rename("link-to-file/","name").
84   dnl These bugs require stripping trailing slash to avoid corrupting
85   dnl symlinks with a trailing slash.
86   AC_CACHE_CHECK([whether rename honors trailing slash on source],
87     [gl_cv_func_rename_slash_src_works],
88     [rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
89     touch conftest.f && touch conftest.f1 && mkdir conftest.d1 ||
90       AC_MSG_ERROR([cannot create temporary files])
91     # Assume that if we have lstat, we can also check symlinks.
92     if test $ac_cv_func_lstat = yes; then
93       ln -s conftest.f conftest.lnk
94     fi
95     AC_RUN_IFELSE(
96       [AC_LANG_PROGRAM([[
97 #        include <stdio.h>
98 #        include <stdlib.h>
99          ]],
100          [[int result = 0;
101            if (rename ("conftest.f1/", "conftest.d3") == 0)
102              result |= 1;
103            if (rename ("conftest.d1/", "conftest.d2") != 0)
104              result |= 2;
105 #if HAVE_LSTAT
106            if (rename ("conftest.lnk/", "conftest.f") == 0)
107              result |= 4;
108 #endif
109            return result;
110          ]])],
111       [gl_cv_func_rename_slash_src_works=yes],
112       [gl_cv_func_rename_slash_src_works=no],
113       dnl When crosscompiling, assume rename is broken.
114       [case "$host_os" in
115                             # Guess yes on Linux systems
116                             # and on systems that emulate the Linux system calls.
117          linux* | midipix*) gl_cv_func_rename_slash_src_works="guessing yes" ;;
118                             # Guess yes on glibc systems.
119          *-gnu*)            gl_cv_func_rename_slash_src_works="guessing yes" ;;
120                             # Guess yes on native Windows.
121          mingw* | windows*) gl_cv_func_rename_slash_src_works="guessing yes" ;;
122                             # If we don't know, obey --enable-cross-guesses.
123          *)                 gl_cv_func_rename_slash_src_works="$gl_cross_guess_normal" ;;
124        esac
125       ])
126     rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
127   ])
128   case "$gl_cv_func_rename_slash_src_works" in
129     *yes) ;;
130     *)
131       REPLACE_RENAME=1
132       AC_DEFINE([RENAME_TRAILING_SLASH_SOURCE_BUG], [1],
133         [Define if rename does not correctly handle slashes on the source
134          argument, such as on Solaris 9 or cygwin 1.5.])
135       ;;
136   esac
138   dnl NetBSD 1.6 and cygwin 1.5.x mistakenly reduce hard link count
139   dnl on rename("h1","h2").
140   dnl This bug requires stat'ting targets prior to attempting rename.
141   AC_CHECK_FUNCS_ONCE([link])
142   AC_CACHE_CHECK([whether rename manages hard links correctly],
143     [gl_cv_func_rename_link_works],
144     [if test $ac_cv_func_link = yes; then
145        if test $cross_compiling != yes; then
146          rm -rf conftest.f conftest.f1 conftest.f2
147          if touch conftest.f conftest.f2 && ln conftest.f conftest.f1 &&
148              set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
149            AC_RUN_IFELSE(
150              [AC_LANG_PROGRAM([[
151 #               include <errno.h>
152 #               include <stdio.h>
153 #               include <stdlib.h>
154 #               include <unistd.h>
155                 ]GL_MDA_DEFINES],
156                 [[int result = 0;
157                   if (rename ("conftest.f", "conftest.f1"))
158                     result |= 1;
159                   if (unlink ("conftest.f1"))
160                     result |= 2;
162                   /* Allow either the POSIX-required behavior, where the
163                      previous rename kept conftest.f, or the (better) NetBSD
164                      behavior, where it removed conftest.f.  */
165                   if (rename ("conftest.f", "conftest.f") != 0
166                       && errno != ENOENT)
167                     result |= 4;
169                   if (rename ("conftest.f1", "conftest.f1") == 0)
170                     result |= 8;
171                   if (rename ("conftest.f2", "conftest.f2") != 0)
172                     result |= 16;
173                   return result;
174                 ]])],
175              [gl_cv_func_rename_link_works=yes],
176              [gl_cv_func_rename_link_works=no],
177              [dnl We don't get here.
178               :
179              ])
180          else
181            gl_cv_func_rename_link_works="guessing no"
182          fi
183          rm -rf conftest.f conftest.f1 conftest.f2
184        else
185          dnl When crosscompiling, assume rename is broken.
186          case "$host_os" in
187                               # Guess yes on Linux systems
188                               # and on systems that emulate the Linux system calls.
189            linux* | midipix*) gl_cv_func_rename_link_works="guessing yes" ;;
190                               # Guess yes on glibc systems.
191            *-gnu*)            gl_cv_func_rename_link_works="guessing yes" ;;
192                               # Guess yes on native Windows.
193            mingw* | windows*) gl_cv_func_rename_link_works="guessing yes" ;;
194                               # If we don't know, obey --enable-cross-guesses.
195            *)                 gl_cv_func_rename_link_works="$gl_cross_guess_normal" ;;
196          esac
197        fi
198      else
199        gl_cv_func_rename_link_works=yes
200      fi
201     ])
202   case "$gl_cv_func_rename_link_works" in
203     *yes) ;;
204     *)
205       REPLACE_RENAME=1
206       AC_DEFINE([RENAME_HARD_LINK_BUG], [1],
207         [Define if rename fails to leave hard links alone, as on NetBSD 1.6
208          or Cygwin 1.5.])
209       ;;
210   esac
212   dnl Cygwin 1.5.x mistakenly allows rename("dir","file").
213   dnl mingw mistakenly forbids rename("dir1","dir2").
214   dnl These bugs require stripping trailing slash to avoid corrupting
215   dnl symlinks with a trailing slash.
216   AC_CACHE_CHECK([whether rename manages existing destinations correctly],
217     [gl_cv_func_rename_dest_works],
218     [rm -rf conftest.f conftest.d1 conftest.d2
219     touch conftest.f && mkdir conftest.d1 conftest.d2 ||
220       AC_MSG_ERROR([cannot create temporary files])
221     AC_RUN_IFELSE(
222       [AC_LANG_PROGRAM([[
223 #        include <stdio.h>
224 #        include <stdlib.h>
225          ]],
226          [[int result = 0;
227            if (rename ("conftest.d1", "conftest.d2") != 0)
228              result |= 1;
229            if (rename ("conftest.d2", "conftest.f") == 0)
230              result |= 2;
231            return result;
232          ]])],
233       [gl_cv_func_rename_dest_works=yes],
234       [gl_cv_func_rename_dest_works=no],
235       dnl When crosscompiling, assume rename is broken.
236       [case "$host_os" in
237                             # Guess yes on Linux systems
238                             # and on systems that emulate the Linux system calls.
239          linux* | midipix*) gl_cv_func_rename_dest_works="guessing yes" ;;
240                             # Guess yes on glibc systems.
241          *-gnu*)            gl_cv_func_rename_dest_works="guessing yes" ;;
242                             # Guess no on native Windows.
243          mingw* | windows*) gl_cv_func_rename_dest_works="guessing no" ;;
244                             # If we don't know, obey --enable-cross-guesses.
245          *)                 gl_cv_func_rename_dest_works="$gl_cross_guess_normal" ;;
246        esac
247       ])
248     rm -rf conftest.f conftest.d1 conftest.d2
249   ])
250   case "$gl_cv_func_rename_dest_works" in
251     *yes) ;;
252     *)
253       REPLACE_RENAME=1
254       AC_DEFINE([RENAME_DEST_EXISTS_BUG], [1],
255         [Define if rename does not work when the destination file exists,
256          as on Cygwin 1.5 or Windows.])
257       ;;
258   esac