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-2010 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 runtime test.
16 AC_DEFUN([gl_FUNC_LINK_FOLLOWS_SYMLINK],
18 AC_CHECK_FUNCS_ONCE([readlink])
19 dnl Mingw lacks link, although gnulib provides a good replacement.
20 dnl However, it also lacks symlink, so there's nothing to test in
21 dnl the first place, and no reason to need to distinguish between
22 dnl linkat variants. So, we set LINK_FOLLOWS_SYMLINKS to 0.
23 gl_link_follows_symlinks=0 # assume GNU behavior
24 if test $ac_cv_func_readlink = yes; then
25 AC_CACHE_CHECK([whether link(2) dereferences a symlink],
26 gl_cv_func_link_follows_symlink,
28 # Create a regular file.
30 AC_RUN_IFELSE([AC_LANG_SOURCE([[
31 # include <sys/types.h>
32 # include <sys/stat.h>
36 # define SAME_INODE(Stat_buf_1, Stat_buf_2) \
37 ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
38 && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
43 const char *file = "conftest.file";
44 const char *sym = "conftest.sym";
45 const char *hard = "conftest.hard";
46 struct stat sb_file, sb_hard;
48 /* Create a symlink to the regular file. */
49 if (symlink (file, sym))
52 /* Create a hard link to that symlink. */
56 if (lstat (hard, &sb_hard))
58 if (lstat (file, &sb_file))
61 /* If the dev/inode of hard and file are the same, then
62 the link call followed the symlink. */
63 return SAME_INODE (sb_hard, sb_file) ? 1 : 0;
66 [gl_cv_func_link_follows_symlink=no], dnl GNU behavior
67 [gl_cv_func_link_follows_symlink=yes], dnl Followed link/compile failed
68 [gl_cv_func_link_follows_symlink=unknown] dnl We're cross compiling.
70 rm -f conftest.file conftest.sym conftest.hard
72 case $gl_cv_func_link_follows_symlink in
73 yes) gl_link_follows_symlinks=1 ;;
74 no) ;; # already defaulted to 0
75 *) gl_link_follows_symlinks=-1 ;;
78 AC_DEFINE_UNQUOTED([LINK_FOLLOWS_SYMLINKS], [$gl_link_follows_symlinks],
79 [Define to 1 if `link(2)' dereferences symbolic links, 0 if it
80 creates hard links to symlinks, and -1 if unknown.])