+dnl This macro can be used to emulate POSIX linkat. If
+dnl LINK_FOLLOWS_SYMLINKS is 0, link matches linkat(,0), and
+dnl linkat(,AT_SYMLINK_FOLLOW) requires a readlink. If it is 1,
+dnl link matches linkat(,AT_SYMLINK_FOLLOW), and there is no way
+dnl to do linkat(,0) on symlinks (on all other file types,
+dnl link() is sufficient). If it is -1, use a Solaris specific
+dnl runtime test. If it is -2, use a generic runtime test.
+AC_DEFUN([gl_FUNC_LINK_FOLLOWS_SYMLINK],
+[dnl
+ AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
+ AC_CHECK_FUNCS_ONCE([readlink])
+ dnl Mingw lacks link, although gnulib provides a good replacement.
+ dnl However, it also lacks symlink, so there's nothing to test in
+ dnl the first place, and no reason to need to distinguish between
+ dnl linkat variants. So, we set LINK_FOLLOWS_SYMLINKS to 0.
+ gl_link_follows_symlinks=0 # assume GNU behavior
+ if test $ac_cv_func_readlink = yes; then
+ dnl Solaris has an __xpg4 variable in libc, and it determines the
+ dnl behaviour of link(): It dereferences a symlink if and only if
+ dnl __xpg4 != 0.
+ AC_CACHE_CHECK([for __xpg4], [gl_cv_have___xpg4],
+ [AC_LINK_IFELSE(
+ [AC_LANG_PROGRAM(
+ [[extern int __xpg4;]],
+ [[return __xpg4;]])],
+ [gl_cv_have___xpg4=yes],
+ [gl_cv_have___xpg4=no])
+ ])
+ if test $gl_cv_have___xpg4 = yes; then
+ gl_link_follows_symlinks=-1
+ else
+ AC_CACHE_CHECK([whether link(2) dereferences a symlink],
+ [gl_cv_func_link_follows_symlink],
+ [
+ # Create a regular file.
+ echo > conftest.file
+ AC_RUN_IFELSE(
+ [AC_LANG_SOURCE([[