maint: update copyright
[gnulib.git] / m4 / chown.m4
1 # serial 27
2 # Determine whether we need the chown wrapper.
3
4 dnl Copyright (C) 1997-2001, 2003-2005, 2007, 2009-2014 Free Software
5 dnl Foundation, Inc.
6
7 dnl This file is free software; the Free Software Foundation
8 dnl gives unlimited permission to copy and/or distribute it,
9 dnl with or without modifications, as long as this notice is preserved.
10
11 # chown should accept arguments of -1 for uid and gid, and it should
12 # dereference symlinks.  If it doesn't, arrange to use the replacement
13 # function.
14
15 # From Jim Meyering.
16
17 m4_version_prereq([2.70], [] ,[
18
19 # This is taken from the following Autoconf patch:
20 # http://git.savannah.gnu.org/gitweb/?p=autoconf.git;a=commitdiff;h=7fbb553727ed7e0e689a17594b58559ecf3ea6e9
21 AC_DEFUN([AC_FUNC_CHOWN],
22 [
23   AC_REQUIRE([AC_TYPE_UID_T])dnl
24   AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
25   AC_CHECK_HEADERS([unistd.h])
26   AC_CACHE_CHECK([for working chown],
27     [ac_cv_func_chown_works],
28     [AC_RUN_IFELSE(
29        [AC_LANG_PROGRAM(
30           [AC_INCLUDES_DEFAULT
31            [#include <fcntl.h>
32           ]],
33           [[
34             char *f = "conftest.chown";
35             struct stat before, after;
36
37             if (creat (f, 0600) < 0)
38               return 1;
39             if (stat (f, &before) < 0)
40               return 1;
41             if (chown (f, (uid_t) -1, (gid_t) -1) == -1)
42               return 1;
43             if (stat (f, &after) < 0)
44               return 1;
45             return ! (before.st_uid == after.st_uid && before.st_gid == after.st_gid);
46           ]])
47        ],
48        [ac_cv_func_chown_works=yes],
49        [ac_cv_func_chown_works=no],
50        [case "$host_os" in # ((
51                   # Guess yes on glibc systems.
52           *-gnu*) ac_cv_func_chown_works=yes ;;
53                   # If we don't know, assume the worst.
54           *)      ac_cv_func_chown_works=no ;;
55         esac
56        ])
57      rm -f conftest.chown
58     ])
59   if test $ac_cv_func_chown_works = yes; then
60     AC_DEFINE([HAVE_CHOWN], [1],
61       [Define to 1 if your system has a working `chown' function.])
62   fi
63 ])# AC_FUNC_CHOWN
64
65 ])
66
67 AC_DEFUN_ONCE([gl_FUNC_CHOWN],
68 [
69   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
70   AC_REQUIRE([AC_TYPE_UID_T])
71   AC_REQUIRE([AC_FUNC_CHOWN])
72   AC_REQUIRE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK])
73   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
74   AC_CHECK_FUNCS_ONCE([chown fchown])
75
76   dnl mingw lacks chown altogether.
77   if test $ac_cv_func_chown = no; then
78     HAVE_CHOWN=0
79   else
80     dnl Some old systems treated chown like lchown.
81     if test $gl_cv_func_chown_follows_symlink = no; then
82       REPLACE_CHOWN=1
83     fi
84
85     dnl Some old systems tried to use uid/gid -1 literally.
86     if test $ac_cv_func_chown_works = no; then
87       AC_DEFINE([CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE], [1],
88         [Define if chown is not POSIX compliant regarding IDs of -1.])
89       REPLACE_CHOWN=1
90     fi
91
92     dnl Solaris 9 ignores trailing slash.
93     dnl FreeBSD 7.2 mishandles trailing slash on symlinks.
94     dnl Likewise for AIX 7.1.
95     AC_CACHE_CHECK([whether chown honors trailing slash],
96       [gl_cv_func_chown_slash_works],
97       [touch conftest.file && rm -f conftest.link
98        AC_RUN_IFELSE([AC_LANG_PROGRAM([[
99 #include <unistd.h>
100 #include <stdlib.h>
101 #include <errno.h>
102 ]], [[    if (symlink ("conftest.file", "conftest.link")) return 1;
103           if (chown ("conftest.link/", getuid (), getgid ()) == 0) return 2;
104         ]])],
105         [gl_cv_func_chown_slash_works=yes],
106         [gl_cv_func_chown_slash_works=no],
107         [case "$host_os" in
108                    # Guess yes on glibc systems.
109            *-gnu*) gl_cv_func_chown_slash_works="guessing yes" ;;
110                    # If we don't know, assume the worst.
111            *)      gl_cv_func_chown_slash_works="guessing no" ;;
112          esac
113         ])
114       rm -f conftest.link conftest.file])
115     case "$gl_cv_func_chown_slash_works" in
116       *yes) ;;
117       *)
118         AC_DEFINE([CHOWN_TRAILING_SLASH_BUG], [1],
119           [Define to 1 if chown mishandles trailing slash.])
120         REPLACE_CHOWN=1
121         ;;
122     esac
123
124     dnl OpenBSD fails to update ctime if ownership does not change.
125     AC_CACHE_CHECK([whether chown always updates ctime],
126       [gl_cv_func_chown_ctime_works],
127       [AC_RUN_IFELSE([AC_LANG_PROGRAM([[
128 #include <unistd.h>
129 #include <stdlib.h>
130 #include <errno.h>
131 #include <fcntl.h>
132 #include <sys/stat.h>
133 ]], [[    struct stat st1, st2;
134           if (close (creat ("conftest.file", 0600))) return 1;
135           if (stat ("conftest.file", &st1)) return 2;
136           sleep (1);
137           if (chown ("conftest.file", st1.st_uid, st1.st_gid)) return 3;
138           if (stat ("conftest.file", &st2)) return 4;
139           if (st2.st_ctime <= st1.st_ctime) return 5;
140         ]])],
141         [gl_cv_func_chown_ctime_works=yes],
142         [gl_cv_func_chown_ctime_works=no],
143         [case "$host_os" in
144                    # Guess yes on glibc systems.
145            *-gnu*) gl_cv_func_chown_ctime_works="guessing yes" ;;
146                    # If we don't know, assume the worst.
147            *)      gl_cv_func_chown_ctime_works="guessing no" ;;
148          esac
149         ])
150       rm -f conftest.file])
151     case "$gl_cv_func_chown_ctime_works" in
152       *yes) ;;
153       *)
154         AC_DEFINE([CHOWN_CHANGE_TIME_BUG], [1], [Define to 1 if chown fails
155           to change ctime when at least one argument was not -1.])
156         REPLACE_CHOWN=1
157         ;;
158     esac
159   fi
160 ])
161
162 # Determine whether chown follows symlinks (it should).
163 AC_DEFUN_ONCE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK],
164 [
165   AC_CACHE_CHECK(
166     [whether chown dereferences symlinks],
167     [gl_cv_func_chown_follows_symlink],
168     [
169       AC_RUN_IFELSE([AC_LANG_SOURCE([[
170 #include <unistd.h>
171 #include <stdlib.h>
172 #include <errno.h>
173
174         int
175         main ()
176         {
177           int result = 0;
178           char const *dangling_symlink = "conftest.dangle";
179
180           unlink (dangling_symlink);
181           if (symlink ("conftest.no-such", dangling_symlink))
182             abort ();
183
184           /* Exit successfully on a conforming system,
185              i.e., where chown must fail with ENOENT.  */
186           if (chown (dangling_symlink, getuid (), getgid ()) == 0)
187             result |= 1;
188           if (errno != ENOENT)
189             result |= 2;
190           return result;
191         }
192         ]])],
193         [gl_cv_func_chown_follows_symlink=yes],
194         [gl_cv_func_chown_follows_symlink=no],
195         [gl_cv_func_chown_follows_symlink=yes]
196       )
197     ]
198   )
199
200   if test $gl_cv_func_chown_follows_symlink = no; then
201     AC_DEFINE([CHOWN_MODIFIES_SYMLINK], [1],
202       [Define if chown modifies symlinks.])
203   fi
204 ])