Re: files not cleaned even by veryclean
[p5sagit/p5-mst-13.2.git] / t / io / pipe.t
CommitLineData
378cc40b 1#!./perl
2
774d564b 3BEGIN {
4 chdir 't' if -d 't';
93430cb4 5 unshift @INC, '../lib';
774d564b 6 require Config; import Config;
7 unless ($Config{'d_fork'}) {
45c0de28 8 print "1..0 # Skip: no fork\n";
774d564b 9 exit 0;
10 }
11}
12
378cc40b 13$| = 1;
f2b5be74 14print "1..15\n";
378cc40b 15
092bebab 16# External program 'tr' assumed.
c07a80fd 17open(PIPE, "|-") || (exec 'tr', 'YX', 'ko');
18print PIPE "Xk 1\n";
19print PIPE "oY 2\n";
378cc40b 20close PIPE;
21
092bebab 22if ($^O eq 'vmesa') {
23 # Doesn't work, yet.
6fd78b6e 24 for (3..6) {
25 print "ok $_ # skipped\n";
26 }
092bebab 27} else {
28 if (open(PIPE, "-|")) {
29 while(<PIPE>) {
30 s/^not //;
31 print;
32 }
33 close PIPE; # avoid zombies which disrupt test 12
34 }
35 else {
36 # External program 'echo' assumed.
37 print STDOUT "not ok 3\n";
38 exec 'echo', 'not ok 4';
378cc40b 39 }
ac58e20f 40
092bebab 41 pipe(READER,WRITER) || die "Can't open pipe";
ac58e20f 42
092bebab 43 if ($pid = fork) {
44 close WRITER;
45 while(<READER>) {
46 s/^not //;
47 y/A-Z/a-z/;
48 print;
49 }
50 close READER; # avoid zombies which disrupt test 12
51 }
52 else {
53 die "Couldn't fork" unless defined $pid;
54 close READER;
55 print WRITER "not ok 5\n";
56 open(STDOUT,">&WRITER") || die "Can't dup WRITER to STDOUT";
57 close WRITER;
58 # External program 'echo' assumed.
59 exec 'echo', 'not ok 6';
ac58e20f 60 }
ac58e20f 61}
d6a255e6 62wait; # Collect from $pid
ac58e20f 63
ac58e20f 64pipe(READER,WRITER) || die "Can't open pipe";
65close READER;
66
67$SIG{'PIPE'} = 'broken_pipe';
68
69sub broken_pipe {
1d2dff63 70 $SIG{'PIPE'} = 'IGNORE'; # loop preventer
ac58e20f 71 print "ok 7\n";
72}
73
74print WRITER "not ok 7\n";
75close WRITER;
3d57aefb 76sleep 1;
ac58e20f 77print "ok 8\n";
03136e13 78
79# VMS doesn't like spawning subprocesses that are still connected to
1d3434b8 80# STDOUT. Someone should modify tests #9 to #12 to work with VMS.
03136e13 81
82if ($^O eq 'VMS') {
6fd78b6e 83 print "ok 9 # skipped\n";
84 print "ok 10 # skipped\n";
85 print "ok 11 # skipped\n";
86 print "ok 12 # skipped\n";
03136e13 87 exit;
88}
89
e5e1b98b 90if ($Config{d_sfio} || $^O eq 'machten' || $^O eq 'beos' || $^O eq 'posix-bc') {
03136e13 91 # Sfio doesn't report failure when closing a broken pipe
fc261528 92 # that has pending output. Go figure. MachTen doesn't either,
93 # but won't write to broken pipes, so nothing's pending at close.
6ee623d5 94 # BeOS will not write to broken pipes, either.
e5e1b98b 95 # Nor does POSIX-BC.
6fd78b6e 96 print "ok 9 # skipped\n";
03136e13 97}
98else {
99 local $SIG{PIPE} = 'IGNORE';
100 open NIL, '|true' or die "open failed: $!";
ac230105 101 sleep 5;
03136e13 102 print NIL 'foo' or die "print failed: $!";
103 if (close NIL) {
104 print "not ok 9\n";
105 }
106 else {
107 print "ok 9\n";
108 }
109}
110
092bebab 111if ($^O eq 'vmesa') {
112 # These don't work, yet.
6fd78b6e 113 print "ok 10 # skipped\n";
114 print "ok 11 # skipped\n";
115 print "ok 12 # skipped\n";
092bebab 116 exit;
117}
118
03136e13 119# check that errno gets forced to 0 if the piped program exited non-zero
120open NIL, '|exit 23;' or die "fork failed: $!";
121$! = 1;
122if (close NIL) {
123 print "not ok 10\n# successful close\n";
124}
125elsif ($! != 0) {
126 print "not ok 10\n# errno $!\n";
127}
128elsif ($? == 0) {
129 print "not ok 10\n# status 0\n";
130}
131else {
132 print "ok 10\n";
133}
1d3434b8 134
0994c4d0 135if ($^O eq 'mpeix') {
136 print "ok 11 # skipped\n";
137 print "ok 12 # skipped\n";
1d3434b8 138} else {
0994c4d0 139 # check that status for the correct process is collected
140 my $zombie = fork or exit 37;
141 my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n";
142 $SIG{ALRM} = sub { return };
143 alarm(1);
144 my $close = close FH;
145 if ($? == 13*256 && ! length $close && ! $!) {
146 print "ok 11\n";
147 } else {
148 print "not ok 11\n# close $close\$?=$? \$!=", $!+0, ":$!\n";
149 };
150 my $wait = wait;
151 if ($? == 37*256 && $wait == $zombie && ! $!) {
152 print "ok 12\n";
153 } else {
154 print "not ok 12\n# pid=$wait first=$pid pipe=$pipe zombie=$zombie me=$$ \$?=$? \$!=", $!+0, ":$!\n";
155 }
1d3434b8 156}
06eaf0bc 157
158# Test new semantics for missing command in piped open
159# 19990114 M-J. Dominus mjd@plover.com
160{ local *P;
161 print (((open P, "| " ) ? "not " : ""), "ok 13\n");
162 print (((open P, " |" ) ? "not " : ""), "ok 14\n");
163}
f2b5be74 164
165# check that status is unaffected by implicit close
166{
167 local(*NIL);
168 open NIL, '|exit 23;' or die "fork failed: $!";
169 $? = 42;
170 # NIL implicitly closed here
171}
172if ($? != 42) {
173 print "# status $?, expected 42\nnot ";
174}
175print "ok 15\n";
176$? = 0;