pkgin_all: script to auto-install all packages
[minix.git] / test / test22.c
blobbdefc73a45dc47c981eef8ccd784ae6e3e52243c
1 /* test22: umask() (p) Jan-Mark Wams. email: jms@cs.vu.nl */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <sys/wait.h>
10 #include <stdio.h>
12 #define MAX_ERROR 4 /* Stop after ``MAX_ERROR'' errors. */
13 #define ITERATIONS 2
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 #include "common.c"
21 void test22a(void);
22 int mode(char *filename);
23 int umode(char *filename);
24 void quit(void);
26 int main(int argc, char *argv[])
28 int i, m = 0xFFFF;
30 sync();
31 if (argc == 2) m = atoi(argv[1]);
32 start(22);
34 for (i = 0; i < ITERATIONS; i++) {
35 if (m & 0001) test22a();
38 quit();
40 return (-1); /* Unreachable */
43 void test22a()
45 int fd1, fd2;
46 int i, oldmask;
47 int stat_loc; /* For the wait sys call. */
49 subtest = 1;
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);
72 /* Clean up */
73 if (close(fd1) != 0) e(10);
74 if (close(fd2) != 0) e(11); /* close fd's and */
75 unlink("open"); /* clean the dir */
76 unlink("creat");
77 rmdir("dir");
78 unlink("fifo");
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);
100 /* Clean up */
101 if (close(fd1) != 0) e(22);
102 if (close(fd2) != 0) e(23);
103 if (unlink("open") != 0) e(24);
104 unlink("creat");
105 rmdir("dir");
106 unlink("fifo");
109 /* Check if umask survives a fork() */
110 if (umask(0124) != 0000) e(25);
111 switch (fork()) {
112 case -1: fprintf(stderr, "Can't fork\n"); break;
113 case 0:
114 mkdir("bar", 0777); /* child makes a dir */
115 exit(0);
116 default:
117 if (wait(&stat_loc) == -1) e(26);
119 if (umode("bar") != 0124) e(27);
120 rmdir("bar");
122 /* Check if umask in child changes umask in parent. */
123 switch (fork()) {
124 case -1: fprintf(stderr, "Can't fork\n"); break;
125 case 0:
126 switch (fork()) {
127 case -1:
128 fprintf(stderr, "Can't fork\n");
129 break;
130 case 0:
131 if (umask(0432) != 0124) e(28);
132 exit(0);
133 default:
134 if (wait(&stat_loc) == -1) e(29);
136 if (umask(0423) != 0124) e(30);
137 exit(0);
138 default:
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);
150 int mode(arg)
151 char *arg;
152 { /* return the file mode. */
153 struct stat st;
154 Stat(arg, &st);
155 return st.st_mode & 0777;
158 int umode(arg)
159 char *arg;
160 { /* return the umask used for this file */
161 return 0777 ^ mode(arg);