<div dir="auto"><div>Please ignore this one for the moment, it seems to be bugged. Sorry for the noise.<br><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">A quarta, 29/01/2020, 11:50, Rui Salvaterra <<a href="mailto:rsalvaterra@gmail.com">rsalvaterra@gmail.com</a>> escreveu:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Both fw3_has_table and fw3_has_target do the same thing. Factor out the<br>
common code into a separate function.<br>
<br>
Signed-off-by: Rui Salvaterra <<a href="mailto:rsalvaterra@gmail.com" target="_blank" rel="noreferrer">rsalvaterra@gmail.com</a>><br>
---<br>
utils.c | 42 ++++++++++++++----------------------------<br>
1 file changed, 14 insertions(+), 28 deletions(-)<br>
<br>
diff --git a/utils.c b/utils.c<br>
index da65632..228d63f 100644<br>
--- a/utils.c<br>
+++ b/utils.c<br>
@@ -316,23 +316,19 @@ fw3_command_close(void)<br>
pipe_pid = -1;<br>
}<br>
<br>
-bool<br>
-fw3_has_table(bool ipv6, const char *table)<br>
+static bool<br>
+file_contains(const char *path, const char* str)<br>
{<br>
FILE *f;<br>
-<br>
char line[12];<br>
bool seen = false;<br>
<br>
- const char *path = ipv6<br>
- ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";<br>
-<br>
- if (!(f = fopen(path, "r")))<br>
+ if(!(f = fopen(path, "r")))<br>
return false;<br>
<br>
- while (fgets(line, sizeof(line), f))<br>
+ while(fgets(line, sizeof(line), f))<br>
{<br>
- if (!strncmp(line, table, strlen(table)))<br>
+ if (!strcmp(line, str))<br>
{<br>
seen = true;<br>
break;<br>
@@ -345,31 +341,21 @@ fw3_has_table(bool ipv6, const char *table)<br>
}<br>
<br>
bool<br>
-fw3_has_target(const bool ipv6, const char *target)<br>
+fw3_has_table(const bool ipv6, const char *table)<br>
{<br>
- FILE *f;<br>
+ const char *path = ipv6<br>
+ ? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";<br>
<br>
- char line[12];<br>
- bool seen = false;<br>
+ return file_contains(path, table);<br>
+}<br>
<br>
+bool<br>
+fw3_has_target(const bool ipv6, const char *target)<br>
+{<br>
const char *path = ipv6<br>
? "/proc/net/ip6_tables_targets" : "/proc/net/ip_tables_targets";<br>
<br>
- if (!(f = fopen(path, "r")))<br>
- return false;<br>
-<br>
- while (fgets(line, sizeof(line), f))<br>
- {<br>
- if (!strcmp(line, target))<br>
- {<br>
- seen = true;<br>
- break;<br>
- }<br>
- }<br>
-<br>
- fclose(f);<br>
-<br>
- return seen;<br>
+ return file_contains(path, target);<br>
}<br>
<br>
bool<br>
-- <br>
2.25.0<br>
<br>
</blockquote></div></div></div>