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