This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Merge changes which eliminate VOIDFLAGS and VOIDUSED from active code.
[metaconfig.git] / U / perl / fflushall.U
CommitLineData
959f3c4c
JH
1?RCS: $Id$
2?RCS:
3?RCS: Copyright (c) 1999, Jarkko Hietaniemi
0065f4af 4?RCS:
65a32477 5?RCS: You may redistribute only under the terms of the Artistic License,
959f3c4c
JH
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
65a32477 8?RCS: that same Artistic License; a copy of which may be found at the root
959f3c4c
JH
9?RCS: of the source tree for dist 3.0.
10?RCS:
2cb64bf6 11?MAKE:fflushNULL fflushall: Compile cat rm rm_try test osname run to from \
4bc6cd65 12 Oldconfig Myread Setvar echo \
1273bb5e 13 d_sysconf i_unistd d_stdio_stream_array stdio_stream_array i_stdlib
959f3c4c
JH
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
41322e17 41?F:!try.out !try !tryp
0065f4af 42: Check how to flush
959f3c4c
JH
43echo " "
44$cat >&4 <<EOM
45Checking how to flush all pending stdio output...
46EOM
47# I only know how to find the first 32 possibly open files on SunOS.
48# See also hints/sunos_4_1.sh and util.c --AD
49case "$osname" in
50sunos) $echo '#define PERL_FFLUSH_ALL_FOPEN_MAX 32' > try.c ;;
51esac
52$cat >>try.c <<EOCP
53#include <stdio.h>
1273bb5e
JH
54#$i_stdlib I_STDLIB
55#ifdef I_STDLIB
56#include <stdlib.h>
57#endif
959f3c4c
JH
58#$i_unistd I_UNISTD
59#ifdef I_UNISTD
60# include <unistd.h>
61#endif
62#$d_sysconf HAS_SYSCONF
63#$d_stdio_stream_array HAS_STDIO_STREAM_ARRAY
64#ifdef HAS_STDIO_STREAM_ARRAY
65# define STDIO_STREAM_ARRAY $stdio_stream_array
66#endif
67int main() {
0f00356b
JH
68 FILE* p;
69 unlink("try.out");
70 p = fopen("try.out", "w");
959f3c4c
JH
71#ifdef TRY_FPUTC
72 fputc('x', p);
73#else
74# ifdef TRY_FPRINTF
75 fprintf(p, "x");
76# endif
77#endif
78#ifdef TRY_FFLUSH_NULL
79 fflush(NULL);
80#endif
81#ifdef TRY_FFLUSH_ALL
82 {
83 long open_max = -1;
84# ifdef PERL_FFLUSH_ALL_FOPEN_MAX
85 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
86# else
87# if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
88 open_max = sysconf(_SC_OPEN_MAX);
89# else
90# ifdef FOPEN_MAX
91 open_max = FOPEN_MAX;
92# else
93# ifdef OPEN_MAX
94 open_max = OPEN_MAX;
95# else
96# ifdef _NFILE
97 open_max = _NFILE;
98# endif
99# endif
100# endif
101# endif
2cb64bf6 102# endif
959f3c4c
JH
103# ifdef HAS_STDIO_STREAM_ARRAY
104 if (open_max > 0) {
105 long i;
106 for (i = 0; i < open_max; i++)
107 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
108 STDIO_STREAM_ARRAY[i]._file < open_max &&
109 STDIO_STREAM_ARRAY[i]._flag)
110 fflush(&STDIO_STREAM_ARRAY[i]);
2cb64bf6 111 }
959f3c4c
JH
112 }
113# endif
114#endif
115 _exit(42);
116}
117EOCP
118: first we have to find out how _not_ to flush
0f00356b 119$to try.c
959f3c4c
JH
120if $test "X$fflushNULL" = X -o "X$fflushall" = X; then
121 output=''
122 set try -DTRY_FPUTC
123 if eval $compile; then
f5a7caaa
JH
124 $run ./try 2>/dev/null
125 code="$?"
0f00356b 126 $from try.out
f5a7caaa 127 if $test ! -s try.out -a "X$code" = X42; then
959f3c4c
JH
128 output=-DTRY_FPUTC
129 fi
130 fi
131 case "$output" in
132 '')
133 set try -DTRY_FPRINTF
959f3c4c 134 if eval $compile; then
f5a7caaa
JH
135 $run ./try 2>/dev/null
136 code="$?"
0f00356b 137 $from try.out
f5a7caaa 138 if $test ! -s try.out -a "X$code" = X42; then
959f3c4c
JH
139 output=-DTRY_FPRINTF
140 fi
141 fi
142 ;;
143 esac
144fi
65a32477 145: check for fflush NULL behavior
959f3c4c
JH
146case "$fflushNULL" in
147'') set try -DTRY_FFLUSH_NULL $output
148 if eval $compile; then
0f00356b 149 $run ./try 2>/dev/null
959f3c4c 150 code="$?"
0f00356b 151 $from try.out
959f3c4c
JH
152 if $test -s try.out -a "X$code" = X42; then
153 fflushNULL="`$cat try.out`"
154 else
155 if $test "X$code" != X42; then
156 $cat >&4 <<EOM
157(If this test failed, don't worry, we'll try another method shortly.)
158EOM
159 fi
160 fi
161 fi
a45723d1 162 $rm -f core try.core core.try.*
959f3c4c
JH
163 case "$fflushNULL" in
164 x) $cat >&4 <<EOM
5ef94ee9
JH
165Your fflush(NULL) works okay for output streams.
166Let's see if it clobbers input pipes...
959f3c4c 167EOM
5ef94ee9
JH
168# As of mid-March 2000 all versions of Solaris appear to have a stdio
169# bug that improperly flushes the input end of pipes. So we avoid the
170# autoflush on fork/system/exec support for now. :-(
171$cat >tryp.c <<EOCP
172#include <stdio.h>
173int
174main(int argc, char **argv)
175{
176 char buf[1024];
177 int i;
178 char *bp = buf;
179 while (1) {
180 while ((i = getc(stdin)) != -1
181 && (*bp++ = i) != '\n'
182 && bp < &buf[1024])
183 /* DO NOTHING */ ;
184 *bp = '\0';
185 fprintf(stdout, "%s", buf);
186 fflush(NULL);
187 if (i == -1)
1b13c38f 188 return 0;
5ef94ee9
JH
189 bp = buf;
190 }
191}
192EOCP
193 fflushNULL="$define"
194 set tryp
195 if eval $compile; then
196 $rm -f tryp.out
0f00356b 197 $cat tryp.c | $run ./tryp 2>/dev/null > tryp.out
5ef94ee9
JH
198 if cmp tryp.c tryp.out >/dev/null 2>&1; then
199 $cat >&4 <<EOM
200fflush(NULL) seems to behave okay with input streams.
201EOM
202 fflushNULL="$define"
203 else
204 $cat >&4 <<EOM
205Ouch, fflush(NULL) clobbers input pipes! We will not use it.
206EOM
207 fflushNULL="$undef"
208 fi
209 fi
1b13c38f 210 $rm -f core tryp.c tryp.core core.tryp.*
959f3c4c
JH
211 ;;
212 '') $cat >&4 <<EOM
213Your fflush(NULL) isn't working (contrary to ANSI C).
214EOM
215 fflushNULL="$undef"
216 ;;
217 *) $cat >&4 <<EOM
218Cannot figure out whether your fflush(NULL) works or not.
219I'm assuming it doesn't (contrary to ANSI C).
220EOM
221 fflushNULL="$undef"
222 ;;
223 esac
224 ;;
225$define|true|[yY]*)
226 fflushNULL="$define"
227 ;;
228*)
229 fflushNULL="$undef"
230 ;;
231esac
1b13c38f
JH
232: check explicit looping only if NULL did not work, and if the pipe
233: bug does not show up on an explicit flush too
959f3c4c
JH
234case "$fflushNULL" in
235"$undef")
1b13c38f
JH
236 $cat >tryp.c <<EOCP
237#include <stdio.h>
238int
239main(int argc, char **argv)
240{
241 char buf[1024];
242 int i;
243 char *bp = buf;
244 while (1) {
245 while ((i = getc(stdin)) != -1
246 && (*bp++ = i) != '\n'
247 && bp < &buf[1024])
248 /* DO NOTHING */ ;
249 *bp = '\0';
250 fprintf(stdout, "%s", buf);
251 fflush(stdin);
252 if (i == -1)
253 return 0;
254 bp = buf;
255 }
256}
257EOCP
258 set tryp
259 if eval $compile; then
260 $rm -f tryp.out
0f00356b 261 $cat tryp.c | $run ./tryp 2>/dev/null > tryp.out
1b13c38f
JH
262 if cmp tryp.c tryp.out >/dev/null 2>&1; then
263 $cat >&4 <<EOM
264Good, at least fflush(stdin) seems to behave okay when stdin is a pipe.
959f3c4c 265EOM
1b13c38f 266 : now check for fflushall behaviour
959f3c4c 267 case "$fflushall" in
1b13c38f
JH
268 '') set try -DTRY_FFLUSH_ALL $output
269 if eval $compile; then
270 $cat >&4 <<EOM
271(Now testing the other method--but note that this also may fail.)
272EOM
0f00356b 273 $run ./try 2>/dev/null
f5a7caaa
JH
274 code=$?
275 $from try.out
276 if $test -s try.out -a "X$code" = X42; then
1b13c38f
JH
277 fflushall="`$cat try.out`"
278 fi
279 fi
2cb64bf6 280 $rm_try
1b13c38f
JH
281 case "$fflushall" in
282 x) $cat >&4 <<EOM
959f3c4c
JH
283Whew. Flushing explicitly all the stdio streams works.
284EOM
1b13c38f
JH
285 fflushall="$define"
286 ;;
287 '') $cat >&4 <<EOM
959f3c4c
JH
288Sigh. Flushing explicitly all the stdio streams doesn't work.
289EOM
1b13c38f
JH
290 fflushall="$undef"
291 ;;
292 *) $cat >&4 <<EOM
959f3c4c
JH
293Cannot figure out whether flushing stdio streams explicitly works or not.
294I'm assuming it doesn't.
295EOM
1b13c38f
JH
296 fflushall="$undef"
297 ;;
298 esac
299 ;;
300 "$define"|true|[yY]*)
301 fflushall="$define"
302 ;;
303 *)
959f3c4c
JH
304 fflushall="$undef"
305 ;;
306 esac
1b13c38f
JH
307 else
308 $cat >&4 <<EOM
309All is futile. Even fflush(stdin) clobbers input pipes!
310EOM
959f3c4c 311 fflushall="$undef"
1b13c38f
JH
312 fi
313 else
314 fflushall="$undef"
315 fi
316 $rm -f core tryp.c tryp.core core.tryp.*
959f3c4c 317 ;;
1b13c38f 318*) fflushall="$undef"
959f3c4c
JH
319 ;;
320esac
1b13c38f 321
959f3c4c
JH
322case "$fflushNULL$fflushall" in
323undefundef)
324 $cat <<EOM
1b13c38f
JH
325OK, I give up. I cannot figure out how to flush pending stdio output.
326We won't be flushing handles at all before fork/exec/popen.
959f3c4c
JH
327EOM
328 ;;
329esac
41322e17 330$rm_try tryp
959f3c4c 331