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)
19 int errct
= 0; /* Total error counter. */
22 _PROTOTYPE(void main
, (int argc
, char *argv
[]));
23 _PROTOTYPE(void test22a
, (void));
24 _PROTOTYPE(int mode
, (char *filename
));
25 _PROTOTYPE(int umode
, (char *filename
));
26 _PROTOTYPE(void e
, (int number
));
27 _PROTOTYPE(void quit
, (void));
36 if (argc
== 2) m
= atoi(argv
[1]);
39 system("chmod 777 DIR_22/* DIR_22/*/* > /dev/null 2>&1");
40 System("rm -rf DIR_22; mkdir DIR_22");
43 for (i
= 0; i
< ITERATIONS
; i
++) {
44 if (m
& 0001) test22a();
54 int stat_loc
; /* For the wait sys call. */
58 system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
59 System("rm -rf ../DIR_22/*");
61 oldmask
= 0123; /* Set oldmask and umask. */
62 umask(oldmask
); /* Set oldmask and umask. */
64 /* Check all the possible values of umask. */
65 for (i
= 0000; i
<= 0777; i
++) {
66 if (oldmask
!= umask(i
)) e(1); /* set umask() */
67 fd1
= open("open", O_CREAT
, 0777);
68 if (fd1
!= 3) e(2); /* test open(), */
69 fd2
= creat("creat", 0777);
70 if (fd2
!= 4) e(3); /* creat(), */
71 if (mkdir("dir", 0777) != 0) e(4); /* mkdir(), */
72 if (mkfifo("fifo", 0777) != 0) e(5); /* and mkfifo(). */
74 if (umode("open") != i
) e(6); /* see if they have */
75 if (umode("creat") != i
) e(7); /* the proper mode */
76 if (umode("dir") != i
) e(8);
77 if (umode("fifo") != i
) e(9);
80 if (close(fd1
) != 0) e(10);
81 if (close(fd2
) != 0) e(11); /* close fd's and */
82 unlink("open"); /* clean the dir */
86 oldmask
= i
; /* save current mask */
89 /* Check-reset mask */
90 if (umask(0124) != 0777) e(12);
92 /* Check if a umask of 0000 leaves the modes alone. */
93 if (umask(0000) != 0124) e(13);
94 for (i
= 0000; i
<= 0777; i
++) {
95 fd1
= open("open", O_CREAT
, i
);
96 if (fd1
!= 3) e(14); /* test open(), */
97 fd2
= creat("creat", i
);
98 if (fd2
!= 4) e(15); /* creat(), */
99 if (mkdir("dir", i
) != 0) e(16); /* mkdir(), */
100 if (mkfifo("fifo", i
) != 0) e(17); /* and mkfifo(). */
102 if (mode("open") != i
) e(18); /* see if they have */
103 if (mode("creat") != i
) e(19); /* the proper mode */
104 if (mode("dir") != i
) e(20);
105 if (mode("fifo") != i
) e(21);
108 if (close(fd1
) != 0) e(22);
109 if (close(fd2
) != 0) e(23);
110 if (unlink("open") != 0) e(24);
116 /* Check if umask survives a fork() */
117 if (umask(0124) != 0000) e(25);
119 case -1: fprintf(stderr
, "Can't fork\n"); break;
121 mkdir("bar", 0777); /* child makes a dir */
124 if (wait(&stat_loc
) == -1) e(26);
126 if (umode("bar") != 0124) e(27);
129 /* Check if umask in child changes umask in parent. */
131 case -1: fprintf(stderr
, "Can't fork\n"); break;
135 fprintf(stderr
, "Can't fork\n");
138 if (umask(0432) != 0124) e(28);
141 if (wait(&stat_loc
) == -1) e(29);
143 if (umask(0423) != 0124) e(30);
146 if (wait(&stat_loc
) == -1) e(31);
148 if (umask(0342) != 0124) e(32);
150 /* See if extra bits are ignored */
151 if (umask(0xFFFF) != 0342) e(33);
152 if (umask(0xFE00) != 0777) e(34);
153 if (umask(01777) != 0000) e(35);
154 if (umask(0022) != 0777) e(36);
159 { /* return the file mode. */
162 return st
.st_mode
& 0777;
167 { /* return the umask used for this file */
168 return 0777 ^ mode(arg
);
174 int err_num
= errno
; /* Save in case printf clobbers it. */
176 printf("Subtest %d, error %d errno=%d: ", subtest
, n
, errno
);
179 if (errct
++ > MAX_ERROR
) {
180 printf("Too many errors; test aborted\n");
182 system("rm -rf DIR*");
191 system("chmod 777 ../DIR_22/* ../DIR_22/*/* > /dev/null 2>&1");
192 System("rm -rf DIR_22");
198 printf("%d errors\n", errct
);