autoupdate
[gnulib.git] / m4 / fopen.m4
blobf3b7aadd8331dead619b4d8067e3bd7b8510362e
1 # fopen.m4
2 # serial 16
3 dnl Copyright (C) 2007-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_FOPEN_ITSELF],
10   AC_REQUIRE([gl_STDIO_H_DEFAULTS])
11   AC_REQUIRE([AC_CANONICAL_HOST])
12   case "$host_os" in
13     mingw* | windows* | pw*)
14       dnl Replace fopen, for handling of "/dev/null".
15       REPLACE_FOPEN=1
16       dnl fopen on mingw also has the trailing slash bug.
17       gl_cv_func_fopen_slash="guessing no"
18       ;;
19     *)
20       dnl fopen("foo/", "w") should not create a file when the file name has a
21       dnl trailing slash.
22       AC_CACHE_CHECK([whether fopen recognizes a trailing slash],
23         [gl_cv_func_fopen_slash],
24         [
25           AC_RUN_IFELSE(
26             [AC_LANG_SOURCE([[
27 #include <stddef.h>
28 #include <stdio.h>
29 int main ()
31   FILE *fp = fopen ("conftest.sl/", "w");
32   int result = (fp != NULL);
33   if (fp != NULL)
34     fclose (fp);
35   return result;
36 }]])],
37             [gl_cv_func_fopen_slash=yes],
38             [gl_cv_func_fopen_slash=no],
39             [
40 changequote(,)dnl
41              case "$host_os" in
42                aix* | hpux* | solaris2.[0-9] | solaris2.[0-9].*)
43                  gl_cv_func_fopen_slash="guessing no" ;;
44                *)
45                  gl_cv_func_fopen_slash="guessing yes" ;;
46              esac
47 changequote([,])dnl
48             ])
49           rm -f conftest.sl
50         ])
51       ;;
52   esac
53   case "$gl_cv_func_fopen_slash" in
54     *no)
55       AC_DEFINE([FOPEN_TRAILING_SLASH_BUG], [1],
56         [Define to 1 if fopen() fails to recognize a trailing slash.])
57       REPLACE_FOPEN=1
58       ;;
59   esac
62 AC_DEFUN([gl_FUNC_FOPEN],
64   AC_REQUIRE([gl_FUNC_FOPEN_ITSELF])
65   AC_REQUIRE([gl_FUNC_FCLOSE])
66   if test $REPLACE_FCLOSE = 1; then
67     REPLACE_FOPEN=1
68   fi
71 AC_DEFUN([gl_FUNC_FOPEN_GNU],
73   AC_REQUIRE([gl_FUNC_FOPEN])
74   AC_CACHE_CHECK([whether fopen supports the mode character 'x'],
75     [gl_cv_func_fopen_mode_x],
76     [rm -f conftest.x
77      AC_RUN_IFELSE(
78        [AC_LANG_SOURCE([[
79 #include <stdio.h>
80 #include <errno.h>
81 int main ()
83   FILE *fp;
84   fp = fopen ("conftest.x", "w");
85   fclose (fp);
86   fp = fopen ("conftest.x", "wx");
87   if (fp != NULL)
88     /* 'x' ignored */
89     return 1;
90   else if (errno == EEXIST)
91     return 0;
92   else
93     /* 'x' rejected */
94     return 2;
95 }]])],
96        [gl_cv_func_fopen_mode_x=yes],
97        [gl_cv_func_fopen_mode_x=no],
98        [case "$host_os" in
99           # Guess yes on glibc and musl systems.
100           linux*-gnu* | gnu* | kfreebsd*-gnu | *-musl* | midipix*)
101             gl_cv_func_fopen_mode_x="guessing yes" ;;
102           # If we don't know, obey --enable-cross-guesses.
103           *)
104             gl_cv_func_fopen_mode_x="$gl_cross_guess_normal" ;;
105         esac
106        ])
107      rm -f conftest.x
108     ])
109   AC_CACHE_CHECK([whether fopen supports the mode character 'e'],
110     [gl_cv_func_fopen_mode_e],
111     [echo foo > conftest.x
112      AC_RUN_IFELSE(
113        [AC_LANG_SOURCE([[
114 #include <stdio.h>
115 #include <errno.h>
116 #include <fcntl.h>
117 ]GL_MDA_DEFINES[
118 int main ()
120   FILE *fp = fopen ("conftest.x", "re");
121   if (fp != NULL)
122     {
123       if (fcntl (fileno (fp), F_GETFD) & FD_CLOEXEC)
124         return 0;
125       else
126         /* 'e' ignored */
127         return 1;
128     }
129   else
130     /* 'e' rejected */
131     return 2;
132 }]])],
133        [gl_cv_func_fopen_mode_e=yes],
134        [gl_cv_func_fopen_mode_e=no],
135        [case "$host_os" in
136           # Guess yes on glibc and musl systems.
137           linux*-gnu* | gnu* | kfreebsd*-gnu | *-musl* | midipix*)
138             gl_cv_func_fopen_mode_e="guessing yes" ;;
139           # Guess no on native Windows.
140           mingw* | windows*)
141             gl_cv_func_fopen_mode_e="guessing no" ;;
142           # If we don't know, obey --enable-cross-guesses.
143           *)
144             gl_cv_func_fopen_mode_e="$gl_cross_guess_normal" ;;
145         esac
146        ])
147      rm -f conftest.x
148     ])
149   REPLACE_FOPEN_FOR_FOPEN_GNU="$REPLACE_FOPEN"
150   case "$gl_cv_func_fopen_mode_x" in
151     *no) REPLACE_FOPEN_FOR_FOPEN_GNU=1 ;;
152   esac
153   case "$gl_cv_func_fopen_mode_e" in
154     *no) REPLACE_FOPEN_FOR_FOPEN_GNU=1 ;;
155   esac
158 # Prerequisites of lib/fopen.c.
159 AC_DEFUN([gl_PREREQ_FOPEN], [:])