From: Teemu Toivola Date: Fri, 28 Jun 2019 22:22:09 +0000 (+0300) Subject: fix interface list ordering and simplify iflist structure X-Git-Tag: v2.3~8 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d0c7b8895c86520ac714722cbb8675f37ce19827;p=vnstat fix interface list ordering and simplify iflist structure --- diff --git a/src/dbsql.c b/src/dbsql.c index 0ffc4f3..4852dc0 100644 --- a/src/dbsql.c +++ b/src/dbsql.c @@ -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); diff --git a/src/ifinfo.c b/src/ifinfo.c index a058527..a4e9893 100644 --- a/src/ifinfo.c +++ b/src/ifinfo.c @@ -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); diff --git a/src/iflist.c b/src/iflist.c index 0b83078..e964168 100644 --- a/src/iflist.c +++ b/src/iflist.c @@ -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; diff --git a/src/iflist.h b/src/iflist.h index 4de7b68..a267702 100644 --- a/src/iflist.h +++ b/src/iflist.h @@ -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); diff --git a/tests/iflist_tests.c b/tests/iflist_tests.c index 5f00567..474b685 100644 --- a/tests/iflist_tests.c +++ b/tests/iflist_tests.c @@ -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);