- Moved unused argc, temp variable into small scope.
[dragonfly.git] / contrib / perl5 / 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     $SIG{'PIPE'} = 'IGNORE';       # loop preventer
63     print "ok 7\n";
64 }
65
66 print WRITER "not ok 7\n";
67 close WRITER;
68 sleep 1;
69 print "ok 8\n";
70
71 # VMS doesn't like spawning subprocesses that are still connected to
72 # STDOUT.  Someone should modify tests #9 to #12 to work with VMS.
73
74 if ($^O eq 'VMS') {
75     print "ok 9\n";
76     print "ok 10\n";
77     print "ok 11\n";
78     print "ok 12\n";
79     exit;
80 }
81
82 if ($Config{d_sfio} || $^O eq machten || $^O eq beos) {
83     # Sfio doesn't report failure when closing a broken pipe
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.
86     # BeOS will not write to broken pipes, either.
87     print "ok 9\n";
88 }
89 else {
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
103 open NIL, '|exit 23;' or die "fork failed: $!";
104 $! = 1;
105 if (close NIL) {
106     print "not ok 10\n# successful close\n";
107 }
108 elsif ($! != 0) {
109     print "not ok 10\n# errno $!\n";
110 }
111 elsif ($? == 0) {
112     print "not ok 10\n# status 0\n";
113 }
114 else {
115     print "ok 10\n";
116 }
117
118 # check that status for the correct process is collected
119 wait;                           # Collect from $pid
120 my $zombie = fork or exit 37;
121 my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n";
122 $SIG{ALRM} = sub { return };
123 alarm(1);
124 my $close = close FH;
125 if ($? == 13*256 && ! length $close && ! $!) {
126     print "ok 11\n";
127 } else {
128     print "not ok 11\n# close $close\$?=$?   \$!=", $!+0, ":$!\n";
129 };
130 my $wait = wait;
131 if ($? == 37*256 && $wait == $zombie && ! $!) {
132     print "ok 12\n";
133 } else {
134     print "not ok 12\n# pid=$wait first=$pid pipe=$pipe zombie=$zombie me=$$ \$?=$?   \$!=", $!+0, ":$!\n";
135 }