From: Vadim Kochan <vadi...@gmail.com>

Move piece of code from netsniff-ng.c to str.c as func which
converts cmdline args vector to string.

Signed-off-by: Vadim Kochan <vadi...@gmail.com>
---
 netsniff-ng.c | 18 ++----------------
 str.c         | 23 +++++++++++++++++++++++
 str.h         |  1 +
 3 files changed, 26 insertions(+), 16 deletions(-)

diff --git a/netsniff-ng.c b/netsniff-ng.c
index 91356ba..6720c73 100644
--- a/netsniff-ng.c
+++ b/netsniff-ng.c
@@ -1481,22 +1481,8 @@ int main(int argc, char **argv)
                }
        }
 
-       if (!ctx.filter && optind != argc) {
-               int ret;
-               off_t offset = 0;
-
-               for (i = optind; i < argc; ++i) {
-                       size_t alen = strlen(argv[i]) + 2;
-                       size_t flen = ctx.filter ? strlen(ctx.filter) : 0;
-
-                       ctx.filter = xrealloc(ctx.filter, flen + alen);
-                       ret = slprintf(ctx.filter + offset, strlen(argv[i]) + 
2, "%s ", argv[i]);
-                       if (ret < 0)
-                               panic("Cannot concatenate filter string!\n");
-                       else
-                               offset += ret;
-               }
-       }
+       if (!ctx.filter && optind != argc)
+               ctx.filter = cmdline_args2str(optind, argc, argv);
 
        if (!ctx.device_in)
                ctx.device_in = xstrdup("any");
diff --git a/str.c b/str.c
index 7a5cb49..ec221af 100644
--- a/str.c
+++ b/str.c
@@ -9,6 +9,8 @@
 #include <stdarg.h>
 
 #include "str.h"
+#include "die.h"
+#include "xmalloc.h"
 
 size_t strlcpy(char *dest, const char *src, size_t size)
 {
@@ -86,3 +88,24 @@ char *strtrim_right(char *p, char c)
 
        return p;
 }
+
+char *cmdline_args2str(int from_idx, int argc, char **argv)
+{
+       off_t offset = 0;
+       char *str = NULL;
+       int ret, i;
+
+       for (i = from_idx; i < argc; ++i) {
+               size_t alen = strlen(argv[i]) + 2;
+               size_t slen = str ? strlen(str) : 0;
+
+               str = xrealloc(str, slen + alen);
+               ret = slprintf(str + offset, strlen(argv[i]) + 2, "%s ", 
argv[i]);
+               if (ret < 0)
+                       panic("Cannot concatenate string!\n");
+               else
+                       offset += ret;
+       }
+
+       return str;
+}
diff --git a/str.h b/str.h
index 7b8916c..56456a0 100644
--- a/str.h
+++ b/str.h
@@ -8,5 +8,6 @@ extern int slprintf(char *dst, size_t size, const char *fmt, 
...)  __check_forma
 extern int slprintf_nocheck(char *dst, size_t size, const char *fmt, ...);
 extern char *strtrim_right(char *p, char c);
 extern noinline void *xmemset(void *s, int c, size_t n);
+extern char *cmdline_args2str(int from_idx, int argc, char **argv);
 
 #endif /* STR_H */
-- 
2.4.2

-- 
You received this message because you are subscribed to the Google Groups 
"netsniff-ng" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to netsniff-ng+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to