Import upstream version 1.26
[debian/tar] / m4 / link-follow.m4
1 # serial 16
2 dnl Run a program to determine whether link(2) follows symlinks.
3 dnl Set LINK_FOLLOWS_SYMLINKS accordingly.
4
5 # Copyright (C) 1999-2001, 2004-2006, 2009-2011 Free Software Foundation, Inc.
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
9
10 dnl This macro can be used to emulate POSIX linkat.  If
11 dnl LINK_FOLLOWS_SYMLINKS is 0, link matches linkat(,0), and
12 dnl linkat(,AT_SYMLINK_FOLLOW) requires a readlink. If it is 1,
13 dnl link matches linkat(,AT_SYMLINK_FOLLOW), and there is no way
14 dnl to do linkat(,0) on symlinks (on all other file types,
15 dnl link() is sufficient).  If it is -1, use a Solaris specific
16 dnl runtime test.  If it is -2, use a generic runtime test.
17 AC_DEFUN([gl_FUNC_LINK_FOLLOWS_SYMLINK],
18 [dnl
19   AC_CHECK_FUNCS_ONCE([readlink])
20   dnl Mingw lacks link, although gnulib provides a good replacement.
21   dnl However, it also lacks symlink, so there's nothing to test in
22   dnl the first place, and no reason to need to distinguish between
23   dnl linkat variants.  So, we set LINK_FOLLOWS_SYMLINKS to 0.
24   gl_link_follows_symlinks=0 # assume GNU behavior
25   if test $ac_cv_func_readlink = yes; then
26     dnl Solaris has an __xpg4 variable in libc, and it determines the
27     dnl behaviour of link(): It dereferences a symlink if and only if
28     dnl __xpg4 != 0.
29     AC_CACHE_CHECK([for __xpg4], [gl_cv_have___xpg4],
30       [AC_LINK_IFELSE(
31          [AC_LANG_PROGRAM(
32             [[extern int __xpg4;]],
33             [[return __xpg4;]])],
34          [gl_cv_have___xpg4=yes],
35          [gl_cv_have___xpg4=no])
36       ])
37     if test $gl_cv_have___xpg4 = yes; then
38       gl_link_follows_symlinks=-1
39     else
40       AC_CACHE_CHECK([whether link(2) dereferences a symlink],
41                      [gl_cv_func_link_follows_symlink],
42         [
43          # Create a regular file.
44          echo > conftest.file
45          AC_RUN_IFELSE(
46            [AC_LANG_SOURCE([[
47 #       include <sys/types.h>
48 #       include <sys/stat.h>
49 #       include <unistd.h>
50 #       include <stdlib.h>
51
52 #       define SAME_INODE(Stat_buf_1, Stat_buf_2) \
53           ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
54            && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
55
56         int
57         main ()
58         {
59           const char *file = "conftest.file";
60           const char *sym = "conftest.sym";
61           const char *hard = "conftest.hard";
62           struct stat sb_file, sb_hard;
63
64           /* Create a symlink to the regular file. */
65           if (symlink (file, sym))
66             return 2;
67
68           /* Create a hard link to that symlink.  */
69           if (link (sym, hard))
70             return 3;
71
72           if (lstat (hard, &sb_hard))
73             return 4;
74           if (lstat (file, &sb_file))
75             return 5;
76
77           /* If the dev/inode of hard and file are the same, then
78              the link call followed the symlink.  */
79           return SAME_INODE (sb_hard, sb_file) ? 1 : 0;
80         }
81            ]])],
82            [gl_cv_func_link_follows_symlink=no], dnl GNU behavior
83            [gl_cv_func_link_follows_symlink=yes], dnl Followed link/compile failed
84            [gl_cv_func_link_follows_symlink=unknown] dnl We're cross compiling.
85          )
86          rm -f conftest.file conftest.sym conftest.hard
87         ])
88       case $gl_cv_func_link_follows_symlink in
89         yes) gl_link_follows_symlinks=1 ;;
90         no) ;; # already defaulted to 0
91         *) gl_link_follows_symlinks=-2 ;;
92       esac
93     fi
94   fi
95   AC_DEFINE_UNQUOTED([LINK_FOLLOWS_SYMLINKS], [$gl_link_follows_symlinks],
96     [Define to 1 if `link(2)' dereferences symbolic links, 0 if it
97      creates hard links to symlinks, -1 if it depends on the variable __xpg4,
98      and -2 if unknown.])
99 ])