pkgin_all: script to auto-install all packages
[minix.git] / test / common.c
blob40b2fccc6f1fd614ccb51b018b1dff5689cad078
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 #define e(errn) e_f(__FILE__, __LINE__, (errn))
17 void cleanup(void);
18 int does_fs_truncate(void);
19 void e_f(char *file, int lineno, int n);
20 int name_max(char *path);
21 void quit(void);
22 void rm_rf_dir(int test_nr);
23 void rm_rf_ppdir(int test_nr);
24 void start(int test_nr);
26 void start(test_nr)
27 int test_nr;
29 char buf[64];
30 int i;
32 common_test_nr = test_nr;
33 printf("Test %2d ", test_nr);
34 fflush(stdout); /* since stdout is probably line buffered */
35 sync();
36 rm_rf_dir(test_nr);
37 sprintf(buf, "mkdir DIR_%02d", test_nr);
38 if (system(buf) != 0) {
39 e(666);
40 quit();
42 sprintf(buf, "DIR_%02d", test_nr);
43 if (chdir(buf) != 0) {
44 e(6666);
45 quit();
48 for (i = 3; i < OPEN_MAX; ++i) {
49 /* Close all files except stdin, stdout, and stderr */
50 (void) close(i);
54 int does_fs_truncate(void)
56 struct statvfs stvfs;
57 int does_truncate = 0;
58 char cwd[PATH_MAX]; /* Storage for path to current working dir */
60 if (realpath(".", cwd) == NULL) e(7777); /* Get current working dir */
61 if (statvfs(cwd, &stvfs) != 0) e(7778); /* Get FS information */
62 /* Depending on how an FS handles too long file names, we have to adjust our
63 * error checking. If an FS does not truncate file names, it should generate
64 * an ENAMETOOLONG error when we provide too long a file name.
66 if (!(stvfs.f_flag & ST_NOTRUNC)) does_truncate = 1;
68 return(does_truncate);
71 int name_max(char *path)
73 struct statvfs stvfs;
75 if (statvfs(path, &stvfs) != 0) e(7779);
76 return(stvfs.f_namemax);
80 void rm_rf_dir(test_nr)
81 int test_nr;
83 char buf[128];
85 sprintf(buf, "rm -rf DIR_%02d >/dev/null 2>&1", test_nr);
86 if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
89 void rm_rf_ppdir(test_nr)
90 int test_nr;
92 /* Attempt to remove everything in the test directory (== the current dir). */
94 char buf[128];
96 sprintf(buf, "chmod 777 ../DIR_%02d/* ../DIR_%02d/*/* >/dev/null 2>&1",
97 test_nr, test_nr);
98 (void) system(buf);
99 sprintf(buf, "rm -rf ../DIR_%02d >/dev/null 2>&1", test_nr);
100 if (system(buf) != 0) printf("Warning: system(\"%s\") failed\n", buf);
103 void e_f(char *file, int line, int n)
105 int err_number;
106 err_number = errno; /* Store before printf can clobber it */
107 if (errct == 0) printf("\n"); /* finish header */
108 printf("%s:%d: Subtest %d, error %d, errno %d: %s\n",
109 file, line, subtest, n, errno, strerror(errno));
110 if (++errct > MAX_ERROR) {
111 printf("Too many errors; test aborted\n");
112 cleanup();
113 exit(1);
115 errno = err_number;
118 void cleanup()
120 if (chdir("..") == 0 && common_test_nr != -1) rm_rf_dir(common_test_nr);
123 void quit()
125 cleanup();
126 if (errct == 0) {
127 printf("ok\n");
128 exit(0);
129 } else {
130 printf("%d errors\n", errct);
131 exit(1);