This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More cross-compilation defaults gleaned from -Dcc
[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
0f00356b 119 $run ./try 2>/dev/null
959f3c4c 120 $rm -f try.out
0f00356b 121 $from try.out
959f3c4c
JH
122 if $test ! -s try.out -a "X$?" = X42; then
123 output=-DTRY_FPUTC
124 fi
125 fi
126 case "$output" in
127 '')
128 set try -DTRY_FPRINTF
959f3c4c 129 if eval $compile; then
0f00356b 130 $run ./try 2>/dev/null
959f3c4c 131 $rm -f try.out
0f00356b 132 $from try.out
959f3c4c
JH
133 if $test ! -s try.out -a "X$?" = X42; then
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
JH
146 $rm -f try.out
147 $from try.out
959f3c4c
JH
148 if $test -s try.out -a "X$code" = X42; then
149 fflushNULL="`$cat try.out`"
150 else
151 if $test "X$code" != X42; then
152 $cat >&4 <<EOM
153(If this test failed, don't worry, we'll try another method shortly.)
154EOM
155 fi
156 fi
157 fi
158 $rm -f core try.core core.try.*
159 case "$fflushNULL" in
160 x) $cat >&4 <<EOM
5ef94ee9
JH
161Your fflush(NULL) works okay for output streams.
162Let's see if it clobbers input pipes...
959f3c4c 163EOM
5ef94ee9
JH
164# As of mid-March 2000 all versions of Solaris appear to have a stdio
165# bug that improperly flushes the input end of pipes. So we avoid the
166# autoflush on fork/system/exec support for now. :-(
167$cat >tryp.c <<EOCP
168#include <stdio.h>
169int
170main(int argc, char **argv)
171{
172 char buf[1024];
173 int i;
174 char *bp = buf;
175 while (1) {
176 while ((i = getc(stdin)) != -1
177 && (*bp++ = i) != '\n'
178 && bp < &buf[1024])
179 /* DO NOTHING */ ;
180 *bp = '\0';
181 fprintf(stdout, "%s", buf);
182 fflush(NULL);
183 if (i == -1)
1b13c38f 184 return 0;
5ef94ee9
JH
185 bp = buf;
186 }
187}
188EOCP
189 fflushNULL="$define"
190 set tryp
191 if eval $compile; then
192 $rm -f tryp.out
0f00356b 193 $cat tryp.c | $run ./tryp 2>/dev/null > tryp.out
5ef94ee9
JH
194 if cmp tryp.c tryp.out >/dev/null 2>&1; then
195 $cat >&4 <<EOM
196fflush(NULL) seems to behave okay with input streams.
197EOM
198 fflushNULL="$define"
199 else
200 $cat >&4 <<EOM
201Ouch, fflush(NULL) clobbers input pipes! We will not use it.
202EOM
203 fflushNULL="$undef"
204 fi
205 fi
1b13c38f 206 $rm -f core tryp.c tryp.core core.tryp.*
959f3c4c
JH
207 ;;
208 '') $cat >&4 <<EOM
209Your fflush(NULL) isn't working (contrary to ANSI C).
210EOM
211 fflushNULL="$undef"
212 ;;
213 *) $cat >&4 <<EOM
214Cannot figure out whether your fflush(NULL) works or not.
215I'm assuming it doesn't (contrary to ANSI C).
216EOM
217 fflushNULL="$undef"
218 ;;
219 esac
220 ;;
221$define|true|[yY]*)
222 fflushNULL="$define"
223 ;;
224*)
225 fflushNULL="$undef"
226 ;;
227esac
1b13c38f
JH
228: check explicit looping only if NULL did not work, and if the pipe
229: bug does not show up on an explicit flush too
959f3c4c
JH
230case "$fflushNULL" in
231"$undef")
1b13c38f
JH
232 $cat >tryp.c <<EOCP
233#include <stdio.h>
234int
235main(int argc, char **argv)
236{
237 char buf[1024];
238 int i;
239 char *bp = buf;
240 while (1) {
241 while ((i = getc(stdin)) != -1
242 && (*bp++ = i) != '\n'
243 && bp < &buf[1024])
244 /* DO NOTHING */ ;
245 *bp = '\0';
246 fprintf(stdout, "%s", buf);
247 fflush(stdin);
248 if (i == -1)
249 return 0;
250 bp = buf;
251 }
252}
253EOCP
254 set tryp
255 if eval $compile; then
256 $rm -f tryp.out
0f00356b 257 $cat tryp.c | $run ./tryp 2>/dev/null > tryp.out
1b13c38f
JH
258 if cmp tryp.c tryp.out >/dev/null 2>&1; then
259 $cat >&4 <<EOM
260Good, at least fflush(stdin) seems to behave okay when stdin is a pipe.
959f3c4c 261EOM
1b13c38f 262 : now check for fflushall behaviour
959f3c4c 263 case "$fflushall" in
1b13c38f
JH
264 '') set try -DTRY_FFLUSH_ALL $output
265 if eval $compile; then
266 $cat >&4 <<EOM
267(Now testing the other method--but note that this also may fail.)
268EOM
269 $rm -f try.out
0f00356b 270 $run ./try 2>/dev/null
1b13c38f
JH
271 if $test -s try.out -a "X$?" = X42; then
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