6 require Config; import Config;
9 if (!$Config{'d_fork'}) {
10 skip_all("fork required to pipe");
17 my $Perl = which_perl();
22 open(PIPE, "|-") || exec $Perl, '-pe', 'tr/YX/ko/';
24 printf PIPE "Xk %d - open |- || exec\n", curr_test();
26 printf PIPE "oY %d - again\n", curr_test();
31 if (open(PIPE, "-|")) {
36 close PIPE; # avoid zombies
39 printf STDOUT "not ok %d - open -|\n", curr_test();
43 exec $Perl, '-le', "print q{not ok $tnum - again}";
46 # This has to be *outside* the fork
49 my $raw = "abc\nrst\rxyz\r\nfoo\n";
50 if (open(PIPE, "-|")) {
52 (my $raw1 = $_) =~ s/not ok \d+ - //;
53 my @r = map ord, split //, $raw;
54 my @r1 = map ord, split //, $raw1;
56 s/^not (ok \d+ -) .*/$1 '@r1' passes through '-|'\n/s;
58 s/^(not ok \d+ -) .*/$1 expect '@r', got '@r1'\n/s;
61 close PIPE; # avoid zombies
64 printf STDOUT "not ok %d - $raw", curr_test();
65 exec $Perl, '-e0'; # Do not run END()...
68 # This has to be *outside* the fork
71 if (open(PIPE, "|-")) {
72 printf PIPE "not ok %d - $raw", curr_test();
73 close PIPE; # avoid zombies
76 $_ = join '', <STDIN>;
77 (my $raw1 = $_) =~ s/not ok \d+ - //;
78 my @r = map ord, split //, $raw;
79 my @r1 = map ord, split //, $raw1;
81 s/^not (ok \d+ -) .*/$1 '@r1' passes through '|-'\n/s;
83 s/^(not ok \d+ -) .*/$1 expect '@r', got '@r1'\n/s;
86 exec $Perl, '-e0'; # Do not run END()...
89 # This has to be *outside* the fork
93 skip "fork required", 2 unless $Config{d_fork};
95 pipe(READER,WRITER) || die "Can't open pipe";
104 close READER; # avoid zombies
107 die "Couldn't fork" unless defined $pid;
109 printf WRITER "not ok %d - pipe & fork\n", curr_test;
112 open(STDOUT,">&WRITER") || die "Can't dup WRITER to STDOUT";
115 my $tnum = curr_test;
117 exec $Perl, '-le', "print q{not ok $tnum - with fh dup }";
120 # This has to be done *outside* the fork.
121 next_test() for 1..2;
124 wait; # Collect from $pid
126 pipe(READER,WRITER) || die "Can't open pipe";
129 $SIG{'PIPE'} = 'broken_pipe';
132 $SIG{'PIPE'} = 'IGNORE'; # loop preventer
133 printf "ok %d - SIGPIPE\n", curr_test;
136 printf WRITER "not ok %d - SIGPIPE\n", curr_test;
142 # VMS doesn't like spawning subprocesses that are still connected to
143 # STDOUT. Someone should modify these tests to work with VMS.
146 skip "doesn't like spawning subprocesses that are still connected", 10
150 # Sfio doesn't report failure when closing a broken pipe
151 # that has pending output. Go figure.
152 # BeOS will not write to broken pipes, either.
154 skip "Won't report failure on broken pipe", 1
155 if $Config{d_sfio} || $^O eq 'beos' ||
158 local $SIG{PIPE} = 'IGNORE';
159 open NIL, qq{|$Perl -e "exit 0"} or die "open failed: $!";
161 if (print NIL 'foo') {
162 # If print was allowed we had better get an error on close
163 ok( !close NIL, 'close error on broken pipe' );
166 ok(close NIL, 'print failed on broken pipe');
171 # check that errno gets forced to 0 if the piped program exited
173 open NIL, qq{|$Perl -e "exit 23";} or die "fork failed: $!";
175 ok(!close NIL, 'close failure on non-zero piped exit');
176 is($!, '', ' errno');
177 isnt($?, 0, ' status');
181 # check that status for the correct process is collected
183 unless( $zombie = fork ) {
187 my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n";
188 $SIG{ALRM} = sub { return };
190 is( close FH, '', 'close failure for... umm, something' );
191 is( $?, 13*256, ' status' );
192 is( $!, '', ' errno');
195 is( $?, 37*256, 'status correct after wait' );
196 is( $wait, $zombie, ' wait pid' );
197 is( $!, '', ' errno');
202 # Test new semantics for missing command in piped open
203 # 19990114 M-J. Dominus mjd@plover.com
206 ok( !open(P, "| "), 'missing command in piped open input' );
207 ok( !open(P, " |"), ' output');
210 # check that status is unaffected by implicit close
213 open NIL, qq{|$Perl -e "exit 23"} or die "fork failed: $!";
215 # NIL implicitly closed here
217 is($?, 42, 'status unaffected by implicit close');
220 # check that child is reaped if the piped program can't be executed
222 skip "/no_such_process exists", 1 if -e "/no_such_process";
223 open NIL, '/no_such_process |';
228 local $SIG{ALRM} = sub { die; };
234 is($child, -1, 'child reaped if piped program cannot be executed');