renameat: fix Solaris bugs
[gnulib.git] / m4 / rename.m4
1 # serial 20
2
3 # Copyright (C) 2001, 2003, 2005, 2006, 2009 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
21   dnl Solaris 10 mistakenly allows rename("file","name/").
22   dnl NetBSD 1.6 mistakenly forbids rename("dir","name/").
23   dnl The Solaris bug can be worked around without stripping
24   dnl trailing slash, while the NetBSD bug requires stripping;
25   dnl the two conditions can be distinguished by whether hard
26   dnl links are also broken.
27   AC_CACHE_CHECK([whether rename honors trailing slash on destination],
28     [gl_cv_func_rename_slash_dst_works],
29     [rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2
30     touch conftest.f && mkdir conftest.d1 ||
31       AC_MSG_ERROR([cannot create temporary files])
32     AC_RUN_IFELSE([AC_LANG_PROGRAM([[
33 #       include <stdio.h>
34 #       include <stdlib.h>
35 ]], [if (rename ("conftest.f", "conftest.f1/") == 0) return 1;
36      if (rename ("conftest.d1", "conftest.d2/") != 0) return 2;])],
37       [gl_cv_func_rename_slash_dst_works=yes],
38       [gl_cv_func_rename_slash_dst_works=no],
39       dnl When crosscompiling, assume rename is broken.
40       [gl_cv_func_rename_slash_dst_works="guessing no"])
41     rm -rf conftest.f conftest.f1 conftest.d1 conftest.d2
42   ])
43   if test "x$gl_cv_func_rename_slash_dst_works" != xyes; then
44     AC_LIBOBJ([rename])
45     REPLACE_RENAME=1
46     AC_DEFINE([RENAME_TRAILING_SLASH_DEST_BUG], [1],
47       [Define if rename does not correctly handle slashes on the destination
48        argument, such as on Solaris 10 or NetBSD 1.6.])
49   fi
50
51   dnl SunOS 4.1.1_U1 mistakenly forbids rename("dir/","name").
52   dnl Solaris 9 mistakenly allows rename("file/","name").
53   dnl These bugs require stripping trailing slash to avoid corrupting
54   dnl symlinks with a trailing slash.
55   AC_CACHE_CHECK([whether rename honors trailing slash on source],
56     [gl_cv_func_rename_slash_src_works],
57     [rm -rf conftest.f conftest.d1 conftest.d2
58     touch conftest.f && mkdir conftest.d1 ||
59       AC_MSG_ERROR([cannot create temporary files])
60     AC_RUN_IFELSE([AC_LANG_PROGRAM([[
61 #       include <stdio.h>
62 #       include <stdlib.h>
63 ]], [if (rename ("conftest.f/", "conftest.d2") == 0) return 1;
64      if (rename ("conftest.d1/", "conftest.d2") != 0) return 2;])],
65       [gl_cv_func_rename_slash_src_works=yes],
66       [gl_cv_func_rename_slash_src_works=no],
67       dnl When crosscompiling, assume rename is broken.
68       [gl_cv_func_rename_slash_src_works="guessing no"])
69     rm -rf conftest.f conftest.d1 conftest.d2
70   ])
71   if test "x$gl_cv_func_rename_slash_src_works" != xyes; then
72     AC_LIBOBJ([rename])
73     REPLACE_RENAME=1
74     AC_DEFINE([RENAME_TRAILING_SLASH_SOURCE_BUG], [1],
75       [Define if rename does not correctly handle slashes on the source
76        argument, such as on Solaris 9 or cygwin 1.5.])
77   fi
78
79   dnl NetBSD 1.6 and cygwin 1.5.x mistakenly reduce hard link count
80   dnl on rename("h1","h2").
81   dnl This bug requires stat'ting targets prior to attempting rename.
82   AC_CACHE_CHECK([whether rename manages hard links correctly],
83     [gl_cv_func_rename_link_works],
84     [rm -rf conftest.f conftest.f1
85     if touch conftest.f && ln conftest.f conftest.f1 &&
86         set x `ls -i conftest.f conftest.f1` && test "$2" = "$4"; then
87       AC_RUN_IFELSE([AC_LANG_PROGRAM([[
88 #       include <stdio.h>
89 #       include <stdlib.h>
90 #       include <unistd.h>
91 ]], [if (rename ("conftest.f", "conftest.f1")) return 1;
92      if (unlink ("conftest.f1")) return 2;
93      if (rename ("conftest.f", "conftest.f")) return 3;
94      if (rename ("conftest.f1", "conftest.f1") == 0) return 4;])],
95         [gl_cv_func_rename_link_works=yes],
96         [gl_cv_func_rename_link_works=no],
97         dnl When crosscompiling, assume rename is broken.
98         [gl_cv_func_rename_link_works="guessing no"])
99     else
100       gl_cv_func_rename_link_works="guessing no"
101     fi
102     rm -rf conftest.f conftest.f1
103   ])
104   if test "x$gl_cv_func_rename_link_works" != xyes; then
105     AC_LIBOBJ([rename])
106     REPLACE_RENAME=1
107     AC_DEFINE([RENAME_HARD_LINK_BUG], [1],
108       [Define if rename fails to leave hard links alone, as on NetBSD 1.6
109        or Cygwin 1.5.])
110   fi
111
112   dnl Cygwin 1.5.x mistakenly allows rename("dir","file").
113   dnl mingw mistakenly forbids rename("dir1","dir2").
114   dnl These bugs require stripping trailing slash to avoid corrupting
115   dnl symlinks with a trailing slash.
116   AC_CACHE_CHECK([whether rename manages existing destinations correctly],
117     [gl_cv_func_rename_dest_works],
118     [rm -rf conftest.f conftest.d1 conftest.d2
119     touch conftest.f && mkdir conftest.d1 conftest.d2 ||
120       AC_MSG_ERROR([cannot create temporary files])
121     AC_RUN_IFELSE([AC_LANG_PROGRAM([[
122 #       include <stdio.h>
123 #       include <stdlib.h>
124 ]], [if (rename ("conftest.d1", "conftest.d2") != 0) return 1;
125      if (rename ("conftest.d2", "conftest.f") == 0) return 2;])],
126       [gl_cv_func_rename_dest_works=yes],
127       [gl_cv_func_rename_dest_works=no],
128       dnl When crosscompiling, assume rename is broken.
129       [gl_cv_func_rename_dest_works="guessing no"])
130     rm -rf conftest.f conftest.d1 conftest.d2
131   ])
132   if test "x$gl_cv_func_rename_dest_works" != xyes; then
133     AC_LIBOBJ([rename])
134     REPLACE_RENAME=1
135     AC_DEFINE([RENAME_DEST_EXISTS_BUG], [1],
136       [Define if rename does not work when the destination file exists,
137        as on Cygwin 1.5 or Windows.])
138   fi
139 ])