better AC_REQUIRE expanded-before-required-warning avoidance
[gnulib.git] / m4 / chown.m4
1 # serial 21
2 # Determine whether we need the chown wrapper.
3
4 dnl Copyright (C) 1997-2001, 2003-2005, 2007, 2009
5 dnl Free Software 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 AC_DEFUN_ONCE([gl_FUNC_CHOWN],
18 [
19   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
20   AC_REQUIRE([AC_TYPE_UID_T])
21   AC_REQUIRE([AC_FUNC_CHOWN])
22   AC_REQUIRE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK])
23   AC_CHECK_FUNCS_ONCE([chown])
24
25   if test $ac_cv_func_chown_works = no; then
26     AC_DEFINE([CHOWN_FAILS_TO_HONOR_ID_OF_NEGATIVE_ONE], [1],
27       [Define if chown is not POSIX compliant regarding IDs of -1.])
28   fi
29
30   # If chown has either of the above problems, then we need the wrapper.
31   if test $ac_cv_func_chown_works$gl_cv_func_chown_follows_symlink = yesyes; then
32     : # no wrapper needed
33   else
34     REPLACE_CHOWN=1
35     AC_LIBOBJ([chown])
36     gl_PREREQ_CHOWN
37   fi
38 ])
39
40 # Determine whether chown follows symlinks (it should).
41 AC_DEFUN_ONCE([gl_FUNC_CHOWN_FOLLOWS_SYMLINK],
42 [
43   AC_CACHE_CHECK(
44     [whether chown(2) dereferences symlinks],
45     gl_cv_func_chown_follows_symlink,
46     [
47       AC_RUN_IFELSE([AC_LANG_SOURCE([[
48 #include <unistd.h>
49 #include <stdlib.h>
50 #include <errno.h>
51
52         int
53         main ()
54         {
55           char const *dangling_symlink = "conftest.dangle";
56
57           unlink (dangling_symlink);
58           if (symlink ("conftest.no-such", dangling_symlink))
59             abort ();
60
61           /* Exit successfully on a conforming system,
62              i.e., where chown must fail with ENOENT.  */
63           exit ( ! (chown (dangling_symlink, getuid (), getgid ()) != 0
64                     && errno == ENOENT));
65         }
66         ]])],
67         [gl_cv_func_chown_follows_symlink=yes],
68         [gl_cv_func_chown_follows_symlink=no],
69         [gl_cv_func_chown_follows_symlink=yes]
70       )
71     ]
72   )
73
74   if test $gl_cv_func_chown_follows_symlink = no; then
75     AC_DEFINE([CHOWN_MODIFIES_SYMLINK], [1],
76       [Define if chown modifies symlinks.])
77   fi
78 ])
79
80 # Prerequisites of lib/chown.c.
81 AC_DEFUN([gl_PREREQ_CHOWN],
82 [
83   AC_CHECK_FUNC([fchown], , [AC_LIBOBJ([fchown-stub])])
84 ])