14 int errct
, subtest
, passes
, pipesigs
;
18 char b
[4] = {0, 1, 2, 3}, c
[4] = {10, 20, 30, 40}, d
[4] = {6, 7, 8, 9};
19 long bounds
[NBOUNDS
] = {7, 9, 50, 519, 520, 40000L};
22 _PROTOTYPE(int main
, (int argc
, char *argv
[]));
23 _PROTOTYPE(void test19a
, (void));
24 _PROTOTYPE(void test19b
, (void));
25 _PROTOTYPE(void test19c
, (void));
26 _PROTOTYPE(void test19d
, (void));
27 _PROTOTYPE(void test19e
, (void));
28 _PROTOTYPE(void test19f
, (void));
29 _PROTOTYPE(void test19g
, (void));
30 _PROTOTYPE(void clraa
, (void));
31 _PROTOTYPE(void pipecatcher
, (int s
));
32 _PROTOTYPE(void e
, (int n
));
33 _PROTOTYPE(void quit
, (void));
41 m
= (argc
== 2 ? atoi(argv
[1]) : 0xFFFF);
43 if (geteuid() == 0 || getuid() == 0) {
44 printf("Test 19 cannot run as root; test aborted\n");
48 system("rm -rf DIR_19; mkdir DIR_19");
53 for (i
= 0; i
< 4; i
++) {
54 if (m
& 0001) test19a();
55 if (m
& 0002) test19b();
56 if (m
& 0004) test19c();
57 if (m
& 0010) test19d();
58 if (m
& 0020) test19e();
59 if (m
& 0040) test19f();
60 if (m
& 0100) test19g();
64 return(-1); /* impossible */
69 /* Test open with O_CREAT and O_EXCL. */
75 if ( (fd
= creat("T19.a1", 0777)) != 3) e(1); /* create test file */
76 if (close(fd
) != 0) e(2);
77 if ( (fd
= open("T19.a1", O_RDONLY
)) != 3) e(3);
78 if (close(fd
) != 0) e(4);
79 if ( (fd
= open("T19.a1", O_WRONLY
)) != 3) e(5);
80 if (close(fd
) != 0) e(6);
81 if ( (fd
= open("T19.a1", O_RDWR
)) != 3) e(7);
82 if (close(fd
) != 0) e(8);
84 /* See if O_CREAT actually creates a file. */
85 if ( (fd
= open("T19.a2", O_RDONLY
)) != -1) e(9); /* must fail */
86 if ( (fd
= open("T19.a2", O_RDONLY
| O_CREAT
, 0444)) != 3) e(10);
87 if (close(fd
) != 0) e(11);
88 if ( (fd
= open("T19.a2", O_RDONLY
)) != 3) e(12);
89 if (close(fd
) != 0) e(13);
90 if ( (fd
= open("T19.a2", O_WRONLY
)) != -1) e(14);
91 if ( (fd
= open("T19.a2", O_RDWR
)) != -1) e(15);
93 /* See what O_CREAT does on an existing file. */
94 if ( (fd
= open("T19.a2", O_RDONLY
| O_CREAT
, 0777)) != 3) e(16);
95 if (close(fd
) != 0) e(17);
96 if ( (fd
= open("T19.a2", O_RDONLY
)) != 3) e(18);
97 if (close(fd
) != 0) e(19);
98 if ( (fd
= open("T19.a2", O_WRONLY
)) != -1) e(20);
99 if ( (fd
= open("T19.a2", O_RDWR
)) != -1) e(21);
101 /* See if O_EXCL works. */
102 if ( (fd
= open("T19.a2", O_RDONLY
| O_EXCL
)) != 3) e(22);
103 if (close(fd
) != 0) e(23);
104 if ( (fd
= open("T19.a2", O_WRONLY
| O_EXCL
)) != -1) e(24);
105 if ( (fd
= open("T19.a3", O_RDONLY
| O_EXCL
)) != -1) e(25);
106 if ( (fd
= open("T19.a3", O_RDONLY
| O_CREAT
| O_EXCL
, 0444)) != 3) e(26);
107 if (close(fd
) != 0) e(27);
109 if ( (fd
= open("T19.a3", O_RDONLY
| O_CREAT
| O_EXCL
, 0444)) != -1) e(28);
110 if (errno
!= EEXIST
) e(29);
112 if (unlink("T19.a1") != 0) e(30);
113 if (unlink("T19.a2") != 0) e(31);
114 if (unlink("T19.a3") != 0) e(32);
119 /* Test open with O_APPEND and O_TRUNC. */
125 if ( (fd
= creat("T19.b1", 0777)) != 3) e(1); /* create test file */
126 if (write(fd
, b
, 4) != 4) e(2);
127 if (close(fd
) != 0) e(3);
129 if ( (fd
= open("T19.b1", O_RDWR
| O_APPEND
)) != 3) e(4);
130 if (read(fd
, aa
, 100) != 4) e(5);
131 if (aa
[0] != 0 || aa
[1] != 1 || aa
[2] != 2 || aa
[3] != 3) e(6);
132 if (close(fd
) != 0) e(7);
133 if ( (fd
= open("T19.b1", O_RDWR
| O_APPEND
)) != 3) e(8);
134 if (write(fd
, b
, 4) != 4) e(9);
135 if (lseek(fd
, 0L, SEEK_SET
) != 0L) e(10);
137 if (read(fd
, aa
, 100) != 8) e(11);
138 if (aa
[4] != 0 || aa
[5] != 1 || aa
[6] != 2 || aa
[7] != 3) e(12);
139 if (close(fd
) != 0) e(13);
141 if ( (fd
= open("T19.b1", O_RDWR
| O_TRUNC
)) != 3) e(14);
142 if (read(fd
, aa
, 100) != 0) e(15);
143 if (close(fd
) != 0) e(16);
150 /* Test program for open(), close(), creat(), read(), write(), lseek(). */
155 if ((n
= creat("foop", 0777)) != 3) e(1);
156 if ((n1
= creat("foop", 0777)) != 4) e(2);
157 if ((n2
= creat("/", 0777)) != -1) e(3);
158 if (close(n
) != 0) e(4);
159 if ((n
= open("foop", O_RDONLY
)) != 3) e(5);
160 if ((n2
= open("nofile", O_RDONLY
)) != -1) e(6);
161 if (close(n1
) != 0) e(7);
163 /* N is the only one open now. */
164 for (i
= 0; i
< 2; i
++) {
165 n1
= creat("File2", 0777);
167 printf("creat yielded fd=%d, expected 4\n", n1
);
170 if ((n2
= open("File2", O_RDONLY
)) != 5) e(9);
171 if (close(n1
) != 0) e(10);
172 if (close(n2
) != 0) e(11);
175 if (close(n
) != 0) e(12);
177 /* All files closed now. */
178 for (i
= 0; i
< 2; i
++) {
179 if ((n
= creat("foop", 0777)) != 3) e(13);
180 if (close(n
) != 0) e(14);
181 if ((n
= open("foop", O_RDWR
)) != 3) e(15);
183 /* Read/write tests */
184 if (write(n
, b
, 4) != 4) e(16);
185 if (read(n
, aa
, 4) != 0) e(17);
186 if (lseek(n
, 0L, SEEK_SET
) != 0L) e(18);
187 if (read(n
, aa
, 4) != 4) e(19);
188 if (aa
[0] != 0 || aa
[1] != 1 || aa
[2] != 2 || aa
[3] != 3) e(20);
189 if (lseek(n
, 0L, SEEK_SET
) != 0L) e(21);
190 if (lseek(n
, 2L, SEEK_CUR
) != 2L) e(22);
191 if (read(n
, aa
, 4) != 2) e(23);
192 if (aa
[0] != 2 || aa
[1] != 3 || aa
[2] != 2 || aa
[3] != 3) e(24);
193 if (lseek(n
, 2L, SEEK_SET
) != 2L) e(25);
195 if (write(n
, c
, 4) != 4) e(26);
196 if (lseek(n
, 0L, SEEK_SET
) != 0L) e(27);
197 if (read(n
, aa
, 10) != 6) e(28);
198 if (aa
[0] != 0 || aa
[1] != 1 || aa
[2] != 10 || aa
[3] != 20) e(29);
199 if (lseek(n
, 16L, SEEK_SET
) != 16L) e(30);
200 if (lseek(n
, 2040L, SEEK_END
) != 2046L) e(31);
201 if (read(n
, aa
, 10) != 0) e(32);
202 if (lseek(n
, 0L, SEEK_CUR
) != 2046L) e(33);
204 if (write(n
, c
, 4) != 4) e(34);
205 if (lseek(n
, 0L, SEEK_CUR
) != 2050L) e(35);
206 if (lseek(n
, 2040L, SEEK_SET
) != 2040L) e(36);
208 if (read(n
, aa
, 20) != 10) e(37);
209 if (aa
[0] != 0 || aa
[5] != 0 || aa
[6] != 10 || aa
[9] != 40) e(38);
210 if (lseek(n
, 10239L, SEEK_SET
) != 10239L) e(39);
211 if (write(n
, d
, 2) != 2) e(40);
212 if (lseek(n
, -2L, SEEK_END
) != 10239L) e(41);
213 if (read(n
, aa
, 2) != 2) e(42);
214 if (aa
[0] != 6 || aa
[1] != 7) e(43);
215 if (lseek(n
, NB
* 1024L - 2L, SEEK_SET
) != NB
* 1024L - 2L) e(44);
216 if (write(n
, b
, 4) != 4) e(45);
217 if (lseek(n
, 0L, SEEK_SET
) != 0L) e(46);
218 if (lseek(n
, -6L, SEEK_END
) != 1024L * NB
- 4) e(47);
220 if (read(n
, aa
, 100) != 6) e(48);
221 if (aa
[0] != 0 || aa
[1] != 0 || aa
[3] != 1 || aa
[4] != 2|| aa
[5] != 3)
223 if (lseek(n
, 20000L, SEEK_SET
) != 20000L) e(50);
224 if (write(n
, c
, 4) != 4) e(51);
225 if (lseek(n
, -4L, SEEK_CUR
) != 20000L) e(52);
226 if (read(n
, aa
, 4) != 4) e(53);
227 if (aa
[0] != 10 || aa
[1] != 20 || aa
[2] != 30 || aa
[3] != 40) e(54);
228 if (close(n
) != 0) e(55);
229 if ((n1
= creat("foop", 0777)) != 3) e(56);
230 if (close(n1
) != 0) e(57);
245 for (i
= 0; i
< 100; i
++) bb
[i
] = i
;
246 if ( (fd
= creat("T19.d1", 0777)) != 3) e(1); /* create test file */
247 if (write(fd
, bb
, 100) != 100) e(2);
248 if (close(fd
) != 0) e(3);
250 if ( (fd
= open("T19.d1", O_RDONLY
)) != 3) e(4);
252 if (read(fd
, aa
, 0) != 0) e(5);
253 if (errno
!= 1000) e(6);
254 if (read(fd
, aa
, 100) != 100) e(7);
255 if (lseek(fd
, 37L, SEEK_SET
) != 37L) e(8);
256 if (read(fd
, aa
, 10) != 10) e(9);
257 if (lseek(fd
, 0L, SEEK_CUR
) != 47L) e(10);
258 if (read(fd
, aa
, 100) != 53) e(11);
259 if (aa
[0] != 47) e(12);
260 if (read(fd
, aa
, 1) != 0) e(13);
261 if (close(fd
) != 0) e(14);
263 /* Read from pipe with no writer open. */
264 if (pipe(pd
) != 0) e(15);
265 if (close(pd
[1]) != 0) e(16);
267 if (read(pd
[0], aa
, 1) != 0) e(17); /* must return EOF */
268 if (errno
!= 2000) e(18);
270 /* Read from a pipe with O_NONBLOCK set. */
271 if (fcntl(pd
[0], F_SETFL
, O_NONBLOCK
) != 0) e(19); /* set O_NONBLOCK */
273 if (read(pd[0], aa, 1) != -1) e(20);
274 if (errno != EAGAIN) e(21);
276 if (close(pd
[0]) != 0) e(19);
277 if (unlink("T19.d1") != 0) e(20);
282 /* Test link, unlink, stat, fstat, dup, umask. */
284 int i
, j
, n
, n1
, flag
;
289 for (i
= 0; i
< 2; i
++) {
292 if ((n
= creat("T3", 0702)) < 0) e(1);
293 if (link("T3", "newT3") < 0) e(2);
294 if ((n1
= open("newT3", O_RDWR
)) < 0) e(3);
295 for (j
= 0; j
< 255; j
++) a
[j
] = j
;
296 if (write(n
, a
, 255) != 255) e(4);
297 if (read(n1
, b
, 255) != 255) e(5);
299 for (j
= 0; j
< 255; j
++)
300 if (a
[j
] != b
[j
]) flag
++;
302 if (unlink("T3") < 0) e(7);
303 if (close(n
) < 0) e(8);
304 if (close(n1
) < 0) e(9);
305 if ((n1
= open("newT3", O_RDONLY
)) < 0) e(10);
306 if (read(n1
, b
, 255) != 255) e(11);
308 for (j
= 0; j
< 255; j
++)
309 if (a
[j
] != b
[j
]) flag
++;
312 /* Now check out stat, fstat. */
313 if (stat("newT3", &s
) < 0) e(13);
314 if (s
.st_mode
!= (mode_t
) 0100702) e(14);
315 /* The cast was because regular modes are
316 * negative :-(. Anyway, the magic number
317 * should be (S_IFREG | S_IRWXU | S_IWOTH)
320 if (s
.st_nlink
!= 1) e(15);
321 if (s
.st_size
!= 255L) e(16);
322 if (fstat(n1
, &s1
) < 0) e(17);
323 if (s
.st_dev
!= s1
.st_dev
) e(18);
324 if (s
.st_ino
!= s1
.st_ino
) e(19);
325 if (s
.st_mode
!= s1
.st_mode
) e(20);
326 if (s
.st_nlink
!= s1
.st_nlink
) e(21);
327 if (s
.st_uid
!= s1
.st_uid
) e(22);
328 if (s
.st_gid
!= s1
.st_gid
) e(23);
329 if (s
.st_rdev
!= s1
.st_rdev
) e(24);
330 if (s
.st_size
!= s1
.st_size
) e(25);
331 if (s
.st_atime
!= s1
.st_atime
) e(26);
332 if (close(n1
) < 0) e(27);
333 if (unlink("newT3") < 0) e(28);
336 if ((n
= creat("T3a", 0777)) < 0) e(29);
337 if (stat("T3a", &s
) < 0) e(30);
338 if (s
.st_mode
!= (mode_t
) 0100737) e(31); /* negative :-( */
339 if (unlink("T3a") < 0) e(32);
340 if (close(n1
) < 0) e(33);
343 if ((n
= creat("T3b", 0777)) < 0) e(34);
344 if (close(n
) < 0) e(35);
345 if ((n
= open("T3b", O_RDWR
)) < 0) e(36);
346 if ((n1
= dup(n
)) != n
+ 1) e(37);
347 if (write(n
, a
, 255) != 255) e(38);
349 if (lseek(n
, 0L, SEEK_SET
) != 0L) e(39);
350 if ((j
= read(n1
, b
, 512)) != 255) e(40);
351 if (unlink("T3b") < 0) e(41);
352 if (close(n
) < 0) e(42);
353 if (close(n1
) < 0) e(43);
360 /* Test large files to see if indirect block stuff works. */
367 if (passes
> 0) return; /* takes too long to repeat this test */
368 for (i
= 0; i
< NBOUNDS
; i
++) {
369 pos
= 1024L * bounds
[i
];
370 fd
= creat("T19f", 0777);
371 if (fd
< 0) e(10*i
+1);
372 if (lseek(fd
, pos
, 0) < 0) e(10*i
+2);
373 if (write(fd
, buff
, 30720) != 30720) e(10*i
+3);
374 if (close(fd
) < 0) e(10*i
+3);
375 if (unlink("T19f") < 0) e(10*i
+4);
381 /* Test POSIX calls for pipe, read, write, lseek and close. */
383 int pipefd
[2], n
, i
, fd
;
384 char buf
[512], buf2
[512];
388 for (i
= 0; i
< 512; i
++) buf
[i
] = i
% 128;
390 if (pipe(pipefd
) < 0) e(1);
391 if (write(pipefd
[1], buf
, 512) != 512) e(2);
392 if (read(pipefd
[0], buf2
, 512) != 512) e(3);
393 if (close(pipefd
[1]) != 0) e(4);
394 if (close(pipefd
[1]) >= 0) e(5);
395 if (read(pipefd
[0], buf2
, 1) != 0) e(6);
396 if (close(pipefd
[0]) != 0) e(7);
398 /* Test O_NONBLOCK on pipes. */
399 if (pipe(pipefd
) < 0) e(8);
400 if (fcntl(pipefd
[0], F_SETFL
, O_NONBLOCK
) != 0) e(9);
401 if (read(pipefd
[0], buf2
, 1) != -1) e(10);
402 if (errno
!= EAGAIN
) e(11);
403 if (close(pipefd
[0]) != 0) e(12);
404 if (close(pipefd
[1]) != 0) e(13);
406 /* Test read and lseek. */
407 if ( (fd
= creat("T19.g1", 0777)) != 3) e(14); /* create test file */
408 if (write(fd
, buf
, 512) != 512) e(15);
410 if (read(fd
, buf2
, 512) != -1) e(16);
411 if (errno
!= EBADF
) e(17);
412 if (close(fd
) != 0) e(18);
413 if ( (fd
= open("T19.g1", O_RDWR
)) != 3) e(19);
414 if (read(fd
, buf2
, 512) != 512) e(20);
415 if (read(fd
, buf2
, 512) != 0) e(21);
416 if (lseek(fd
, 100L, SEEK_SET
) != 100L) e(22);
417 if (read(fd
, buf2
, 512) != 412) e(23);
418 if (lseek(fd
, 1000L, SEEK_SET
) != 1000L) e(24);
421 if (lseek(fd
, -1000L, SEEK_CUR
) != 0) e(25);
422 if (write(fd
, buf
, 512) != 512) e(26);
423 if (lseek(fd
, 2L, SEEK_SET
) != 2) e(27);
424 if (write(fd
, buf
, 3) != 3) e(28);
425 if (lseek(fd
, -2L, SEEK_CUR
) != 3) e(29);
426 if (write(fd
, &buf
[30], 1) != 1) e(30);
427 if (lseek(fd
, 2L, SEEK_CUR
) != 6) e(31);
428 if (write(fd
, &buf
[60], 1) != 1) e(32);
429 if (lseek(fd
, -512L, SEEK_END
) != 0) e(33);
430 if (read(fd
, buf2
, 8) != 8) e(34);
432 if (buf2
[0] != 0 || buf2
[1] != 1 || buf2
[2] != 0 || buf2
[3] != 30) e(35);
433 if (buf2
[4] != 2 || buf2
[5] != 5 || buf2
[6] != 60 || buf2
[7] != 7) e(36);
435 /* Turn the O_APPEND flag on. */
436 if (fcntl(fd
, F_SETFL
, O_APPEND
) != 0) e(37);
437 if (lseek(fd
, 0L, SEEK_SET
) != 0) e(38);
438 if (write(fd
, &buf
[100], 1) != 1) e(39);
439 if (lseek(fd
, 0L, SEEK_SET
) != 0) e(40);
440 if (read(fd
, buf2
, 10) != 10) e(41);
441 if (buf2
[0] != 0) e(42);
442 if (lseek(fd
, -1L, SEEK_END
) != 512) e(43);
443 if (read(fd
, buf2
, 10) != 1) e(44);
444 if (buf2
[0] != 100) e(45);
445 if (close(fd
) != 0) e(46);
447 /* Now try write with O_NONBLOCK. */
448 if (pipe(pipefd
) != 0) e(47);
449 if (fcntl(pipefd
[1], F_SETFL
, O_NONBLOCK
) != 0) e(48);
450 if (write(pipefd
[1], buf
, 512) != 512) e(49);
451 if (write(pipefd
[1], buf
, 512) != 512) e(50);
453 for (i
= 1; i
< 20; i
++) {
454 n
= write(pipefd
[1], buf
, 512);
455 if (n
== 512) continue;
456 if (n
!= -1 || errno
!= EAGAIN
) {e(51); break;}
458 if (read(pipefd
[0], buf
, 512) != 512) e(52);
459 if (close(pipefd
[0]) != 0) e(53);
461 /* Write to a pipe with no reader. This should generate a signal. */
462 signal(SIGPIPE
, pipecatcher
);
464 if (write(pipefd
[1], buf
, 1) != -1) e(54);
465 if (errno
!= EPIPE
) e(55);
466 if (pipesigs
!= passes
+ 1) e(56); /* we should have had the sig now */
467 if (close(pipefd
[1]) != 0) e(57);
469 if (write(100, buf
, 512) != -1) e(58);
470 if (errno
!= EBADF
) e(59);
471 if (unlink("T19.g1") != 0) e(60);
477 for (i
= 0; i
< 100; i
++) aa
[i
] = 0;
481 int s
; /* it is supposed to have an arg */
489 int err_num
= errno
; /* save errno in case printf clobbers it */
491 printf("Subtest %d, error %d errno=%d ", subtest
, n
, errno
);
492 fflush(stdout
); /* aargh! Most results go to stdout and are
493 * messed up by perror going to stderr.
494 * Should replace perror by printf and strerror
497 errno
= err_num
; /* restore errno, just in case */
499 if (errct
++ > MAX_ERROR
) {
500 printf("Too many errors; test aborted\n");
502 system("rm -rf DIR*");
511 system("rm -rf DIR*");
517 printf("%d errors\n", errct
);