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