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