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
);
26 int main(int argc
, char *argv
[])
31 if (argc
== 2) m
= atoi(argv
[1]);
34 for (i
= 0; i
< ITERATIONS
; i
++) {
35 if (m
& 0001) test22a();
40 return (-1); /* Unreachable */
47 int stat_loc
; /* For the wait sys call. */
51 system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
52 System("rm -rf ../DIR_22/*");
54 oldmask
= 0123; /* Set oldmask and umask. */
55 umask(oldmask
); /* Set oldmask and umask. */
57 /* Check all the possible values of umask. */
58 for (i
= 0000; i
<= 0777; i
++) {
59 if (oldmask
!= umask(i
)) e(1); /* set umask() */
60 fd1
= open("open", O_CREAT
, 0777);
61 if (fd1
!= 3) e(2); /* test open(), */
62 fd2
= creat("creat", 0777);
63 if (fd2
!= 4) e(3); /* creat(), */
64 if (mkdir("dir", 0777) != 0) e(4); /* mkdir(), */
65 if (mkfifo("fifo", 0777) != 0) e(5); /* and mkfifo(). */
67 if (umode("open") != i
) e(6); /* see if they have */
68 if (umode("creat") != i
) e(7); /* the proper mode */
69 if (umode("dir") != i
) e(8);
70 if (umode("fifo") != i
) e(9);
73 if (close(fd1
) != 0) e(10);
74 if (close(fd2
) != 0) e(11); /* close fd's and */
75 unlink("open"); /* clean the dir */
79 oldmask
= i
; /* save current mask */
82 /* Check-reset mask */
83 if (umask(0124) != 0777) e(12);
85 /* Check if a umask of 0000 leaves the modes alone. */
86 if (umask(0000) != 0124) e(13);
87 for (i
= 0000; i
<= 0777; i
++) {
88 fd1
= open("open", O_CREAT
, i
);
89 if (fd1
!= 3) e(14); /* test open(), */
90 fd2
= creat("creat", i
);
91 if (fd2
!= 4) e(15); /* creat(), */
92 if (mkdir("dir", i
) != 0) e(16); /* mkdir(), */
93 if (mkfifo("fifo", i
) != 0) e(17); /* and mkfifo(). */
95 if (mode("open") != i
) e(18); /* see if they have */
96 if (mode("creat") != i
) e(19); /* the proper mode */
97 if (mode("dir") != i
) e(20);
98 if (mode("fifo") != i
) e(21);
101 if (close(fd1
) != 0) e(22);
102 if (close(fd2
) != 0) e(23);
103 if (unlink("open") != 0) e(24);
109 /* Check if umask survives a fork() */
110 if (umask(0124) != 0000) e(25);
112 case -1: fprintf(stderr
, "Can't fork\n"); break;
114 mkdir("bar", 0777); /* child makes a dir */
117 if (wait(&stat_loc
) == -1) e(26);
119 if (umode("bar") != 0124) e(27);
122 /* Check if umask in child changes umask in parent. */
124 case -1: fprintf(stderr
, "Can't fork\n"); break;
128 fprintf(stderr
, "Can't fork\n");
131 if (umask(0432) != 0124) e(28);
134 if (wait(&stat_loc
) == -1) e(29);
136 if (umask(0423) != 0124) e(30);
139 if (wait(&stat_loc
) == -1) e(31);
141 if (umask(0342) != 0124) e(32);
143 /* See if extra bits are ignored */
144 if (umask(0xFFFF) != 0342) e(33);
145 if (umask(0xFE00) != 0777) e(34);
146 if (umask(01777) != 0000) e(35);
147 if (umask(0022) != 0777) e(36);
152 { /* return the file mode. */
155 return st
.st_mode
& 0777;
160 { /* return the umask used for this file */
161 return 0777 ^ mode(arg
);