Blame m4/link-follow.m4

Packit 9e4112
# serial 20
Packit 9e4112
dnl Run a program to determine whether link(2) follows symlinks.
Packit 9e4112
dnl Set LINK_FOLLOWS_SYMLINKS accordingly.
Packit 9e4112
Packit 9e4112
# Copyright (C) 1999-2001, 2004-2006, 2009-2018 Free Software Foundation, Inc.
Packit 9e4112
# This file is free software; the Free Software Foundation
Packit 9e4112
# gives unlimited permission to copy and/or distribute it,
Packit 9e4112
# with or without modifications, as long as this notice is preserved.
Packit 9e4112
Packit 9e4112
dnl This macro can be used to emulate POSIX linkat.  If
Packit 9e4112
dnl LINK_FOLLOWS_SYMLINKS is 0, link matches linkat(,0), and
Packit 9e4112
dnl linkat(,AT_SYMLINK_FOLLOW) requires a readlink. If it is 1,
Packit 9e4112
dnl link matches linkat(,AT_SYMLINK_FOLLOW), and there is no way
Packit 9e4112
dnl to do linkat(,0) on symlinks (on all other file types,
Packit 9e4112
dnl link() is sufficient).  If it is -1, use a Solaris specific
Packit 9e4112
dnl runtime test.  If it is -2, use a generic runtime test.
Packit 9e4112
AC_DEFUN([gl_FUNC_LINK_FOLLOWS_SYMLINK],
Packit 9e4112
[dnl
Packit 9e4112
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Packit 9e4112
  AC_CHECK_FUNCS_ONCE([readlink])
Packit 9e4112
  dnl Mingw lacks link, although gnulib provides a good replacement.
Packit 9e4112
  dnl However, it also lacks symlink, so there's nothing to test in
Packit 9e4112
  dnl the first place, and no reason to need to distinguish between
Packit 9e4112
  dnl linkat variants.  So, we set LINK_FOLLOWS_SYMLINKS to 0.
Packit 9e4112
  gl_link_follows_symlinks=0 # assume GNU behavior
Packit 9e4112
  if test $ac_cv_func_readlink = yes; then
Packit 9e4112
    dnl Solaris has an __xpg4 variable in libc, and it determines the
Packit 9e4112
    dnl behaviour of link(): It dereferences a symlink if and only if
Packit 9e4112
    dnl __xpg4 != 0.
Packit 9e4112
    AC_CACHE_CHECK([for __xpg4], [gl_cv_have___xpg4],
Packit 9e4112
      [AC_LINK_IFELSE(
Packit 9e4112
         [AC_LANG_PROGRAM(
Packit 9e4112
            [[extern int __xpg4;]],
Packit 9e4112
            [[return __xpg4;]])],
Packit 9e4112
         [gl_cv_have___xpg4=yes],
Packit 9e4112
         [gl_cv_have___xpg4=no])
Packit 9e4112
      ])
Packit 9e4112
    if test $gl_cv_have___xpg4 = yes; then
Packit 9e4112
      gl_link_follows_symlinks=-1
Packit 9e4112
    else
Packit 9e4112
      AC_CACHE_CHECK([whether link(2) dereferences a symlink],
Packit 9e4112
                     [gl_cv_func_link_follows_symlink],
Packit 9e4112
        [
Packit 9e4112
         # Create a regular file.
Packit 9e4112
         echo > conftest.file
Packit 9e4112
         AC_RUN_IFELSE(
Packit 9e4112
           [AC_LANG_SOURCE([[
Packit 9e4112
#       include <sys/types.h>
Packit 9e4112
#       include <sys/stat.h>
Packit 9e4112
#       include <unistd.h>
Packit 9e4112
#       include <stdlib.h>
Packit 9e4112
Packit 9e4112
#       define SAME_INODE(Stat_buf_1, Stat_buf_2) \
Packit 9e4112
          ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
Packit 9e4112
           && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
Packit 9e4112
Packit 9e4112
        int
Packit 9e4112
        main ()
Packit 9e4112
        {
Packit 9e4112
          const char *file = "conftest.file";
Packit 9e4112
          const char *sym = "conftest.sym";
Packit 9e4112
          const char *hard = "conftest.hard";
Packit 9e4112
          struct stat sb_file, sb_hard;
Packit 9e4112
Packit 9e4112
          /* Create a symlink to the regular file. */
Packit 9e4112
          if (symlink (file, sym))
Packit 9e4112
            return 2;
Packit 9e4112
Packit 9e4112
          /* Create a hard link to that symlink.  */
Packit 9e4112
          if (link (sym, hard))
Packit 9e4112
            return 3;
Packit 9e4112
Packit 9e4112
          if (lstat (hard, &sb_hard))
Packit 9e4112
            return 4;
Packit 9e4112
          if (lstat (file, &sb_file))
Packit 9e4112
            return 5;
Packit 9e4112
Packit 9e4112
          /* If the dev/inode of hard and file are the same, then
Packit 9e4112
             the link call followed the symlink.  */
Packit 9e4112
          return SAME_INODE (sb_hard, sb_file) ? 1 : 0;
Packit 9e4112
        }
Packit 9e4112
           ]])],
Packit 9e4112
           [gl_cv_func_link_follows_symlink=no], dnl GNU behavior
Packit 9e4112
           [gl_cv_func_link_follows_symlink=yes], dnl Followed link/compile failed
Packit 9e4112
           [dnl We're cross compiling.
Packit 9e4112
            dnl The past results are "yes" on Mac OS X, FreeBSD, NetBSD,
Packit 9e4112
            dnl OpenBSD, Minix, AIX, HP-UX, OSF/1, and "no" on Linux, Cygwin.
Packit 9e4112
            case "$host_os" in
Packit 9e4112
                                  # On glibc/Linux we know the result.
Packit 9e4112
              linux*-gnu* | gnu*) gl_cv_func_link_follows_symlink="guessing no" ;;
Packit 9e4112
                                  # Otherwise, we don't know.
Packit 9e4112
              *)                  gl_cv_func_link_follows_symlink=unknown ;;
Packit 9e4112
            esac
Packit 9e4112
           ])
Packit 9e4112
         rm -f conftest.file conftest.sym conftest.hard
Packit 9e4112
        ])
Packit 9e4112
      case "$gl_cv_func_link_follows_symlink" in
Packit 9e4112
        *yes) gl_link_follows_symlinks=1 ;;
Packit 9e4112
        *no) ;; # already defaulted to 0
Packit 9e4112
        *) gl_link_follows_symlinks=-2 ;;
Packit 9e4112
      esac
Packit 9e4112
    fi
Packit 9e4112
  fi
Packit 9e4112
  AC_DEFINE_UNQUOTED([LINK_FOLLOWS_SYMLINKS], [$gl_link_follows_symlinks],
Packit 9e4112
    [Define to 1 if 'link(2)' dereferences symbolic links, 0 if it
Packit 9e4112
     creates hard links to symlinks, -1 if it depends on the variable __xpg4,
Packit 9e4112
     and -2 if unknown.])
Packit 9e4112
])