]> granicus.if.org Git - vnstat/commitdiff
fix interface list ordering and simplify iflist structure
authorTeemu Toivola <git@humdi.net>
Fri, 28 Jun 2019 22:22:09 +0000 (01:22 +0300)
committerTeemu Toivola <git@humdi.net>
Fri, 28 Jun 2019 22:22:09 +0000 (01:22 +0300)
src/dbsql.c
src/ifinfo.c
src/iflist.c
src/iflist.h
tests/iflist_tests.c

index 0ffc4f36444a3d88281833f1531657e77f97c579..4852dc0236c5f7b27c8c02a462f15a8d0251867d 100644 (file)
@@ -706,11 +706,10 @@ int db_getiflist_sorted(iflist **ifl, const int orderbytraffic)
        char *sql;
        sqlite3_stmt *sqlstmt;
 
-       /* last entry added to list is the first entry when the list is read */
        if (!orderbytraffic) {
-               sql = "select name from interface order by name desc";
+               sql = "select name from interface order by name asc";
        } else {
-               sql = "select name from interface order by rxtotal+txtotal asc";
+               sql = "select name from interface order by rxtotal+txtotal desc";
        }
 
        rc = sqlite3_prepare_v2(db, sql, -1, &sqlstmt, NULL);
index a05852794f3d09026ec0cda247235f3f43dee294..a4e9893a1cb367c78adc4e4051d698d5836ea15d 100644 (file)
@@ -76,11 +76,6 @@ int getifliststring(char **ifacelist, int showspeed)
 
                ifl_iterator = ifl;
 
-               /* seek to end of list to show entries in original input order */
-               while (ifl_iterator->next != NULL) {
-                       ifl_iterator = ifl_iterator->next;
-               }
-
                while (ifl_iterator != NULL) {
                        *ifacelist = (char *)realloc(*ifacelist, ((strlen(*ifacelist) + strlen(ifl_iterator->interface) + 2) * sizeof(char)));
                        if (*ifacelist == NULL) {
@@ -98,7 +93,7 @@ int getifliststring(char **ifacelist, int showspeed)
                                strcat(*ifacelist, temp);
                        }
 
-                       ifl_iterator = ifl_iterator->prev;
+                       ifl_iterator = ifl_iterator->next;
                }
 
                iflistfree(&ifl);
index 0b8307816ea74bd9a060e7028dd9da4684cfd204..e96416842467aa67936ca21c68e47e45bd55ff34 100644 (file)
@@ -3,22 +3,24 @@
 
 int iflistadd(iflist **ifl, const char *iface, const uint32_t bandwidth)
 {
-       iflist *newif;
+       iflist *newif = NULL, *ifl_iterator = *ifl;
 
        newif = malloc(sizeof(iflist));
        if (newif == NULL) {
                return 0;
        }
 
-       newif->next = *ifl;
-       newif->prev = NULL;
+       newif->next = NULL;
 
        if (*ifl != NULL) {
-               (*ifl)->prev = newif;
+               while (ifl_iterator->next != NULL) {
+                       ifl_iterator = ifl_iterator->next;
+               }
+               ifl_iterator->next = newif;
+       } else {
+               *ifl = newif;
        }
 
-       *ifl = newif;
-
        strncpy_nt(newif->interface, iface, 32);
        newif->bandwidth = bandwidth;
 
index 4de7b68a416bae1188df7605c53ca35a17a04cd8..a267702da39d31257eb0b692a3d19d6d0130aded 100644 (file)
@@ -5,7 +5,6 @@ typedef struct iflist {
        char interface[32];
        uint32_t bandwidth;
        struct iflist *next;
-       struct iflist *prev;
 } iflist;
 
 int iflistadd(iflist **ifl, const char *iface, const uint32_t bandwidth);
index 5f00567186b6fca75ffa973873b23abe506c9baa..474b685938ca68d71d61d5d51c6354ee3c82afa3 100644 (file)
@@ -18,31 +18,28 @@ START_TEST(iflistadd_can_add)
 
     ret = iflistadd(&ifl, "eth0", 0);
     ck_assert_int_eq(ret, 1);
-    ck_assert_int_eq(ifl->bandwidth, 0);
     ck_assert_str_eq(ifl->interface, "eth0");
+    ck_assert_int_eq(ifl->bandwidth, 0);
     ck_assert_ptr_eq(ifl->next, NULL);
-    ck_assert_ptr_eq(ifl->prev, NULL);
 
     ret = iflistadd(&ifl, "eth1", 1);
     ck_assert_int_eq(ret, 1);
-    ck_assert_int_eq(ifl->bandwidth, 1);
-    ck_assert_str_eq(ifl->interface, "eth1");
+    ck_assert_str_eq(ifl->interface, "eth0");
+    ck_assert_int_eq(ifl->bandwidth, 0);
     ck_assert_ptr_ne(ifl->next, NULL);
-    ck_assert_ptr_eq(ifl->prev, NULL);
+    ck_assert_str_eq(ifl->next->interface, "eth1");
+    ck_assert_int_eq(ifl->next->bandwidth, 1);
 
     ret = iflistadd(&ifl, "eth0", 2);
     ck_assert_int_eq(ret, 1);
-    ck_assert_int_eq(ifl->bandwidth, 2);
     ck_assert_str_eq(ifl->interface, "eth0");
+    ck_assert_int_eq(ifl->bandwidth, 0);
     ck_assert_ptr_ne(ifl->next, NULL);
-    ck_assert_ptr_eq(ifl->prev, NULL);
-
-    ret = iflistadd(&ifl, "eth2", 10);
-    ck_assert_int_eq(ret, 1);
-    ck_assert_int_eq(ifl->bandwidth, 10);
-    ck_assert_str_eq(ifl->interface, "eth2");
-    ck_assert_ptr_ne(ifl->next, NULL);
-    ck_assert_ptr_eq(ifl->prev, NULL);
+    ck_assert_str_eq(ifl->next->interface, "eth1");
+    ck_assert_int_eq(ifl->next->bandwidth, 1);
+    ck_assert_ptr_ne(ifl->next->next, NULL);
+    ck_assert_str_eq(ifl->next->next->interface, "eth0");
+    ck_assert_int_eq(ifl->next->next->bandwidth, 2);
 
     iflistfree(&ifl);
     ck_assert_ptr_eq(ifl, NULL);