3 /* Comment on usage and program: ark!/mnt/rene/prac/os/unix/comment.changes */
5 /* "const.h", created by Rene Montsma and Menno Wilcke */
7 #include <sys/types.h> /* needed in struct stat */
8 #include <sys/stat.h> /* struct stat */
10 #include <errno.h> /* the error-numbers */
20 #define NOCRASH 1 /* test11(), 2nd pipe */
21 #define PDPNOHANG 1 /* test03(), write_standards() */
26 #define FF 3 /* first free filedes. */
27 #define USER 1 /* uid */
28 #define GROUP 0 /* gid */
30 #define ARSIZE 256 /* array size */
31 #define PIPESIZE 3584 /* maxnumber of bytes to be written on pipe */
32 #define MAXOPEN (OPEN_MAX-3) /* maximum number of extra open files */
33 #define MAXLINK 0177 /* maximum number of links per file */
34 #define MASK 0777 /* selects lower nine bits */
35 #define READ_EOF 0 /* returned by read-call at eof */
40 #define R 0 /* read (open-call) */
41 #define W 1 /* write (open-call) */
42 #define RW 2 /* read & write (open-call) */
44 #define RWX 7 /* read & write & execute (mode) */
50 #define WRITEV "writev"
56 #define ACCESS "access"
60 #define UNLINK "unlink"
70 /* "decl.c", created by Rene Montsma and Menno Wilcke */
72 /* Used in open_alot, close_alot */
74 char *fnames
[8] = {"---", "--x", "-w-", "-wx", "r--", "r-x", "rw-", "rwx"},
75 *dir
[8] = {"d---", "d--x", "d-w-", "d-wx", "dr--", "dr-x", "drw-", "drwx"};
77 /* Needed for easy creating and deleting of directories */
79 /* "test.c", created by Rene Montsma and Menno Wilcke */
83 int main(int argc
, char **argv
);
88 void write_standards(int filedes
, char a
[]);
90 void read_standards(int filedes
, char a
[]);
91 void read_more(int filedes
, char a
[]);
93 void try_open(char *fname
, int mode
, int test
);
96 void access_standards(void);
98 static int iovec_is_equal(struct iovec
*x
, struct iovec
*y
, size_t
100 static size_t iovec_setup(int pattern
, struct iovec
*iovec
, char
102 static int power(int base
, int exponent
);
103 void try_access(char *fname
, int mode
, int test
);
104 void make_and_fill_dirs(void);
105 void put_file_in_dir(char *dirname
, int mode
);
106 void init_array(char *a
);
107 void clear_array(char *b
);
108 int comp_array(char *a
, char *b
, int range
);
109 void try_close(int filedes
, char *name
);
110 void try_unlink(char *fname
);
111 void Remove(int fdes
, char *fname
);
112 int get_mode(char *name
);
113 void check(char *scall
, int number
);
116 int close_alot(int number
);
117 void clean_up_the_mess(void);
118 void chmod_8_dirs(int sw
);
120 /*****************************************************************************
122 ****************************************************************************/
123 int main(int argc
, char **argv
)
130 /* Create filenames for MAXOPEN files, the *file[] array. */
131 for(i
= 0; i
< MAXOPEN
; i
++) {
132 if(asprintf(&file
[i
], "file%d", i
) == -1) {
133 fprintf(stderr
, "asprintf failed\n");
143 } else if (child
== 0) {
157 umask(0); /* not honest, but i always forget */
160 make_and_fill_dirs();
171 /* "t1.c" created by Rene Montsma and Menno Wilcke */
173 /*****************************************************************************
175 ****************************************************************************/
178 int oldvalue
, newvalue
, tempvalue
;
183 if ((oldvalue
= umask(0777)) != 0) e(1);
185 /* Special test: only the lower 9 bits (protection bits) may part- *
186 * icipate. ~0777 means: 111 000 000 000. Giving this to umask must*
187 * not change any value. */
189 if ((newvalue
= umask(~0777)) != 0777) e(2);
190 if (oldvalue
== newvalue
) e(3);
192 if ((tempvalue
= umask(0)) != 0) e(4);
194 /* Now test all possible modes of umask on a file */
195 for (newvalue
= MASK
; newvalue
>= 0; newvalue
-= 0111) {
196 tempvalue
= umask(newvalue
);
197 if (tempvalue
!= oldvalue
) {
199 break; /* no use trying more */
200 } else if ((nr
= creat("file01", 0777)) < 0)
203 try_close(nr
, "'file01'");
204 if (get_mode("file01") != (MASK
& ~newvalue
)) e(7);
205 try_unlink("file01");
210 /* The loop has terminated with umask(0) */
211 if ((tempvalue
= umask(0)) != 0) e(8);
214 /*****************************************************************************
216 ****************************************************************************/
220 char a
[ARSIZE
], b
[ARSIZE
];
225 /* Create MAXOPEN files, check filedes */
226 for (n
= 0; n
< MAXOPEN
; n
++) {
227 if (creat(file
[n
], mode
) != FF
+ n
)
230 if (get_mode(file
[n
]) != mode
) e(2);
232 /* Change mode of file to standard mode, we want to *
233 * use a lot (20) of files to be opened later, see *
234 * open_alot(), close_alot(). */
235 if (chmod(file
[n
], 0700) != OK
) e(3);
238 mode
= (mode
+ 0100) % 01000;
241 /* Already twenty files opened; opening another has to fail */
242 if (creat("file02", 0777) != FAIL
) e(4);
244 if (errno
!= EMFILE
) e(5);;
247 /* Close all files: seems blunt, but it isn't because we've *
248 * checked all fd's already */
249 if ((n
= close_alot(MAXOPEN
)) < MAXOPEN
) e(6);
251 /* Creat 1 file twice; check */
252 if ((n
= creat("file02", 0777)) < 0) e(7);
255 if (write(n
, a
, ARSIZE
) != ARSIZE
) e(8);
257 if ((n1
= creat("file02", 0755)) < 0) e(9);
259 /* Fd should be at the top after recreation */
260 if (lseek(n1
, 0L, SEEK_END
) != 0) e(10);
262 /* Try to write on recreated file */
265 if (lseek(n1
, 0L, SEEK_SET
) != 0) e(11);
266 if (write(n1
, a
, ARSIZE
) != ARSIZE
) e(12);
268 /* In order to read we've to close and open again */
269 try_close(n1
, "'file02' (2nd creation)");
270 if ((n1
= open("file02", RW
)) < 0) e(13);
273 if (lseek(n1
, 0L, SEEK_SET
) != 0) e(14);
274 if (read(n1
, b
, ARSIZE
) != ARSIZE
) e(15);
276 if (comp_array(a
, b
, ARSIZE
) != OK
) e(16);
278 if (get_mode("file02") != 0777) e(17);
279 try_close(n1
, "recreated 'file02'");
285 /* Give 'creat' wrong input: dir not searchable */
286 if (creat("drw-/file02", 0777) != FAIL
) e(18);
288 if (errno
!= EACCES
) e(19);
290 /* Dir not writable */
291 if (creat("dr-x/file02", 0777) != FAIL
) e(20);
293 if (errno
!= EACCES
) e(21);
295 /* File not writable */
296 if (creat("drwx/r-x", 0777) != FAIL
) e(22);
298 if (errno
!= EACCES
) e(23);
300 /* Try to creat a dir */
301 if ((n
= creat("dir", 040777)) != FAIL
) {
302 if (fstat(n
, &stbf1
) != OK
) e(24);
303 else if (stbf1
.st_mode
!= (mode_t
) 0100777)
304 /* Cast because mode is negative :-(.
305 * HACK DEBUG FIXME: this appears to duplicate
312 /* We don't consider it to be a bug when creat * does not accept
315 /* File is an existing dir */
316 if (creat("drwx", 0777) != FAIL
) e(26);
318 if (errno
!= EISDIR
) e(27);
321 /*****************************************************************************
323 ****************************************************************************/
333 /* Test write after a CREAT */
334 if ((n
= creat("file03", 0700)) != FF
) e(1);
336 write_standards(n
, a
); /* test simple writes, wrong input too */
337 try_close(n
, "'file03'");
340 /* Test write after an OPEN */
341 if ((n
= open("file03", W
)) < 0) e(2);
343 write_standards(n
, a
); /* test simple writes, wrong input too */
345 /* Test write after a DUP */
346 if ((n1
= dup(n
)) < 0) e(3);
348 write_standards(n1
, a
);
349 try_close(n1
, "duplicated fd 'file03'");
352 /* Remove testfile */
355 /* Test write after a PIPE */
356 if (pipe(fd
) < 0) e(4);
358 write_standards(fd
[1], a
);
359 try_close(fd
[0], "'fd[0]'");
360 try_close(fd
[1], "'fd[1]'");
363 /* Last test: does write check protections ? */
364 if ((n
= open("drwx/r--", R
)) < 0) e(5);
366 if (write(n
, a
, ARSIZE
) != FAIL
) e(6);
368 if (errno
!= EBADF
) e(7);
369 try_close(n
, "'drwx/r--'");
373 void write_standards(filedes
, a
)
378 /* Write must return written account of numbers */
379 if (write(filedes
, a
, ARSIZE
) != ARSIZE
) e(80);
381 /* Try giving 'write' wrong input */
383 if (write(-1, a
, ARSIZE
) != FAIL
) e(81);
385 if (errno
!= EBADF
) e(82);
387 /* Wrong length (illegal) */
389 if (write(filedes
, a
, -ARSIZE
) != FAIL
) e(83);
391 if (errno
!= EINVAL
) e(84);
393 } /* write_standards */
396 /*****************************************************************************
398 ****************************************************************************/
406 /* Test read after creat */
407 if ((n
= creat("file04", 0700)) != FF
) e(1);
409 /* Closing and opening needed before writing */
410 try_close(n
, "'file04'");
411 if ((n
= open("file04", RW
)) < 0) e(2);
415 if (write(n
, a
, ARSIZE
) != ARSIZE
) e(3);
417 if (lseek(n
, 0L, SEEK_SET
) != 0) e(4);
418 read_standards(n
, a
);
421 try_close(n
, "'file04'");
424 /* Test read after OPEN */
425 if ((n
= open("file04", R
)) < 0) e(5);
427 read_standards(n
, a
);
429 try_close(n
, "'file04'");
432 /* Test read after DUP */
433 if ((n
= open("file04", R
)) < 0) e(6);
434 if ((n1
= dup(n
)) < 0) e(7);
436 read_standards(n1
, a
);
438 try_close(n1
, "duplicated fd 'file04'");
441 /* Remove testfile */
444 /* Test read after pipe */
445 if (pipe(fd
) < 0) e(8);
447 if (write(fd
[1], a
, ARSIZE
) != ARSIZE
) {
449 try_close(fd
[1], "'fd[1]'");
451 try_close(fd
[1], "'fd[1]'");
452 read_standards(fd
[0], a
);
454 try_close(fd
[0], "'fd[0]'");
457 /* Last test: try to read a read-protected file */
458 if ((n
= open("drwx/-wx", W
)) < 0) e(10);
460 if (read(n
, a
, ARSIZE
) != FAIL
) e(11);
462 if (errno
!= EBADF
) e(12);
463 try_close(n
, "'/drwx/-wx'");
467 void read_standards(filedes
, a
)
474 if (read(filedes
, b
, ARSIZE
) != ARSIZE
) e(85);
475 else if (comp_array(a
, b
, ARSIZE
) != OK
) e(86);
476 else if (read(filedes
, b
, ARSIZE
) != READ_EOF
) e(87);
478 /* Try giving read wrong input: wrong filedes */
479 if (read(FAIL
, b
, ARSIZE
) != FAIL
) e(88);
481 if (errno
!= EBADF
) e(89);
484 if (read(filedes
, b
, -ARSIZE
) != FAIL
) e(90);
486 if (errno
!= EINVAL
) e(91);
487 } /* read_standards */
489 void read_more(filedes
, a
)
492 /* Separated from read_standards() because the PIPE test * would fail. */
497 if (lseek(filedes
, (long) (ARSIZE
/ 2), SEEK_SET
) != ARSIZE
/ 2) e(92);
500 if (read(filedes
, b
, ARSIZE
) != ARSIZE
/ 2) e(93);
502 for (i
= 0; i
< ARSIZE
/ 2; i
++)
503 if (b
[i
] != a
[(ARSIZE
/ 2) + i
]) e(94);
506 /*****************************************************************************
508 ****************************************************************************/
511 int n
, n1
, mode
, fd
[2];
515 /* Test open after CREAT */
516 if ((n
= creat("file05", 0700)) != FF
) e(1);
518 if ((n1
= open("file05", RW
)) != FF
+ 1) e(2);
519 try_close(n1
, "'file05' (open after creation)");
521 try_close(n
, "'file05'");
522 if ((n
= open("file05", R
)) != FF
) e(3);
524 try_close(n
, "'file05' (open after closing)");
526 /* Remove testfile */
527 try_unlink("file05");
530 /* Test all possible modes, try_open not only opens file (sometimes) *
531 * but closes files too (when opened) */
532 if ((n
= creat("file05", 0700)) < 0) e(6);
534 try_close(n
, "file05");
535 for (mode
= 0; mode
<= 0700; mode
+= 0100) {
536 if (chmod("file05", mode
) != OK
) e(7);
539 try_open("file05", R
, FAIL
);
540 try_open("file05", W
, FAIL
);
541 try_open("file05", RW
, FAIL
);
542 } else if (mode
>= 0200 && mode
<= 0300) {
543 try_open("file05", R
, FAIL
);
544 try_open("file05", W
, FF
);
545 try_open("file05", RW
, FAIL
);
546 } else if (mode
>= 0400 && mode
<= 0500) {
547 try_open("file05", R
, FF
);
548 try_open("file05", W
, FAIL
);
549 try_open("file05", RW
, FAIL
);
551 try_open("file05", R
, FF
);
552 try_open("file05", W
, FF
);
553 try_open("file05", RW
, FF
);
558 /* Test opening existing file */
559 if ((n
= open("drwx/rwx", R
)) < 0) e(8);
560 else { /* test close after DUP */
561 if ((n1
= dup(n
)) < 0) e(9);
563 try_close(n1
, "duplicated fd 'drwx/rwx'");
565 if (read(n1
, b
, ARSIZE
) != FAIL
) e(10);
567 if (errno
!= EBADF
) e(11);
569 if (read(n
, b
, ARSIZE
) == FAIL
) e(12);/* should read an eof */
571 try_close(n
, "'drwx/rwx'");
574 /* Test close after PIPE */
575 if (pipe(fd
) < 0) e(13);
577 try_close(fd
[1], "duplicated fd 'fd[1]'");
579 /* Fd[1] really should be closed now; check */
581 if (read(fd
[0], b
, ARSIZE
) != READ_EOF
) e(14);
582 try_close(fd
[0], "duplicated fd 'fd[0]'");
585 /* Try to open a non-existing file */
586 if (open("non-file", R
) != FAIL
) e(15);
588 if (errno
!= ENOENT
) e(16);
590 /* Dir does not exist */
591 if (open("dzzz/file05", R
) != FAIL
) e(17);
593 if (errno
!= ENOENT
) e(18);
595 /* Dir is not searchable */
596 if ((n
= open("drw-/rwx", R
)) != FAIL
) e(19);
598 if (errno
!= EACCES
) e(20);
600 /* Unlink testfile */
601 try_unlink("file05");
603 /* File is not readable */
604 if (open("drwx/-wx", R
) != FAIL
) e(21);
606 if (errno
!= EACCES
) e(22);
608 /* File is not writable */
609 if (open("drwx/r-x", W
) != FAIL
) e(23);
611 if (errno
!= EACCES
) e(24);
613 /* Try opening more than MAXOPEN ('extra' (19-8-85)) files */
614 if ((n
= open_alot()) != MAXOPEN
) e(25);
616 /* Maximum # of files opened now, another open should fail
617 * because * all filedescriptors have already been used. */
618 if (open("drwx/rwx", RW
) != FAIL
) e(26);
620 if (errno
!= EMFILE
) e(27);
621 if (close_alot(n
) != n
) e(28);
623 /* Can close make mistakes ? */
624 if (close(-1) != FAIL
) e(29);
626 if (errno
!= EBADF
) e(30);
629 void try_open(fname
, mode
, test
)
635 if ((n
= open(fname
, mode
)) != test
) e(95);
636 if (n
!= FAIL
) try_close(n
, fname
); /* cleanup */
639 /*****************************************************************************
641 ****************************************************************************/
644 char a
[ARSIZE
], b
[ARSIZE
];
649 if ((fd
= open("drwx/rwx", RW
)) != FF
) e(1);
652 if (write(fd
, a
, 10) != 10) e(2);
654 /* Lseek back to begin file */
655 if (lseek(fd
, 0L, SEEK_SET
) != 0) e(3);
656 else if (read(fd
, b
, 10) != 10) e(4);
657 else if (comp_array(a
, b
, 10) != OK
) e(5);
659 /* Lseek to endoffile */
660 if (lseek(fd
, 0L, SEEK_END
) != 10) e(6);
661 else if (read(fd
, b
, 1) != READ_EOF
) e(7);
663 /* Lseek beyond file */
664 if (lseek(fd
, 10L, SEEK_CUR
) != 20) e(8);
665 else if (write(fd
, a
, 10) != 10) e(9);
667 /* Lseek to begin second write */
668 if (lseek(fd
, 20L, SEEK_SET
) != 20) e(10);
669 if (read(fd
, b
, 10) != 10) e(11);
670 else if (comp_array(a
, b
, 10) != OK
) e(12);
674 /* Lseek to position before begin of file */
675 if (lseek(fd
, -1L, 0) != FAIL
) e(13);
677 try_close(fd
, "'drwx/rwx'");
680 /* Lseek on invalid filediscriptor */
681 if (lseek(-1, 0L, SEEK_SET
) != FAIL
) e(14);
683 if (errno
!= EBADF
) e(15);
687 /*****************************************************************************
689 ****************************************************************************/
694 /* Check with proper parameters */
695 if (access("drwx/rwx", RWX
) != OK
) e(1);
697 if (access("./././drwx/././rwx", 0) != OK
) e(2);
699 /* Check 8 files with 8 different modes on 8 accesses */
700 if (chdir("drwx") != OK
) e(3);
704 if (chdir("..") != OK
) e(4);
706 /* Check several wrong combinations */
707 /* File does not exist */
708 if (access("non-file", 0) != FAIL
) e(5);
710 if (errno
!= ENOENT
) e(6);
712 /* Non-searchable dir */
713 if (access("drw-/rwx", 0) != FAIL
) e(7);
715 if (errno
!= EACCES
) e(8);
717 /* Searchable dir, but wrong file-mode */
718 if (access("drwx/--x", RWX
) != FAIL
) e(9);
720 if (errno
!= EACCES
) e(10);
724 void access_standards()
728 for (i
= 0; i
< 8; i
++)
730 try_access(fnames
[mode
], i
, OK
);
732 try_access(fnames
[mode
], i
, FAIL
);
735 for (i
= 0; i
< 8; i
++)
737 try_access(fnames
[mode
], i
, OK
);
739 try_access(fnames
[mode
], i
, FAIL
);
742 for (i
= 0; i
< 8; i
++)
743 if (i
== 0 || i
== 2)
744 try_access(fnames
[mode
], i
, OK
);
746 try_access(fnames
[mode
], i
, FAIL
);
749 for (i
= 0; i
< 8; i
++)
751 try_access(fnames
[mode
], i
, OK
);
753 try_access(fnames
[mode
], i
, FAIL
);
756 for (i
= 0; i
< 8; i
++)
757 if (i
== 0 || i
== 4)
758 try_access(fnames
[mode
], i
, OK
);
760 try_access(fnames
[mode
], i
, FAIL
);
763 for (i
= 0; i
< 8; i
++)
764 if (i
== 0 || i
== 1 || i
== 4 || i
== 5)
765 try_access(fnames
[mode
], i
, OK
);
767 try_access(fnames
[mode
], i
, FAIL
);
770 for (i
= 0; i
< 8; i
++)
772 try_access(fnames
[mode
], i
, OK
);
774 try_access(fnames
[mode
], i
, FAIL
);
777 for (i
= 0; i
< 8; i
++) try_access(fnames
[mode
], i
, OK
);
778 } /* access_standards */
780 void try_access(fname
, mode
, test
)
784 if (access(fname
, mode
) != test
) e(96);
788 /* Err, make_and_fill_dirs, init_array, clear_array, comp_array,
789 try_close, try_unlink, Remove, get_mode, check, open_alot,
790 close_alot, clean_up_the_mess.
793 /*****************************************************************************
794 * test READV/WRITEV *
795 ****************************************************************************/
796 #define TEST8_BUFSZCOUNT 3
797 #define TEST8_BUFSZMAX 65536
798 #define TEST8_IOVCOUNT 4
802 char buffer_read
[TEST8_IOVCOUNT
* TEST8_BUFSZMAX
];
803 char buffer_write
[TEST8_IOVCOUNT
* TEST8_BUFSZMAX
];
804 struct iovec iovec_read
[TEST8_IOVCOUNT
];
805 struct iovec iovec_write
[TEST8_IOVCOUNT
];
806 int fd
, i
, j
, k
, l
, m
;
807 ssize_t sz_read
, sz_write
;
808 size_t sz_read_exp
, sz_read_sum
, sz_write_sum
;
812 /* try various combinations of buffer sizes */
813 for (i
= 0; i
<= TEST8_IOVCOUNT
; i
++)
814 for (j
= 0; j
< power(TEST8_BUFSZCOUNT
, i
); j
++)
815 for (k
= 0; k
<= TEST8_IOVCOUNT
; k
++)
816 for (l
= 0; l
< power(TEST8_BUFSZCOUNT
, k
); l
++)
818 /* put data in the buffers */
819 for (m
= 0; m
< sizeof(buffer_write
); m
++)
821 buffer_write
[m
] = m
^ (m
>> 8);
822 buffer_read
[m
] = ~buffer_write
[m
];
825 /* set up the vectors to point to the buffers */
826 sz_read_sum
= iovec_setup(j
, iovec_read
, buffer_read
, i
);
827 sz_write_sum
= iovec_setup(l
, iovec_write
, buffer_write
, k
);
828 sz_read_exp
= (sz_read_sum
< sz_write_sum
) ?
829 sz_read_sum
: sz_write_sum
;
831 /* test reading and writing */
832 if ((fd
= open("file08", O_RDWR
| O_CREAT
| O_TRUNC
, 0644)) < 0) e(1);
834 sz_write
= writev(fd
, iovec_write
, k
);
835 if (sz_write
!= sz_write_sum
) e(2);
836 if (lseek(fd
, 0, SEEK_SET
) != 0) e(3);
837 sz_read
= readv(fd
, iovec_read
, i
);
838 if (sz_read
!= sz_read_exp
) e(4);
840 if (!iovec_is_equal(iovec_read
, iovec_write
, sz_read
))
844 /* Remove testfile */
845 Remove(fd
, "file08");
850 static int iovec_is_equal(struct iovec
*x
, struct iovec
*y
, size_t size
)
852 int xpos
= 0, xvec
= 0, ypos
= 0, yvec
= 0;
854 /* compare byte by byte */
857 /* skip over zero-byte buffers and those that have been completed */
858 while (xpos
>= x
[xvec
].iov_len
)
860 xpos
-= x
[xvec
++].iov_len
;
861 assert(xvec
< TEST8_IOVCOUNT
);
863 while (ypos
>= y
[yvec
].iov_len
)
865 ypos
-= y
[yvec
++].iov_len
;
866 assert(yvec
< TEST8_IOVCOUNT
);
870 if (((char *) x
[xvec
].iov_base
)[xpos
++] !=
871 ((char *) y
[yvec
].iov_base
)[ypos
++])
875 /* no difference found */
879 static size_t iovec_setup(int pattern
, struct iovec
*iovec
, char *buffer
, int count
)
881 static const size_t bufsizes
[TEST8_BUFSZCOUNT
] = { 0, 1, TEST8_BUFSZMAX
};
885 /* the pattern specifies each buffer */
886 for (i
= 0; i
< TEST8_IOVCOUNT
; i
++)
888 iovec
->iov_base
= buffer
;
889 sum
+= iovec
->iov_len
= bufsizes
[pattern
% TEST8_BUFSZCOUNT
];
892 buffer
+= TEST8_BUFSZMAX
;
893 pattern
/= TEST8_BUFSZCOUNT
;
899 static int power(int base
, int exponent
)
903 /* compute base^exponent */
904 while (exponent
-- > 0)
911 /*****************************************************************************
913 * MAKE_AND_FILL_DIRS *
915 *****************************************************************************/
917 void make_and_fill_dirs()
918 /* Create 8 dir.'s: "d---", "d--x", "d-w-", "d-wx", "dr--", "dr-x", *
919 * "drw-", "drwx". * Then create 8 files
920 * in "drwx", and some needed files in other dirs. */
924 for (i
= 0; i
< 8; i
++) {
926 chown(dir
[i
], USER_ID
, GROUP_ID
);
931 for (mode
= 0; mode
< 8; mode
++) put_file_in_dir("drwx", mode
);
933 put_file_in_dir("d-wx", RWX
);
934 put_file_in_dir("dr-x", RWX
);
935 put_file_in_dir("drw-", RWX
);
937 chmod_8_dirs(8); /* 8 means; 8 different modes */
939 } /* make_and_fill_dirs */
941 void put_file_in_dir(dirname
, mode
)
944 /* Fill directory 'dirname' with file with mode 'mode'. */
948 if (chdir(dirname
) != OK
) e(97);
951 if ((nr
= creat(fnames
[mode
], mode
* 0100)) < 0) e(98);
953 try_close(nr
, fnames
[mode
]);
955 if (chdir("..") != OK
) e(99);
957 } /* put_file_in_dir */
959 /*****************************************************************************
963 *(all about arrays, 'try_close', 'try_unlink', 'Remove', 'get_mode') *
965 *****************************************************************************/
973 while (i
++ < ARSIZE
) *a
++ = 'a' + (i
% 26);
982 while (i
++ < ARSIZE
) *b
++ = '0';
986 int comp_array(a
, b
, range
)
990 assert(range
>= 0 && range
<= ARSIZE
);
992 while (range
-- && (*a
++ == *b
++));
999 void try_close(filedes
, name
)
1003 if (close(filedes
) != OK
) e(100);
1006 void try_unlink(fname
)
1009 if (unlink(fname
) != 0) e(101);
1012 void Remove(fdes
, fname
)
1016 try_close(fdes
, fname
);
1025 if (stat(name
, &stbf1
) != OK
) {
1027 return(stbf1
.st_mode
); /* return a mode which will cause *
1028 * error in the calling function *
1031 return(stbf1
.st_mode
& 07777); /* take last 4 bits */
1035 /*****************************************************************************
1039 *****************************************************************************/
1045 for (i
= 0; i
< MAXOPEN
; i
++)
1046 if (open(file
[i
], R
) == FAIL
) break;
1053 int close_alot(number
)
1058 if (number
> MAXOPEN
) e(104);
1060 for (i
= FF
; i
< number
+ FF
; i
++)
1061 if (close(i
) != OK
) count
++;
1063 return(number
- count
); /* return number of closed files */
1066 /*****************************************************************************
1068 * CLEAN UP THE MESS *
1070 *****************************************************************************/
1072 void clean_up_the_mess()
1077 /* First remove 'alot' files */
1078 for (i
= 0; i
< MAXOPEN
; i
++) try_unlink(file
[i
]);
1080 /* Unlink the files in dir 'drwx' */
1081 if (chdir("drwx") != OK
) e(105);
1083 for (i
= 0; i
< 8; i
++) try_unlink(fnames
[i
]);
1084 if (chdir("..") != OK
) e(106);
1087 /* Before unlinking files in some dirs, make them writable */
1090 /* Unlink files in other dirs */
1091 try_unlink("d-wx/rwx");
1092 try_unlink("dr-x/rwx");
1093 try_unlink("drw-/rwx");
1096 for (i
= 0; i
< 8; i
++) {
1097 strcpy(dirname
, "d");
1098 strcat(dirname
, fnames
[i
]);
1099 /* 'dirname' contains the directoryname */
1104 } /* clean_up_the_mess */
1106 void chmod_8_dirs(sw
)
1107 int sw
; /* if switch == 8, give all different
1108 * mode,else the same mode */
1118 for (i
= 0; i
< 8; i
++) {
1119 chmod(dir
[i
], 040000 + mode
* 0100);
1120 if (sw
== 8) mode
++;