(jm_AC_FUNC_LINK_FOLLOWS_SYMLINK): New file/macro.
[gnulib.git] / m4 / link-follow.m4
1 #serial 1
2 dnl Run a program to determine whether whether link(2) follows symlinks.
3 dnl Set LINK_FOLLOWS_SYMLINKS accordingly.
4
5 AC_DEFUN(jm_AC_FUNC_LINK_FOLLOWS_SYMLINK,
6 [dnl
7   AC_CACHE_CHECK([whether link(2) follows symlinks],
8                  jm_ac_cv_func_link_follows_symlink,
9   [
10     # Create a regular file, `conftest.file'.
11     echo > conftest.file
12     AC_TRY_RUN(
13       [
14 #       include <sys/types.h>
15 #       include <sys/stat.h>
16 #       ifdef HAVE_UNISTD_H
17 #        include <unistd.h>
18 #       endif
19
20 #       define SAME_INODE(Stat_buf_1, Stat_buf_2) \
21           ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
22            && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
23
24         int
25         main ()
26         {
27           const char *file = "conftest.file";
28           const char *sym = "conftest.sym";
29           const char *hard = "conftest.hard";
30           struct stat sb_file, sb_hard;
31
32           /* Create a symlink `conftest.sym' to it. */
33           if (symlink (file, sym))
34             abort ();
35
36           /* Create a hard link `conftest.hard' to that symlink.  */
37           if (link (sym, hard))
38             abort ();
39
40           if (lstat (hard, &sb_hard))
41             abort ();
42           if (lstat (file, &sb_file))
43             abort ();
44
45           /* If the dev/inode of hard and file are the same, then
46              the link call followed the symlink.  */
47           return SAME_INODE (sb_hard, sb_file) ? 0 : 1;
48         }
49       ],
50       jm_ac_cv_func_link_follows_symlink=yes,
51       jm_ac_cv_func_link_follows_symlink=no,
52       jm_ac_cv_func_link_follows_symlink=yes, We're cross compiling.
53     )
54   ])
55 ])