2 dnl Run a program to determine whether link(2) follows symlinks.
3 dnl Set LINK_FOLLOWS_SYMLINKS accordingly.
5 # Copyright (C) 1999-2001, 2004-2006, 2009-2020 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.
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],
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
30 AC_CACHE_CHECK([for __xpg4], [gl_cv_have___xpg4],
33 [[extern int __xpg4;]],
35 [gl_cv_have___xpg4=yes],
36 [gl_cv_have___xpg4=no])
38 if test $gl_cv_have___xpg4 = yes; then
39 gl_link_follows_symlinks=-1
41 AC_CACHE_CHECK([whether link(2) dereferences a symlink],
42 [gl_cv_func_link_follows_symlink],
44 # Create a regular file.
48 # include <sys/types.h>
49 # include <sys/stat.h>
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)
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;
65 /* Create a symlink to the regular file. */
66 if (symlink (file, sym))
69 /* Create a hard link to that symlink. */
73 if (lstat (hard, &sb_hard))
75 if (lstat (file, &sb_file))
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;
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.
89 # On glibc/Linux we know the result.
90 linux*-gnu* | gnu*) gl_cv_func_link_follows_symlink="guessing no" ;;
91 # On musl/Linux we know the result.
92 linux*-musl*) gl_cv_func_link_follows_symlink="guessing no" ;;
93 # Otherwise, we don't know.
94 *) gl_cv_func_link_follows_symlink=unknown ;;
97 rm -f conftest.file conftest.sym conftest.hard
99 case "$gl_cv_func_link_follows_symlink" in
100 *yes) gl_link_follows_symlinks=1 ;;
101 *no) ;; # already defaulted to 0
102 *) gl_link_follows_symlinks=-2 ;;
106 AC_DEFINE_UNQUOTED([LINK_FOLLOWS_SYMLINKS], [$gl_link_follows_symlinks],
107 [Define to 1 if 'link(2)' dereferences symbolic links, 0 if it
108 creates hard links to symlinks, -1 if it depends on the variable __xpg4,