summaryrefslogtreecommitdiffstats
path: root/gl/m4/fclose.m4
diff options
context:
space:
mode:
Diffstat (limited to 'gl/m4/fclose.m4')
-rw-r--r--gl/m4/fclose.m499
1 files changed, 99 insertions, 0 deletions
diff --git a/gl/m4/fclose.m4 b/gl/m4/fclose.m4
new file mode 100644
index 0000000..0c1358e
--- /dev/null
+++ b/gl/m4/fclose.m4
@@ -0,0 +1,99 @@
1# fclose.m4
2# serial 12
3dnl Copyright (C) 2008-2024 Free Software Foundation, Inc.
4dnl This file is free software; the Free Software Foundation
5dnl gives unlimited permission to copy and/or distribute it,
6dnl with or without modifications, as long as this notice is preserved.
7
8AC_DEFUN_ONCE([gl_FUNC_FCLOSE],
9[
10 AC_REQUIRE([gl_STDIO_H_DEFAULTS])
11 AC_REQUIRE([AC_CANONICAL_HOST])
12
13 gl_FUNC_FFLUSH_STDIN
14 case "$gl_cv_func_fflush_stdin" in
15 *yes) ;;
16 *) REPLACE_FCLOSE=1 ;;
17 esac
18
19 AC_REQUIRE([gl_FUNC_CLOSE])
20 if test $REPLACE_CLOSE = 1; then
21 REPLACE_FCLOSE=1
22 fi
23
24 case "$host_os" in
25 openedition) REPLACE_FCLOSE=1 ;;
26 esac
27
28 if test $REPLACE_FCLOSE = 0; then
29 gl_FUNC_FCLOSE_STDIN
30 case "$gl_cv_func_fclose_stdin" in
31 *yes) ;;
32 *) REPLACE_FCLOSE=1 ;;
33 esac
34 fi
35])
36
37dnl Determine whether fclose works on input streams.
38dnl Sets gl_cv_func_fclose_stdin.
39
40AC_DEFUN([gl_FUNC_FCLOSE_STDIN],
41[
42 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
43 AC_CHECK_HEADERS_ONCE([unistd.h])
44 AC_CACHE_CHECK([whether fclose works on input streams],
45 [gl_cv_func_fclose_stdin],
46 [echo hello world > conftest.txt
47 AC_RUN_IFELSE(
48 [AC_LANG_PROGRAM(
49 [[#include <fcntl.h>
50 #include <stdio.h>
51 #if HAVE_UNISTD_H
52 # include <unistd.h>
53 #else /* on Windows with MSVC */
54 # include <io.h>
55 #endif
56 ]GL_MDA_DEFINES],
57 [[int fd;
58 int fd2;
59 FILE *fp;
60 fd = open ("conftest.txt", O_RDONLY);
61 if (fd < 0)
62 return 1;
63 if (lseek (fd, 1, SEEK_SET) != 1)
64 return 2;
65 fd2 = dup (fd);
66 if (fd2 < 0)
67 return 3;
68 fp = fdopen (fd2, "r");
69 if (fp == NULL)
70 return 4;
71 if (fgetc (fp) != 'e')
72 { fclose (fp); return 5; }
73 /* This fclose() call should reposition the underlying file
74 descriptor. */
75 if (fclose (fp) != 0)
76 return 6;
77 if (lseek (fd2, 0, SEEK_CUR) != -1) /* should fail with EBADF */
78 return 7;
79 /* Verify the file position. */
80 if (lseek (fd, 0, SEEK_CUR) != 2)
81 return 8;
82 return 0;
83 ]])],
84 [gl_cv_func_fclose_stdin=yes],
85 [gl_cv_func_fclose_stdin=no],
86 [case "$host_os" in
87 # Guess no on glibc systems.
88 *-gnu* | gnu*) gl_cv_func_fclose_stdin="guessing no" ;;
89 # Guess yes on musl systems.
90 *-musl* | midipix*) gl_cv_func_fclose_stdin="guessing yes" ;;
91 # Guess no on native Windows.
92 mingw* | windows*) gl_cv_func_fclose_stdin="guessing no" ;;
93 # If we don't know, obey --enable-cross-guesses.
94 *) gl_cv_func_fclose_stdin="$gl_cross_guess_normal" ;;
95 esac
96 ])
97 rm conftest.txt
98 ])
99])