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