690cc648d0730279909b28b14f9c504a5ca33999
[gnulib.git] / m4 / open.m4
1 # open.m4 serial 12
2 dnl Copyright (C) 2007-2011 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.
6
7 AC_DEFUN([gl_FUNC_OPEN],
8 [
9   AC_REQUIRE([AC_CANONICAL_HOST])
10   case "$host_os" in
11     mingw* | pw*)
12       gl_REPLACE_OPEN
13       ;;
14     *)
15       dnl open("foo/") should not create a file when the file name has a
16       dnl trailing slash.  FreeBSD only has the problem on symlinks.
17       AC_CHECK_FUNCS_ONCE([lstat])
18       AC_CACHE_CHECK([whether open recognizes a trailing slash],
19         [gl_cv_func_open_slash],
20         [# Assume that if we have lstat, we can also check symlinks.
21           if test $ac_cv_func_lstat = yes; then
22             touch conftest.tmp
23             ln -s conftest.tmp conftest.lnk
24           fi
25           AC_RUN_IFELSE(
26             [AC_LANG_SOURCE([[
27 #include <fcntl.h>
28 #if HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 int main ()
32 {
33   int result = 0;
34 #if HAVE_LSTAT
35   if (open ("conftest.lnk/", O_RDONLY) != -1)
36     result |= 1;
37 #endif
38   if (open ("conftest.sl/", O_CREAT, 0600) >= 0)
39     result |= 2;
40   return result;
41 }]])],
42             [gl_cv_func_open_slash=yes],
43             [gl_cv_func_open_slash=no],
44             [
45 changequote(,)dnl
46              case "$host_os" in
47                freebsd* | aix* | hpux* | solaris2.[0-9] | solaris2.[0-9].*)
48                  gl_cv_func_open_slash="guessing no" ;;
49                *)
50                  gl_cv_func_open_slash="guessing yes" ;;
51              esac
52 changequote([,])dnl
53             ])
54           rm -f conftest.sl conftest.tmp conftest.lnk
55         ])
56       case "$gl_cv_func_open_slash" in
57         *no)
58           AC_DEFINE([OPEN_TRAILING_SLASH_BUG], [1],
59             [Define to 1 if open() fails to recognize a trailing slash.])
60           gl_REPLACE_OPEN
61           ;;
62       esac
63       ;;
64   esac
65   dnl Replace open() for supporting the gnulib-defined O_NONBLOCK flag.
66   m4_ifdef([gl_NONBLOCKING_IO], [
67     if test $REPLACE_OPEN = 0; then
68       gl_NONBLOCKING_IO
69       if test $gl_cv_have_open_O_NONBLOCK != yes; then
70         gl_REPLACE_OPEN
71       fi
72     fi
73   ])
74 ])
75
76 AC_DEFUN([gl_REPLACE_OPEN],
77 [
78   AC_REQUIRE([gl_FCNTL_H_DEFAULTS])
79   REPLACE_OPEN=1
80   AC_LIBOBJ([open])
81   gl_PREREQ_OPEN
82 ])
83
84 # Prerequisites of lib/open.c.
85 AC_DEFUN([gl_PREREQ_OPEN],
86 [
87   AC_REQUIRE([AC_C_INLINE])
88   AC_REQUIRE([gl_PROMOTED_TYPE_MODE_T])
89   :
90 ])