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 # Technically this should be TODO. Someone try it if you happen to
32 # have a vmesa machine.
33 skip "Doesn't work here yet", 6 if $^O eq 'vmesa';
35 if (open(PIPE, "-|")) {
40 close PIPE; # avoid zombies
43 printf STDOUT "not ok %d - open -|\n", curr_test();
47 exec $Perl, '-le', "print q{not ok $tnum - again}";
50 # This has to be *outside* the fork
53 my $raw = "abc\nrst\rxyz\r\nfoo\n";
54 if (open(PIPE, "-|")) {
56 (my $raw1 = $_) =~ s/not ok \d+ - //;
57 my @r = map ord, split //, $raw;
58 my @r1 = map ord, split //, $raw1;
60 s/^not (ok \d+ -) .*/$1 '@r1' passes through '-|'\n/s;
62 s/^(not ok \d+ -) .*/$1 expect '@r', got '@r1'\n/s;
65 close PIPE; # avoid zombies
68 printf STDOUT "not ok %d - $raw", curr_test();
69 exec $Perl, '-e0'; # Do not run END()...
72 # This has to be *outside* the fork
75 if (open(PIPE, "|-")) {
76 printf PIPE "not ok %d - $raw", curr_test();
77 close PIPE; # avoid zombies
80 $_ = join '', <STDIN>;
81 (my $raw1 = $_) =~ s/not ok \d+ - //;
82 my @r = map ord, split //, $raw;
83 my @r1 = map ord, split //, $raw1;
85 s/^not (ok \d+ -) .*/$1 '@r1' passes through '|-'\n/s;
87 s/^(not ok \d+ -) .*/$1 expect '@r', got '@r1'\n/s;
90 exec $Perl, '-e0'; # Do not run END()...
93 # This has to be *outside* the fork
97 skip "fork required", 2 unless $Config{d_fork};
99 pipe(READER,WRITER) || die "Can't open pipe";
108 close READER; # avoid zombies
111 die "Couldn't fork" unless defined $pid;
113 printf WRITER "not ok %d - pipe & fork\n", curr_test;
116 open(STDOUT,">&WRITER") || die "Can't dup WRITER to STDOUT";
119 my $tnum = curr_test;
121 exec $Perl, '-le', "print q{not ok $tnum - with fh dup }";
124 # This has to be done *outside* the fork.
125 next_test() for 1..2;
128 wait; # Collect from $pid
130 pipe(READER,WRITER) || die "Can't open pipe";
133 $SIG{'PIPE'} = 'broken_pipe';
136 $SIG{'PIPE'} = 'IGNORE'; # loop preventer
137 printf "ok %d - SIGPIPE\n", curr_test;
140 printf WRITER "not ok %d - SIGPIPE\n", curr_test;
146 # VMS doesn't like spawning subprocesses that are still connected to
147 # STDOUT. Someone should modify these tests to work with VMS.
150 skip "doesn't like spawning subprocesses that are still connected", 10
154 # Sfio doesn't report failure when closing a broken pipe
155 # that has pending output. Go figure.
156 # BeOS will not write to broken pipes, either.
158 skip "Won't report failure on broken pipe", 1
159 if $Config{d_sfio} || $^O eq 'beos' ||
162 local $SIG{PIPE} = 'IGNORE';
163 open NIL, qq{|$Perl -e "exit 0"} or die "open failed: $!";
165 if (print NIL 'foo') {
166 # If print was allowed we had better get an error on close
167 ok( !close NIL, 'close error on broken pipe' );
170 ok(close NIL, 'print failed on broken pipe');
175 skip "Don't work yet", 9 if $^O eq 'vmesa';
177 # check that errno gets forced to 0 if the piped program exited
179 open NIL, qq{|$Perl -e "exit 23";} or die "fork failed: $!";
181 ok(!close NIL, 'close failure on non-zero piped exit');
182 is($!, '', ' errno');
183 isnt($?, 0, ' status');
186 skip "Don't work yet", 6 if $^O eq 'mpeix';
188 # check that status for the correct process is collected
190 unless( $zombie = fork ) {
194 my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n";
195 $SIG{ALRM} = sub { return };
197 is( close FH, '', 'close failure for... umm, something' );
198 is( $?, 13*256, ' status' );
199 is( $!, '', ' errno');
202 is( $?, 37*256, 'status correct after wait' );
203 is( $wait, $zombie, ' wait pid' );
204 is( $!, '', ' errno');
209 # Test new semantics for missing command in piped open
210 # 19990114 M-J. Dominus mjd@plover.com
213 ok( !open(P, "| "), 'missing command in piped open input' );
214 ok( !open(P, " |"), ' output');
217 # check that status is unaffected by implicit close
220 open NIL, qq{|$Perl -e "exit 23"} or die "fork failed: $!";
222 # NIL implicitly closed here
224 is($?, 42, 'status unaffected by implicit close');
227 # check that child is reaped if the piped program can't be executed
229 skip "/no_such_process exists", 1 if -e "/no_such_process";
230 open NIL, '/no_such_process |';
235 local $SIG{ALRM} = sub { die; };
241 is($child, -1, 'child reaped if piped program cannot be executed');