This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate mainperl.
[perl5.git] / t / io / pipe.t
CommitLineData
378cc40b
LW
1#!./perl
2
79072805 3# $RCSfile: pipe.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:31 $
378cc40b 4
774d564b
PP
5BEGIN {
6 chdir 't' if -d 't';
7 @INC = '../lib';
8 require Config; import Config;
9 unless ($Config{'d_fork'}) {
10 print "1..0\n";
11 exit 0;
12 }
13}
14
378cc40b 15$| = 1;
1d3434b8 16print "1..12\n";
378cc40b 17
c07a80fd
PP
18open(PIPE, "|-") || (exec 'tr', 'YX', 'ko');
19print PIPE "Xk 1\n";
20print PIPE "oY 2\n";
378cc40b
LW
21close PIPE;
22
23if (open(PIPE, "-|")) {
24 while(<PIPE>) {
ac58e20f 25 s/^not //;
378cc40b
LW
26 print;
27 }
1d3434b8 28 close PIPE; # avoid zombies which disrupt test 12
378cc40b
LW
29}
30else {
ac58e20f
LW
31 print STDOUT "not ok 3\n";
32 exec 'echo', 'not ok 4';
378cc40b 33}
ac58e20f
LW
34
35pipe(READER,WRITER) || die "Can't open pipe";
36
37if ($pid = fork) {
38 close WRITER;
39 while(<READER>) {
40 s/^not //;
41 y/A-Z/a-z/;
42 print;
43 }
1d3434b8 44 close READER; # avoid zombies which disrupt test 12
ac58e20f
LW
45}
46else {
47 die "Couldn't fork" unless defined $pid;
48 close READER;
49 print WRITER "not ok 5\n";
50 open(STDOUT,">&WRITER") || die "Can't dup WRITER to STDOUT";
51 close WRITER;
52 exec 'echo', 'not ok 6';
53}
54
55
56pipe(READER,WRITER) || die "Can't open pipe";
57close READER;
58
59$SIG{'PIPE'} = 'broken_pipe';
60
61sub broken_pipe {
1d2dff63 62 $SIG{'PIPE'} = 'IGNORE'; # loop preventer
ac58e20f
LW
63 print "ok 7\n";
64}
65
66print WRITER "not ok 7\n";
67close WRITER;
3d57aefb 68sleep 1;
ac58e20f 69print "ok 8\n";
03136e13
CS
70
71# VMS doesn't like spawning subprocesses that are still connected to
1d3434b8 72# STDOUT. Someone should modify tests #9 to #12 to work with VMS.
03136e13
CS
73
74if ($^O eq 'VMS') {
75 print "ok 9\n";
76 print "ok 10\n";
1d3434b8
GS
77 print "ok 11\n";
78 print "ok 12\n";
03136e13
CS
79 exit;
80}
81
6ee623d5 82if ($Config{d_sfio} || $^O eq machten || $^O eq beos) {
03136e13 83 # Sfio doesn't report failure when closing a broken pipe
fc261528
DD
84 # that has pending output. Go figure. MachTen doesn't either,
85 # but won't write to broken pipes, so nothing's pending at close.
6ee623d5 86 # BeOS will not write to broken pipes, either.
03136e13
CS
87 print "ok 9\n";
88}
89else {
90 local $SIG{PIPE} = 'IGNORE';
91 open NIL, '|true' or die "open failed: $!";
92 sleep 2;
93 print NIL 'foo' or die "print failed: $!";
94 if (close NIL) {
95 print "not ok 9\n";
96 }
97 else {
98 print "ok 9\n";
99 }
100}
101
102# check that errno gets forced to 0 if the piped program exited non-zero
103open NIL, '|exit 23;' or die "fork failed: $!";
104$! = 1;
105if (close NIL) {
106 print "not ok 10\n# successful close\n";
107}
108elsif ($! != 0) {
109 print "not ok 10\n# errno $!\n";
110}
111elsif ($? == 0) {
112 print "not ok 10\n# status 0\n";
113}
114else {
115 print "ok 10\n";
116}
1d3434b8
GS
117
118# check that status for the correct process is collected
062dddbe 119wait; # Collect from $pid
1d3434b8
GS
120my $zombie = fork or exit 37;
121my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n";
122$SIG{ALRM} = sub { return };
123alarm(1);
124my $close = close FH;
125if ($? == 13*256 && ! length $close && ! $!) {
126 print "ok 11\n";
127} else {
128 print "not ok 11\n# close $close\$?=$? \$!=", $!+0, ":$!\n";
129};
130my $wait = wait;
131if ($? == 37*256 && $wait == $zombie && ! $!) {
132 print "ok 12\n";
133} else {
062dddbe 134 print "not ok 12\n# pid=$wait first=$pid pipe=$pipe zombie=$zombie me=$$ \$?=$? \$!=", $!+0, ":$!\n";
1d3434b8 135}