1 /* test26: lseek() Author: Jan-Mark Wams (jms@cs.vu.nl) */
18 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
19 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
20 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
21 #define Mkfifo(f) if (mkfifo(f,0777)!=0) printf("Can't make fifo %s\n", f)
25 char MaxName
[NAME_MAX
+ 1]; /* Name of maximum length */
26 char MaxPath
[PATH_MAX
]; /* Same for path */
27 char ToLongName
[NAME_MAX
+ 2]; /* Name of maximum +1 length */
28 char ToLongPath
[PATH_MAX
+ 1]; /* Same for path, both too long */
30 _PROTOTYPE(void main
, (int argc
, char *argv
[]));
31 _PROTOTYPE(void test26a
, (void));
32 _PROTOTYPE(void test26b
, (void));
33 _PROTOTYPE(void test26c
, (void));
34 _PROTOTYPE(void e
, (int number
));
35 _PROTOTYPE(void quit
, (void));
44 if (argc
== 2) m
= atoi(argv
[1]);
47 System("rm -rf DIR_26; mkdir DIR_26");
50 for (i
= 0; i
< 10; i
++) {
51 if (m
& 0001) test26a();
52 if (m
& 0002) test26b();
53 if (m
& 0004) test26c();
59 { /* Test normal operation. */
66 System("rm -rf ../DIR_26/*");
68 System("echo -n hihaho > hihaho");
69 if ((fd
= open("hihaho", O_RDONLY
)) != 3) e(1);
70 if (lseek(fd
, (off_t
) 3, SEEK_SET
) != (off_t
) 3) e(2);
71 if (read(fd
, buf
, 1) != 1) e(3);
72 if (buf
[0] != 'a') e(4);
73 if (lseek(fd
, (off_t
) - 1, SEEK_END
) != 5) e(5);
74 if (read(fd
, buf
, 1) != 1) e(6);
75 if (buf
[0] != 'o') e(7);
77 /* Seek past end of file. */
78 if (lseek(fd
, (off_t
) 1000, SEEK_END
) != 1006) e(8);
79 if (read(fd
, buf
, 1) != 0) e(9);
81 /* Lseek() should not extend the file. */
82 if (fstat(fd
, &st
) != 0) e(10);
83 if (st
.st_size
!= (off_t
) 6) e(11);
84 if (close(fd
) != 0) e(12);
86 /* Probeer lseek met write. */
87 if ((fd
= open("hihaho", O_WRONLY
)) != 3) e(13);
88 if (lseek(fd
, (off_t
) 3, SEEK_SET
) != (off_t
) 3) e(14);
89 if (write(fd
, "e", 1) != 1) e(15);
90 if (lseek(fd
, (off_t
) 1000, SEEK_END
) != 1006) e(16);
92 /* Lseek() should not extend the file. */
93 if (fstat(fd
, &st
) != 0) e(17);
94 if (st
.st_size
!= (off_t
) 6) e(18);
95 if (write(fd
, "e", 1) != 1) e(19);
97 /* Lseek() and a subsequent write should! */
98 if (fstat(fd
, &st
) != 0) e(20);
99 if (st
.st_size
!= (off_t
) 1007) e(21);
101 if (close(fd
) != 0) e(22);
103 /* Check the file, it should start with hiheho. */
104 if ((fd
= open("hihaho", O_RDONLY
)) != 3) e(23);
105 if (read(fd
, buf
, 6) != 6) e(24);
106 if (strncmp(buf
, "hiheho", 6) != 0) e(25);
108 /* The should be zero bytes and a trailing ``e''. */
109 if (sizeof(buf
) < 10) e(26);
110 for (i
= 1; i
<= 20; i
++) {
111 if (read(fd
, buf
, 10) != 10) e(27);
112 for (j
= 0; j
< 10; j
++)
113 if (buf
[j
] != '\0') break;
115 if (lseek(fd
, (off_t
) 15, SEEK_CUR
) != (off_t
) i
* 25 + 6) e(29);
118 if (lseek(fd
, (off_t
) 1006, SEEK_SET
) != (off_t
) 1006) e(30);
119 if (read(fd
, buf
, sizeof(buf
)) != 1) e(31);
120 if (buf
[0] != 'e') e(32);
122 if (lseek(fd
, (off_t
) - 1, SEEK_END
) != (off_t
) 1006) e(33);
123 if (read(fd
, buf
, sizeof(buf
)) != 1) e(34);
124 if (buf
[0] != 'e') e(35);
127 if (close(fd
) != 0) e(36);
136 System("rm -rf ../DIR_26/*");
138 /* See if childs lseek() is effecting the parent. * See also if
139 * lseeking() on same file messes things up. */
141 /* Creat a file of 11 bytes. */
142 if ((fd1
= open("santa", O_WRONLY
| O_CREAT
, 0777)) != 3) e(1);
143 if (write(fd1
, "ho ho ho ho", 11) != 11) e(2);
144 if (close(fd1
) != 0) e(3);
146 /* Open it multiple times. */
147 if ((fd1
= open("santa", O_RDONLY
)) != 3) e(4);
148 if ((fd2
= open("santa", O_WRONLY
)) != 4) e(5);
149 if ((fd3
= open("santa", O_RDWR
)) != 5) e(6);
151 /* Set all offsets different. */
152 if (lseek(fd1
, (off_t
) 2, SEEK_SET
) != 2) e(7);
153 if (lseek(fd2
, (off_t
) 4, SEEK_SET
) != 4) e(8);
154 if (lseek(fd3
, (off_t
) 7, SEEK_SET
) != 7) e(9);
156 /* Have a child process do additional offset changes. */
158 case -1: printf("Can't fork\n"); break;
161 if (lseek(fd1
, (off_t
) 1, SEEK_CUR
) != 3) e(10);
162 if (lseek(fd2
, (off_t
) 5, SEEK_SET
) != 5) e(11);
163 if (lseek(fd3
, (off_t
) - 4, SEEK_END
) != 7) e(12);
167 if (stat_loc
!= 0) e(13); /* Alarm? */
170 /* Check if the new offsets are correct. */
171 if (lseek(fd1
, (off_t
) 0, SEEK_CUR
) != 3) e(14);
172 if (lseek(fd2
, (off_t
) 0, SEEK_CUR
) != 5) e(15);
173 if (lseek(fd3
, (off_t
) 0, SEEK_CUR
) != 7) e(16);
175 /* Close the file. */
176 if (close(fd1
) != 0) e(17);
177 if (close(fd2
) != 0) e(18);
178 if (close(fd3
) != 0) e(19);
182 { /* Test error returns. */
188 System("rm -rf ../DIR_26/*");
190 /* Fifo's can't be lseeked(). */
193 case -1: printf("Can't fork\n"); break;
195 alarm(3); /* Try for max 3 secs. */
196 if ((fd
= open("fifo", O_RDONLY
)) != 3) e(1);
197 if (lseek(fd
, (off_t
) 0, SEEK_SET
) != (off_t
) - 1) e(2);
198 if (errno
!= ESPIPE
) e(3);
199 if (close(fd
) != 0) e(4);
202 if ((fd
= open("fifo", O_WRONLY
)) != 3) e(5);
204 if (stat_loc
!= 0) e(6);/* Alarm? */
205 if (close(fd
) != 0) e(7);
208 /* Pipes can't be lseeked() eigther. */
209 if (pipe(tube
) != 0) e(8);
211 case -1: printf("Can't fork\n"); break;
213 alarm(3); /* Max 3 sconds wait. */
214 if (lseek(tube
[0], (off_t
) 0, SEEK_SET
) != (off_t
) - 1) e(9);
215 if (errno
!= ESPIPE
) e(10);
216 if (lseek(tube
[1], (off_t
) 0, SEEK_SET
) != (off_t
) - 1) e(11);
217 if (errno
!= ESPIPE
) e(12);
221 if (stat_loc
!= 0) e(14); /* Alarm? */
224 /* Close the pipe. */
225 if (close(tube
[0]) != 0) e(15);
226 if (close(tube
[1]) != 0) e(16);
228 /* Whence arument invalid. */
229 System("echo -n contact > file");
230 if ((fd
= open("file", O_RDWR
)) != 3) e(17);
231 for (i
= -1000; i
< 1000; i
++) {
232 if (i
== SEEK_SET
|| i
== SEEK_END
|| i
== SEEK_CUR
) continue;
233 if (lseek(fd
, (off_t
) 0, i
) != (off_t
) -1) e(18);
234 if (errno
!= EINVAL
) e(19);
236 if (close(fd
) != 0) e(20);
238 /* EBADF for bad fides. */
239 for (i
= -1000; i
< 1000; i
++) {
240 if (i
>= 0 && i
< OPEN_MAX
) continue;
241 if (lseek(i
, (off_t
) 0, SEEK_SET
) != (off_t
) - 1) e(21);
242 if (lseek(i
, (off_t
) 0, SEEK_END
) != (off_t
) - 1) e(22);
243 if (lseek(i
, (off_t
) 0, SEEK_CUR
) != (off_t
) - 1) e(23);
250 int err_num
= errno
; /* Save in case printf clobbers it. */
252 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
255 if (errct
++ > MAX_ERROR
) {
256 printf("Too many errors; test aborted\n");
258 system("rm -rf DIR*");
267 System("rm -rf DIR_26");
273 printf("%d errors\n", errct
);