sort: add -x hex sort feature back
[minix.git] / test / test16.c
blob2f174479fdb6f00f91185433f75ff885ce034c67
1 /* test 16 */
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <errno.h>
6 #include <fcntl.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <utime.h>
10 #include <stdio.h>
12 #define MAX_ERROR 4
14 int subtest, passes;
16 #include "common.c"
18 int main(int argc, char *argv []);
19 void test16a(void);
20 void get_times(char *name, time_t *a, time_t *c, time_t *m);
22 int main(argc, argv)
23 int argc;
24 char *argv[];
26 int i, m;
28 start(16);
30 m = (argc == 2 ? atoi(argv[1]) : 0xFFFF);
33 for (i = 0; i < 4; i++) {
34 if (m & 0001) test16a();
35 passes++;
37 quit();
38 return(-1); /* impossible */
41 void test16a()
43 /* Test atime, ctime, and mtime. */
45 int fd, fd1, fd2, fd3, fd4;
46 time_t a, c, m, pa, pc, pm, xa, xc, xm, ya, yc, ym, za, zc, zm, ta, tc, tm;
47 time_t wa, wc, wm;
48 char buf[1024];
49 struct stat s;
51 subtest = 1;
52 if (passes > 0) return; /* takes too long to repeat this test */
54 if ( (fd = creat("T16.a", 0666)) < 0) e(1);
55 if (write(fd, buf, 1024) != 1024) e(2);
56 if (close(fd) < 0) e(3);
57 sleep(1); /* wait 1 sec before continuing */
58 if ( (fd = open("T16.a", O_RDONLY)) < 0) e(4);
59 if (read(fd, buf, 3) != 3) e(5);
60 if (close(fd) != 0) e(6);
61 if (stat("T16.a", &s) != 0) e(7);
62 a = s.st_atime;
63 c = s.st_ctime;
64 m = s.st_mtime;
65 if (a == 0) {
66 /* Almost certainly means we are running a V1 file system. */
67 printf(" (atime = 0. Probably V1 file system. V2 tests skipped.) ");
68 return;
71 /* Many system calls affect atime, ctime, and mtime. Test them. They
72 * fall into several groups. The members of each group can be tested
73 * together. Start with creat(), mkdir(), and mkfifo, all of which
74 * set all 3 times on the created object, and ctime and mtime of the dir.
76 if ( (fd = creat("T16.b", 0666)) < 0) e(8);
77 if (close(fd) != 0) e(9);
78 get_times("T16.b", &a, &c, &m);
79 get_times(".", &pa, &pc, &pm);
80 if (a != c) e(10);
81 if (a != m) e(11);
82 if (a != pc) e(12);
83 if (a != pm) e(13);
84 if (unlink("T16.b") < 0) e(14);
86 /* Test the times for mkfifo. */
87 if ( (fd = mkfifo("T16.c", 0666)) != 0) e(15);
88 if (access("T16.c", R_OK | W_OK) != 0) e(16);
89 get_times("T16.c", &a, &c, &m);
90 get_times(".", &pa, &pc, &pm);
91 if (a != c) e(17);
92 if (a != m) e(18);
93 if (a != pc) e(19);
94 if (a != pm) e(20);
95 if (unlink("T16.c") < 0) e(21);
97 /* Test the times for mkdir. */
98 if (mkdir("T16.d", 0666) < 0) e(22);
99 get_times("T16.d", &a, &c, &m);
100 get_times(".", &pa, &pc, &pm);
101 if (a != c) e(23);
102 if (a != m) e(24);
103 if (a != pc) e(25);
104 if (a != pm) e(26);
105 sleep(1);
106 if (rmdir("T16.d") < 0) e(27);
107 get_times(".", &xa, &xc, &xm);
108 if (c == xc) e(28);
109 if (m == xm) e(29);
110 if (xc != xm) e(30);
112 /* Test open(file, O_TRUNC). */
113 if ( (fd = open("T16.e", O_WRONLY|O_CREAT, 0666)) < 0) e(31);
114 if (write(fd, buf, 1024) != 1024) e(32);
115 if (close(fd) != 0) e(33);
116 get_times("T16.e", &a, &c, &m);
117 get_times(".", &pa, &pc, &pm);
118 sleep(1);
119 if ( (fd = open("T16.e", O_WRONLY|O_TRUNC)) < 0) e(34);
120 get_times("T16.e", &xa, &xc, &xm);
121 get_times(".", &ya, &yc, &ym);
122 if (c != m) e(35);
123 if (pc != pm) e(36);
124 if (c == xc) e(37);
125 if (m == xm) e(38);
126 if (yc != pc) e(39);
127 if (ym != pm) e(40);
128 if (close(fd) != 0) e(41);
129 /* Try once more, now without changing the file size. */
130 sleep(1);
131 if ( (fd = open("T16.e", O_WRONLY|O_TRUNC)) < 0) e(89);
132 get_times("T16.e", &a, &c, &m);
133 if (c != m) e(90);
134 if (c == xc) e(91);
135 if (m == xm) e(92);
136 if (close(fd) != 0) e(93);
138 /* Test the times for link/unlink. */
139 get_times("T16.e", &a, &c, &m);
140 get_times(".", &ya, &yc, &ym);
141 sleep(1);
142 if (link("T16.e", "T16.f") != 0) e(42); /* second link */
143 get_times("T16.e", &xa, &xc, &xm);
144 get_times(".", &pa, &pc, &pm);
145 if (a != xa) e(43);
146 if (m != xm) e(44);
147 #ifndef V1_FILESYSTEM
148 if (c == xc) e(45);
149 #endif
150 if (ya != pa) e(46);
151 if (yc == pc) e(47);
152 if (ym == pm) e(48);
153 if (yc != ym) e(49);
154 if (pc != pm) e(50);
155 sleep(1);
156 if (unlink("T16.f") != 0) e(46);
157 get_times("T16.e", &a, &c, &m);
158 get_times(".", &ya, &yc, &ym);
159 if (a != xa) e(51);
160 if (m != xm) e(52);
161 #ifndef V1_FILESYSTEM
162 if (c == xc) e(53);
163 #endif
164 if (pa != ya) e(54);
165 if (pc == yc) e(55);
166 if (pm == ym) e(56);
167 if (yc != ym) e(57);
168 if (unlink("T16.e") != 0) e(58);
170 /* Test rename, read, write, chmod, utime. */
171 get_times(".", &pa, &pc, &pm);
172 if ( (fd = open("T16.g", O_RDWR|O_CREAT)) < 0) e(59);
173 if ( (fd1 = open("T16.h", O_WRONLY|O_CREAT, 0666)) < 0) e(60);
174 if ( (fd2 = open("T16.i", O_WRONLY|O_CREAT, 0666)) < 0) e(61);
175 if ( (fd3 = open("T16.j", O_WRONLY|O_CREAT, 0666)) < 0) e(62);
176 if ( (fd4 = open("T16.k", O_RDWR|O_CREAT, 0666)) < 0) e(63);
177 if (write(fd, buf, 1024) != 1024) e(64);
178 get_times("T16.g", &a, &c, &m);
179 get_times("T16.h", &pa, &pc, &pm);
180 get_times("T16.i", &xa, &xc, &xm);
181 get_times("T16.j", &ya, &yc, &ym);
182 get_times("T16.k", &za, &zc, &zm);
183 get_times(".", &wa, &wc, &wm);
184 sleep(1);
185 lseek(fd, 0L, SEEK_SET);
186 if (read(fd, buf, 35) != 35) e(65);
187 get_times("T16.g", &ta, &tc, &tm);
188 if (a == ta || c != tc || m != tm) e(66);
189 if (write(fd1, buf, 35) != 35) e(67);
190 get_times("T16.h", &ta, &tc, &tm);
191 if (pa != ta || pc == tc || pm == tm) e(69);
192 if (rename("T16.i", "T16.i1") != 0) e(70);
193 get_times("T16.i1", &ta, &tc, &tm);
194 if (xa != ta || xc != tc || xm != tm) e(71);
195 get_times(".", &a, &c, &m);
196 if (a != wa || c == wc || m == wm || wc != wm) e(72);
197 if (chmod("T16.j", 0777) != 0) e(73);
198 get_times("T16.j", &ta, &tc, &tm);
199 if (ya != ta || yc == tc || ym != tm) e(74);
200 if (utime("T16.k", (void *) 0) != 0) e(75);
201 get_times("T16.k", &ta, &tc, &tm);
202 if (za == ta || zc == tc) e(76);
203 if (close(fd) != 0) e(77);
204 if (close(fd1) != 0) e(78);
205 if (close(fd2) != 0) e(79);
206 if (close(fd3) != 0) e(80);
207 if (close(fd4) != 0) e(81);
208 if (unlink("T16.g") != 0) e(82);
209 if (unlink("T16.h") != 0) e(83);
210 if (unlink("T16.i1") != 0) e(84);
211 if (unlink("T16.j") != 0) e(85);
212 if (unlink("T16.k") != 0) e(86);
214 /* Test the times for truncate. */
215 if (system("echo 1 > T16.l") != 0) e(87);
216 stat("T16.l", &s);
217 get_times("T16.l", &a, &c, &m);
218 sleep(1);
219 truncate("T16.l", s.st_size);
220 get_times("T16.l", &ta, &tc, &tm);
221 if (a != ta || c != tc || m != tm) e(88);
224 void get_times(name, a, c, m)
225 char *name;
226 time_t *a, *c, *m;
228 struct stat s;
230 if (stat(name, &s) != 0) e(500);
231 *a = s.st_atime;
232 *c = s.st_ctime;
233 *m = s.st_mtime;