maint: update copyright
[gnulib.git] / m4 / rename.m4
1 # serial 26
2
3 # Copyright (C) 2001, 2003, 2005-2006, 2009-2014 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       [case "$host_os" in
58                  # Guess yes on glibc systems.
59          *-gnu*) gl_cv_func_rename_slash_dst_works="guessing yes" ;;
60                  # If we don't know, assume the worst.
61          *)      gl_cv_func_rename_slash_dst_works="guessing no" ;;
62        esac
63       ])
64     rm -rf conftest.f conftest.f1 conftest.f2 conftest.d1 conftest.d2 conftest.lnk
65   ])
66   case "$gl_cv_func_rename_slash_dst_works" in
67     *yes) ;;
68     *)
69       REPLACE_RENAME=1
70       AC_DEFINE([RENAME_TRAILING_SLASH_DEST_BUG], [1],
71         [Define if rename does not correctly handle slashes on the destination
72          argument, such as on Solaris 10 or NetBSD 1.6.])
73       ;;
74   esac
75
76   dnl SunOS 4.1.1_U1 mistakenly forbids rename("dir/","name").
77   dnl Solaris 9 mistakenly allows rename("file/","name").
78   dnl FreeBSD 7.2 mistakenly allows rename("link-to-file/","name").
79   dnl These bugs require stripping trailing slash to avoid corrupting
80   dnl symlinks with a trailing slash.
81   AC_CACHE_CHECK([whether rename honors trailing slash on source],
82     [gl_cv_func_rename_slash_src_works],
83     [rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
84     touch conftest.f && touch conftest.f1 && mkdir conftest.d1 ||
85       AC_MSG_ERROR([cannot create temporary files])
86     # Assume that if we have lstat, we can also check symlinks.
87     if test $ac_cv_func_lstat = yes; then
88       ln -s conftest.f conftest.lnk
89     fi
90     AC_RUN_IFELSE(
91       [AC_LANG_PROGRAM([[
92 #        include <stdio.h>
93 #        include <stdlib.h>
94          ]],
95          [[int result = 0;
96            if (rename ("conftest.f1/", "conftest.d3") == 0)
97              result |= 1;
98            if (rename ("conftest.d1/", "conftest.d2") != 0)
99              result |= 2;
100 #if HAVE_LSTAT
101            if (rename ("conftest.lnk/", "conftest.f") == 0)
102              result |= 4;
103 #endif
104            return result;
105          ]])],
106       [gl_cv_func_rename_slash_src_works=yes],
107       [gl_cv_func_rename_slash_src_works=no],
108       dnl When crosscompiling, assume rename is broken.
109       [case "$host_os" in
110                  # Guess yes on glibc systems.
111          *-gnu*) gl_cv_func_rename_slash_src_works="guessing yes" ;;
112                  # If we don't know, assume the worst.
113          *)      gl_cv_func_rename_slash_src_works="guessing no" ;;
114        esac
115       ])
116     rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2 conftest.d3 conftest.lnk
117   ])
118   case "$gl_cv_func_rename_slash_src_works" in
119     *yes) ;;
120     *)
121       REPLACE_RENAME=1
122       AC_DEFINE([RENAME_TRAILING_SLASH_SOURCE_BUG], [1],
123         [Define if rename does not correctly handle slashes on the source
124          argument, such as on Solaris 9 or cygwin 1.5.])
125       ;;
126   esac
127
128   dnl NetBSD 1.6 and cygwin 1.5.x mistakenly reduce hard link count
129   dnl on rename("h1","h2").
130   dnl This bug requires stat'ting targets prior to attempting rename.
131   AC_CHECK_FUNCS_ONCE([link])
132   AC_CACHE_CHECK([whether rename manages hard links correctly],
133     [gl_cv_func_rename_link_works],
134     [if test $ac_cv_func_link = yes; then
135        rm -rf conftest.f conftest.f1
136        if touch conftest.f && ln conftest.f conftest.f1 &&
137            set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
138          AC_RUN_IFELSE(
139            [AC_LANG_PROGRAM([[
140 #             include <stdio.h>
141 #             include <stdlib.h>
142 #             include <unistd.h>
143               ]],
144               [[int result = 0;
145                 if (rename ("conftest.f", "conftest.f1"))
146                   result |= 1;
147                 if (unlink ("conftest.f1"))
148                   result |= 2;
149                 if (rename ("conftest.f", "conftest.f"))
150                   result |= 4;
151                 if (rename ("conftest.f1", "conftest.f1") == 0)
152                   result |= 8;
153                 return result;
154               ]])],
155            [gl_cv_func_rename_link_works=yes],
156            [gl_cv_func_rename_link_works=no],
157            dnl When crosscompiling, assume rename is broken.
158            [case "$host_os" in
159                       # Guess yes on glibc systems.
160               *-gnu*) gl_cv_func_rename_link_works="guessing yes" ;;
161                       # If we don't know, assume the worst.
162               *)      gl_cv_func_rename_link_works="guessing no" ;;
163             esac
164            ])
165        else
166          gl_cv_func_rename_link_works="guessing no"
167        fi
168        rm -rf conftest.f conftest.f1
169      else
170        gl_cv_func_rename_link_works=yes
171      fi
172     ])
173   case "$gl_cv_func_rename_link_works" in
174     *yes) ;;
175     *)
176       REPLACE_RENAME=1
177       AC_DEFINE([RENAME_HARD_LINK_BUG], [1],
178         [Define if rename fails to leave hard links alone, as on NetBSD 1.6
179          or Cygwin 1.5.])
180       ;;
181   esac
182
183   dnl Cygwin 1.5.x mistakenly allows rename("dir","file").
184   dnl mingw mistakenly forbids rename("dir1","dir2").
185   dnl These bugs require stripping trailing slash to avoid corrupting
186   dnl symlinks with a trailing slash.
187   AC_CACHE_CHECK([whether rename manages existing destinations correctly],
188     [gl_cv_func_rename_dest_works],
189     [rm -rf conftest.f conftest.d1 conftest.d2
190     touch conftest.f && mkdir conftest.d1 conftest.d2 ||
191       AC_MSG_ERROR([cannot create temporary files])
192     AC_RUN_IFELSE(
193       [AC_LANG_PROGRAM([[
194 #        include <stdio.h>
195 #        include <stdlib.h>
196          ]],
197          [[int result = 0;
198            if (rename ("conftest.d1", "conftest.d2") != 0)
199              result |= 1;
200            if (rename ("conftest.d2", "conftest.f") == 0)
201              result |= 2;
202            return result;
203          ]])],
204       [gl_cv_func_rename_dest_works=yes],
205       [gl_cv_func_rename_dest_works=no],
206       dnl When crosscompiling, assume rename is broken.
207       [case "$host_os" in
208                  # Guess yes on glibc systems.
209          *-gnu*) gl_cv_func_rename_dest_works="guessing yes" ;;
210                  # If we don't know, assume the worst.
211          *)      gl_cv_func_rename_dest_works="guessing no" ;;
212        esac
213       ])
214     rm -rf conftest.f conftest.d1 conftest.d2
215   ])
216   case "$gl_cv_func_rename_dest_works" in
217     *yes) ;;
218     *)
219       REPLACE_RENAME=1
220       AC_DEFINE([RENAME_DEST_EXISTS_BUG], [1],
221         [Define if rename does not work when the destination file exists,
222          as on Cygwin 1.5 or Windows.])
223       ;;
224   esac
225 ])