canonicalize-lgpl: fix glibc bug with trailing slash
[gnulib.git] / m4 / canonicalize.m4
1 # canonicalize.m4 serial 15
2
3 dnl Copyright (C) 2003-2007, 2009 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_FUNC_REALPATH_WORKS])
18   if test $ac_cv_func_canonicalize_file_name = no; then
19     HAVE_CANONICALIZE_FILE_NAME=0
20   elif test $gl_cv_func_realpath_works != yes; then
21     REPLACE_CANONICALIZE_FILE_NAME=1
22   fi
23 ])
24
25 # Provides canonicalize_file_name and realpath.
26 AC_DEFUN([gl_CANONICALIZE_LGPL],
27 [
28   AC_REQUIRE([gl_CANONICALIZE_LGPL_SEPARATE])
29   if test $ac_cv_func_canonicalize_file_name = no; then
30     HAVE_CANONICALIZE_FILE_NAME=0
31     AC_LIBOBJ([canonicalize-lgpl])
32     if test $ac_cv_func_realpath = no; then
33       HAVE_REALPATH=0
34     elif test $gl_cv_func_realpath_works != yes; then
35       REPLACE_REALPATH=1
36     fi
37   elif test $gl_cv_func_realpath_works != yes; then
38     AC_LIBOBJ([canonicalize-lgpl])
39     REPLACE_REALPATH=1
40     REPLACE_CANONICALIZE_FILE_NAME=1
41   fi
42 ])
43
44 # Like gl_CANONICALIZE_LGPL, except prepare for separate compilation
45 # (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_FUNC_REALPATH_WORKS])
51   AC_CHECK_HEADERS_ONCE([sys/param.h])
52 ])
53
54 # Check whether realpath works.  Assume that if a platform has both
55 # realpath and canonicalize_file_name, but the former is broken, then
56 # so is the latter.
57 AC_DEFUN([gl_FUNC_REALPATH_WORKS],
58 [
59   AC_CHECK_FUNCS_ONCE([realpath])
60   AC_CACHE_CHECK([whether realpath works], [gl_cv_func_realpath_works], [
61     touch conftest.a
62     AC_RUN_IFELSE([
63       AC_LANG_PROGRAM([[
64         #include <stdlib.h>
65       ]], [[
66         char *name1 = realpath ("conftest.a", NULL);
67         char *name2 = realpath ("conftest.b/../conftest.a", NULL);
68         char *name3 = realpath ("conftest.a/", NULL);
69         return !(name1 && *name1 == '/' && !name2 && !name3);
70       ]])
71     ], [gl_cv_func_realpath_works=yes], [gl_cv_func_realpath_works=no],
72        [gl_cv_func_realpath_works="guessing no"])
73   ])
74   if test $gl_cv_func_realpath_works = yes; then
75     AC_DEFINE([FUNC_REALPATH_WORKS], [1], [Define to 1 if realpath()
76       can malloc memory, always gives an absolute path, and handles
77       trailing slash correctly.])
78   fi
79 ])