[OpenWrt-Devel] firewall3: utils: create a common helper to find strings is files.
diff mbox series

Message ID 20200129115033.22430-1-rsalvaterra@gmail.com
State Not Applicable
Delegated to: Petr Štetiar
Headers show
Series
  • [OpenWrt-Devel] firewall3: utils: create a common helper to find strings is files.
Related show

Commit Message

Rui Salvaterra Jan. 29, 2020, 11:50 a.m. UTC
Both fw3_has_table and fw3_has_target do the same thing. Factor out the
common code into a separate function.

Signed-off-by: Rui Salvaterra <rsalvaterra@gmail.com>
---
 utils.c | 42 ++++++++++++++----------------------------
 1 file changed, 14 insertions(+), 28 deletions(-)

Comments

Rui Salvaterra Jan. 29, 2020, 7:24 p.m. UTC | #1
Please ignore this one for the moment, it seems to be bugged. Sorry for the
noise.

A quarta, 29/01/2020, 11:50, Rui Salvaterra <rsalvaterra@gmail.com>
escreveu:

> Both fw3_has_table and fw3_has_target do the same thing. Factor out the
> common code into a separate function.
>
> Signed-off-by: Rui Salvaterra <rsalvaterra@gmail.com>
> ---
>  utils.c | 42 ++++++++++++++----------------------------
>  1 file changed, 14 insertions(+), 28 deletions(-)
>
> diff --git a/utils.c b/utils.c
> index da65632..228d63f 100644
> --- a/utils.c
> +++ b/utils.c
> @@ -316,23 +316,19 @@ fw3_command_close(void)
>         pipe_pid = -1;
>  }
>
> -bool
> -fw3_has_table(bool ipv6, const char *table)
> +static bool
> +file_contains(const char *path, const char* str)
>  {
>         FILE *f;
> -
>         char line[12];
>         bool seen = false;
>
> -       const char *path = ipv6
> -               ? "/proc/net/ip6_tables_names" :
> "/proc/net/ip_tables_names";
> -
> -       if (!(f = fopen(path, "r")))
> +       if(!(f = fopen(path, "r")))
>                 return false;
>
> -       while (fgets(line, sizeof(line), f))
> +       while(fgets(line, sizeof(line), f))
>         {
> -               if (!strncmp(line, table, strlen(table)))
> +               if (!strcmp(line, str))
>                 {
>                         seen = true;
>                         break;
> @@ -345,31 +341,21 @@ fw3_has_table(bool ipv6, const char *table)
>  }
>
>  bool
> -fw3_has_target(const bool ipv6, const char *target)
> +fw3_has_table(const bool ipv6, const char *table)
>  {
> -       FILE *f;
> +       const char *path = ipv6
> +               ? "/proc/net/ip6_tables_names" :
> "/proc/net/ip_tables_names";
>
> -       char line[12];
> -       bool seen = false;
> +       return file_contains(path, table);
> +}
>
> +bool
> +fw3_has_target(const bool ipv6, const char *target)
> +{
>         const char *path = ipv6
>                 ? "/proc/net/ip6_tables_targets" :
> "/proc/net/ip_tables_targets";
>
> -       if (!(f = fopen(path, "r")))
> -               return false;
> -
> -       while (fgets(line, sizeof(line), f))
> -       {
> -               if (!strcmp(line, target))
> -               {
> -                       seen = true;
> -                       break;
> -               }
> -       }
> -
> -       fclose(f);
> -
> -       return seen;
> +       return file_contains(path, target);
>  }
>
>  bool
> --
> 2.25.0
>
>

Patch
diff mbox series

diff --git a/utils.c b/utils.c
index da65632..228d63f 100644
--- a/utils.c
+++ b/utils.c
@@ -316,23 +316,19 @@  fw3_command_close(void)
 	pipe_pid = -1;
 }
 
-bool
-fw3_has_table(bool ipv6, const char *table)
+static bool
+file_contains(const char *path, const char* str)
 {
 	FILE *f;
-
 	char line[12];
 	bool seen = false;
 
-	const char *path = ipv6
-		? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
-
-	if (!(f = fopen(path, "r")))
+	if(!(f = fopen(path, "r")))
 		return false;
 
-	while (fgets(line, sizeof(line), f))
+	while(fgets(line, sizeof(line), f))
 	{
-		if (!strncmp(line, table, strlen(table)))
+		if (!strcmp(line, str))
 		{
 			seen = true;
 			break;
@@ -345,31 +341,21 @@  fw3_has_table(bool ipv6, const char *table)
 }
 
 bool
-fw3_has_target(const bool ipv6, const char *target)
+fw3_has_table(const bool ipv6, const char *table)
 {
-	FILE *f;
+	const char *path = ipv6
+		? "/proc/net/ip6_tables_names" : "/proc/net/ip_tables_names";
 
-	char line[12];
-	bool seen = false;
+	return file_contains(path, table);
+}
 
+bool
+fw3_has_target(const bool ipv6, const char *target)
+{
 	const char *path = ipv6
 		? "/proc/net/ip6_tables_targets" : "/proc/net/ip_tables_targets";
 
-	if (!(f = fopen(path, "r")))
-		return false;
-
-	while (fgets(line, sizeof(line), f))
-	{
-		if (!strcmp(line, target))
-		{
-			seen = true;
-			break;
-		}
-	}
-
-	fclose(f);
-
-	return seen;
+	return file_contains(path, target);
 }
 
 bool