v2: Actually make it work. Use strncmp instead of strcmp (strangely
enough, strcmp fails for the ip*names case, but I don't understand why).

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 <[email protected]>
---
 utils.c | 38 ++++++++++++--------------------------
 utils.h |  7 ++++++-
 2 files changed, 18 insertions(+), 27 deletions(-)

diff --git a/utils.c b/utils.c
index da65632..6289ec9 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")))
                return false;
 
        while (fgets(line, sizeof(line), f))
        {
-               if (!strncmp(line, table, strlen(table)))
+               if (!strncmp(line, str, MIN(sizeof(line), strlen(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
diff --git a/utils.h b/utils.h
index 254bea4..ff58cc6 100644
--- a/utils.h
+++ b/utils.h
@@ -55,6 +55,11 @@ void error(const char *format, ...)
 void info(const char *format, ...)
        __attribute__ ((format (printf, 1, 2)));
 
+#define MIN(x, y) ({           \
+       typeof(x) _x = (x);     \
+       typeof(y) _y = (y);     \
+       (void) (&_x == &_y);    \
+       _x < _y ? _x : _y; })
 
 #define warn_section(t, r, e, fmt, ...)                                        
\
        do {                                                                    
\
@@ -103,7 +108,7 @@ void fw3_command_close(void);
 void fw3_pr(const char *fmt, ...)
        __attribute__ ((format (printf, 1, 2)));
 
-bool fw3_has_table(bool ipv6, const char *table);
+bool fw3_has_table(const bool ipv6, const char *table);
 
 bool fw3_has_target(const bool ipv6, const char *target);
 
-- 
2.25.0


_______________________________________________
openwrt-devel mailing list
[email protected]
https://lists.openwrt.org/mailman/listinfo/openwrt-devel

Reply via email to