sys/arch/x86/include updates
[minix.git] / test / common.c
blob7807d79da6b19caf318ebf96a44205baa2100b35
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>
11 #include <sys/statvfs.h>
13 int common_test_nr = -1, errct = 0, subtest;
15 void cleanup(void);
16 int does_fs_truncate(void);
17 void e(int n);
18 int name_max(char *path);
19 void quit(void);
20 void rm_rf_dir(int test_nr);
21 void rm_rf_ppdir(int test_nr);
22 void start(int test_nr);
24 void start(test_nr)
25 int test_nr;
27 char buf[64];
28 int i;
30 common_test_nr = test_nr;
31 printf("Test %2d ", test_nr);
32 fflush(stdout); /* since stdout is probably line buffered */
33 sync();
34 rm_rf_dir(test_nr);
35 sprintf(buf, "mkdir DIR_%02d", test_nr);
36 if (system(buf) != 0) {
37 e(666);
38 quit();
40 sprintf(buf, "DIR_%02d", test_nr);
41 if (chdir(buf) != 0) {
42 e(6666);
43 quit();
46 for (i = 3; i < OPEN_MAX; ++i) {
47 /* Close all files except stdin, stdout, and stderr */
48 (void) close(i);
52 int does_fs_truncate(void)
54 struct statvfs stvfs;
55 int does_truncate = 0;
56 char cwd[PATH_MAX]; /* Storage for path to current working dir */
58 if (realpath(".", cwd) == NULL) e(7777); /* Get current working dir */
59 if (statvfs(cwd, &stvfs) != 0) e(7778); /* Get FS information */
60 /* Depending on how an FS handles too long file names, we have to adjust our
61 * error checking. If an FS does not truncate file names, it should generate
62 * an ENAMETOOLONG error when we provide too long a file name.
64 if (!(stvfs.f_flag & ST_NOTRUNC)) does_truncate = 1;
66 return(does_truncate);
69 int name_max(char *path)
71 struct statvfs stvfs;
73 if (statvfs(path, &stvfs) != 0) e(7779);
74 return(stvfs.f_namemax);
78 void rm_rf_dir(test_nr)
79 int test_nr;
81 char buf[128];
83 sprintf(buf, "rm -rf DIR_%02d >/dev/null 2>&1", test_nr);
84 if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
87 void rm_rf_ppdir(test_nr)
88 int test_nr;
90 /* Attempt to remove everything in the test directory (== the current dir). */
92 char buf[128];
94 sprintf(buf, "chmod 777 ../DIR_%02d/* ../DIR_%02d/*/* >/dev/null 2>&1",
95 test_nr, test_nr);
96 (void) system(buf);
97 sprintf(buf, "rm -rf ../DIR_%02d >/dev/null 2>&1", test_nr);
98 if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
101 void e(n)
102 int n;
104 int err_number;
105 err_number = errno; /* Store before printf can clobber it */
106 if (errct == 0) printf("\n"); /* finish header */
107 printf("Subtest %d, error %d, errno %d: %s\n",
108 subtest, n, errno, strerror(errno));
109 if (++errct > MAX_ERROR) {
110 printf("Too many errors; test aborted\n");
111 cleanup();
112 exit(1);
114 errno = err_number;
117 void cleanup()
119 if (chdir("..") == 0 && common_test_nr != -1) rm_rf_dir(common_test_nr);
122 void quit()
124 cleanup();
125 if (errct == 0) {
126 printf("ok\n");
127 exit(0);
128 } else {
129 printf("%d errors\n", errct);
130 exit(1);