1 | /* Utility routines for Minix tests.
|
---|
2 | * This is designed to be #includ'ed near the top of test programs. It is
|
---|
3 | * self-contained except for MAX_ERRORS.
|
---|
4 | */
|
---|
5 |
|
---|
6 | #include <errno.h>
|
---|
7 | #include <stdlib.h>
|
---|
8 | #include <string.h>
|
---|
9 | #include <unistd.h>
|
---|
10 | #include <stdio.h>
|
---|
11 |
|
---|
12 | int common_test_nr = -1, errct = 0, subtest;
|
---|
13 |
|
---|
14 | _PROTOTYPE(void cleanup, (void));
|
---|
15 | _PROTOTYPE(void e, (int n));
|
---|
16 | _PROTOTYPE(void quit, (void));
|
---|
17 | _PROTOTYPE(void rm_rf_dir, (int test_nr));
|
---|
18 | _PROTOTYPE(void rm_rf_ppdir, (int test_nr));
|
---|
19 | _PROTOTYPE(void start, (int test_nr));
|
---|
20 |
|
---|
21 | void start(test_nr)
|
---|
22 | int test_nr;
|
---|
23 | {
|
---|
24 | char buf[64];
|
---|
25 |
|
---|
26 | common_test_nr = test_nr;
|
---|
27 | printf("Test %2d ", test_nr);
|
---|
28 | fflush(stdout); /* since stdout is probably line buffered */
|
---|
29 | sync();
|
---|
30 | rm_rf_dir(test_nr);
|
---|
31 | sprintf(buf, "mkdir DIR_%02d", test_nr);
|
---|
32 | if (system(buf) != 0) {
|
---|
33 | e(666);
|
---|
34 | quit();
|
---|
35 | }
|
---|
36 | sprintf(buf, "DIR_%02d", test_nr);
|
---|
37 | if (chdir(buf) != 0) {
|
---|
38 | e(6666);
|
---|
39 | quit();
|
---|
40 | }
|
---|
41 | }
|
---|
42 |
|
---|
43 | void rm_rf_dir(test_nr)
|
---|
44 | int test_nr;
|
---|
45 | {
|
---|
46 | char buf[128];
|
---|
47 |
|
---|
48 | /* "rm -rf dir" will not work unless all the subdirectories have suitable
|
---|
49 | * permissions. Minix chmod is not recursive so it is not easy to change
|
---|
50 | * all the permissions. I had to fix opendir() to stop the bash shell
|
---|
51 | * from hanging when it opendir()s fifos.
|
---|
52 | */
|
---|
53 | sprintf(buf, "chmod 777 DIR_%02d DIR_%02d/* DIR_%02d/*/* >/dev/null 2>&1",
|
---|
54 | test_nr, test_nr, test_nr);
|
---|
55 | (void) system(buf); /* usually fails */
|
---|
56 | sprintf(buf, "rm -rf DIR_%02d >/dev/null 2>&1", test_nr);
|
---|
57 | if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void rm_rf_ppdir(test_nr)
|
---|
61 | int test_nr;
|
---|
62 | {
|
---|
63 | /* Attempt to remove everything in the test directory (== the current dir). */
|
---|
64 |
|
---|
65 | char buf[128];
|
---|
66 |
|
---|
67 | sprintf(buf, "chmod 777 ../DIR_%02d/* ../DIR_%02d/*/* >/dev/null 2>&1",
|
---|
68 | test_nr, test_nr);
|
---|
69 | (void) system(buf);
|
---|
70 | sprintf(buf, "rm -rf ../DIR_%02d >/dev/null 2>&1", test_nr);
|
---|
71 | if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
|
---|
72 | }
|
---|
73 |
|
---|
74 | void e(n)
|
---|
75 | int n;
|
---|
76 | {
|
---|
77 | if (errct == 0) printf("\n"); /* finish header */
|
---|
78 | printf("Subtest %d, error %d, errno %d: %s\n",
|
---|
79 | subtest, n, errno, strerror(errno));
|
---|
80 | if (errct++ > MAX_ERROR) {
|
---|
81 | printf("Too many errors; test aborted\n");
|
---|
82 | cleanup();
|
---|
83 | exit(1);
|
---|
84 | }
|
---|
85 | errno = 0; /* don't leave it around to confuse next e() */
|
---|
86 | }
|
---|
87 |
|
---|
88 | void cleanup()
|
---|
89 | {
|
---|
90 | if (chdir("..") == 0 && common_test_nr != -1) rm_rf_dir(common_test_nr);
|
---|
91 | }
|
---|
92 |
|
---|
93 | void quit()
|
---|
94 | {
|
---|
95 | cleanup();
|
---|
96 | if (errct == 0) {
|
---|
97 | printf("ok\n");
|
---|
98 | exit(0);
|
---|
99 | } else {
|
---|
100 | printf("%d errors\n", errct);
|
---|
101 | exit(1);
|
---|
102 | }
|
---|
103 | }
|
---|