xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/t/op/sysio.t (revision 0:68f95e015346)
1*0Sstevel@tonic-gate#!./perl
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateprint "1..42\n";
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gatechdir('op') || chdir('t/op') || die "sysio.t: cannot look for myself: $!";
6*0Sstevel@tonic-gate@INC = '../../lib';
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gateopen(I, 'sysio.t') || die "sysio.t: cannot find myself: $!";
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gate$reopen = ($^O eq 'VMS' ||
11*0Sstevel@tonic-gate           $^O eq 'os2' ||
12*0Sstevel@tonic-gate           $^O eq 'MSWin32' ||
13*0Sstevel@tonic-gate           $^O eq 'NetWare' ||
14*0Sstevel@tonic-gate           $^O eq 'dos' ||
15*0Sstevel@tonic-gate	   $^O eq 'mpeix');
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate$x = 'abc';
18*0Sstevel@tonic-gate
19*0Sstevel@tonic-gate# should not be able to do negative lengths
20*0Sstevel@tonic-gateeval { sysread(I, $x, -1) };
21*0Sstevel@tonic-gateprint 'not ' unless ($@ =~ /^Negative length /);
22*0Sstevel@tonic-gateprint "ok 1\n";
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate# $x should be intact
25*0Sstevel@tonic-gateprint 'not ' unless ($x eq 'abc');
26*0Sstevel@tonic-gateprint "ok 2\n";
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate# should not be able to read before the buffer
29*0Sstevel@tonic-gateeval { sysread(I, $x, 1, -4) };
30*0Sstevel@tonic-gateprint 'not ' unless ($x eq 'abc');
31*0Sstevel@tonic-gateprint "ok 3\n";
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate# $x should be intact
34*0Sstevel@tonic-gateprint 'not ' unless ($x eq 'abc');
35*0Sstevel@tonic-gateprint "ok 4\n";
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate$a ='0123456789';
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate# default offset 0
40*0Sstevel@tonic-gateprint 'not ' unless(sysread(I, $a, 3) == 3);
41*0Sstevel@tonic-gateprint "ok 5\n";
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate# $a should be as follows
44*0Sstevel@tonic-gateprint 'not ' unless ($a eq '#!.');
45*0Sstevel@tonic-gateprint "ok 6\n";
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate# reading past the buffer should zero pad
48*0Sstevel@tonic-gateprint 'not ' unless(sysread(I, $a, 2, 5) == 2);
49*0Sstevel@tonic-gateprint "ok 7\n";
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate# the zero pad should be seen now
52*0Sstevel@tonic-gateprint 'not ' unless ($a eq "#!.\0\0/p");
53*0Sstevel@tonic-gateprint "ok 8\n";
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate# try changing the last two characters of $a
56*0Sstevel@tonic-gateprint 'not ' unless(sysread(I, $a, 3, -2) == 3);
57*0Sstevel@tonic-gateprint "ok 9\n";
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gate# the last two characters of $a should have changed (into three)
60*0Sstevel@tonic-gateprint 'not ' unless ($a eq "#!.\0\0erl");
61*0Sstevel@tonic-gateprint "ok 10\n";
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate$outfile = 'sysio.out';
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gateopen(O, ">$outfile") || die "sysio.t: cannot write $outfile: $!";
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gateselect(O); $|=1; select(STDOUT);
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate# cannot write negative lengths
70*0Sstevel@tonic-gateeval { syswrite(O, $x, -1) };
71*0Sstevel@tonic-gateprint 'not ' unless ($@ =~ /^Negative length /);
72*0Sstevel@tonic-gateprint "ok 11\n";
73*0Sstevel@tonic-gate
74*0Sstevel@tonic-gate# $x still intact
75*0Sstevel@tonic-gateprint 'not ' unless ($x eq 'abc');
76*0Sstevel@tonic-gateprint "ok 12\n";
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate# $outfile still intact
79*0Sstevel@tonic-gateprint 'not ' if (-s $outfile);
80*0Sstevel@tonic-gateprint "ok 13\n";
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate# should not be able to write from after the buffer
83*0Sstevel@tonic-gateeval { syswrite(O, $x, 1, 3) };
84*0Sstevel@tonic-gateprint 'not ' unless ($@ =~ /^Offset outside string /);
85*0Sstevel@tonic-gateprint "ok 14\n";
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate# $x still intact
88*0Sstevel@tonic-gateprint 'not ' unless ($x eq 'abc');
89*0Sstevel@tonic-gateprint "ok 15\n";
90*0Sstevel@tonic-gate
91*0Sstevel@tonic-gate# $outfile still intact
92*0Sstevel@tonic-gateif ($reopen) {  # must close file to update EOF marker for stat
93*0Sstevel@tonic-gate  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
94*0Sstevel@tonic-gate}
95*0Sstevel@tonic-gateprint 'not ' if (-s $outfile);
96*0Sstevel@tonic-gateprint "ok 16\n";
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate# should not be able to write from before the buffer
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gateeval { syswrite(O, $x, 1, -4) };
101*0Sstevel@tonic-gateprint 'not ' unless ($@ =~ /^Offset outside string /);
102*0Sstevel@tonic-gateprint "ok 17\n";
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate# $x still intact
105*0Sstevel@tonic-gateprint 'not ' unless ($x eq 'abc');
106*0Sstevel@tonic-gateprint "ok 18\n";
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate# $outfile still intact
109*0Sstevel@tonic-gateif ($reopen) {  # must close file to update EOF marker for stat
110*0Sstevel@tonic-gate  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
111*0Sstevel@tonic-gate}
112*0Sstevel@tonic-gateprint 'not ' if (-s $outfile);
113*0Sstevel@tonic-gateprint "ok 19\n";
114*0Sstevel@tonic-gate
115*0Sstevel@tonic-gate# default offset 0
116*0Sstevel@tonic-gateif (syswrite(O, $a, 2) == 2){
117*0Sstevel@tonic-gate  print "ok 20\n";
118*0Sstevel@tonic-gate} else {
119*0Sstevel@tonic-gate  print "# $!\nnot ok 20\n";
120*0Sstevel@tonic-gate  # most other tests make no sense after e.g. "No space left on device"
121*0Sstevel@tonic-gate  die $!;
122*0Sstevel@tonic-gate}
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate# $a still intact
126*0Sstevel@tonic-gateprint 'not ' unless ($a eq "#!.\0\0erl");
127*0Sstevel@tonic-gateprint "ok 21\n";
128*0Sstevel@tonic-gate
129*0Sstevel@tonic-gate# $outfile should have grown now
130*0Sstevel@tonic-gateif ($reopen) {  # must close file to update EOF marker for stat
131*0Sstevel@tonic-gate  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
132*0Sstevel@tonic-gate}
133*0Sstevel@tonic-gateprint 'not ' unless (-s $outfile == 2);
134*0Sstevel@tonic-gateprint "ok 22\n";
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate# with offset
137*0Sstevel@tonic-gateprint 'not ' unless (syswrite(O, $a, 2, 5) == 2);
138*0Sstevel@tonic-gateprint "ok 23\n";
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gate# $a still intact
141*0Sstevel@tonic-gateprint 'not ' unless ($a eq "#!.\0\0erl");
142*0Sstevel@tonic-gateprint "ok 24\n";
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate# $outfile should have grown now
145*0Sstevel@tonic-gateif ($reopen) {  # must close file to update EOF marker for stat
146*0Sstevel@tonic-gate  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
147*0Sstevel@tonic-gate}
148*0Sstevel@tonic-gateprint 'not ' unless (-s $outfile == 4);
149*0Sstevel@tonic-gateprint "ok 25\n";
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate# with negative offset and a bit too much length
152*0Sstevel@tonic-gateprint 'not ' unless (syswrite(O, $a, 5, -3) == 3);
153*0Sstevel@tonic-gateprint "ok 26\n";
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate# $a still intact
156*0Sstevel@tonic-gateprint 'not ' unless ($a eq "#!.\0\0erl");
157*0Sstevel@tonic-gateprint "ok 27\n";
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate# $outfile should have grown now
160*0Sstevel@tonic-gateif ($reopen) {  # must close file to update EOF marker for stat
161*0Sstevel@tonic-gate  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
162*0Sstevel@tonic-gate}
163*0Sstevel@tonic-gateprint 'not ' unless (-s $outfile == 7);
164*0Sstevel@tonic-gateprint "ok 28\n";
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate# with implicit length argument
167*0Sstevel@tonic-gateprint 'not ' unless (syswrite(O, $x) == 3);
168*0Sstevel@tonic-gateprint "ok 29\n";
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gate# $a still intact
171*0Sstevel@tonic-gateprint 'not ' unless ($x eq "abc");
172*0Sstevel@tonic-gateprint "ok 30\n";
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate# $outfile should have grown now
175*0Sstevel@tonic-gateif ($reopen) {  # must close file to update EOF marker for stat
176*0Sstevel@tonic-gate  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
177*0Sstevel@tonic-gate}
178*0Sstevel@tonic-gateprint 'not ' unless (-s $outfile == 10);
179*0Sstevel@tonic-gateprint "ok 31\n";
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gateclose(O);
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gateopen(I, $outfile) || die "sysio.t: cannot read $outfile: $!";
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate$b = 'xyz';
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate# reading too much only return as much as available
188*0Sstevel@tonic-gateprint 'not ' unless (sysread(I, $b, 100) == 10);
189*0Sstevel@tonic-gateprint "ok 32\n";
190*0Sstevel@tonic-gate# this we should have
191*0Sstevel@tonic-gateprint 'not ' unless ($b eq '#!ererlabc');
192*0Sstevel@tonic-gateprint "ok 33\n";
193*0Sstevel@tonic-gate
194*0Sstevel@tonic-gate# test sysseek
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gateprint 'not ' unless sysseek(I, 2, 0) == 2;
197*0Sstevel@tonic-gateprint "ok 34\n";
198*0Sstevel@tonic-gatesysread(I, $b, 3);
199*0Sstevel@tonic-gateprint 'not ' unless $b eq 'ere';
200*0Sstevel@tonic-gateprint "ok 35\n";
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gateprint 'not ' unless sysseek(I, -2, 1) == 3;
203*0Sstevel@tonic-gateprint "ok 36\n";
204*0Sstevel@tonic-gatesysread(I, $b, 4);
205*0Sstevel@tonic-gateprint 'not ' unless $b eq 'rerl';
206*0Sstevel@tonic-gateprint "ok 37\n";
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gateprint 'not ' unless sysseek(I, 0, 0) eq '0 but true';
209*0Sstevel@tonic-gateprint "ok 38\n";
210*0Sstevel@tonic-gateprint 'not ' if defined sysseek(I, -1, 1);
211*0Sstevel@tonic-gateprint "ok 39\n";
212*0Sstevel@tonic-gate
213*0Sstevel@tonic-gateclose(I);
214*0Sstevel@tonic-gate
215*0Sstevel@tonic-gateunlink $outfile;
216*0Sstevel@tonic-gate
217*0Sstevel@tonic-gate# Check that utf8 IO doesn't upgrade the scalar
218*0Sstevel@tonic-gateopen(I, ">$outfile") || die "sysio.t: cannot write $outfile: $!";
219*0Sstevel@tonic-gate# Will skip harmlessly on stdioperl
220*0Sstevel@tonic-gateeval {binmode STDOUT, ":utf8"};
221*0Sstevel@tonic-gatedie $@ if $@ and $@ !~ /^IO layers \(like ':utf8'\) unavailable/;
222*0Sstevel@tonic-gate
223*0Sstevel@tonic-gate# y diaresis is \w when UTF8
224*0Sstevel@tonic-gate$a = chr 255;
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gateprint $a =~ /\w/ ? "not ok 40\n" : "ok 40\n";
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gatesyswrite I, $a;
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate# Should not be upgraded as a side effect of syswrite.
231*0Sstevel@tonic-gateprint $a =~ /\w/ ? "not ok 41\n" : "ok 41\n";
232*0Sstevel@tonic-gate
233*0Sstevel@tonic-gate# This should work
234*0Sstevel@tonic-gateeval {syswrite I, 2;};
235*0Sstevel@tonic-gateprint $@ eq "" ? "ok 42\n" : "not ok 42 # $@";
236*0Sstevel@tonic-gate
237*0Sstevel@tonic-gateclose(I);
238*0Sstevel@tonic-gateunlink $outfile;
239*0Sstevel@tonic-gate
240*0Sstevel@tonic-gatechdir('..');
241*0Sstevel@tonic-gate
242*0Sstevel@tonic-gate1;
243*0Sstevel@tonic-gate
244*0Sstevel@tonic-gate# eof
245