xref: /openbsd-src/gnu/usr.bin/perl/t/op/sysio.t (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1#!./perl
2
3print "1..39\n";
4
5chdir('op') || chdir('t/op') || die "sysio.t: cannot look for myself: $!";
6
7open(I, 'sysio.t') || die "sysio.t: cannot find myself: $!";
8
9$reopen = ($^O eq 'VMS' || $^O eq 'os2' || $^O eq 'MSWin32' || $^O eq 'dos' ||
10	   $^O eq 'mpeix');
11
12$x = 'abc';
13
14# should not be able to do negative lengths
15eval { sysread(I, $x, -1) };
16print 'not ' unless ($@ =~ /^Negative length /);
17print "ok 1\n";
18
19# $x should be intact
20print 'not ' unless ($x eq 'abc');
21print "ok 2\n";
22
23# should not be able to read before the buffer
24eval { sysread(I, $x, 1, -4) };
25print 'not ' unless ($x eq 'abc');
26print "ok 3\n";
27
28# $x should be intact
29print 'not ' unless ($x eq 'abc');
30print "ok 4\n";
31
32$a ='0123456789';
33
34# default offset 0
35print 'not ' unless(sysread(I, $a, 3) == 3);
36print "ok 5\n";
37
38# $a should be as follows
39print 'not ' unless ($a eq '#!.');
40print "ok 6\n";
41
42# reading past the buffer should zero pad
43print 'not ' unless(sysread(I, $a, 2, 5) == 2);
44print "ok 7\n";
45
46# the zero pad should be seen now
47print 'not ' unless ($a eq "#!.\0\0/p");
48print "ok 8\n";
49
50# try changing the last two characters of $a
51print 'not ' unless(sysread(I, $a, 3, -2) == 3);
52print "ok 9\n";
53
54# the last two characters of $a should have changed (into three)
55print 'not ' unless ($a eq "#!.\0\0erl");
56print "ok 10\n";
57
58$outfile = 'sysio.out';
59
60open(O, ">$outfile") || die "sysio.t: cannot write $outfile: $!";
61
62select(O); $|=1; select(STDOUT);
63
64# cannot write negative lengths
65eval { syswrite(O, $x, -1) };
66print 'not ' unless ($@ =~ /^Negative length /);
67print "ok 11\n";
68
69# $x still intact
70print 'not ' unless ($x eq 'abc');
71print "ok 12\n";
72
73# $outfile still intact
74print 'not ' if (-s $outfile);
75print "ok 13\n";
76
77# should not be able to write from after the buffer
78eval { syswrite(O, $x, 1, 3) };
79print 'not ' unless ($@ =~ /^Offset outside string /);
80print "ok 14\n";
81
82# $x still intact
83print 'not ' unless ($x eq 'abc');
84print "ok 15\n";
85
86# $outfile still intact
87if ($reopen) {  # must close file to update EOF marker for stat
88  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
89}
90print 'not ' if (-s $outfile);
91print "ok 16\n";
92
93# should not be able to write from before the buffer
94
95eval { syswrite(O, $x, 1, -4) };
96print 'not ' unless ($@ =~ /^Offset outside string /);
97print "ok 17\n";
98
99# $x still intact
100print 'not ' unless ($x eq 'abc');
101print "ok 18\n";
102
103# $outfile still intact
104if ($reopen) {  # must close file to update EOF marker for stat
105  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
106}
107print 'not ' if (-s $outfile);
108print "ok 19\n";
109
110# default offset 0
111print 'not ' unless (syswrite(O, $a, 2) == 2);
112print "ok 20\n";
113
114# $a still intact
115print 'not ' unless ($a eq "#!.\0\0erl");
116print "ok 21\n";
117
118# $outfile should have grown now
119if ($reopen) {  # must close file to update EOF marker for stat
120  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
121}
122print 'not ' unless (-s $outfile == 2);
123print "ok 22\n";
124
125# with offset
126print 'not ' unless (syswrite(O, $a, 2, 5) == 2);
127print "ok 23\n";
128
129# $a still intact
130print 'not ' unless ($a eq "#!.\0\0erl");
131print "ok 24\n";
132
133# $outfile should have grown now
134if ($reopen) {  # must close file to update EOF marker for stat
135  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
136}
137print 'not ' unless (-s $outfile == 4);
138print "ok 25\n";
139
140# with negative offset and a bit too much length
141print 'not ' unless (syswrite(O, $a, 5, -3) == 3);
142print "ok 26\n";
143
144# $a still intact
145print 'not ' unless ($a eq "#!.\0\0erl");
146print "ok 27\n";
147
148# $outfile should have grown now
149if ($reopen) {  # must close file to update EOF marker for stat
150  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
151}
152print 'not ' unless (-s $outfile == 7);
153print "ok 28\n";
154
155# with implicit length argument
156print 'not ' unless (syswrite(O, $x) == 3);
157print "ok 29\n";
158
159# $a still intact
160print 'not ' unless ($x eq "abc");
161print "ok 30\n";
162
163# $outfile should have grown now
164if ($reopen) {  # must close file to update EOF marker for stat
165  close O; open(O, ">>$outfile") || die "sysio.t: cannot write $outfile: $!";
166}
167print 'not ' unless (-s $outfile == 10);
168print "ok 31\n";
169
170close(O);
171
172open(I, $outfile) || die "sysio.t: cannot read $outfile: $!";
173
174$b = 'xyz';
175
176# reading too much only return as much as available
177print 'not ' unless (sysread(I, $b, 100) == 10);
178print "ok 32\n";
179# this we should have
180print 'not ' unless ($b eq '#!ererlabc');
181print "ok 33\n";
182
183# test sysseek
184
185print 'not ' unless sysseek(I, 2, 0) == 2;
186print "ok 34\n";
187sysread(I, $b, 3);
188print 'not ' unless $b eq 'ere';
189print "ok 35\n";
190
191print 'not ' unless sysseek(I, -2, 1) == 3;
192print "ok 36\n";
193sysread(I, $b, 4);
194print 'not ' unless $b eq 'rerl';
195print "ok 37\n";
196
197print 'not ' unless sysseek(I, 0, 0) eq '0 but true';
198print "ok 38\n";
199print 'not ' if defined sysseek(I, -1, 1);
200print "ok 39\n";
201
202close(I);
203
204unlink $outfile;
205
206chdir('..');
207
2081;
209
210# eof
211