This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[win32] merge change#981 from maintbranch, add XXX comment about
[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;
03136e13 16print "1..10\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 }
28}
29else {
ac58e20f
LW
30 print STDOUT "not ok 3\n";
31 exec 'echo', 'not ok 4';
378cc40b 32}
ac58e20f
LW
33
34pipe(READER,WRITER) || die "Can't open pipe";
35
36if ($pid = fork) {
37 close WRITER;
38 while(<READER>) {
39 s/^not //;
40 y/A-Z/a-z/;
41 print;
42 }
43}
44else {
45 die "Couldn't fork" unless defined $pid;
46 close READER;
47 print WRITER "not ok 5\n";
48 open(STDOUT,">&WRITER") || die "Can't dup WRITER to STDOUT";
49 close WRITER;
50 exec 'echo', 'not ok 6';
51}
52
53
54pipe(READER,WRITER) || die "Can't open pipe";
55close READER;
56
57$SIG{'PIPE'} = 'broken_pipe';
58
59sub broken_pipe {
60 print "ok 7\n";
61}
62
63print WRITER "not ok 7\n";
64close WRITER;
3d57aefb 65sleep 1;
ac58e20f 66print "ok 8\n";
03136e13
CS
67
68# VMS doesn't like spawning subprocesses that are still connected to
69# STDOUT. Someone should modify tests #9 and #10 to work with VMS.
70
71if ($^O eq 'VMS') {
72 print "ok 9\n";
73 print "ok 10\n";
74 exit;
75}
76
6ee623d5 77if ($Config{d_sfio} || $^O eq machten || $^O eq beos) {
03136e13 78 # Sfio doesn't report failure when closing a broken pipe
fc261528
DD
79 # that has pending output. Go figure. MachTen doesn't either,
80 # but won't write to broken pipes, so nothing's pending at close.
6ee623d5 81 # BeOS will not write to broken pipes, either.
03136e13
CS
82 print "ok 9\n";
83}
84else {
85 local $SIG{PIPE} = 'IGNORE';
86 open NIL, '|true' or die "open failed: $!";
87 sleep 2;
88 print NIL 'foo' or die "print failed: $!";
89 if (close NIL) {
90 print "not ok 9\n";
91 }
92 else {
93 print "ok 9\n";
94 }
95}
96
97# check that errno gets forced to 0 if the piped program exited non-zero
98open NIL, '|exit 23;' or die "fork failed: $!";
99$! = 1;
100if (close NIL) {
101 print "not ok 10\n# successful close\n";
102}
103elsif ($! != 0) {
104 print "not ok 10\n# errno $!\n";
105}
106elsif ($? == 0) {
107 print "not ok 10\n# status 0\n";
108}
109else {
110 print "ok 10\n";
111}