3 ?RCS: Copyright (c) 1999, Jarkko Hietaniemi
5 ?RCS: You may redistribute only under the terms of the Artistic License,
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 License; a copy of which may be found at the root
9 ?RCS: of the source tree for dist 3.0.
11 ?MAKE:fflushNULL fflushall: Compile cat rm rm_try test osname run to from \
12 Oldconfig Myread Setvar echo targethost \
13 d_sysconf i_unistd d_stdio_stream_array stdio_stream_array i_stdlib
14 ?MAKE: -pick add $@ %<
16 ?S: This symbol, if defined, tells that fflush(NULL) correctly
17 ?S: flushes all pending stdio output without side effects. In
18 ?S: particular, on some platforms calling fflush(NULL) *still*
19 ?S: corrupts STDIN if it is a pipe.
22 ?S: This symbol, if defined, tells that to flush
23 ?S: all pending stdio output one must loop through all
24 ?S: the stdio file handles stored in an array and fflush them.
25 ?S: Note that if fflushNULL is defined, fflushall will not
26 ?S: even be probed for and will be left undefined.
29 ?C: This symbol, if defined, tells that fflush(NULL) correctly
30 ?C: flushes all pending stdio output without side effects. In
31 ?C: particular, on some platforms calling fflush(NULL) *still*
32 ?C: corrupts STDIN if it is a pipe.
35 ?C: This symbol, if defined, tells that to flush
36 ?C: all pending stdio output one must loop through all
37 ?C: the stdio file handles stored in an array and fflush them.
38 ?C: Note that if fflushNULL is defined, fflushall will not
39 ?C: even be probed for and will be left undefined.
41 ?H:#$fflushNULL FFLUSH_NULL /**/
42 ?H:#$fflushall FFLUSH_ALL /**/
45 ?F:!try.out !try !tryp
49 Checking how to flush all pending stdio output...
51 # I only know how to find the first 32 possibly open files on SunOS.
52 # See also hints/sunos_4_1.sh and util.c --AD
54 sunos) $echo '#define PERL_FFLUSH_ALL_FOPEN_MAX 32' > try.c ;;
66 #$d_sysconf HAS_SYSCONF
67 #$d_stdio_stream_array HAS_STDIO_STREAM_ARRAY
68 #ifdef HAS_STDIO_STREAM_ARRAY
69 # define STDIO_STREAM_ARRAY $stdio_stream_array
74 p = fopen("try.out", "w");
82 #ifdef TRY_FFLUSH_NULL
88 # ifdef PERL_FFLUSH_ALL_FOPEN_MAX
89 open_max = PERL_FFLUSH_ALL_FOPEN_MAX;
91 # if defined(HAS_SYSCONF) && defined(_SC_OPEN_MAX)
92 open_max = sysconf(_SC_OPEN_MAX);
107 # ifdef HAS_STDIO_STREAM_ARRAY
110 for (i = 0; i < open_max; i++)
111 if (STDIO_STREAM_ARRAY[i]._file >= 0 &&
112 STDIO_STREAM_ARRAY[i]._file < open_max &&
113 STDIO_STREAM_ARRAY[i]._flag)
114 fflush(&STDIO_STREAM_ARRAY[i]);
122 : first we have to find out how _not_ to flush
124 if $test "X$fflushNULL" = X -o "X$fflushall" = X; then
127 if eval $compile; then
128 $run ./try 2>/dev/null
131 if $test ! -s try.out -a "X$code" = X42; then
137 set try -DTRY_FPRINTF
138 if eval $compile; then
139 $run ./try 2>/dev/null
142 if $test ! -s try.out -a "X$code" = X42; then
149 : check for fflush NULL behavior
150 case "$fflushNULL" in
151 '') set try -DTRY_FFLUSH_NULL $output
152 if eval $compile; then
153 $run ./try 2>/dev/null
156 if $test -s try.out -a "X$code" = X42; then
157 fflushNULL="`$cat try.out`"
159 if $test "X$code" != X42; then
161 (If this test failed, don't worry, we'll try another method shortly.)
166 $rm -f core try.core core.try.*
167 case "$fflushNULL" in
169 Your fflush(NULL) works okay for output streams.
170 Let's see if it clobbers input pipes...
172 # As of mid-March 2000 all versions of Solaris appear to have a stdio
173 # bug that improperly flushes the input end of pipes. So we avoid the
174 # autoflush on fork/system/exec support for now. :-(
178 main(int argc, char **argv)
184 while ((i = getc(stdin)) != -1
185 && (*bp++ = i) != '\n'
189 fprintf(stdout, "%s", buf);
199 if eval $compile; then
201 # Copy the .c file to the remote host ($to is an ssh-alike if targethost is set)
202 if $test "X$targethost" != X; then
205 $run "cat tryp.c | ./tryp " 2>/dev/null > tryp.out
207 $cat tryp.c | $run ./tryp 2>/dev/null > tryp.out
209 if cmp tryp.c tryp.out >/dev/null 2>&1; then
211 fflush(NULL) seems to behave okay with input streams.
216 Ouch, fflush(NULL) clobbers input pipes! We will not use it.
221 $rm -f core tryp.c tryp.core core.tryp.*
224 Your fflush(NULL) isn't working (contrary to ANSI C).
229 Cannot figure out whether your fflush(NULL) works or not.
230 I'm assuming it doesn't (contrary to ANSI C).
243 : check explicit looping only if NULL did not work, and if the pipe
244 : bug does not show up on an explicit flush too
245 case "$fflushNULL" in
250 main(int argc, char **argv)
256 while ((i = getc(stdin)) != -1
257 && (*bp++ = i) != '\n'
261 fprintf(stdout, "%s", buf);
270 if eval $compile; then
272 if $test "X$targethost" != X; then
275 $run "cat tryp.c | ./tryp " 2>/dev/null > tryp.out
277 $cat tryp.c | $run ./tryp 2>/dev/null > tryp.out
279 if cmp tryp.c tryp.out >/dev/null 2>&1; then
281 Good, at least fflush(stdin) seems to behave okay when stdin is a pipe.
283 : now check for fflushall behaviour
285 '') set try -DTRY_FFLUSH_ALL $output
286 if eval $compile; then
288 (Now testing the other method--but note that this also may fail.)
290 $run ./try 2>/dev/null
293 if $test -s try.out -a "X$code" = X42; then
294 fflushall="`$cat try.out`"
300 Whew. Flushing explicitly all the stdio streams works.
305 Sigh. Flushing explicitly all the stdio streams doesn't work.
310 Cannot figure out whether flushing stdio streams explicitly works or not.
311 I'm assuming it doesn't.
317 "$define"|true|[yY]*)
326 All is futile. Even fflush(stdin) clobbers input pipes!
333 $rm -f core tryp.c tryp.core core.tryp.*
335 *) fflushall="$undef"
339 case "$fflushNULL$fflushall" in
342 OK, I give up. I cannot figure out how to flush pending stdio output.
343 We won't be flushing handles at all before fork/exec/popen.