]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-netlink/local-addresses.c
tree-wide: use typesafe_qsort()
[thirdparty/systemd.git] / src / libsystemd / sd-netlink / local-addresses.c
index 5467ba432f239df9b9f1f630de9d1bca7d0a4ca5..5c37279bd20acfe3923da6a79897ef8f5d81c4ec 100644 (file)
@@ -7,8 +7,8 @@
 #include "macro.h"
 #include "netlink-util.h"
 
-static int address_compare(const void *_a, const void *_b) {
-        const struct local_address *a = _a, *b = _b;
+static int address_compare(const struct local_address *a, const struct local_address *b) {
+        int r;
 
         /* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
 
@@ -17,20 +17,17 @@ static int address_compare(const void *_a, const void *_b) {
         if (a->family == AF_INET6 && b->family == AF_INET)
                 return 1;
 
-        if (a->scope < b->scope)
-                return -1;
-        if (a->scope > b->scope)
-                return 1;
+        r = CMP(a->scope, b->scope);
+        if (r != 0)
+                return r;
 
-        if (a->metric < b->metric)
-                return -1;
-        if (a->metric > b->metric)
-                return 1;
+        r = CMP(a->metric, b->metric);
+        if (r != 0)
+                return r;
 
-        if (a->ifindex < b->ifindex)
-                return -1;
-        if (a->ifindex > b->ifindex)
-                return 1;
+        r = CMP(a->ifindex, b->ifindex);
+        if (r != 0)
+                return r;
 
         return memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family));
 }
@@ -137,7 +134,7 @@ int local_addresses(sd_netlink *context, int ifindex, int af, struct local_addre
                 n_list++;
         };
 
-        qsort_safe(list, n_list, sizeof(struct local_address), address_compare);
+        typesafe_qsort(list, n_list, address_compare);
 
         *ret = TAKE_PTR(list);
 
@@ -248,8 +245,7 @@ int local_gateways(sd_netlink *context, int ifindex, int af, struct local_addres
                 n_list++;
         }
 
-        if (n_list > 0)
-                qsort(list, n_list, sizeof(struct local_address), address_compare);
+        typesafe_qsort(list, n_list, address_compare);
 
         *ret = TAKE_PTR(list);