linkat: Simplify autoconf macro.
[gnulib.git] / m4 / linkat.m4
1 # serial 5
2 # See if we need to provide linkat replacement.
3
4 dnl Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 dnl This file is free software; the Free Software Foundation
6 dnl gives unlimited permission to copy and/or distribute it,
7 dnl with or without modifications, as long as this notice is preserved.
8
9 # Written by Eric Blake.
10
11 AC_DEFUN([gl_FUNC_LINKAT],
12 [
13   AC_REQUIRE([gl_FUNC_OPENAT])
14   AC_REQUIRE([gl_FUNC_LINK_FOLLOWS_SYMLINK])
15   AC_REQUIRE([gl_UNISTD_H_DEFAULTS])
16   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
17   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
18   AC_CHECK_FUNCS_ONCE([linkat symlink])
19   AC_CHECK_HEADERS_ONCE([sys/param.h])
20   if test $ac_cv_func_linkat = no; then
21     HAVE_LINKAT=0
22     AC_LIBOBJ([linkat])
23     AC_LIBOBJ([at-func2])
24   else
25     AC_CACHE_CHECK([whether linkat(,AT_SYMLINK_FOLLOW) works],
26       [gl_cv_func_linkat_follow],
27       [rm -rf conftest.f1 conftest.f2
28        touch conftest.f1
29        AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
30 #include <fcntl.h>
31 #include <unistd.h>
32 #ifdef __linux__
33 /* Linux added linkat in 2.6.16, but did not add AT_SYMLINK_FOLLOW
34    until 2.6.18.  Always replace linkat to support older kernels.  */
35 choke me
36 #endif
37 ]], [return linkat (AT_FDCWD, "conftest.f1", AT_FDCWD, "conftest.f2",
38                     AT_SYMLINK_FOLLOW);])],
39          [gl_cv_func_linkat_follow=yes],
40          [gl_cv_func_linkat_follow="need runtime check"])
41        rm -rf conftest.f1 conftest.f2])
42     AC_CACHE_CHECK([whether linkat handles trailing slash correctly],
43       [gl_cv_func_linkat_slash],
44       [rm -rf conftest.a conftest.b conftest.c conftest.d
45        AC_RUN_IFELSE(
46          [AC_LANG_PROGRAM(
47             [[#include <unistd.h>
48               #include <fcntl.h>
49               #include <errno.h>
50               #include <stdio.h>
51             ]],
52             [[int result;
53               int fd;
54               /* Create a regular file.  */
55               fd = open ("conftest.a", O_CREAT | O_EXCL | O_WRONLY, 0600);
56               if (fd < 0)
57                 return 1;
58               if (write (fd, "hello", 5) < 5)
59                 return 2;
60               if (close (fd) < 0)
61                 return 3;
62               /* Test whether hard links are supported on the current
63                  device.  */
64               if (linkat (AT_FDCWD, "conftest.a", AT_FDCWD, "conftest.b",
65                           AT_SYMLINK_FOLLOW) < 0)
66                 return 0;
67               result = 0;
68               /* Test whether a trailing "/" is treated like "/.".  */
69               if (linkat (AT_FDCWD, "conftest.a/", AT_FDCWD, "conftest.c",
70                           AT_SYMLINK_FOLLOW) == 0)
71                 result |= 4;
72               if (linkat (AT_FDCWD, "conftest.a", AT_FDCWD, "conftest.d/",
73                           AT_SYMLINK_FOLLOW) == 0)
74                 result |= 8;
75               return result;
76             ]])],
77          [gl_cv_func_linkat_slash=yes],
78          [gl_cv_func_linkat_slash=no],
79          [# Guess yes on glibc systems, no otherwise.
80           case "$host_os" in
81             *-gnu*) gl_cv_func_linkat_slash="guessing yes";;
82             *)      gl_cv_func_linkat_slash="guessing no";;
83           esac
84          ])
85        rm -rf conftest.a conftest.b conftest.c conftest.d])
86     case "$gl_cv_func_linkat_slash" in
87       *yes) gl_linkat_slash_bug=0 ;;
88       *)    gl_linkat_slash_bug=1 ;;
89     esac
90     if test "$gl_cv_func_linkat_follow" != yes \
91        || test $gl_linkat_slash_bug = 1; then
92       REPLACE_LINKAT=1
93       AC_LIBOBJ([linkat])
94       AC_DEFINE_UNQUOTED([LINKAT_TRAILING_SLASH_BUG], [$gl_linkat_slash_bug],
95         [Define to 1 if linkat fails to recognize a trailing slash.])
96     fi
97   fi
98 ])