1# fclose.m4 serial 9
2dnl Copyright (C) 2008-2021 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_FCLOSE],
8[
9  AC_REQUIRE([gl_STDIO_H_DEFAULTS])
10  AC_REQUIRE([AC_CANONICAL_HOST])
11
12  gl_FUNC_FFLUSH_STDIN
13  case "$gl_cv_func_fflush_stdin" in
14    *yes) ;;
15    *) REPLACE_FCLOSE=1 ;;
16  esac
17
18  AC_REQUIRE([gl_FUNC_CLOSE])
19  if test $REPLACE_CLOSE = 1; then
20    REPLACE_FCLOSE=1
21  fi
22
23  case "$host_os" in
24    openedition) REPLACE_FCLOSE=1 ;;
25  esac
26
27  if test $REPLACE_FCLOSE = 0; then
28    gl_FUNC_FCLOSE_STDIN
29    case "$gl_cv_func_fclose_stdin" in
30      *yes) ;;
31      *) REPLACE_FCLOSE=1 ;;
32    esac
33  fi
34])
35
36dnl Determine whether fclose works on input streams.
37dnl Sets gl_cv_func_fclose_stdin.
38
39AC_DEFUN([gl_FUNC_FCLOSE_STDIN],
40[
41  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
42  AC_CHECK_HEADERS_ONCE([unistd.h])
43  AC_CACHE_CHECK([whether fclose works on input streams],
44    [gl_cv_func_fclose_stdin],
45    [echo hello world > conftest.txt
46     AC_RUN_IFELSE(
47       [AC_LANG_PROGRAM(
48          [[#include <fcntl.h>
49            #include <stdio.h>
50            #if HAVE_UNISTD_H
51            # include <unistd.h>
52            #else /* on Windows with MSVC */
53            # include <io.h>
54            #endif
55           ]GL_MDA_DEFINES],
56          [[int fd;
57            int fd2;
58            FILE *fp;
59            fd = open ("conftest.txt", O_RDONLY);
60            if (fd < 0)
61              return 1;
62            if (lseek (fd, 1, SEEK_SET) != 1)
63              return 2;
64            fd2 = dup (fd);
65            if (fd2 < 0)
66              return 3;
67            fp = fdopen (fd2, "r");
68            if (fp == NULL)
69              return 4;
70            if (fgetc (fp) != 'e')
71              { fclose (fp); return 5; }
72            /* This fclose() call should reposition the underlying file
73               descriptor.  */
74            if (fclose (fp) != 0)
75              return 6;
76            if (lseek (fd2, 0, SEEK_CUR) != -1) /* should fail with EBADF */
77              return 7;
78            /* Verify the file position.  */
79            if (lseek (fd, 0, SEEK_CUR) != 2)
80              return 8;
81            return 0;
82          ]])],
83       [gl_cv_func_fclose_stdin=yes],
84       [gl_cv_func_fclose_stdin=no],
85       [case "$host_os" in
86                         # Guess no on glibc systems.
87          *-gnu* | gnu*) gl_cv_func_fclose_stdin="guessing no" ;;
88                         # Guess yes on musl systems.
89          *-musl*)       gl_cv_func_fclose_stdin="guessing yes" ;;
90                         # Guess no on native Windows.
91          mingw*)        gl_cv_func_fclose_stdin="guessing no" ;;
92                         # If we don't know, obey --enable-cross-guesses.
93          *)             gl_cv_func_fclose_stdin="$gl_cross_guess_normal" ;;
94        esac
95       ])
96     rm conftest.txt
97    ])
98])
99