tools: use tal instead of talloc.
[ccan] / tools / ccanlint / tests / license_comment.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/foreach/foreach.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <unistd.h>
7 #include <limits.h>
8 #include <errno.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <err.h>
12 #include <ccan/str/str.h>
13
14 static void check_license_comment(struct manifest *m,
15                                   unsigned int *timeleft, struct score *score)
16 {
17         struct list_head *list;
18
19         /* No requirements on public domain. */
20         if (m->license == LICENSE_PUBLIC_DOMAIN
21             || m->license == LICENSE_UNKNOWN) {
22                 score->pass = true;
23                 score->score = score->total;
24                 return;
25         }
26
27         foreach_ptr(list, &m->c_files, &m->h_files) {
28                 struct ccan_file *f;
29
30                 list_for_each(list, f, list) {
31                         unsigned int i;
32                         char **lines = get_ccan_file_lines(f);
33                         struct line_info *info = get_ccan_line_info(f);
34                         bool found_license = false, found_flavor = false;
35
36                         for (i = 0; lines[i]; i++) {
37                                 if (info[i].type == CODE_LINE)
38                                         break;
39                                 if (strstr(lines[i], "LICENSE"))
40                                         found_license = true;
41                                 if (strstr(lines[i],
42                                            licenses[m->license].shortname))
43                                         found_flavor = true;
44                         }
45                         if ((!found_license || !found_flavor)
46                             && !find_boilerplate(f, m->license)) {
47                                 score_file_error(score, f, lines[i] ? i : 0,
48                                                  "No reference to license"
49                                                  " found");
50                         }
51                 }
52         }
53
54         if (list_empty(&score->per_file_errors)) {
55                 score->pass = true;
56                 score->score = score->total;
57         }
58 }
59
60 struct ccanlint license_comment = {
61         .key = "license_comment",
62         .name = "Source and header files refer to LICENSE",
63         .check = check_license_comment,
64         .needs = "license_exists"
65 };
66 REGISTER_TEST(license_comment);