1 /* test22: umask() (p) Jan-Mark Wams. email: jms@cs.vu.nl */
12 #define MAX_ERROR 4 /* Stop after ``MAX_ERROR'' errors. */
15 #define System(cmd) if (system(cmd) != 0) printf("``%s'' failed\n", cmd)
16 #define Chdir(dir) if (chdir(dir) != 0) printf("Can't goto %s\n", dir)
17 #define Stat(a,b) if (stat(a,b) != 0) printf("Can't stat %s\n", a)
22 int mode(char *filename
);
23 int umode(char *filename
);
27 int main(int argc
, char *argv
[])
32 if (argc
== 2) m
= atoi(argv
[1]);
35 for (i
= 0; i
< ITERATIONS
; i
++) {
36 if (m
& 0001) test22a();
41 return (-1); /* Unreachable */
48 int stat_loc
; /* For the wait sys call. */
52 system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
53 System("rm -rf ../DIR_22/*");
55 oldmask
= 0123; /* Set oldmask and umask. */
56 umask(oldmask
); /* Set oldmask and umask. */
58 /* Check all the possible values of umask. */
59 for (i
= 0000; i
<= 0777; i
++) {
60 if (oldmask
!= umask(i
)) e(1); /* set umask() */
61 fd1
= open("open", O_CREAT
, 0777);
62 if (fd1
!= 3) e(2); /* test open(), */
63 fd2
= creat("creat", 0777);
64 if (fd2
!= 4) e(3); /* creat(), */
65 if (mkdir("dir", 0777) != 0) e(4); /* mkdir(), */
66 if (mkfifo("fifo", 0777) != 0) e(5); /* and mkfifo(). */
68 if (umode("open") != i
) e(6); /* see if they have */
69 if (umode("creat") != i
) e(7); /* the proper mode */
70 if (umode("dir") != i
) e(8);
71 if (umode("fifo") != i
) e(9);
74 if (close(fd1
) != 0) e(10);
75 if (close(fd2
) != 0) e(11); /* close fd's and */
76 unlink("open"); /* clean the dir */
80 oldmask
= i
; /* save current mask */
83 /* Check-reset mask */
84 if (umask(0124) != 0777) e(12);
86 /* Check if a umask of 0000 leaves the modes alone. */
87 if (umask(0000) != 0124) e(13);
88 for (i
= 0000; i
<= 0777; i
++) {
89 fd1
= open("open", O_CREAT
, i
);
90 if (fd1
!= 3) e(14); /* test open(), */
91 fd2
= creat("creat", i
);
92 if (fd2
!= 4) e(15); /* creat(), */
93 if (mkdir("dir", i
) != 0) e(16); /* mkdir(), */
94 if (mkfifo("fifo", i
) != 0) e(17); /* and mkfifo(). */
96 if (mode("open") != i
) e(18); /* see if they have */
97 if (mode("creat") != i
) e(19); /* the proper mode */
98 if (mode("dir") != i
) e(20);
99 if (mode("fifo") != i
) e(21);
102 if (close(fd1
) != 0) e(22);
103 if (close(fd2
) != 0) e(23);
104 if (unlink("open") != 0) e(24);
110 /* Check if umask survives a fork() */
111 if (umask(0124) != 0000) e(25);
113 case -1: fprintf(stderr
, "Can't fork\n"); break;
115 mkdir("bar", 0777); /* child makes a dir */
118 if (wait(&stat_loc
) == -1) e(26);
120 if (umode("bar") != 0124) e(27);
123 /* Check if umask in child changes umask in parent. */
125 case -1: fprintf(stderr
, "Can't fork\n"); break;
129 fprintf(stderr
, "Can't fork\n");
132 if (umask(0432) != 0124) e(28);
135 if (wait(&stat_loc
) == -1) e(29);
137 if (umask(0423) != 0124) e(30);
140 if (wait(&stat_loc
) == -1) e(31);
142 if (umask(0342) != 0124) e(32);
144 /* See if extra bits are ignored */
145 if (umask(0xFFFF) != 0342) e(33);
146 if (umask(0xFE00) != 0777) e(34);
147 if (umask(01777) != 0000) e(35);
148 if (umask(0022) != 0777) e(36);
153 { /* return the file mode. */
156 return st
.st_mode
& 0777;
161 { /* return the umask used for this file */
162 return 0777 ^ mode(arg
);