This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Also IRIX wants setlogsock('stream').
[perl5.git] / t / io / dup.t
CommitLineData
378cc40b
LW
1#!./perl
2
ba553610
MS
3BEGIN {
4 chdir 't' if -d 't';
ff8b5bfb
JH
5 @INC = qw(. ../lib);
6 require "./test.pl";
ba553610 7}
378cc40b 8
1bb0a50f
JH
9use Config;
10
ba553610 11my $test = 1;
ad1c9500 12print "1..26\n";
378cc40b
LW
13print "ok 1\n";
14
ba553610
MS
15open(DUPOUT,">&STDOUT");
16open(DUPERR,">&STDERR");
378cc40b 17
ba553610 18open(STDOUT,">Io.dup") || die "Can't open stdout";
a687059c 19open(STDERR,">&STDOUT") || die "Can't open stderr";
378cc40b 20
a687059c
LW
21select(STDERR); $| = 1;
22select(STDOUT); $| = 1;
378cc40b 23
a687059c
LW
24print STDOUT "ok 2\n";
25print STDERR "ok 3\n";
ba553610
MS
26
27# Since some systems don't have echo, we use Perl.
dc459aad 28$echo = qq{$^X -le "print q(ok %d)"};
ba553610 29
dc459aad 30$cmd = sprintf $echo, 4;
ba553610
MS
31print `$cmd`;
32
dc459aad
JH
33$cmd = sprintf "$echo 1>&2", 5;
34$cmd = sprintf $echo, 5 if $^O eq 'MacOS'; # don't know if we can do this ...
ba553610
MS
35print `$cmd`;
36
37# KNOWN BUG system() does not honor STDOUT redirections on VMS.
38if( $^O eq 'VMS' ) {
31775886 39 print "not ok $_ # TODO system() not honoring STDOUT redirect on VMS\n"
ba553610
MS
40 for 6..7;
41}
42else {
43 system sprintf $echo, 6;
dc459aad
JH
44 if ($^O eq 'MacOS') {
45 system sprintf $echo, 7;
46 }
47 else {
48 system sprintf "$echo 1>&2", 7;
49 }
ba553610 50}
378cc40b 51
d1e4d418
A
52close(STDOUT) or die "Could not close: $!";
53close(STDERR) or die "Could not close: $!";
378cc40b 54
d1e4d418
A
55open(STDOUT,">&DUPOUT") or die "Could not open: $!";
56open(STDERR,">&DUPERR") or die "Could not open: $!";
378cc40b 57
cda41bc1 58if (($^O eq 'MSWin32') || ($^O eq 'NetWare') || ($^O eq 'VMS')) { print `type Io.dup` }
dc459aad
JH
59elsif ($^O eq 'MacOS') { system 'catenate Io.dup' }
60else { system 'cat Io.dup' }
378cc40b
LW
61unlink 'Io.dup';
62
e4a4387c 63print STDOUT "ok 8\n";
9394203c 64
1bb0a50f 65open(F,">&",1) or die "Cannot dup to numeric 1: $!";
31775886
NIS
66print F "ok 9\n";
67close(F);
68
1bb0a50f 69open(F,">&",'1') or die "Cannot dup to string '1': $!";
31775886
NIS
70print F "ok 10\n";
71close(F);
72
1bb0a50f 73open(F,">&=",1) or die "Cannot dup to numeric 1: $!";
31775886
NIS
74print F "ok 11\n";
75close(F);
76
1bb0a50f
JH
77if ($Config{useperlio}) {
78 open(F,">&=",'1') or die "Cannot dup to string '1': $!";
79 print F "ok 12\n";
80 close(F);
81} else {
82 open(F, ">&DUPOUT") or die "Cannot dup stdout back: $!";
83 print F "ok 12\n";
84 close(F);
85}
31775886 86
ff8b5bfb
JH
87curr_test(13);
88
89SKIP: {
ad1c9500 90 skip("need perlio", 14) unless $Config{useperlio};
ff8b5bfb
JH
91
92 ok(open(F, ">&", STDOUT));
93 isnt(fileno(F), fileno(STDOUT));
94 close F;
95
96 ok(open(F, "<&=STDIN"));
97 is(fileno(F), fileno(STDIN));
98 close F;
99
100 ok(open(F, ">&=STDOUT"));
101 is(fileno(F), fileno(STDOUT));
102 close F;
103
104 ok(open(F, ">&=STDERR"));
105 is(fileno(F), fileno(STDERR));
106 close F;
107
108 open(G, ">dup$$") or die;
109 my $g = fileno(G);
110
111 ok(open(F, ">&=$g"));
112 is(fileno(F), $g);
113 close F;
114
115 ok(open(F, ">&=G"));
116 is(fileno(F), $g);
117
118 print G "ggg\n";
119 print F "fff\n";
120
121 close G; # flush first
122 close F; # flush second
123
124 open(G, "<dup$$") or die;
ad1c9500
JH
125 {
126 my $line;
127 $line = <G>; chomp $line; is($line, "ggg");
128 $line = <G>; chomp $line; is($line, "fff");
129 }
ff8b5bfb
JH
130 close G;
131
132 END { 1 while unlink "dup$$" }
133}