This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Even better use64bitall logic.
[metaconfig.git] / U / perl / fflushall.U
CommitLineData
959f3c4c
JH
1?RCS: $Id$
2?RCS:
3?RCS: Copyright (c) 1999, Jarkko Hietaniemi
4?RCS:
5?RCS: You may redistribute only under the terms of the Artistic Licence,
6?RCS: as specified in the README file that comes with the distribution.
7?RCS: You may reuse parts of this distribution only within the terms of
8?RCS: that same Artistic Licence; a copy of which may be found at the root
9?RCS: of the source tree for dist 3.0.
10?RCS:
11?MAKE:fflushNULL fflushall: Compile cat rm test osname \
12 Oldconfig Myread Setvar exe_ext echo \
13 d_sysconf i_unistd d_stdio_stream_array stdio_stream_array
14?MAKE: -pick add $@ %<
15?S:fflushNULL:
16?S: This symbol, if defined, tells that fflush(NULL) does flush
17?S: all pending stdio output.
18?S:.
19?S:fflushall:
20?S: This symbol, if defined, tells that to flush
21?S: all pending stdio output one must loop through all
22?S: the stdio file handles stored in an array and fflush them.
23?S: Note that if fflushNULL is defined, fflushall will not
24?S: even be probed for and will be left undefined.
25?S:.
26?C:FFLUSH_NULL:
27?C: This symbol, if defined, tells that fflush(NULL) does flush
28?C: all pending stdio output.
29?C:.
30?C:FFLUSH_ALL:
31?C: This symbol, if defined, tells that to flush
32?C: all pending stdio output one must loop through all
33?C: the stdio file handles stored in an array and fflush them.
34?C: Note that if fflushNULL is defined, fflushall will not
35?C: even be probed for and will be left undefined.
36?C:.
37?H:#$fflushNULL FFLUSH_NULL /**/
38?H:#$fflushall FFLUSH_ALL /**/
39?H:.
40?T:output code
41?F:!try.out
42echo " "
43$cat >&4 <<EOM
44Checking how to flush all pending stdio output...
45EOM
46# I only know how to find the first 32 possibly open files on SunOS.
47# See also hints/sunos_4_1.sh and util.c --AD
48case "$osname" in
49sunos) $echo '#define PERL_FFLUSH_ALL_FOPEN_MAX 32' > try.c ;;
50esac
51$cat >>try.c <<EOCP
52#include <stdio.h>
53#$i_unistd I_UNISTD
54#ifdef I_UNISTD
55# include <unistd.h>
56#endif
57#$d_sysconf HAS_SYSCONF
58#$d_stdio_stream_array HAS_STDIO_STREAM_ARRAY
59#ifdef HAS_STDIO_STREAM_ARRAY
60# define STDIO_STREAM_ARRAY $stdio_stream_array
61#endif
62int main() {
63 FILE* p = fopen("try.out", "w");
64#ifdef TRY_FPUTC
65 fputc('x', p);
66#else
67# ifdef TRY_FPRINTF
68 fprintf(p, "x");
69# endif
70#endif
71#ifdef TRY_FFLUSH_NULL
72 fflush(NULL);
73#endif
74#ifdef TRY_FFLUSH_ALL
75 {
76 long open_max = -1;
77# ifdef PERL_FFLUSH_ALL_FOPEN_MAX
78 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
79# else
80# if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
81 open_max = sysconf(_SC_OPEN_MAX);
82# else
83# ifdef FOPEN_MAX
84 open_max = FOPEN_MAX;
85# else
86# ifdef OPEN_MAX
87 open_max = OPEN_MAX;
88# else
89# ifdef _NFILE
90 open_max = _NFILE;
91# endif
92# endif
93# endif
94# endif
95# endif
96# ifdef HAS_STDIO_STREAM_ARRAY
97 if (open_max > 0) {
98 long i;
99 for (i = 0; i < open_max; i++)
100 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
101 STDIO_STREAM_ARRAY[i]._file < open_max &&
102 STDIO_STREAM_ARRAY[i]._flag)
103 fflush(&STDIO_STREAM_ARRAY[i]);
104 }
105 }
106# endif
107#endif
108 _exit(42);
109}
110EOCP
111: first we have to find out how _not_ to flush
112if $test "X$fflushNULL" = X -o "X$fflushall" = X; then
113 output=''
114 set try -DTRY_FPUTC
115 if eval $compile; then
116 $rm -f try.out
117 ./try$exe_ext 2>/dev/null
118 if $test ! -s try.out -a "X$?" = X42; then
119 output=-DTRY_FPUTC
120 fi
121 fi
122 case "$output" in
123 '')
124 set try -DTRY_FPRINTF
125 $rm -f try.out
126 if eval $compile; then
127 $rm -f try.out
128 ./try$exe_ext 2>/dev/null
129 if $test ! -s try.out -a "X$?" = X42; then
130 output=-DTRY_FPRINTF
131 fi
132 fi
133 ;;
134 esac
135fi
136: check for fflush NULL behaviour
137case "$fflushNULL" in
138'') set try -DTRY_FFLUSH_NULL $output
139 if eval $compile; then
140 $rm -f try.out
141 ./try$exe_ext 2>/dev/null
142 code="$?"
143 if $test -s try.out -a "X$code" = X42; then
144 fflushNULL="`$cat try.out`"
145 else
146 if $test "X$code" != X42; then
147 $cat >&4 <<EOM
148(If this test failed, don't worry, we'll try another method shortly.)
149EOM
150 fi
151 fi
152 fi
153 $rm -f core try.core core.try.*
154 case "$fflushNULL" in
155 x) $cat >&4 <<EOM
156Your fflush(NULL) works okay.
157EOM
158 fflushNULL="$define"
159 ;;
160 '') $cat >&4 <<EOM
161Your fflush(NULL) isn't working (contrary to ANSI C).
162EOM
163 fflushNULL="$undef"
164 ;;
165 *) $cat >&4 <<EOM
166Cannot figure out whether your fflush(NULL) works or not.
167I'm assuming it doesn't (contrary to ANSI C).
168EOM
169 fflushNULL="$undef"
170 ;;
171 esac
172 ;;
173$define|true|[yY]*)
174 fflushNULL="$define"
175 ;;
176*)
177 fflushNULL="$undef"
178 ;;
179esac
180: check explicit looping only if NULL did not work
181case "$fflushNULL" in
182"$undef")
183 : check for fflush all behaviour
184 case "$fflushall" in
185 '') set try -DTRY_FFLUSH_ALL $output
186 if eval $compile; then
187 $cat >&4 <<EOM
188(Now testing the other method--but note that also this may fail.)
189EOM
190 $rm -f try.out
191 ./try$exe_ext 2>/dev/null
192 if $test -s try.out -a "X$?" = X42; then
193 fflushall="`$cat try.out`"
194 fi
195 fi
196 $rm -f core try.core core.try.*
197 case "$fflushall" in
198 x) $cat >&4 <<EOM
199Whew. Flushing explicitly all the stdio streams works.
200EOM
201 fflushall="$define"
202 ;;
203 '') $cat >&4 <<EOM
204Sigh. Flushing explicitly all the stdio streams doesn't work.
205EOM
206 fflushall="$undef"
207 ;;
208 *) $cat >&4 <<EOM
209Cannot figure out whether flushing stdio streams explicitly works or not.
210I'm assuming it doesn't.
211EOM
212 fflushall="$undef"
213 ;;
214 esac
215 ;;
216 "$define"|true|[yY]*)
217 fflushall="$define"
218 ;;
219 *)
220 fflushall="$undef"
221 ;;
222 esac
223 ;;
224*) fflushall="$undef"
225 ;;
226esac
227case "$fflushNULL$fflushall" in
228undefundef)
229 $cat <<EOM
230I cannot figure out how to flush pending stdio output.
231EOM
232 ;;
233esac
234$rm -f try.* try$exe_ext
235