sort: add -x hex sort feature back
[minix.git] / test / test21.c
blob80d5b866c14101029f6c917db8ee481981855545
1 /* POSIX test program (21). Author: Andy Tanenbaum */
3 /* The following POSIX calls are tested:
5 * rename(), mkdir(), rmdir()
6 */
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <limits.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <stdio.h>
18 #define ITERATIONS 1
19 #define MAX_ERROR 3
21 #include "common.c"
23 int main(int argc, char *argv []);
24 void test21a(void);
25 void test21b(void);
26 void test21c(void);
27 void test21d(void);
28 void test21e(void);
29 void test21f(void);
30 void test21g(void);
31 void test21h(void);
32 void test21i(void);
33 void test21k(void);
34 void test21l(void);
35 void test21m(void);
36 void test21n(void);
37 void test21o(void);
38 int get_link(char *name);
40 int main(argc, argv)
41 int argc;
42 char *argv[];
45 int i, m = 0xFFFF;
47 start(21);
49 if (argc == 2) m = atoi(argv[1]);
51 for (i = 0; i < ITERATIONS; i++) {
52 if (m & 00001) test21a();
53 if (m & 00002) test21b();
54 if (m & 00004) test21c();
55 if (m & 00010) test21d();
56 if (m & 00020) test21e();
57 if (m & 00040) test21f();
58 if (m & 01000) test21g();
59 if (m & 00200) test21h();
60 if (m & 00400) test21i();
61 if (m & 01000) test21k();
62 if (m & 02000) test21l();
63 if (m & 04000) test21m();
64 if (m & 010000) test21n();
65 if (m & 020000) test21o();
67 quit();
69 return(-1); /* Unreachable */
72 void test21a()
74 /* Test rename(). */
76 int fd, fd2;
77 char buf[PATH_MAX+1], buf1[PATH_MAX+1], buf2[PATH_MAX+1];
78 struct stat stat1, stat2;
80 subtest = 1;
82 unlink("A1"); /* get rid of it if it exists */
83 unlink("A2"); /* get rid of it if it exists */
84 unlink("A3"); /* get rid of it if it exists */
85 unlink("A4"); /* get rid of it if it exists */
86 unlink("A5"); /* get rid of it if it exists */
87 unlink("A6"); /* get rid of it if it exists */
88 unlink("A7"); /* get rid of it if it exists */
90 /* Basic test. Rename A1 to A2 and then A2 to A3. */
91 if ( (fd=creat("A1", 0666)) < 0) e(1);
92 if (write(fd, buf, 20) != 20) e(2);
93 if (close(fd) < 0) e(3);
94 if (rename("A1", "A2") < 0) e(4);
95 if ( (fd=open("A2", O_RDONLY)) < 0) e(5);
96 if (rename("A2", "A3") < 0) e(6);
97 if ( (fd2=open("A3", O_RDONLY)) < 0) e(7);
98 if (close(fd) != 0) e(8);
99 if (close(fd2) != 0) e(9);
100 if (unlink("A3") != 0) e(10);
102 /* Now get the absolute path name of the current directory using getcwd()
103 * and use it to test RENAME using different combinations of relative and
104 * absolute path names.
106 if (getcwd(buf, PATH_MAX) == (char *) NULL) e(11);
107 if ( (fd = creat("A4", 0666)) < 0) e(12);
108 if (write(fd, buf, 30) != 30) e(13);
109 if (close(fd) != 0) e(14);
110 strcpy(buf1, buf);
111 strcat(buf1, "/A4");
112 if (rename(buf1, "A5") != 0) e(15); /* rename(absolute, relative) */
113 if (access("A5", 6) != 0) e(16); /* use access to see if file exists */
114 strcpy(buf2, buf);
115 strcat(buf2, "/A6");
116 if (rename("A5", buf2) != 0) e(17); /* rename(relative, absolute) */
117 if (access("A6", 6) != 0) e(18);
118 if (access(buf2, 6) != 0) e(19);
119 strcpy(buf1, buf);
120 strcat(buf1, "/A6");
121 strcpy(buf2, buf);
122 strcat(buf2, "/A7");
123 if (rename(buf1, buf2) != 0) e(20); /* rename(absolute, absolute) */
124 if (access("A7", 6) != 0) e(21);
125 if (access(buf2, 6) != 0) e(22);
127 /* Try renaming using names like "./A8" */
128 if (rename("A7", "./A8") != 0) e(23);
129 if (access("A8", 6) != 0) e(24);
130 if (rename("./A8", "A9") != 0) e(25);
131 if (access("A9", 6) != 0) e(26);
132 if (rename("./A9", "./A10") != 0) e(27);
133 if (access("A10", 6) != 0) e(28);
134 if (access("./A10", 6) != 0) e(29);
135 if (unlink("A10") != 0) e(30);
137 /* Now see if directories can be renamed. */
138 if (system("rm -rf ?uzzy scsi") != 0) e(31);
139 if (system("mkdir fuzzy") != 0) e(32);
140 if (rename("fuzzy", "wuzzy") != 0) e(33);
141 if ( (fd=creat("wuzzy/was_a_bear", 0666)) < 0) e(34);
142 if (access("wuzzy/was_a_bear", 6) != 0) e(35);
143 if (unlink("wuzzy/was_a_bear") != 0) e(36);
144 if (close(fd) != 0) e(37);
145 if (rename("wuzzy", "buzzy") != 0) e(38);
146 if (system("rmdir buzzy") != 0) e(39);
148 /* Now start testing the case that 'new' exists. */
149 if ( (fd = creat("horse", 0666)) < 0) e(40);
150 if ( (fd2 = creat("sheep", 0666)) < 0) e(41);
151 if (write(fd, buf, PATH_MAX) != PATH_MAX) e(42);
152 if (write(fd2, buf, 23) != 23) e(43);
153 if (stat("horse", &stat1) != 0) e(44);
154 if (rename("horse", "sheep") != 0) e(45);
155 if (stat("sheep", &stat2) != 0) e(46);
156 if (stat1.st_dev != stat2.st_dev) e(47);
157 if (stat1.st_ino != stat2.st_ino) e(48);
158 if (stat2.st_size != PATH_MAX) e(49);
159 if (access("horse", 6) == 0) e(50);
160 if (close(fd) != 0) e(51);
161 if (close(fd2) != 0) e(52);
162 if (rename("sheep", "sheep") != 0) e(53);
163 if (unlink("sheep") != 0) e(54);
165 /* Now try renaming something to a directory that already exists. */
166 if (system("mkdir fuzzy wuzzy") != 0) e(55);
167 if ( (fd = creat("fuzzy/was_a_bear", 0666)) < 0) e(56);
168 if (close(fd) != 0) e(57);
169 if (rename("fuzzy", "wuzzy") != 0) e(58); /* 'new' is empty dir */
170 if (system("mkdir scsi") != 0) e(59);
171 if (rename("scsi", "wuzzy") == 0) e(60); /* 'new' is full dir */
172 if (errno != EEXIST && errno != ENOTEMPTY) e(61);
174 /* Test 14 character names--always tricky. */
175 if (rename("wuzzy/was_a_bear", "wuzzy/was_not_a_bear") != 0) e(62);
176 if (access("wuzzy/was_not_a_bear", 6) != 0) e(63);
177 if (rename("wuzzy/was_not_a_bear", "wuzzy/was_not_a_duck") != 0) e(64);
178 if (access("wuzzy/was_not_a_duck", 6) != 0) e(65);
179 if (rename("wuzzy/was_not_a_duck", "wuzzy/was_a_bird") != 0) e(65);
180 if (access("wuzzy/was_a_bird", 6) != 0) e(66);
182 /* Test moves between directories. */
183 if (rename("wuzzy/was_a_bird", "beast") != 0) e(67);
184 if (access("beast", 6) != 0) e(68);
185 if (rename("beast", "wuzzy/was_a_cat") != 0) e(69);
186 if (access("wuzzy/was_a_cat", 6) != 0) e(70);
188 /* Test error conditions. 'scsi' and 'wuzzy/was_a_cat' exist now. */
189 if (rename("wuzzy/was_a_cat", "wuzzy/was_a_dog") != 0) e(71);
190 if (access("wuzzy/was_a_dog", 6) != 0) e(72);
191 if (chmod("wuzzy", 0) != 0) e(73);
193 errno = 0;
194 if (rename("wuzzy/was_a_dog", "wuzzy/was_a_pig") != -1) e(74);
195 if (errno != EACCES) e(75);
197 errno = 0;
198 if (rename("wuzzy/was_a_dog", "doggie") != -1) e(76);
199 if (errno != EACCES) e(77);
201 errno = 0;
202 if ( (fd = creat("beast", 0666)) < 0) e(78);
203 if (close(fd) != 0) e(79);
204 if (rename("beast", "wuzzy/was_a_twit") != -1) e(80);
205 if (errno != EACCES) e(81);
207 errno = 0;
208 if (rename("beast", "wuzzy") != -1) e(82);
209 if (errno != EISDIR) e(83);
211 errno = 0;
212 if (rename("beest", "baste") != -1) e(84);
213 if (errno != ENOENT) e(85);
215 errno = 0;
216 if (rename("wuzzy", "beast") != -1) e(86);
217 if (errno != ENOTDIR) e(87);
219 /* Test prefix rule. */
220 errno = 0;
221 if (chmod("wuzzy", 0777) != 0) e(88);
222 if (unlink("wuzzy/was_a_dog") != 0) e(89);
223 strcpy(buf1, buf);
224 strcat(buf1, "/wuzzy");
225 if (rename(buf, buf1) != -1) e(90);
226 if (errno != EINVAL) e(91);
228 if (system("rm -rf wuzzy beast scsi") != 0) e(92);
233 void test21b()
235 /* Test mkdir() and rmdir(). */
237 int i;
238 char name[3];
239 struct stat statbuf;
241 subtest = 2;
243 /* Simple stuff. */
244 if (mkdir("D1", 0700) != 0) e(1);
245 if (stat("D1", &statbuf) != 0) e(2);
246 if (!S_ISDIR(statbuf.st_mode)) e(3);
247 if ( (statbuf.st_mode & 0777) != 0700) e(4);
248 if (rmdir("D1") != 0) e(5);
250 /* Make and remove 40 directories. By doing so, the directory has to
251 * grow to 2 blocks. That presents plenty of opportunity for bugs.
253 name[0] = 'D';
254 name[2] = '\0';
255 for (i = 0; i < 40; i++) {
256 name[1] = 'A' + i;
257 if (mkdir(name, 0700 + i%7) != 0) e(10+i); /* for simplicity */
259 for (i = 0; i < 40; i++) {
260 name[1] = 'A' + i;
261 if (rmdir(name) != 0) e(50+i);
265 void test21c()
267 /* Test mkdir() and rmdir(). */
269 subtest = 3;
271 if (mkdir("D1", 0777) != 0) e(1);
272 if (mkdir("D1/D2", 0777) != 0) e(2);
273 if (mkdir("D1/D2/D3", 0777) != 0) e(3);
274 if (mkdir("D1/D2/D3/D4", 0777) != 0) e(4);
275 if (mkdir("D1/D2/D3/D4/D5", 0777) != 0) e(5);
276 if (mkdir("D1/D2/D3/D4/D5/D6", 0777) != 0) e(6);
277 if (mkdir("D1/D2/D3/D4/D5/D6/D7", 0777) != 0) e(7);
278 if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8", 0777) != 0) e(8);
279 if (mkdir("D1/D2/D3/D4/D5/D6/D7/D8/D9", 0777) != 0) e(9);
280 if (access("D1/D2/D3/D4/D5/D6/D7/D8/D9", 7) != 0) e(10);
281 if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8/D9") != 0) e(11);
282 if (rmdir("D1/D2/D3/D4/D5/D6/D7/D8") != 0) e(12);
283 if (rmdir("D1/D2/D3/D4/D5/D6/D7") != 0) e(13);
284 if (rmdir("D1/D2/D3/D4/D5/D6") != 0) e(11);
285 if (rmdir("D1/D2/D3/D4/D5") != 0) e(13);
286 if (rmdir("D1/D2/D3/D4") != 0) e(14);
287 if (rmdir("D1/D2/D3") != 0) e(15);
288 if (rmdir("D1/D2") != 0) e(16);
289 if (rmdir("D1") != 0) e(17);
292 void test21d()
294 /* Test making directories with files and directories in them. */
296 int fd1, fd2, fd3, fd4, fd5, fd6, fd7, fd8, fd9;
298 subtest = 4;
300 if (mkdir("D1", 0777) != 0) e(1);
301 if (mkdir("D1/D2", 0777) != 0) e(2);
302 if (mkdir("./D1/D3", 0777) != 0) e(3);
303 if (mkdir("././D1/D4", 0777) != 0) e(4);
304 if ( (fd1 = creat("D1/D2/x", 0700)) < 0) e(5);
305 if ( (fd2 = creat("D1/D2/y", 0700)) < 0) e(6);
306 if ( (fd3 = creat("D1/D2/z", 0700)) < 0) e(7);
307 if ( (fd4 = creat("D1/D3/x", 0700)) < 0) e(8);
308 if ( (fd5 = creat("D1/D3/y", 0700)) < 0) e(9);
309 if ( (fd6 = creat("D1/D3/z", 0700)) < 0) e(10);
310 if ( (fd7 = creat("D1/D4/x", 0700)) < 0) e(11);
311 if ( (fd8 = creat("D1/D4/y", 0700)) < 0) e(12);
312 if ( (fd9 = creat("D1/D4/z", 0700)) < 0) e(13);
313 if (unlink("D1/D2/z") != 0) e(14);
314 if (unlink("D1/D2/y") != 0) e(15);
315 if (unlink("D1/D2/x") != 0) e(16);
316 if (unlink("D1/D3/x") != 0) e(17);
317 if (unlink("D1/D3/z") != 0) e(18);
318 if (unlink("D1/D3/y") != 0) e(19);
319 if (unlink("D1/D4/y") != 0) e(20);
320 if (unlink("D1/D4/z") != 0) e(21);
321 if (unlink("D1/D4/x") != 0) e(22);
322 if (rmdir("D1/D2") != 0) e(23);
323 if (rmdir("D1/D3") != 0) e(24);
324 if (rmdir("D1/D4") != 0) e(25);
325 if (rmdir("D1") != 0) e(26);
326 if (close(fd1) != 0) e(27);
327 if (close(fd2) != 0) e(28);
328 if (close(fd3) != 0) e(29);
329 if (close(fd4) != 0) e(30);
330 if (close(fd5) != 0) e(31);
331 if (close(fd6) != 0) e(32);
332 if (close(fd7) != 0) e(33);
333 if (close(fd8) != 0) e(34);
334 if (close(fd9) != 0) e(35);
338 void test21e()
340 /* Test error conditions. */
342 subtest = 5;
344 if (mkdir("D1", 0677) != 0) e(1);
345 errno = 0;
346 if (mkdir("D1/D2", 0777) != -1) e(2);
347 if (errno != EACCES) e(3);
348 if (chmod ("D1", 0577) != 0) e(4);
349 errno = 0;
350 if (mkdir("D1/D2", 0777) != -1) e(5);
351 if (errno != EACCES) e(6);
352 if (chmod ("D1", 0777) != 0) e(7);
353 errno = 0;
354 if (mkdir("D1", 0777) != -1) e(8);
355 if (errno != EEXIST) e(9);
356 errno = 0;
357 if (mkdir("D1/D2/x", 0777) != -1) e(14);
358 if (errno != ENOENT) e(15);
360 /* A particularly nasty test is when the parent has mode 0. Although
361 * this is unlikely to work, it had better not muck up the file system
363 if (mkdir("D1/D2", 0777) != 0) e(16);
364 if (chmod("D1", 0) != 0) e(17);
365 errno = 0;
366 if (rmdir("D1/D2") != -1) e(18);
367 if (errno != EACCES) e(19);
368 if (chmod("D1", 0777) != 0) e(20);
369 if (rmdir("D1/D2") != 0) e(21);
370 if (rmdir("D1") != 0) e(22);
373 void test21f()
375 /* The rename() function affects the link count of all the files and
376 * directories it goes near. Test to make sure it gets everything ok.
377 * There are four cases:
379 * 1. rename("d1/file1", "d1/file2"); - rename file without moving it
380 * 2. rename("d1/file1", "d2/file2"); - move a file to another dir
381 * 3. rename("d1/dir1", "d2/dir2"); - rename a dir without moving it
382 * 4. rename("d1/dir1", "d2/dir2"); - move a dir to another dir
384 * Furthermore, a distinction has to be made when the target file exists
385 * and when it does not exist, giving 8 cases in all.
388 int fd, D1_before, D1_after, x_link, y_link;
390 /* Test case 1: renaming a file within the same directory. */
391 subtest = 6;
392 if (mkdir("D1", 0777) != 0) e(1);
393 if ( (fd = creat("D1/x", 0777)) < 0) e(2);
394 if (close(fd) != 0) e(3);
395 D1_before = get_link("D1");
396 x_link = get_link("D1/x");
397 if (rename("D1/x", "D1/y") != 0) e(4);
398 y_link = get_link("D1/y");
399 D1_after = get_link("D1");
400 if (D1_before != 2) e(5);
401 if (D1_after != 2) e(6);
402 if (x_link != 1) e(7);
403 if (y_link != 1) e(8);
404 if (access("D1/y", 7) != 0) e(9);
405 if (unlink("D1/y") != 0) e(10);
406 if (rmdir("D1") != 0) e(11);
409 void test21g()
411 int fd, D1_before, D1_after, D2_before, D2_after, x_link, y_link;
413 /* Test case 2: move a file to a new directory. */
414 subtest = 7;
415 if (mkdir("D1", 0777) != 0) e(1);
416 if (mkdir("D2", 0777) != 0) e(2);
417 if ( (fd = creat("D1/x", 0777)) < 0) e(3);
418 if (close(fd) != 0) e(4);
419 D1_before = get_link("D1");
420 D2_before = get_link("D2");
421 x_link = get_link("D1/x");
422 if (rename("D1/x", "D2/y") != 0) e(5);
423 y_link = get_link("D2/y");
424 D1_after = get_link("D1");
425 D2_after = get_link("D2");
426 if (D1_before != 2) e(6);
427 if (D2_before != 2) e(7);
428 if (D1_after != 2) e(8);
429 if (D2_after != 2) e(9);
430 if (x_link != 1) e(10);
431 if (y_link != 1) e(11);
432 if (access("D2/y", 7) != 0) e(12);
433 if (unlink("D2/y") != 0) e(13);
434 if (rmdir("D1") != 0) e(14);
435 if (rmdir("D2") != 0) e(15);
438 void test21h()
440 int D1_before, D1_after, x_link, y_link;
442 /* Test case 3: renaming a directory within the same directory. */
443 subtest = 8;
444 if (mkdir("D1", 0777) != 0) e(1);
445 if (mkdir("D1/X", 0777) != 0) e(2);
446 D1_before = get_link("D1");
447 x_link = get_link("D1/X");
448 if (rename("D1/X", "D1/Y") != 0) e(3);
449 y_link = get_link("D1/Y");
450 D1_after = get_link("D1");
451 if (D1_before != 3) e(4);
452 if (D1_after != 3) e(5);
453 if (x_link != 2) e(6);
454 if (y_link != 2) e(7);
455 if (access("D1/Y", 7) != 0) e(8);
456 if (rmdir("D1/Y") != 0) e(9);
457 if (get_link("D1") != 2) e(10);
458 if (rmdir("D1") != 0) e(11);
461 void test21i()
463 int D1_before, D1_after, D2_before, D2_after, x_link, y_link;
465 /* Test case 4: move a directory to a new directory. */
466 subtest = 9;
467 if (mkdir("D1", 0777) != 0) e(1);
468 if (mkdir("D2", 0777) != 0) e(2);
469 if (mkdir("D1/X", 0777) != 0) e(3);
470 D1_before = get_link("D1");
471 D2_before = get_link("D2");
472 x_link = get_link("D1/X");
473 if (rename("D1/X", "D2/Y") != 0) e(4);
474 y_link = get_link("D2/Y");
475 D1_after = get_link("D1");
476 D2_after = get_link("D2");
477 if (D1_before != 3) e(5);
478 if (D2_before != 2) e(6);
479 if (D1_after != 2) e(7);
480 if (D2_after != 3) e(8);
481 if (x_link != 2) e(9);
482 if (y_link != 2) e(10);
483 if (access("D2/Y", 7) != 0) e(11);
484 if (rename("D2/Y", "D1/Z") != 0) e(12);
485 if (get_link("D1") != 3) e(13);
486 if (get_link("D2") != 2) e(14);
487 if (rmdir("D1/Z") != 0) e(15);
488 if (get_link("D1") != 2) e(16);
489 if (rmdir("D1") != 0) e(17);
490 if (rmdir("D2") != 0) e(18);
493 void test21k()
495 /* Now test the same 4 cases, except when the target exists. */
497 int fd, D1_before, D1_after, x_link, y_link;
499 /* Test case 5: renaming a file within the same directory. */
500 subtest = 10;
501 if (mkdir("D1", 0777) != 0) e(1);
502 if ( (fd = creat("D1/x", 0777)) < 0) e(2);
503 if (close(fd) != 0) e(3);
504 if ( (fd = creat("D1/y", 0777)) < 0) e(3);
505 if (close(fd) != 0) e(4);
506 D1_before = get_link("D1");
507 x_link = get_link("D1/x");
508 if (rename("D1/x", "D1/y") != 0) e(5);
509 y_link = get_link("D1/y");
510 D1_after = get_link("D1");
511 if (D1_before != 2) e(6);
512 if (D1_after != 2) e(7);
513 if (x_link != 1) e(8);
514 if (y_link != 1) e(9);
515 if (access("D1/y", 7) != 0) e(10);
516 if (unlink("D1/y") != 0) e(11);
517 if (rmdir("D1") != 0) e(12);
520 void test21l()
522 int fd, D1_before, D1_after, D2_before, D2_after, x_link, y_link;
524 /* Test case 6: move a file to a new directory. */
525 subtest = 11;
526 if (mkdir("D1", 0777) != 0) e(1);
527 if (mkdir("D2", 0777) != 0) e(2);
528 if ( (fd = creat("D1/x", 0777)) < 0) e(3);
529 if (close(fd) != 0) e(4);
530 if ( (fd = creat("D2/y", 0777)) < 0) e(5);
531 if (close(fd) != 0) e(6);
532 D1_before = get_link("D1");
533 D2_before = get_link("D2");
534 x_link = get_link("D1/x");
535 if (rename("D1/x", "D2/y") != 0) e(7);
536 y_link = get_link("D2/y");
537 D1_after = get_link("D1");
538 D2_after = get_link("D2");
539 if (D1_before != 2) e(8);
540 if (D2_before != 2) e(9);
541 if (D1_after != 2) e(10);
542 if (D2_after != 2) e(11);
543 if (x_link != 1) e(12);
544 if (y_link != 1) e(13);
545 if (access("D2/y", 7) != 0) e(14);
546 if (unlink("D2/y") != 0) e(15);
547 if (rmdir("D1") != 0) e(16);
548 if (rmdir("D2") != 0) e(17);
551 void test21m()
553 int D1_before, D1_after, x_link, y_link;
555 /* Test case 7: renaming a directory within the same directory. */
556 subtest = 12;
557 if (mkdir("D1", 0777) != 0) e(1);
558 if (mkdir("D1/X", 0777) != 0) e(2);
559 if (mkdir("D1/Y", 0777) != 0) e(3);
560 D1_before = get_link("D1");
561 x_link = get_link("D1/X");
562 if (rename("D1/X", "D1/Y") != 0) e(4);
563 y_link = get_link("D1/Y");
564 D1_after = get_link("D1");
565 if (D1_before != 4) e(5);
566 if (D1_after != 3) e(6);
567 if (x_link != 2) e(7);
568 if (y_link != 2) e(8);
569 if (access("D1/Y", 7) != 0) e(9);
570 if (rmdir("D1/Y") != 0) e(10);
571 if (get_link("D1") != 2) e(11);
572 if (rmdir("D1") != 0) e(12);
575 void test21n()
577 int D1_before, D1_after, D2_before, D2_after, x_link, y_link;
579 /* Test case 8: move a directory to a new directory. */
580 subtest = 13;
581 if (mkdir("D1", 0777) != 0) e(1);
582 if (mkdir("D2", 0777) != 0) e(2);
583 if (mkdir("D1/X", 0777) != 0) e(3);
584 if (mkdir("D2/Y", 0777) != 0) e(4);
585 D1_before = get_link("D1");
586 D2_before = get_link("D2");
587 x_link = get_link("D1/X");
588 if (rename("D1/X", "D2/Y") != 0) e(5);
589 y_link = get_link("D2/Y");
590 D1_after = get_link("D1");
591 D2_after = get_link("D2");
592 if (D1_before != 3) e(6);
593 if (D2_before != 3) e(7);
594 if (D1_after != 2) e(8);
595 if (D2_after != 3) e(9);
596 if (x_link != 2) e(10);
597 if (y_link != 2) e(11);
598 if (access("D2/Y", 7) != 0) e(12);
599 if (rename("D2/Y", "D1/Z") != 0) e(13);
600 if (get_link("D1") != 3) e(14);
601 if (get_link("D2") != 2) e(15);
602 if (rmdir("D1/Z") != 0) e(16);
603 if (get_link("D1") != 2) e(17);
604 if (rmdir("D1") != 0) e(18);
605 if (rmdir("D2") != 0) e(19);
608 void test21o()
610 /* Test trying to remove . and .. */
611 subtest = 14;
612 if (mkdir("D1", 0777) != 0) e(1);
613 if (chdir("D1") != 0) e(2);
614 if (rmdir(".") == 0) e(3);
615 if (rmdir("..") == 0) e(4);
616 if (mkdir("D2", 0777) != 0) e(5);
617 if (mkdir("D3", 0777) != 0) e(6);
618 if (mkdir("D4", 0777) != 0) e(7);
619 if (rmdir("D2/../D3/../D4") != 0) e(8); /* legal way to remove D4 */
620 if (rmdir("D2/../D3/../D2/..") == 0) e(9); /* removing self is illegal */
621 if (rmdir("D2/../D3/../D2/../..") == 0) e(10);/* removing parent is illegal*/
622 if (rmdir("../D1/../D1/D3") != 0) e(11); /* legal way to remove D3 */
623 if (rmdir("./D2/../D2") != 0) e(12); /* legal way to remove D2 */
624 if (chdir("..") != 0) e(13);
625 if (rmdir("D1") != 0) e(14);
628 int get_link(name)
629 char *name;
631 struct stat statbuf;
633 if (stat(name, &statbuf) != 0) {
634 printf("Unable to stat %s\n", name);
635 errct++;
636 return(-1);
638 return(statbuf.st_nlink);