4a7cb7a423946a5ffe9a26a84fdb17ee8e582cd0
[p5sagit/p5-mst-13.2.git] / t / io / pipe.t
1 #!./perl
2
3 # $RCSfile: pipe.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:31 $
4
5 BEGIN {
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
15 $| = 1;
16 print "1..12\n";
17
18 open(PIPE, "|-") || (exec 'tr', 'YX', 'ko');
19 print PIPE "Xk 1\n";
20 print PIPE "oY 2\n";
21 close PIPE;
22
23 if (open(PIPE, "-|")) {
24     while(<PIPE>) {
25         s/^not //;
26         print;
27     }
28     close PIPE;        # avoid zombies which disrupt test 12
29 }
30 else {
31     print STDOUT "not ok 3\n";
32     exec 'echo', 'not ok 4';
33 }
34
35 pipe(READER,WRITER) || die "Can't open pipe";
36
37 if ($pid = fork) {
38     close WRITER;
39     while(<READER>) {
40         s/^not //;
41         y/A-Z/a-z/;
42         print;
43     }
44     close READER;     # avoid zombies which disrupt test 12
45 }
46 else {
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
56 pipe(READER,WRITER) || die "Can't open pipe";
57 close READER;
58
59 $SIG{'PIPE'} = 'broken_pipe';
60
61 sub broken_pipe {
62     print "ok 7\n";
63 }
64
65 print WRITER "not ok 7\n";
66 close WRITER;
67 sleep 1;
68 print "ok 8\n";
69
70 # VMS doesn't like spawning subprocesses that are still connected to
71 # STDOUT.  Someone should modify tests #9 to #12 to work with VMS.
72
73 if ($^O eq 'VMS') {
74     print "ok 9\n";
75     print "ok 10\n";
76     print "ok 11\n";
77     print "ok 12\n";
78     exit;
79 }
80
81 if ($Config{d_sfio} || $^O eq machten || $^O eq beos) {
82     # Sfio doesn't report failure when closing a broken pipe
83     # that has pending output.  Go figure.  MachTen doesn't either,
84     # but won't write to broken pipes, so nothing's pending at close.
85     # BeOS will not write to broken pipes, either.
86     print "ok 9\n";
87 }
88 else {
89     local $SIG{PIPE} = 'IGNORE';
90     open NIL, '|true'   or die "open failed: $!";
91     sleep 2;
92     print NIL 'foo'     or die "print failed: $!";
93     if (close NIL) {
94         print "not ok 9\n";
95     }
96     else {
97         print "ok 9\n";
98     }
99 }
100
101 # check that errno gets forced to 0 if the piped program exited non-zero
102 open NIL, '|exit 23;' or die "fork failed: $!";
103 $! = 1;
104 if (close NIL) {
105     print "not ok 10\n# successful close\n";
106 }
107 elsif ($! != 0) {
108     print "not ok 10\n# errno $!\n";
109 }
110 elsif ($? == 0) {
111     print "not ok 10\n# status 0\n";
112 }
113 else {
114     print "ok 10\n";
115 }
116
117 # check that status for the correct process is collected
118 my $zombie = fork or exit 37;
119 my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n";
120 $SIG{ALRM} = sub { return };
121 alarm(1);
122 my $close = close FH;
123 if ($? == 13*256 && ! length $close && ! $!) {
124     print "ok 11\n";
125 } else {
126     print "not ok 11\n# close $close\$?=$?   \$!=", $!+0, ":$!\n";
127 };
128 my $wait = wait;
129 if ($? == 37*256 && $wait == $zombie && ! $!) {
130     print "ok 12\n";
131 } else {
132     print "not ok 12\n# pid=$wait   \$?=$?   \$!=", $!+0, ":$!\n";
133 }