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