S_del_body is sufficiently small that inlining it is a space win.
[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;
ad1c9500 12print "1..26\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
939b405b 87# To get STDOUT back.
88open(F, ">&DUPOUT") or die "Cannot dup stdout back: $!";
89
ff8b5bfb 90curr_test(13);
91
92SKIP: {
ad1c9500 93 skip("need perlio", 14) unless $Config{useperlio};
ff8b5bfb 94
95 ok(open(F, ">&", STDOUT));
96 isnt(fileno(F), fileno(STDOUT));
97 close F;
98
99 ok(open(F, "<&=STDIN"));
100 is(fileno(F), fileno(STDIN));
101 close F;
102
103 ok(open(F, ">&=STDOUT"));
104 is(fileno(F), fileno(STDOUT));
105 close F;
106
107 ok(open(F, ">&=STDERR"));
108 is(fileno(F), fileno(STDERR));
109 close F;
110
111 open(G, ">dup$$") or die;
112 my $g = fileno(G);
113
114 ok(open(F, ">&=$g"));
115 is(fileno(F), $g);
116 close F;
117
118 ok(open(F, ">&=G"));
119 is(fileno(F), $g);
120
121 print G "ggg\n";
122 print F "fff\n";
123
124 close G; # flush first
125 close F; # flush second
126
127 open(G, "<dup$$") or die;
ad1c9500 128 {
129 my $line;
130 $line = <G>; chomp $line; is($line, "ggg");
131 $line = <G>; chomp $line; is($line, "fff");
132 }
ff8b5bfb 133 close G;
134
135 END { 1 while unlink "dup$$" }
136}