Don't free bin_dir.
[smatch.git] / smatch.c
blob3eb5dcebcd839e72224d80d17a5150f7d1b08a6a
1 /*
2 * sparse/smatch.c
4 * Copyright (C) 2006 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
10 #include <stdio.h>
11 #include <unistd.h>
12 #include <libgen.h>
13 #include "smatch.h"
15 char *data_dir;
16 int option_no_data = 0;
18 typedef void (*reg_func) (int id);
19 void register_smatch_extra(int id);
20 void register_smatch_ignore(int id);
21 void register_implications(int id);
22 void register_function_hooks(int id);
23 void check_null_deref(int id);
24 void check_overflow(int id);
25 void check_locking(int id);
26 void check_memory(int id);
27 void check_frees_argument(int id);
28 void check_leaks(int id);
29 void check_type(int id);
30 void check_allocation_funcs(int id);
31 /* <- your test goes here */
32 /* void register_template(int id); */
34 static const reg_func reg_funcs[] = {
35 &register_smatch_extra, /* smatch_extra always has to be first */
36 &register_smatch_ignore,
37 &check_null_deref,
38 &check_overflow,
39 &check_locking,
40 &check_memory,
41 // &check_type,
42 // &check_allocation_funcs,
43 // &check_leaks,
44 // &check_frees_argument,
46 /* <- your test goes here */
47 /* &register_template, */
49 &register_implications, /* implications always has to be last */
50 NULL
53 static void help(void)
55 printf("Usage: smatch [smatch arguments][sparse arguments] file.c\n");
56 printf("--debug: print lots of debug output.\n");
57 printf("--debug-implied: print debug output about implications.\n");
58 printf("--no-implied: ignore implications.\n");
59 printf("--assume-loops: assume loops always go through at least once.\n");
60 printf("--known-conditions: don't branch for known conditions.\n");
61 printf("--no-data: do not use the /smatch_data/ directory.\n");
62 printf("--help: print this helpfull message.\n");
63 exit(1);
66 void parse_args(int *argcp, char ***argvp)
68 while(*argcp >= 2) {
69 if (!strcmp((*argvp)[1], "--debug")) {
70 debug_states = 1;
71 (*argvp)[1] = (*argvp)[0];
72 } else if (!strcmp((*argvp)[1], "--debug-implied")) {
73 debug_implied_states = 1;
74 (*argvp)[1] = (*argvp)[0];
75 } else if (!strcmp((*argvp)[1], "--no-implied")) {
76 option_no_implied = 1;
77 (*argvp)[1] = (*argvp)[0];
78 } else if (!strcmp((*argvp)[1], "--assume-loops")) {
79 option_assume_loops = 1;
80 (*argvp)[1] = (*argvp)[0];
81 } else if (!strcmp((*argvp)[1], "--known-conditions")) {
82 option_known_conditions = 1;
83 (*argvp)[1] = (*argvp)[0];
84 } else if (!strcmp((*argvp)[1], "--no-data")) {
85 option_no_data = 1;
86 (*argvp)[1] = (*argvp)[0];
87 } else if (!strcmp((*argvp)[1], "--help")) {
88 help();
89 } else {
90 break;
92 (*argcp)--;
93 (*argvp)++;
97 static char *get_data_dir(char *arg0)
99 char *bin_dir;
100 char buf[256];
101 char *dir;
103 if (option_no_data) {
104 return NULL;
106 bin_dir = dirname(alloc_string(arg0));
107 strncpy(buf, bin_dir, 254);
108 buf[255] = '\0';
109 strncat(buf, "/smatch_data/", 254);
110 dir = alloc_string(buf);
111 if (!access(dir, R_OK))
112 return dir;
113 printf("Warning: %s is not accessible.\n", dir);
114 printf("Use --no-data to suppress this message.\n");
115 return NULL;
118 int main(int argc, char **argv)
120 int i;
121 reg_func func;
123 parse_args(&argc, &argv);
125 data_dir = get_data_dir(argv[0]);
127 /* The script IDs start at 1.
128 0 is used for internal stuff. */
129 create_function_hash();
130 for(i = 0; (func = reg_funcs[i]); i++){
131 func(i + 1);
133 register_function_hooks(-1);
135 smatch(argc, argv);
136 free_string(data_dir);
137 return 0;