re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / m4 / link-follow.m4
1 # serial 19
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-2015 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_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
20   AC_CHECK_FUNCS_ONCE([readlink])
21   dnl Mingw lacks link, although gnulib provides a good replacement.
22   dnl However, it also lacks symlink, so there's nothing to test in
23   dnl the first place, and no reason to need to distinguish between
24   dnl linkat variants.  So, we set LINK_FOLLOWS_SYMLINKS to 0.
25   gl_link_follows_symlinks=0 # assume GNU behavior
26   if test $ac_cv_func_readlink = yes; then
27     dnl Solaris has an __xpg4 variable in libc, and it determines the
28     dnl behaviour of link(): It dereferences a symlink if and only if
29     dnl __xpg4 != 0.
30     AC_CACHE_CHECK([for __xpg4], [gl_cv_have___xpg4],
31       [AC_LINK_IFELSE(
32          [AC_LANG_PROGRAM(
33             [[extern int __xpg4;]],
34             [[return __xpg4;]])],
35          [gl_cv_have___xpg4=yes],
36          [gl_cv_have___xpg4=no])
37       ])
38     if test $gl_cv_have___xpg4 = yes; then
39       gl_link_follows_symlinks=-1
40     else
41       AC_CACHE_CHECK([whether link(2) dereferences a symlink],
42                      [gl_cv_func_link_follows_symlink],
43         [
44          # Create a regular file.
45          echo > conftest.file
46          AC_RUN_IFELSE(
47            [AC_LANG_SOURCE([[
48 #       include <sys/types.h>
49 #       include <sys/stat.h>
50 #       include <unistd.h>
51 #       include <stdlib.h>
52
53 #       define SAME_INODE(Stat_buf_1, Stat_buf_2) \
54           ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
55            && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
56
57         int
58         main ()
59         {
60           const char *file = "conftest.file";
61           const char *sym = "conftest.sym";
62           const char *hard = "conftest.hard";
63           struct stat sb_file, sb_hard;
64
65           /* Create a symlink to the regular file. */
66           if (symlink (file, sym))
67             return 2;
68
69           /* Create a hard link to that symlink.  */
70           if (link (sym, hard))
71             return 3;
72
73           if (lstat (hard, &sb_hard))
74             return 4;
75           if (lstat (file, &sb_file))
76             return 5;
77
78           /* If the dev/inode of hard and file are the same, then
79              the link call followed the symlink.  */
80           return SAME_INODE (sb_hard, sb_file) ? 1 : 0;
81         }
82            ]])],
83            [gl_cv_func_link_follows_symlink=no], dnl GNU behavior
84            [gl_cv_func_link_follows_symlink=yes], dnl Followed link/compile failed
85            [dnl We're cross compiling.
86             dnl The past results are "yes" on Mac OS X, FreeBSD, NetBSD,
87             dnl OpenBSD, Minix, AIX, HP-UX, OSF/1, and "no" on Linux, Cygwin.
88             case "$host_os" in
89                            # On glibc/Linux we know the result.
90               linux*-gnu*) gl_cv_func_link_follows_symlink="guessing no" ;;
91                            # Otherwise, we don't know.
92               *)           gl_cv_func_link_follows_symlink=unknown ;;
93             esac
94            ])
95          rm -f conftest.file conftest.sym conftest.hard
96         ])
97       case "$gl_cv_func_link_follows_symlink" in
98         *yes) gl_link_follows_symlinks=1 ;;
99         *no) ;; # already defaulted to 0
100         *) gl_link_follows_symlinks=-2 ;;
101       esac
102     fi
103   fi
104   AC_DEFINE_UNQUOTED([LINK_FOLLOWS_SYMLINKS], [$gl_link_follows_symlinks],
105     [Define to 1 if 'link(2)' dereferences symbolic links, 0 if it
106      creates hard links to symlinks, -1 if it depends on the variable __xpg4,
107      and -2 if unknown.])
108 ])