Use ${EXEEXT} more uniformly in makefiles
[emacs.git] / m4 / fcntl.m4
blob218e78628ba059c91eb4969544c413ed7809f671
1 # fcntl.m4 serial 7
2 dnl Copyright (C) 2009-2015 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 # For now, this module ensures that fcntl()
8 # - supports F_DUPFD correctly
9 # - supports or emulates F_DUPFD_CLOEXEC
10 # - supports F_GETFD
11 # Still to be ported to mingw:
12 # - F_SETFD
13 # - F_GETFL, F_SETFL
14 # - F_GETOWN, F_SETOWN
15 # - F_GETLK, F_SETLK, F_SETLKW
16 AC_DEFUN([gl_FUNC_FCNTL],
18   dnl Persuade glibc to expose F_DUPFD_CLOEXEC.
19   AC_REQUIRE([gl_USE_SYSTEM_EXTENSIONS])
20   AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
21   AC_REQUIRE([AC_CANONICAL_HOST])
22   AC_CHECK_FUNCS_ONCE([fcntl])
23   if test $ac_cv_func_fcntl = no; then
24     gl_REPLACE_FCNTL
25   else
26     dnl cygwin 1.5.x F_DUPFD has wrong errno, and allows negative target
27     dnl haiku alpha 2 F_DUPFD has wrong errno
28     AC_CACHE_CHECK([whether fcntl handles F_DUPFD correctly],
29       [gl_cv_func_fcntl_f_dupfd_works],
30       [AC_RUN_IFELSE(
31          [AC_LANG_PROGRAM(
32             [[#include <errno.h>
33               #include <fcntl.h>
34               #include <limits.h>
35               #include <sys/resource.h>
36               #include <unistd.h>
37             ]],
38             [[int result = 0;
39               int bad_fd = INT_MAX;
40               struct rlimit rlim;
41               if (getrlimit (RLIMIT_NOFILE, &rlim) == 0
42                   && 0 <= rlim.rlim_cur && rlim.rlim_cur <= INT_MAX
43                   && rlim.rlim_cur != RLIM_INFINITY
44                   && rlim.rlim_cur != RLIM_SAVED_MAX
45                   && rlim.rlim_cur != RLIM_SAVED_CUR)
46                 bad_fd = rlim.rlim_cur;
47               if (fcntl (0, F_DUPFD, -1) != -1) result |= 1;
48               if (errno != EINVAL) result |= 2;
49               if (fcntl (0, F_DUPFD, bad_fd) != -1) result |= 4;
50               if (errno != EINVAL) result |= 8;
51               return result;]])],
52          [gl_cv_func_fcntl_f_dupfd_works=yes],
53          [gl_cv_func_fcntl_f_dupfd_works=no],
54          [case $host_os in
55             aix* | cygwin* | haiku*)
56                gl_cv_func_fcntl_f_dupfd_works="guessing no" ;;
57             *) gl_cv_func_fcntl_f_dupfd_works="guessing yes" ;;
58           esac])])
59     case $gl_cv_func_fcntl_f_dupfd_works in
60       *yes) ;;
61       *) gl_REPLACE_FCNTL
62         AC_DEFINE([FCNTL_DUPFD_BUGGY], [1], [Define this to 1 if F_DUPFD
63           behavior does not match POSIX]) ;;
64     esac
66     dnl Many systems lack F_DUPFD_CLOEXEC
67     AC_CACHE_CHECK([whether fcntl understands F_DUPFD_CLOEXEC],
68       [gl_cv_func_fcntl_f_dupfd_cloexec],
69       [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
70 #include <fcntl.h>
71 #ifndef F_DUPFD_CLOEXEC
72 choke me
73 #endif
74          ]])],
75          [AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[
76 #ifdef __linux__
77 /* The Linux kernel only added F_DUPFD_CLOEXEC in 2.6.24, so we always replace
78    it to support the semantics on older kernels that failed with EINVAL.  */
79 choke me
80 #endif
81            ]])],
82            [gl_cv_func_fcntl_f_dupfd_cloexec=yes],
83            [gl_cv_func_fcntl_f_dupfd_cloexec="needs runtime check"])],
84          [gl_cv_func_fcntl_f_dupfd_cloexec=no])])
85     if test "$gl_cv_func_fcntl_f_dupfd_cloexec" != yes; then
86       gl_REPLACE_FCNTL
87       dnl No witness macro needed for this bug.
88     fi
89   fi
90   dnl Replace fcntl() for supporting the gnulib-defined fchdir() function,
91   dnl to keep fchdir's bookkeeping up-to-date.
92   m4_ifdef([gl_FUNC_FCHDIR], [
93     gl_TEST_FCHDIR
94     if test $HAVE_FCHDIR = 0; then
95       gl_REPLACE_FCNTL
96     fi
97   ])
100 AC_DEFUN([gl_REPLACE_FCNTL],
102   AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
103   AC_CHECK_FUNCS_ONCE([fcntl])
104   if test $ac_cv_func_fcntl = no; then
105     HAVE_FCNTL=0
106   else
107     REPLACE_FCNTL=1
108   fi