]> git.sur5r.net Git - i3/i3status/commitdiff
Simplify the algorithm used to determine the IP address
authorOlivier Gayot <olivier.gayot@sigexec.com>
Mon, 4 Jun 2018 17:59:09 +0000 (19:59 +0200)
committerOlivier Gayot <olivier.gayot@sigexec.com>
Mon, 4 Jun 2018 17:59:09 +0000 (19:59 +0200)
Signed-off-by: Olivier Gayot <olivier.gayot@sigexec.com>
src/print_ip_addr.c

index c955c1cea0cfa84a0671148e10bca046c4d26a65..cf476ae3ee14ac4c2747f58701851814c46be46c 100644 (file)
@@ -37,22 +37,25 @@ const char *get_ip_addr(const char *interface, int family) {
         return NULL;
 
     /* Skip until we are at the input family address of interface */
-    for (addrp = ifaddr;
+    for (addrp = ifaddr; addrp != NULL; addrp = addrp->ifa_next) {
+        if (strncmp(addrp->ifa_name, interface, interface_len) != 0) {
+            /* The interface does not have the right name, skip it. */
+            continue;
+        }
 
-         (addrp != NULL &&
-          (strncmp(addrp->ifa_name, interface, interface_len) != 0 ||
-           addrp->ifa_addr == NULL ||
-           addrp->ifa_addr->sa_family != family));
+        if (addrp->ifa_addr != NULL && addrp->ifa_addr->sa_family == family) {
+            /* We found the right interface with the right address. */
+            break;
+        }
 
-         addrp = addrp->ifa_next) {
-        /* Check if the interface is down */
-        if (strncmp(addrp->ifa_name, interface, interface_len) != 0)
-            continue;
-        found = true;
+        /* Check if the interface is down. If it is, no need to look any
+         * further. */
         if ((addrp->ifa_flags & IFF_RUNNING) == 0) {
             freeifaddrs(ifaddr);
             return NULL;
         }
+
+        found = true;
     }
 
     if (addrp == NULL) {