Introduce strcasecmp_ptr() and use it in a few places
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 2 Jun 2020 15:31:34 +0000 (17:31 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 3 Jun 2020 13:31:28 +0000 (15:31 +0200)
src/basic/string-util.c
src/basic/string-util.h
src/systemctl/systemctl.c

index 9983aa826e69cc68dfdfa1fc952bf52c1ab340ab..755a37f6675aaf4abe6cc0f1e3356437ea3125d8 100644 (file)
 #include "util.h"
 
 int strcmp_ptr(const char *a, const char *b) {
-
         /* Like strcmp(), but tries to make sense of NULL pointers */
+
         if (a && b)
                 return strcmp(a, b);
+        return CMP(a, b); /* Direct comparison of pointers, one of which is NULL */
+}
 
-        if (!a && b)
-                return -1;
-
-        if (a && !b)
-                return 1;
+int strcasecmp_ptr(const char *a, const char *b) {
+        /* Like strcasecmp(), but tries to make sense of NULL pointers */
 
-        return 0;
+        if (a && b)
+                return strcasecmp(a, b);
+        return CMP(a, b); /* Direct comparison of pointers, one of which is NULL */
 }
 
 char* endswith(const char *s, const char *postfix) {
index 2a344b996f9953fd76b890a29ff032c243fc1c95..09131455bf649df216175162a8d4cd22c48909fb 100644 (file)
@@ -27,6 +27,7 @@
 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
 
 int strcmp_ptr(const char *a, const char *b) _pure_;
+int strcasecmp_ptr(const char *a, const char *b) _pure_;
 
 static inline bool streq_ptr(const char *a, const char *b) {
         return strcmp_ptr(a, b) == 0;
index ae5c6910572b068f30a370e138577e3ad4e973a1..e085ff99e9ffbc7538b3fba893456a9572e0719d 100644 (file)
@@ -322,28 +322,17 @@ static bool install_client_side(void) {
 }
 
 static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) {
-        const char *d1, *d2;
         int r;
 
         /* First, order by machine */
-        if (!a->machine && b->machine)
-                return -1;
-        if (a->machine && !b->machine)
-                return 1;
-        if (a->machine && b->machine) {
-                r = strcasecmp(a->machine, b->machine);
-                if (r != 0)
-                        return r;
-        }
+        r = strcasecmp_ptr(a->machine, b->machine);
+        if (r != 0)
+                return r;
 
         /* Second, order by unit type */
-        d1 = strrchr(a->id, '.');
-        d2 = strrchr(b->id, '.');
-        if (d1 && d2) {
-                r = strcasecmp(d1, d2);
-                if (r != 0)
-                        return r;
-        }
+        r = strcasecmp_ptr(strrchr(a->id, '.'), strrchr(b->id, '.'));
+        if (r != 0)
+                return r;
 
         /* Third, order by name */
         return strcasecmp(a->id, b->id);
@@ -975,21 +964,15 @@ static int socket_info_compare(const struct socket_info *a, const struct socket_
         assert(a);
         assert(b);
 
-        if (!a->machine && b->machine)
-                return -1;
-        if (a->machine && !b->machine)
-                return 1;
-        if (a->machine && b->machine) {
-                r = strcasecmp(a->machine, b->machine);
-                if (r != 0)
-                        return r;
-        }
+        r = strcasecmp_ptr(a->machine, b->machine);
+        if (r != 0)
+                return r;
 
         r = strcmp(a->path, b->path);
-        if (r == 0)
-                r = strcmp(a->type, b->type);
+        if (r != 0)
+                return r;
 
-        return r;
+        return strcmp(a->type, b->type);
 }
 
 static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
@@ -1234,15 +1217,9 @@ static int timer_info_compare(const struct timer_info *a, const struct timer_inf
         assert(a);
         assert(b);
 
-        if (!a->machine && b->machine)
-                return -1;
-        if (a->machine && !b->machine)
-                return 1;
-        if (a->machine && b->machine) {
-                r = strcasecmp(a->machine, b->machine);
-                if (r != 0)
-                        return r;
-        }
+        r = strcasecmp_ptr(a->machine, b->machine);
+        if (r != 0)
+                return r;
 
         r = CMP(a->next_elapse, b->next_elapse);
         if (r != 0)