More dup testing.
[p5sagit/p5-mst-13.2.git] / t / io / dup.t
CommitLineData
378cc40b 1#!./perl
2
ba553610 3BEGIN {
4 chdir 't' if -d 't';
ff8b5bfb 5 @INC = qw(. ../lib);
6 require "./test.pl";
ba553610 7}
378cc40b 8
1bb0a50f 9use Config;
10
ba553610 11my $test = 1;
ff8b5bfb 12print "1..25\n";
378cc40b 13print "ok 1\n";
14
ba553610 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 21select(STDERR); $| = 1;
22select(STDOUT); $| = 1;
378cc40b 23
a687059c 24print STDOUT "ok 2\n";
25print STDERR "ok 3\n";
ba553610 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 31print `$cmd`;
32
dc459aad 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 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 40 for 6..7;
41}
42else {
43 system sprintf $echo, 6;
dc459aad 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 52close(STDOUT) or die "Could not close: $!";
53close(STDERR) or die "Could not close: $!";
378cc40b 54
d1e4d418 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 59elsif ($^O eq 'MacOS') { system 'catenate Io.dup' }
60else { system 'cat Io.dup' }
378cc40b 61unlink 'Io.dup';
62
e4a4387c 63print STDOUT "ok 8\n";
9394203c 64
1bb0a50f 65open(F,">&",1) or die "Cannot dup to numeric 1: $!";
31775886 66print F "ok 9\n";
67close(F);
68
1bb0a50f 69open(F,">&",'1') or die "Cannot dup to string '1': $!";
31775886 70print F "ok 10\n";
71close(F);
72
1bb0a50f 73open(F,">&=",1) or die "Cannot dup to numeric 1: $!";
31775886 74print F "ok 11\n";
75close(F);
76
1bb0a50f 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 87curr_test(13);
88
89SKIP: {
90 skip("need perlio", 13) unless $Config{useperlio};
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;
125 { local $/; is(<G>, "ggg\nfff\n") }
126 close G;
127
128 END { 1 while unlink "dup$$" }
129}