ef0a8c2bc74a5781643b91cb6c7092b40297ec01
[gnulib.git] / m4 / canonicalize.m4
1 # canonicalize.m4 serial 22
2
3 dnl Copyright (C) 2003-2007, 2009-2011 Free Software Foundation, Inc.
4
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 # Provides canonicalize_file_name and canonicalize_filename_mode, but does
10 # not provide or fix realpath.
11 AC_DEFUN([gl_FUNC_CANONICALIZE_FILENAME_MODE],
12 [
13   AC_LIBOBJ([canonicalize])
14
15   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
16   AC_CHECK_FUNCS_ONCE([canonicalize_file_name])
17   AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
18   AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
19   if test $ac_cv_func_canonicalize_file_name = no; then
20     HAVE_CANONICALIZE_FILE_NAME=0
21   elif test "$gl_cv_func_realpath_works" != yes; then
22     REPLACE_CANONICALIZE_FILE_NAME=1
23   fi
24 ])
25
26 # Provides canonicalize_file_name and realpath.
27 AC_DEFUN([gl_CANONICALIZE_LGPL],
28 [
29   AC_REQUIRE([gl_STDLIB_H_DEFAULTS])
30   AC_REQUIRE([gl_CANONICALIZE_LGPL_SEPARATE])
31   if test $ac_cv_func_canonicalize_file_name = no; then
32     HAVE_CANONICALIZE_FILE_NAME=0
33     if test $ac_cv_func_realpath = no; then
34       HAVE_REALPATH=0
35     elif test "$gl_cv_func_realpath_works" != yes; then
36       REPLACE_REALPATH=1
37     fi
38   elif test "$gl_cv_func_realpath_works" != yes; then
39     REPLACE_CANONICALIZE_FILE_NAME=1
40     REPLACE_REALPATH=1
41   fi
42 ])
43
44 # Like gl_CANONICALIZE_LGPL, except prepare for separate compilation
45 # (no REPLACE_CANONICALIZE_FILE_NAME, no REPLACE_REALPATH, no AC_LIBOBJ).
46 AC_DEFUN([gl_CANONICALIZE_LGPL_SEPARATE],
47 [
48   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
49   AC_CHECK_FUNCS_ONCE([canonicalize_file_name getcwd readlink])
50   AC_REQUIRE([gl_DOUBLE_SLASH_ROOT])
51   AC_REQUIRE([gl_FUNC_REALPATH_WORKS])
52   AC_CHECK_HEADERS_ONCE([sys/param.h])
53 ])
54
55 # Check whether realpath works.  Assume that if a platform has both
56 # realpath and canonicalize_file_name, but the former is broken, then
57 # so is the latter.
58 AC_DEFUN([gl_FUNC_REALPATH_WORKS],
59 [
60   AC_CHECK_FUNCS_ONCE([realpath])
61   AC_CACHE_CHECK([whether realpath works], [gl_cv_func_realpath_works], [
62     touch conftest.a
63     mkdir conftest.d
64     AC_RUN_IFELSE([
65       AC_LANG_PROGRAM([[
66         ]GL_NOCRASH[
67         #include <stdlib.h>
68         #include <string.h>
69       ]], [[
70         int result = 0;
71         {
72           char *name = realpath ("conftest.a", NULL);
73           if (!(name && *name == '/'))
74             result |= 1;
75         }
76         {
77           char *name = realpath ("conftest.b/../conftest.a", NULL);
78           if (name != NULL)
79             result |= 2;
80         }
81         {
82           char *name = realpath ("conftest.a/", NULL);
83           if (name != NULL)
84             result |= 4;
85         }
86         {
87           char *name1 = realpath (".", NULL);
88           char *name2 = realpath ("conftest.d//./..", NULL);
89           if (strcmp (name1, name2) != 0)
90             result |= 8;
91         }
92         return result;
93       ]])
94     ], [gl_cv_func_realpath_works=yes], [gl_cv_func_realpath_works=no],
95        [gl_cv_func_realpath_works="guessing no"])
96     rm -rf conftest.a conftest.d
97   ])
98   if test "$gl_cv_func_realpath_works" = yes; then
99     AC_DEFINE([FUNC_REALPATH_WORKS], [1], [Define to 1 if realpath()
100       can malloc memory, always gives an absolute path, and handles
101       trailing slash correctly.])
102   fi
103 ])