Now we have helper to iterate over entries in /proc/net/dev we can
simplify and cleanup do_tunnels_list() in ip/iptunnel.c.

While there replace printf("\n") with fputc('\n', stdout) and printf()
with fputs() where string does not contain format specifiers.

Signed-off-by: Serhey Popovych <serhe.popov...@gmail.com>
---
 ip/ip6tunnel.c |   94 ++++++++++++++++++++++----------------------------------
 1 file changed, 37 insertions(+), 57 deletions(-)

diff --git a/ip/ip6tunnel.c b/ip/ip6tunnel.c
index 783e28a..ccd5603 100644
--- a/ip/ip6tunnel.c
+++ b/ip/ip6tunnel.c
@@ -336,68 +336,48 @@ static int ip6_tnl_parm_match(const struct ip6_tnl_parm2 
*p1,
                (!p1->flags || (p1->flags & p2->flags)));
 }
 
-static int do_tunnels_list(struct ip6_tnl_parm2 *p)
+static pnd_result_t do_tunnels_list(char *name, char *stats, void *arg)
 {
-       char buf[512];
-       int err = -1;
-       FILE *fp = fopen("/proc/net/dev", "r");
+       struct ip6_tnl_parm2 p1, *p = arg;
+       int index, type;
 
-       if (fp == NULL) {
-               perror("fopen");
-               return -1;
-       }
+       if (p->name[0] && strcmp(p->name, name))
+               return PND_NEXT;
+
+       index = ll_name_to_index(name);
+       if (index <= 0)
+               return PND_NEXT;
 
-       /* skip two lines at the begenning of the file */
-       if (!fgets(buf, sizeof(buf), fp) ||
-           !fgets(buf, sizeof(buf), fp)) {
-               fprintf(stderr, "/proc/net/dev read error\n");
-               goto end;
+       type = ll_index_to_type(index);
+       if (type == -1) {
+               fprintf(stderr, "Failed to get type of \"%s\"\n", name);
+               return PND_NEXT;
        }
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
-               char name[IFNAMSIZ];
-               int index, type;
-               struct ip6_tnl_parm2 p1 = {};
-               char *ptr;
-
-               buf[sizeof(buf) - 1] = '\0';
-               if ((ptr = strchr(buf, ':')) == NULL ||
-                   (*ptr++ = 0, sscanf(buf, "%s", name) != 1)) {
-                       fprintf(stderr, "Wrong format for /proc/net/dev. Giving 
up.\n");
-                       goto end;
-               }
-               if (p->name[0] && strcmp(p->name, name))
-                       continue;
-               index = ll_name_to_index(name);
-               if (index == 0)
-                       continue;
-               type = ll_index_to_type(index);
-               if (type == -1) {
-                       fprintf(stderr, "Failed to get type of \"%s\"\n", name);
-                       continue;
-               }
-               if (type != ARPHRD_TUNNEL6 && type != ARPHRD_IP6GRE)
-                       continue;
-               ip6_tnl_parm_init(&p1, 0);
-               if (type == ARPHRD_IP6GRE)
-                       p1.proto = IPPROTO_GRE;
-               strcpy(p1.name, name);
-               p1.link = ll_name_to_index(p1.name);
-               if (p1.link == 0)
-                       continue;
-               if (tnl_get_ioctl(p1.name, &p1))
-                       continue;
-               if (!ip6_tnl_parm_match(p, &p1))
-                       continue;
-               print_tunnel(&p1);
-               if (show_stats)
-                       tnl_print_stats(ptr);
-               printf("\n");
+       switch (type) {
+       case ARPHRD_TUNNEL6:
+       case ARPHRD_IP6GRE:
+               return PND_NEXT;
        }
-       err = 0;
- end:
-       fclose(fp);
-       return err;
+
+       ip6_tnl_parm_init(&p1, 0);
+       if (type == ARPHRD_IP6GRE)
+               p1.proto = IPPROTO_GRE;
+       p1.link = index;
+       name = strcpy(p1.name, name);
+
+       if (tnl_get_ioctl(name, &p1))
+               return PND_NEXT;
+
+       if (!ip6_tnl_parm_match(p, &p1))
+               return PND_NEXT;
+
+       print_tunnel(&p1);
+       if (show_stats)
+               tnl_print_stats(stats);
+       fputc('\n', stdout);
+
+       return PND_NEXT;
 }
 
 static int do_show(int argc, char **argv)
@@ -412,7 +392,7 @@ static int do_show(int argc, char **argv)
                return -1;
 
        if (!p.name[0] || show_stats)
-               do_tunnels_list(&p);
+               do_each_proc_net_dev(do_tunnels_list, &p);
        else {
                if (tnl_get_ioctl(p.name, &p))
                        return -1;
-- 
1.7.10.4

Reply via email to