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

Rui Salvaterra rsalvaterra at gmail.com
Wed Jan 29 14:24:57 EST 2020


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 at 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 at 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/openwrt-devel/attachments/20200129/6fd84e4a/attachment.htm>
-------------- next part --------------
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/mailman/listinfo/openwrt-devel


More information about the openwrt-devel mailing list