1 /* test26: lseek() Author: Jan-Mark Wams (jms@cs.vu.nl) */
20 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
21 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
22 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
23 #define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
30 int main(int argc
, char *argv
[])
35 if (argc
== 2) m
= atoi(argv
[1]);
39 for (i
= 0; i
< 10; i
++) {
40 if (m
& 0001) test26a();
41 if (m
& 0002) test26b();
42 if (m
& 0004) test26c();
46 return(-1); /* Unreachable */
50 { /* Test normal operation. */
57 System("rm -rf ../DIR_26/*");
59 System("echo -n hihaho > hihaho");
60 if ((fd
= open("hihaho", O_RDONLY
)) != 3) e(1);
61 if (lseek(fd
, (off_t
) 3, SEEK_SET
) != (off_t
) 3) e(2);
62 if (read(fd
, buf
, 1) != 1) e(3);
63 if (buf
[0] != 'a') e(4);
64 if (lseek(fd
, (off_t
) - 1, SEEK_END
) != 5) e(5);
65 if (read(fd
, buf
, 1) != 1) e(6);
66 if (buf
[0] != 'o') e(7);
68 /* Seek past end of file. */
69 if (lseek(fd
, (off_t
) 1000, SEEK_END
) != 1006) e(8);
70 if (read(fd
, buf
, 1) != 0) e(9);
72 /* Lseek() should not extend the file. */
73 if (fstat(fd
, &st
) != 0) e(10);
74 if (st
.st_size
!= (off_t
) 6) e(11);
75 if (close(fd
) != 0) e(12);
77 /* Probeer lseek met write. */
78 if ((fd
= open("hihaho", O_WRONLY
)) != 3) e(13);
79 if (lseek(fd
, (off_t
) 3, SEEK_SET
) != (off_t
) 3) e(14);
80 if (write(fd
, "e", 1) != 1) e(15);
81 if (lseek(fd
, (off_t
) 1000, SEEK_END
) != 1006) e(16);
83 /* Lseek() should not extend the file. */
84 if (fstat(fd
, &st
) != 0) e(17);
85 if (st
.st_size
!= (off_t
) 6) e(18);
86 if (write(fd
, "e", 1) != 1) e(19);
88 /* Lseek() and a subsequent write should! */
89 if (fstat(fd
, &st
) != 0) e(20);
90 if (st
.st_size
!= (off_t
) 1007) e(21);
92 if (close(fd
) != 0) e(22);
94 /* Check the file, it should start with hiheho. */
95 if ((fd
= open("hihaho", O_RDONLY
)) != 3) e(23);
96 if (read(fd
, buf
, 6) != 6) e(24);
97 if (strncmp(buf
, "hiheho", 6) != 0) e(25);
99 /* The should be zero bytes and a trailing ``e''. */
100 if (sizeof(buf
) < 10) e(26);
101 for (i
= 1; i
<= 20; i
++) {
102 if (read(fd
, buf
, 10) != 10) e(27);
103 for (j
= 0; j
< 10; j
++)
104 if (buf
[j
] != '\0') break;
106 if (lseek(fd
, (off_t
) 15, SEEK_CUR
) != (off_t
) i
* 25 + 6) e(29);
109 if (lseek(fd
, (off_t
) 1006, SEEK_SET
) != (off_t
) 1006) e(30);
110 if (read(fd
, buf
, sizeof(buf
)) != 1) e(31);
111 if (buf
[0] != 'e') e(32);
113 if (lseek(fd
, (off_t
) - 1, SEEK_END
) != (off_t
) 1006) e(33);
114 if (read(fd
, buf
, sizeof(buf
)) != 1) e(34);
115 if (buf
[0] != 'e') e(35);
118 if (close(fd
) != 0) e(36);
127 System("rm -rf ../DIR_26/*");
129 /* See if childs lseek() is effecting the parent. * See also if
130 * lseeking() on same file messes things up. */
132 /* Creat a file of 11 bytes. */
133 if ((fd1
= open("santa", O_WRONLY
| O_CREAT
, 0777)) != 3) e(1);
134 if (write(fd1
, "ho ho ho ho", 11) != 11) e(2);
135 if (close(fd1
) != 0) e(3);
137 /* Open it multiple times. */
138 if ((fd1
= open("santa", O_RDONLY
)) != 3) e(4);
139 if ((fd2
= open("santa", O_WRONLY
)) != 4) e(5);
140 if ((fd3
= open("santa", O_RDWR
)) != 5) e(6);
142 /* Set all offsets different. */
143 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(7);
144 if (lseek(fd2
, (off_t
) 4, SEEK_SET
) != 4) e(8);
145 if (lseek(fd3
, (off_t
) 7, SEEK_SET
) != 7) e(9);
147 /* Have a child process do additional offset changes. */
149 case -1: printf("Can't fork\n"); break;
152 if (lseek(fd1
, (off_t
) 1, SEEK_CUR
) != 3) e(10);
153 if (lseek(fd2
, (off_t
) 5, SEEK_SET
) != 5) e(11);
154 if (lseek(fd3
, (off_t
) - 4, SEEK_END
) != 7) e(12);
158 if (stat_loc
!= 0) e(13); /* Alarm? */
161 /* Check if the new offsets are correct. */
162 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 3) e(14);
163 if (lseek(fd2
, (off_t
) 0, SEEK_CUR
) != 5) e(15);
164 if (lseek(fd3
, (off_t
) 0, SEEK_CUR
) != 7) e(16);
166 /* Close the file. */
167 if (close(fd1
) != 0) e(17);
168 if (close(fd2
) != 0) e(18);
169 if (close(fd3
) != 0) e(19);
173 { /* Test error returns. */
179 System("rm -rf ../DIR_26/*");
181 /* Fifo's can't be lseeked(). */
184 case -1: printf("Can't fork\n"); break;
186 alarm(3); /* Try for max 3 secs. */
187 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(1);
188 if (lseek(fd
, (off_t
) 0, SEEK_SET
) != (off_t
) - 1) e(2);
189 if (errno
!= ESPIPE
) e(3);
190 if (close(fd
) != 0) e(4);
193 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(5);
195 if (stat_loc
!= 0) e(6);/* Alarm? */
196 if (close(fd
) != 0) e(7);
199 /* Pipes can't be lseeked() eigther. */
200 if (pipe(tube
) != 0) e(8);
202 case -1: printf("Can't fork\n"); break;
204 alarm(3); /* Max 3 sconds wait. */
205 if (lseek(tube
[0], (off_t
) 0, SEEK_SET
) != (off_t
) - 1) e(9);
206 if (errno
!= ESPIPE
) e(10);
207 if (lseek(tube
[1], (off_t
) 0, SEEK_SET
) != (off_t
) - 1) e(11);
208 if (errno
!= ESPIPE
) e(12);
212 if (stat_loc
!= 0) e(14); /* Alarm? */
215 /* Close the pipe. */
216 if (close(tube
[0]) != 0) e(15);
217 if (close(tube
[1]) != 0) e(16);
219 /* Whence arument invalid. */
220 System("echo -n contact > file");
221 if ((fd
= open("file", O_RDWR
)) != 3) e(17);
222 for (i
= -1000; i
< 1000; i
++) {
223 if (i
== SEEK_SET
|| i
== SEEK_END
|| i
== SEEK_CUR
) continue;
224 if (lseek(fd
, (off_t
) 0, i
) != (off_t
) -1) e(18);
225 if (errno
!= EINVAL
) e(19);
227 if (close(fd
) != 0) e(20);
229 /* EBADF for bad fides. */
230 for (i
= -1000; i
< 1000; i
++) {
231 if (i
>= 0 && i
< OPEN_MAX
) continue;
232 if (lseek(i
, (off_t
) 0, SEEK_SET
) != (off_t
) - 1) e(21);
233 if (lseek(i
, (off_t
) 0, SEEK_END
) != (off_t
) - 1) e(22);
234 if (lseek(i
, (off_t
) 0, SEEK_CUR
) != (off_t
) - 1) e(23);