Fixed TC-2074: Static analysis issues
[platform/upstream/corewatcher.git] / src / configfile.c
1 #define _GNU_SOURCE
2 /*
3  * Copyright 2007,2012 Intel Corporation
4  *
5  * This program file is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; version 2 of the License.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program in a file named COPYING; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301 USA
19  *
20  * Authors:
21  *      Arjan van de Ven <arjan@linux.intel.com>
22  *      William Douglas <william.douglas@intel.com>
23  *      Tim Pepper <timothy.c.pepper@linux.intel.com>
24  */
25
26 #include <unistd.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <glib.h>
31
32 #include "corewatcher.h"
33
34 char *submit_url[MAX_URLS];
35 int url_count = 0;
36
37 void read_config_file(char *filename)
38 {
39         FILE *file = NULL;
40         char *line = NULL, *line_end = NULL;
41         size_t line_len = 0;
42
43         file = fopen(filename, "r");
44         if (!file)
45                 return;
46         while (!feof(file)) {
47                 char *c = NULL;
48                 char *n = NULL;
49
50                 if (getline(&line, &line_len, file) == -1)
51                         break;
52
53                 if (line[0] == '#')
54                         continue;
55
56                 /* we don't care about any lines that are too short to have config options */
57                 if (line_len < 5)
58                         continue;
59
60                 /* remove trailing\n */
61                 n = strchr(line, '\n');
62                 if (n) *n = 0;
63
64                 line_end = line + line_len;
65
66                 c = strstr(line, "allow-submit");
67                 if (c) {
68                         c+=13;
69                         if (c < line_end) {
70                                 if (!strstr(c, "yes")) {
71                                         printf("Error: allow-submit!=yes in config file.\n");
72                                         printf("Corewatcher will not run.\n");
73                                         exit(-1);
74                                 }
75                         }
76                 }
77
78                 c = strstr(line, "allow-pass-on");
79                 if (c) {
80                         c+=14;
81                         if (c < line_end) {
82                                 if (!strstr(c, "yes")) {
83                                         printf("Error: allow-pass-on!=yes in config file.\n");
84                                         printf("Corewatcher will not run.\n");
85                                         exit(-1);
86                                 }
87                         }
88                 }
89
90                 c = strstr(line, "submit-url");
91                 if (c && url_count <= MAX_URLS) {
92                         c += 11;
93                         if (c < line_end) {
94                                 c = strstr(c, "http");
95                                 if (c) {
96                                         submit_url[url_count] = strdup(c);
97                                         if (!submit_url[url_count])
98                                                 submit_url[url_count] = NULL;
99                                         else
100                                                 url_count++;
101                                 }
102                         }
103                 }
104         }
105
106         fclose(file);
107         free(line);
108
109         if (!url_count) {
110                 submit_url[url_count] = strdup("https://tz.otcshare.org/crashdb/crash_submit/");
111                 if (!submit_url[url_count])
112                         submit_url[url_count] = NULL;
113                 else
114                         url_count++;
115         }
116 }