summaryrefslogtreecommitdiff
path: root/t/io/pipe.t
blob: fc3c0e522156731b5f4f31421076a185958dc1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!./perl

# $RCSfile: pipe.t,v $$Revision: 4.1 $$Date: 92/08/07 18:27:31 $

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
    require Config; import Config;
    unless ($Config{'d_fork'}) {
	print "1..0\n";
	exit 0;
    }
}

$| = 1;
print "1..12\n";

# External program 'tr' assumed.
open(PIPE, "|-") || (exec 'tr', 'YX', 'ko');
print PIPE "Xk 1\n";
print PIPE "oY 2\n";
close PIPE;

if ($^O eq 'vmesa') {
    # Doesn't work, yet.
    print "ok 3\n";
    print "ok 4\n";
    print "ok 5\n";
    print "ok 6\n";
} else {
    if (open(PIPE, "-|")) {
	while(<PIPE>) {
	    s/^not //;
	    print;
	}
	close PIPE;        # avoid zombies which disrupt test 12
    }
    else {
	# External program 'echo' assumed.
	print STDOUT "not ok 3\n";
	exec 'echo', 'not ok 4';
    }

    pipe(READER,WRITER) || die "Can't open pipe";

    if ($pid = fork) {
	close WRITER;
	while(<READER>) {
	    s/^not //;
	    y/A-Z/a-z/;
	    print;
	}
	close READER;     # avoid zombies which disrupt test 12
    }
    else {
	die "Couldn't fork" unless defined $pid;
	close READER;
	print WRITER "not ok 5\n";
	open(STDOUT,">&WRITER") || die "Can't dup WRITER to STDOUT";
	close WRITER;
	# External program 'echo' assumed.
	exec 'echo', 'not ok 6';
    }
}

pipe(READER,WRITER) || die "Can't open pipe";
close READER;

$SIG{'PIPE'} = 'broken_pipe';

sub broken_pipe {
    $SIG{'PIPE'} = 'IGNORE';       # loop preventer
    print "ok 7\n";
}

print WRITER "not ok 7\n";
close WRITER;
sleep 1;
print "ok 8\n";

# VMS doesn't like spawning subprocesses that are still connected to
# STDOUT.  Someone should modify tests #9 to #12 to work with VMS.

if ($^O eq 'VMS') {
    print "ok 9\n";
    print "ok 10\n";
    print "ok 11\n";
    print "ok 12\n";
    exit;
}

if ($Config{d_sfio} || $^O eq machten || $^O eq beos) {
    # Sfio doesn't report failure when closing a broken pipe
    # that has pending output.  Go figure.  MachTen doesn't either,
    # but won't write to broken pipes, so nothing's pending at close.
    # BeOS will not write to broken pipes, either.
    print "ok 9\n";
}
else {
    local $SIG{PIPE} = 'IGNORE';
    open NIL, '|true'	or die "open failed: $!";
    sleep 2;
    print NIL 'foo'	or die "print failed: $!";
    if (close NIL) {
	print "not ok 9\n";
    }
    else {
	print "ok 9\n";
    }
}

if ($^O eq 'vmesa') {
    # These don't work, yet.
    print "ok 10\n";
    print "ok 11\n";
    print "ok 12\n";
    exit;
}

# check that errno gets forced to 0 if the piped program exited non-zero
open NIL, '|exit 23;' or die "fork failed: $!";
$! = 1;
if (close NIL) {
    print "not ok 10\n# successful close\n";
}
elsif ($! != 0) {
    print "not ok 10\n# errno $!\n";
}
elsif ($? == 0) {
    print "not ok 10\n# status 0\n";
}
else {
    print "ok 10\n";
}

# check that status for the correct process is collected
wait;				# Collect from $pid
my $zombie = fork or exit 37;
my $pipe = open *FH, "sleep 2;exit 13|" or die "Open: $!\n";
$SIG{ALRM} = sub { return };
alarm(1);
my $close = close FH;
if ($? == 13*256 && ! length $close && ! $!) {
    print "ok 11\n";
} else {
    print "not ok 11\n# close $close\$?=$?   \$!=", $!+0, ":$!\n";
};
my $wait = wait;
if ($? == 37*256 && $wait == $zombie && ! $!) {
    print "ok 12\n";
} else {
    print "not ok 12\n# pid=$wait first=$pid pipe=$pipe zombie=$zombie me=$$ \$?=$?   \$!=", $!+0, ":$!\n";
}