3 dnl Copyright (C) 1999-2001, 2004-2006, 2009-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl Run a program to determine whether link(2) follows symlinks.
9 dnl Set LINK_FOLLOWS_SYMLINKS accordingly.
11 dnl This macro can be used to emulate POSIX linkat. If
12 dnl LINK_FOLLOWS_SYMLINKS is 0, link matches linkat(,0), and
13 dnl linkat(,AT_SYMLINK_FOLLOW) requires a readlink. If it is 1,
14 dnl link matches linkat(,AT_SYMLINK_FOLLOW), and there is no way
15 dnl to do linkat(,0) on symlinks (on all other file types,
16 dnl link() is sufficient). If it is -1, use a Solaris specific
17 dnl runtime test. If it is -2, use a generic runtime test.
18 AC_DEFUN([gl_FUNC_LINK_FOLLOWS_SYMLINK],
20 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
21 AC_CHECK_FUNCS_ONCE([readlink])
22 dnl Mingw lacks link, although gnulib provides a good replacement.
23 dnl However, it also lacks symlink, so there's nothing to test in
24 dnl the first place, and no reason to need to distinguish between
25 dnl linkat variants. So, we set LINK_FOLLOWS_SYMLINKS to 0.
26 gl_link_follows_symlinks=0 # assume GNU behavior
27 if test $ac_cv_func_readlink = yes; then
28 dnl Solaris has an __xpg4 variable in libc, and it determines the
29 dnl behaviour of link(): It dereferences a symlink if and only if
31 AC_CACHE_CHECK([for __xpg4], [gl_cv_have___xpg4],
34 [[extern int __xpg4;]],
36 [gl_cv_have___xpg4=yes],
37 [gl_cv_have___xpg4=no])
39 if test $gl_cv_have___xpg4 = yes; then
40 gl_link_follows_symlinks=-1
42 AC_CACHE_CHECK([whether link(2) dereferences a symlink],
43 [gl_cv_func_link_follows_symlink],
45 # Create a regular file.
49 # include <sys/types.h>
50 # include <sys/stat.h>
54 # define SAME_INODE(Stat_buf_1, Stat_buf_2) \
55 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
56 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
61 const char *file = "conftest.file";
62 const char *sym = "conftest.sym";
63 const char *hard = "conftest.hard";
64 struct stat sb_file, sb_hard;
66 /* Create a symlink to the regular file. */
67 if (symlink (file, sym))
70 /* Create a hard link to that symlink. */
74 if (lstat (hard, &sb_hard))
76 if (lstat (file, &sb_file))
79 /* If the dev/inode of hard and file are the same, then
80 the link call followed the symlink. */
81 return SAME_INODE (sb_hard, sb_file) ? 1 : 0;
84 [gl_cv_func_link_follows_symlink=no], dnl GNU behavior
85 [gl_cv_func_link_follows_symlink=yes], dnl Followed link/compile failed
86 [dnl We're cross compiling.
87 dnl The past results are "yes" on Mac OS X, FreeBSD, NetBSD,
88 dnl OpenBSD, Minix, AIX, HP-UX, OSF/1, and "no" on Linux, Cygwin.
90 # On glibc/Linux we know the result.
91 linux*-gnu* | gnu*) gl_cv_func_link_follows_symlink="guessing no" ;;
92 # On musl/Linux we know the result.
93 linux*-musl*) gl_cv_func_link_follows_symlink="guessing no" ;;
94 # Otherwise, we don't know.
95 *) gl_cv_func_link_follows_symlink=unknown ;;
98 rm -f conftest.file conftest.sym conftest.hard
100 case "$gl_cv_func_link_follows_symlink" in
101 *yes) gl_link_follows_symlinks=1 ;;
102 *no) ;; # already defaulted to 0
103 *) gl_link_follows_symlinks=-2 ;;
107 AC_DEFINE_UNQUOTED([LINK_FOLLOWS_SYMLINKS], [$gl_link_follows_symlinks],
108 [Define to 1 if 'link(2)' dereferences symbolic links, 0 if it
109 creates hard links to symlinks, -1 if it depends on the variable __xpg4,