1*433d6423SLionel Sambuc /* test26: lseek() Author: Jan-Mark Wams (jms@cs.vu.nl) */
2*433d6423SLionel Sambuc
3*433d6423SLionel Sambuc #include <sys/types.h>
4*433d6423SLionel Sambuc #include <sys/stat.h>
5*433d6423SLionel Sambuc #include <sys/wait.h>
6*433d6423SLionel Sambuc #include <stdlib.h>
7*433d6423SLionel Sambuc #include <unistd.h>
8*433d6423SLionel Sambuc #include <string.h>
9*433d6423SLionel Sambuc #include <fcntl.h>
10*433d6423SLionel Sambuc #include <limits.h>
11*433d6423SLionel Sambuc #include <errno.h>
12*433d6423SLionel Sambuc #include <time.h>
13*433d6423SLionel Sambuc #include <stdio.h>
14*433d6423SLionel Sambuc
15*433d6423SLionel Sambuc int max_error = 4;
16*433d6423SLionel Sambuc #include "common.h"
17*433d6423SLionel Sambuc
18*433d6423SLionel Sambuc #define ITERATIONS 10
19*433d6423SLionel Sambuc
20*433d6423SLionel Sambuc #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
21*433d6423SLionel Sambuc #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
22*433d6423SLionel Sambuc #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
23*433d6423SLionel Sambuc #define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
24*433d6423SLionel Sambuc
25*433d6423SLionel Sambuc
26*433d6423SLionel Sambuc void test26a(void);
27*433d6423SLionel Sambuc void test26b(void);
28*433d6423SLionel Sambuc void test26c(void);
29*433d6423SLionel Sambuc
main(int argc,char * argv[])30*433d6423SLionel Sambuc int main(int argc, char *argv[])
31*433d6423SLionel Sambuc {
32*433d6423SLionel Sambuc int i, m = 0xFFFF;
33*433d6423SLionel Sambuc
34*433d6423SLionel Sambuc sync();
35*433d6423SLionel Sambuc if (argc == 2) m = atoi(argv[1]);
36*433d6423SLionel Sambuc
37*433d6423SLionel Sambuc start(26);
38*433d6423SLionel Sambuc
39*433d6423SLionel Sambuc for (i = 0; i < 10; i++) {
40*433d6423SLionel Sambuc if (m & 0001) test26a();
41*433d6423SLionel Sambuc if (m & 0002) test26b();
42*433d6423SLionel Sambuc if (m & 0004) test26c();
43*433d6423SLionel Sambuc }
44*433d6423SLionel Sambuc quit();
45*433d6423SLionel Sambuc
46*433d6423SLionel Sambuc return(-1); /* Unreachable */
47*433d6423SLionel Sambuc }
48*433d6423SLionel Sambuc
test26a()49*433d6423SLionel Sambuc void test26a()
50*433d6423SLionel Sambuc { /* Test normal operation. */
51*433d6423SLionel Sambuc int fd;
52*433d6423SLionel Sambuc char buf[20];
53*433d6423SLionel Sambuc int i, j;
54*433d6423SLionel Sambuc struct stat st;
55*433d6423SLionel Sambuc
56*433d6423SLionel Sambuc subtest = 1;
57*433d6423SLionel Sambuc System("rm -rf ../DIR_26/*");
58*433d6423SLionel Sambuc
59*433d6423SLionel Sambuc System("echo -n hihaho > hihaho");
60*433d6423SLionel Sambuc if ((fd = open("hihaho", O_RDONLY)) != 3) e(1);
61*433d6423SLionel Sambuc if (lseek(fd, (off_t) 3, SEEK_SET) != (off_t) 3) e(2);
62*433d6423SLionel Sambuc if (read(fd, buf, 1) != 1) e(3);
63*433d6423SLionel Sambuc if (buf[0] != 'a') e(4);
64*433d6423SLionel Sambuc if (lseek(fd, (off_t) - 1, SEEK_END) != 5) e(5);
65*433d6423SLionel Sambuc if (read(fd, buf, 1) != 1) e(6);
66*433d6423SLionel Sambuc if (buf[0] != 'o') e(7);
67*433d6423SLionel Sambuc
68*433d6423SLionel Sambuc /* Seek past end of file. */
69*433d6423SLionel Sambuc if (lseek(fd, (off_t) 1000, SEEK_END) != 1006) e(8);
70*433d6423SLionel Sambuc if (read(fd, buf, 1) != 0) e(9);
71*433d6423SLionel Sambuc
72*433d6423SLionel Sambuc /* Lseek() should not extend the file. */
73*433d6423SLionel Sambuc if (fstat(fd, &st) != 0) e(10);
74*433d6423SLionel Sambuc if (st.st_size != (off_t) 6) e(11);
75*433d6423SLionel Sambuc if (close(fd) != 0) e(12);
76*433d6423SLionel Sambuc
77*433d6423SLionel Sambuc /* Probeer lseek met write. */
78*433d6423SLionel Sambuc if ((fd = open("hihaho", O_WRONLY)) != 3) e(13);
79*433d6423SLionel Sambuc if (lseek(fd, (off_t) 3, SEEK_SET) != (off_t) 3) e(14);
80*433d6423SLionel Sambuc if (write(fd, "e", 1) != 1) e(15);
81*433d6423SLionel Sambuc if (lseek(fd, (off_t) 1000, SEEK_END) != 1006) e(16);
82*433d6423SLionel Sambuc
83*433d6423SLionel Sambuc /* Lseek() should not extend the file. */
84*433d6423SLionel Sambuc if (fstat(fd, &st) != 0) e(17);
85*433d6423SLionel Sambuc if (st.st_size != (off_t) 6) e(18);
86*433d6423SLionel Sambuc if (write(fd, "e", 1) != 1) e(19);
87*433d6423SLionel Sambuc
88*433d6423SLionel Sambuc /* Lseek() and a subsequent write should! */
89*433d6423SLionel Sambuc if (fstat(fd, &st) != 0) e(20);
90*433d6423SLionel Sambuc if (st.st_size != (off_t) 1007) e(21);
91*433d6423SLionel Sambuc
92*433d6423SLionel Sambuc if (close(fd) != 0) e(22);
93*433d6423SLionel Sambuc
94*433d6423SLionel Sambuc /* Check the file, it should start with hiheho. */
95*433d6423SLionel Sambuc if ((fd = open("hihaho", O_RDONLY)) != 3) e(23);
96*433d6423SLionel Sambuc if (read(fd, buf, 6) != 6) e(24);
97*433d6423SLionel Sambuc if (strncmp(buf, "hiheho", 6) != 0) e(25);
98*433d6423SLionel Sambuc
99*433d6423SLionel Sambuc /* The should be zero bytes and a trailing ``e''. */
100*433d6423SLionel Sambuc if (sizeof(buf) < 10) e(26);
101*433d6423SLionel Sambuc for (i = 1; i <= 20; i++) {
102*433d6423SLionel Sambuc if (read(fd, buf, 10) != 10) e(27);
103*433d6423SLionel Sambuc for (j = 0; j < 10; j++)
104*433d6423SLionel Sambuc if (buf[j] != '\0') break;
105*433d6423SLionel Sambuc if (j != 10) e(28);
106*433d6423SLionel Sambuc if (lseek(fd, (off_t) 15, SEEK_CUR) != (off_t) i * 25 + 6) e(29);
107*433d6423SLionel Sambuc }
108*433d6423SLionel Sambuc
109*433d6423SLionel Sambuc if (lseek(fd, (off_t) 1006, SEEK_SET) != (off_t) 1006) e(30);
110*433d6423SLionel Sambuc if (read(fd, buf, sizeof(buf)) != 1) e(31);
111*433d6423SLionel Sambuc if (buf[0] != 'e') e(32);
112*433d6423SLionel Sambuc
113*433d6423SLionel Sambuc if (lseek(fd, (off_t) - 1, SEEK_END) != (off_t) 1006) e(33);
114*433d6423SLionel Sambuc if (read(fd, buf, sizeof(buf)) != 1) e(34);
115*433d6423SLionel Sambuc if (buf[0] != 'e') e(35);
116*433d6423SLionel Sambuc
117*433d6423SLionel Sambuc /* Closing time. */
118*433d6423SLionel Sambuc if (close(fd) != 0) e(36);
119*433d6423SLionel Sambuc }
120*433d6423SLionel Sambuc
test26b()121*433d6423SLionel Sambuc void test26b()
122*433d6423SLionel Sambuc {
123*433d6423SLionel Sambuc int fd1, fd2, fd3;
124*433d6423SLionel Sambuc int stat_loc;
125*433d6423SLionel Sambuc
126*433d6423SLionel Sambuc subtest = 2;
127*433d6423SLionel Sambuc System("rm -rf ../DIR_26/*");
128*433d6423SLionel Sambuc
129*433d6423SLionel Sambuc /* See if childs lseek() is effecting the parent. * See also if
130*433d6423SLionel Sambuc * lseeking() on same file messes things up. */
131*433d6423SLionel Sambuc
132*433d6423SLionel Sambuc /* Creat a file of 11 bytes. */
133*433d6423SLionel Sambuc if ((fd1 = open("santa", O_WRONLY | O_CREAT, 0777)) != 3) e(1);
134*433d6423SLionel Sambuc if (write(fd1, "ho ho ho ho", 11) != 11) e(2);
135*433d6423SLionel Sambuc if (close(fd1) != 0) e(3);
136*433d6423SLionel Sambuc
137*433d6423SLionel Sambuc /* Open it multiple times. */
138*433d6423SLionel Sambuc if ((fd1 = open("santa", O_RDONLY)) != 3) e(4);
139*433d6423SLionel Sambuc if ((fd2 = open("santa", O_WRONLY)) != 4) e(5);
140*433d6423SLionel Sambuc if ((fd3 = open("santa", O_RDWR)) != 5) e(6);
141*433d6423SLionel Sambuc
142*433d6423SLionel Sambuc /* Set all offsets different. */
143*433d6423SLionel Sambuc if (lseek(fd1, (off_t) 2, SEEK_SET) != 2) e(7);
144*433d6423SLionel Sambuc if (lseek(fd2, (off_t) 4, SEEK_SET) != 4) e(8);
145*433d6423SLionel Sambuc if (lseek(fd3, (off_t) 7, SEEK_SET) != 7) e(9);
146*433d6423SLionel Sambuc
147*433d6423SLionel Sambuc /* Have a child process do additional offset changes. */
148*433d6423SLionel Sambuc switch (fork()) {
149*433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
150*433d6423SLionel Sambuc case 0:
151*433d6423SLionel Sambuc alarm(20);
152*433d6423SLionel Sambuc if (lseek(fd1, (off_t) 1, SEEK_CUR) != 3) e(10);
153*433d6423SLionel Sambuc if (lseek(fd2, (off_t) 5, SEEK_SET) != 5) e(11);
154*433d6423SLionel Sambuc if (lseek(fd3, (off_t) - 4, SEEK_END) != 7) e(12);
155*433d6423SLionel Sambuc exit(0);
156*433d6423SLionel Sambuc default:
157*433d6423SLionel Sambuc wait(&stat_loc);
158*433d6423SLionel Sambuc if (stat_loc != 0) e(13); /* Alarm? */
159*433d6423SLionel Sambuc }
160*433d6423SLionel Sambuc
161*433d6423SLionel Sambuc /* Check if the new offsets are correct. */
162*433d6423SLionel Sambuc if (lseek(fd1, (off_t) 0, SEEK_CUR) != 3) e(14);
163*433d6423SLionel Sambuc if (lseek(fd2, (off_t) 0, SEEK_CUR) != 5) e(15);
164*433d6423SLionel Sambuc if (lseek(fd3, (off_t) 0, SEEK_CUR) != 7) e(16);
165*433d6423SLionel Sambuc
166*433d6423SLionel Sambuc /* Close the file. */
167*433d6423SLionel Sambuc if (close(fd1) != 0) e(17);
168*433d6423SLionel Sambuc if (close(fd2) != 0) e(18);
169*433d6423SLionel Sambuc if (close(fd3) != 0) e(19);
170*433d6423SLionel Sambuc }
171*433d6423SLionel Sambuc
test26c()172*433d6423SLionel Sambuc void test26c()
173*433d6423SLionel Sambuc { /* Test error returns. */
174*433d6423SLionel Sambuc int fd;
175*433d6423SLionel Sambuc int tube[2];
176*433d6423SLionel Sambuc int i, stat_loc;
177*433d6423SLionel Sambuc
178*433d6423SLionel Sambuc subtest = 3;
179*433d6423SLionel Sambuc System("rm -rf ../DIR_26/*");
180*433d6423SLionel Sambuc
181*433d6423SLionel Sambuc /* Fifo's can't be lseeked(). */
182*433d6423SLionel Sambuc Mkfifo("fifo");
183*433d6423SLionel Sambuc switch (fork()) {
184*433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
185*433d6423SLionel Sambuc case 0:
186*433d6423SLionel Sambuc alarm(3); /* Try for max 3 secs. */
187*433d6423SLionel Sambuc if ((fd = open("fifo", O_RDONLY)) != 3) e(1);
188*433d6423SLionel Sambuc if (lseek(fd, (off_t) 0, SEEK_SET) != (off_t) - 1) e(2);
189*433d6423SLionel Sambuc if (errno != ESPIPE) e(3);
190*433d6423SLionel Sambuc if (close(fd) != 0) e(4);
191*433d6423SLionel Sambuc exit(0);
192*433d6423SLionel Sambuc default:
193*433d6423SLionel Sambuc if ((fd = open("fifo", O_WRONLY)) != 3) e(5);
194*433d6423SLionel Sambuc wait(&stat_loc);
195*433d6423SLionel Sambuc if (stat_loc != 0) e(6);/* Alarm? */
196*433d6423SLionel Sambuc if (close(fd) != 0) e(7);
197*433d6423SLionel Sambuc }
198*433d6423SLionel Sambuc
199*433d6423SLionel Sambuc /* Pipes can't be lseeked() eigther. */
200*433d6423SLionel Sambuc if (pipe(tube) != 0) e(8);
201*433d6423SLionel Sambuc switch (fork()) {
202*433d6423SLionel Sambuc case -1: printf("Can't fork\n"); break;
203*433d6423SLionel Sambuc case 0:
204*433d6423SLionel Sambuc alarm(3); /* Max 3 sconds wait. */
205*433d6423SLionel Sambuc if (lseek(tube[0], (off_t) 0, SEEK_SET) != (off_t) - 1) e(9);
206*433d6423SLionel Sambuc if (errno != ESPIPE) e(10);
207*433d6423SLionel Sambuc if (lseek(tube[1], (off_t) 0, SEEK_SET) != (off_t) - 1) e(11);
208*433d6423SLionel Sambuc if (errno != ESPIPE) e(12);
209*433d6423SLionel Sambuc exit(0);
210*433d6423SLionel Sambuc default:
211*433d6423SLionel Sambuc wait(&stat_loc);
212*433d6423SLionel Sambuc if (stat_loc != 0) e(14); /* Alarm? */
213*433d6423SLionel Sambuc }
214*433d6423SLionel Sambuc
215*433d6423SLionel Sambuc /* Close the pipe. */
216*433d6423SLionel Sambuc if (close(tube[0]) != 0) e(15);
217*433d6423SLionel Sambuc if (close(tube[1]) != 0) e(16);
218*433d6423SLionel Sambuc
219*433d6423SLionel Sambuc /* Whence arument invalid. */
220*433d6423SLionel Sambuc System("echo -n contact > file");
221*433d6423SLionel Sambuc if ((fd = open("file", O_RDWR)) != 3) e(17);
222*433d6423SLionel Sambuc for (i = -1000; i < 1000; i++) {
223*433d6423SLionel Sambuc if (i == SEEK_SET || i == SEEK_END || i == SEEK_CUR) continue;
224*433d6423SLionel Sambuc if (lseek(fd, (off_t) 0, i) != (off_t) -1) e(18);
225*433d6423SLionel Sambuc if (errno != EINVAL) e(19);
226*433d6423SLionel Sambuc }
227*433d6423SLionel Sambuc if (close(fd) != 0) e(20);
228*433d6423SLionel Sambuc
229*433d6423SLionel Sambuc /* EBADF for bad fides. */
230*433d6423SLionel Sambuc for (i = -1000; i < 1000; i++) {
231*433d6423SLionel Sambuc if (i >= 0 && i < OPEN_MAX) continue;
232*433d6423SLionel Sambuc if (lseek(i, (off_t) 0, SEEK_SET) != (off_t) - 1) e(21);
233*433d6423SLionel Sambuc if (lseek(i, (off_t) 0, SEEK_END) != (off_t) - 1) e(22);
234*433d6423SLionel Sambuc if (lseek(i, (off_t) 0, SEEK_CUR) != (off_t) - 1) e(23);
235*433d6423SLionel Sambuc }
236*433d6423SLionel Sambuc }
237*433d6423SLionel Sambuc
238