Merge from coreutils CVS.
[gnulib.git] / m4 / chown.m4
1 #serial 10
2 # Determine whether we need the chown wrapper.  chown should accept
3 # arguments of -1 for uid and gid, and it should dereference symlinks.
4 # If it doesn't, arrange to use the replacement function.
5 # From Jim Meyering.
6
7 AC_DEFUN([gl_FUNC_CHOWN],
8 [
9   AC_REQUIRE([AC_TYPE_UID_T])
10   AC_REQUIRE([AC_FUNC_CHOWN])
11   AC_REQUIRE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK])
12
13   if test $ac_cv_func_chown_works = yes; then
14     AC_DEFINE(CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE, 1,
15       [Define if chown is not POSIX compliant regarding IDs of -1.])
16   fi
17
18   # If chown has either of the above problems, then we need the wrapper.
19   if test $ac_cv_func_chown_works$gl_cv_func_chown_follows_symlink = yesyes; then
20     : # no wrapper needed
21   else
22     AC_LIBOBJ(chown)
23     AC_DEFINE(chown, rpl_chown,
24       [Define to rpl_chown if the replacement function should be used.])
25     gl_PREREQ_CHOWN
26   fi
27 ])
28
29 # Determine whether chown follows symlinks (it should).
30 AC_DEFUN([gl_FUNC_CHOWN_FOLLOWS_SYMLINK],
31 [
32   AC_CACHE_CHECK(
33     [whether chown(2) dereferences symlinks],
34     gl_cv_func_chown_follows_symlink,
35     [
36       AC_RUN_IFELSE([AC_LANG_SOURCE([[
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <stdlib.h>
41 #include <errno.h>
42
43         int
44         main ()
45         {
46           char const *dangling_symlink = "conftest.dangle";
47
48           unlink (dangling_symlink);
49           if (symlink ("conftest.no-such", dangling_symlink))
50             abort ();
51
52           /* Exit successfully on a conforming system,
53              i.e., where chown must fail with ENOENT.  */
54           exit ( ! (chown (dangling_symlink, getuid (), getgid ()) != 0
55                     && errno == ENOENT));
56         }
57         ]])],
58         [gl_cv_func_chown_follows_symlink=yes],
59         [gl_cv_func_chown_follows_symlink=no],
60         [gl_cv_func_chown_follows_symlink=no]
61       )
62     ]
63   )
64
65   if test $gl_cv_func_chown_follows_symlink = no; then
66     AC_DEFINE(CHOWN_MODIFIES_SYMLINK, 1,
67       [Define if chown modifies symlinks.])
68   fi
69 ])
70
71 # Prerequisites of lib/chown.c.
72 AC_DEFUN([gl_PREREQ_CHOWN],
73 [
74   AC_CHECK_HEADERS_ONCE(unistd.h fcntl.h)
75   AC_CHECK_FUNC([fchown], , [AC_LIBOBJ(fchown-stub)])
76 ])