* m4/getdate.m4 (gl_C_COMPOUND_LITERALS): New macro.
[gnulib.git] / m4 / link-follow.m4
1 #serial 5
2 dnl Run a program to determine whether whether link(2) follows symlinks.
3 dnl Set LINK_FOLLOWS_SYMLINKS accordingly.
4
5 # Copyright (C) 1999, 2000, 2001, 2004 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.
9
10 AC_DEFUN([gl_AC_FUNC_LINK_FOLLOWS_SYMLINK],
11 [dnl
12   AC_CACHE_CHECK(
13     [whether link(2) dereferences a symlink specified with a trailing slash],
14                  jm_ac_cv_func_link_follows_symlink,
15   [
16     dnl poor-man's AC_REQUIRE: FIXME: repair this once autoconf-3 provides
17     dnl the appropriate framework.
18     test -z "$ac_cv_header_unistd_h" \
19       && AC_CHECK_HEADERS(unistd.h)
20
21     # Create a regular file.
22     echo > conftest.file
23     AC_TRY_RUN(
24       [
25 #       include <sys/types.h>
26 #       include <sys/stat.h>
27 #       ifdef HAVE_UNISTD_H
28 #        include <unistd.h>
29 #       endif
30
31 #       define SAME_INODE(Stat_buf_1, Stat_buf_2) \
32           ((Stat_buf_1).st_ino == (Stat_buf_2).st_ino \
33            && (Stat_buf_1).st_dev == (Stat_buf_2).st_dev)
34
35         int
36         main ()
37         {
38           const char *file = "conftest.file";
39           const char *sym = "conftest.sym";
40           const char *hard = "conftest.hard";
41           struct stat sb_file, sb_hard;
42
43           /* Create a symlink to the regular file. */
44           if (symlink (file, sym))
45             abort ();
46
47           /* Create a hard link to that symlink.  */
48           if (link (sym, hard))
49             abort ();
50
51           if (lstat (hard, &sb_hard))
52             abort ();
53           if (lstat (file, &sb_file))
54             abort ();
55
56           /* If the dev/inode of hard and file are the same, then
57              the link call followed the symlink.  */
58           return SAME_INODE (sb_hard, sb_file) ? 0 : 1;
59         }
60       ],
61       jm_ac_cv_func_link_follows_symlink=yes,
62       jm_ac_cv_func_link_follows_symlink=no,
63       jm_ac_cv_func_link_follows_symlink=yes dnl We're cross compiling.
64     )
65   ])
66   if test $jm_ac_cv_func_link_follows_symlink = yes; then
67     AC_DEFINE(LINK_FOLLOWS_SYMLINKS, 1,
68       [Define if `link(2)' dereferences symbolic links.])
69   fi
70 ])