1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 
5 #include <CUnit/Basic.h>
6 
7 #include "options.h"
8 #include "tests.h"
9 
10 #define NO_STDERR
11 
12 #ifdef NO_STDERR
13 #include <fcntl.h>
14 #include <unistd.h>
15 #endif
16 
main(int argc,char ** argv)17 int main(int argc, char** argv)
18 {
19   struct gengetopt_args_info info;
20 
21   if (options(argc, argv, &info) != 0)
22     {
23       fprintf(stderr,"failed to parse command line\n");
24       return EXIT_FAILURE;
25     }
26   bool verbose = info.verbose_given;
27 
28   CU_BasicRunMode mode = (verbose ? CU_BRM_VERBOSE : CU_BRM_SILENT);
29   CU_ErrorAction error_action = CUEA_IGNORE;
30   setvbuf(stdout, NULL, _IONBF, 0);
31 
32   if (CU_initialize_registry())
33     {
34       fprintf(stderr,"failed to initialise registry\n");
35       return EXIT_FAILURE;
36     }
37 
38   tests_load();
39   CU_basic_set_mode(mode);
40   CU_set_error_action(error_action);
41 
42 #ifdef NO_STDERR
43 
44   int devnull = open("/dev/null", O_RDWR|O_CREAT|O_APPEND, 0600);
45   int save_err = dup(fileno(stderr));
46 
47   if (dup2(devnull, fileno(stderr)) == -1)
48     {
49       printf("failed to redirect stderr\n");
50       return EXIT_FAILURE;
51     }
52 
53 #endif
54 
55   int status = CU_basic_run_tests();
56 
57 #ifdef NO_STDERR
58 
59   fflush(stderr);
60   close(devnull);
61 
62   dup2(save_err, fileno(stderr));
63   close(save_err);
64 
65 #endif
66 
67   int nfail = CU_get_number_of_failures();
68 
69   if (verbose)
70     printf("\nSuite %s: %d failed\n",
71 	   (status == 0 ? "OK" : "errored"),
72 	   nfail);
73 
74   CU_cleanup_registry();
75   options_free(&info);
76 
77   return (nfail > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
78 }
79