prototype fix
[minix.git] / test / common.c
blob12fb34a0b8fdc5b8cb0a4397de6631fef486d44b
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 */
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <stdio.h>
12 int common_test_nr = -1, errct = 0, subtest;
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));
21 void start(test_nr)
22 int test_nr;
24 char buf[64];
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();
36 sprintf(buf, "DIR_%02d", test_nr);
37 if (chdir(buf) != 0) {
38 e(6666);
39 quit();
43 void rm_rf_dir(test_nr)
44 int test_nr;
46 char buf[128];
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.
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);
60 void rm_rf_ppdir(test_nr)
61 int test_nr;
63 /* Attempt to remove everything in the test directory (== the current dir). */
65 char buf[128];
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);
74 void e(n)
75 int n;
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);
85 errno = 0; /* don't leave it around to confuse next e() */
88 void cleanup()
90 if (chdir("..") == 0 && common_test_nr != -1) rm_rf_dir(common_test_nr);
93 void quit()
95 cleanup();
96 if (errct == 0) {
97 printf("ok\n");
98 exit(0);
99 } else {
100 printf("%d errors\n", errct);
101 exit(1);