forked from pi-hole/FTL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grep.c
146 lines (123 loc) · 2.94 KB
/
grep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Pi-hole: A black hole for Internet advertisements
* (c) 2017 Pi-hole, LLC (https://pi-hole.net)
* Network-wide ad blocking via your own hardware.
*
* FTL Engine
* grep-like routines
*
* This file is copyright under the latest version of the EUPL.
* Please see LICENSE file for your rights under this license. */
#include "FTL.h"
char ** wildcarddomains = NULL;
unsigned char blockingstatus = 2;
int countlines(const char* fname)
{
FILE *fp;
int ch = 0, lines = 0, chars = 0;
if((fp = fopen(fname, "r")) == NULL) {
return -1;
}
while ((ch = fgetc(fp)) != EOF)
{
chars++;
if (ch=='\n')
{
// Add one to the lines counter
++lines;
// Reset chars counter
chars = 0;
}
}
// Add one more line if there were characters at the
// last line of the file even without a final "\n"
if(chars > 0)
++lines;
// Close the file
fclose(fp);
return lines;
}
int readnumberfromfile(const char* fname)
{
FILE *fp;
int num;
if((fp = fopen(fname, "r")) == NULL)
{
return -1;
}
if(fscanf(fp,"%i",&num) != 1)
{
num = -1;
}
fclose(fp);
return num;
}
int countlineswith(const char* str, const char* fname)
{
FILE *fp;
int found = 0;
char *buffer = NULL;
size_t size = 0;
if((fp = fopen(fname, "r")) == NULL) {
return -1;
}
// Search through file
// getline reads a string from the specified file up to either a
// newline character or EOF
while(getline(&buffer, &size, fp) != -1)
{
// Strip potential newline character at the end of line we just read
if(buffer[strlen(buffer)-1] == '\n')
buffer[strlen(buffer)-1] = '\0';
// Search for exact match
if(strcmp(buffer, str) == 0)
{
found++;
continue;
}
// If line starts with *, search for partial match of
// needle "buffer+1" in haystack "str"
if(buffer[0] == '*')
{
char * buf = strstr(str, buffer+1);
// The strstr() function finds the first occurrence of
// the substring buffer+1 in the string str.
// These functions return a pointer to the beginning of
// the located substring, or NULL if the substring is not
// found. Hence, we compare the length of the substring to
// the wildcard entry to rule out the possiblity that
// there is anything behind the wildcard. This avoids that given
// "*example.com" "example.com.xxxxx" would also match.
if(buf != NULL && strlen(buf) == strlen(buffer+1))
found++;
}
}
// Free allocated memory
if(buffer != NULL)
{
free(buffer);
buffer = NULL;
}
// Close the file
fclose(fp);
return found;
}
void check_blocking_status(void)
{
char* blocking = read_setupVarsconf("BLOCKING_ENABLED");
char* message;
if(blocking == NULL || getSetupVarsBool(blocking))
{
// Parameter either not present in setupVars.conf
// or explicitly set to true
blockingstatus = BLOCKING_ENABLED;
message = "enabled";
clearSetupVarsArray();
}
else
{
// Disabled
blockingstatus = BLOCKING_DISABLED;
message = "disabled";
}
if(debug) logg("Blocking status is %s", message);
}