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