]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
busctl: add and use strcmp_ptr()
authorNamhyung Kim <namhyung@gmail.com>
Fri, 31 Jul 2015 15:23:47 +0000 (00:23 +0900)
committerNamhyung Kim <namhyung@gmail.com>
Fri, 31 Jul 2015 17:16:24 +0000 (02:16 +0900)
In member_compare_func(), it compares interface, type and name of
members.  But as it can contain NULL pointer, it needs to check them
before calling strcmp().  So make it as a separate strcmp_ptr
function (named after streq_ptr) so that it can be used by others.

Also let streq_ptr() to use it in order to make the code simpler.

src/basic/util.c
src/basic/util.h
src/libsystemd/sd-bus/busctl.c
src/test/test-util.c

index 1c15fbc17204885086c82f0a0a852c9c07b16dbb..a968e2156d54586c39076dbefc806a3e12faf78f 100644 (file)
@@ -115,17 +115,23 @@ size_t page_size(void) {
         return pgsz;
 }
 
-bool streq_ptr(const char *a, const char *b) {
-
-        /* Like streq(), but tries to make sense of NULL pointers */
+int strcmp_ptr(const char *a, const char *b) {
 
+        /* Like strcmp(), but tries to make sense of NULL pointers */
         if (a && b)
-                return streq(a, b);
+                return strcmp(a, b);
 
-        if (!a && !b)
-                return true;
+        if (!a && b)
+                return -1;
 
-        return false;
+        if (a && !b)
+                return 1;
+
+        return 0;
+}
+
+bool streq_ptr(const char *a, const char *b) {
+        return strcmp_ptr(a, b) == 0;
 }
 
 char* endswith(const char *s, const char *postfix) {
index c2e5cc610b961677cbd7a0c0a7f086685ea09e72..05ddf1478025d9c5086b6da4eb447ad4cd02f514 100644 (file)
@@ -71,6 +71,7 @@ size_t page_size(void) _pure_;
 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
 
 bool streq_ptr(const char *a, const char *b) _pure_;
+int strcmp_ptr(const char *a, const char *b) _pure_;
 
 #define new(t, n) ((t*) malloc_multiply(sizeof(t), (n)))
 
index 6aaaf0e5ec4448ff47b6be51571d8c8ceaf0ea2d..2f4d977d7242a4e9431c7792296671c5607a9ee7 100644 (file)
@@ -656,28 +656,15 @@ static int member_compare_func(const void *a, const void *b) {
         assert(x->type);
         assert(y->type);
 
-        if (!x->interface && y->interface)
-                return -1;
-        if (x->interface && !y->interface)
-                return 1;
-        if (x->interface && y->interface) {
-                d = strcmp(x->interface, y->interface);
-                if (d != 0)
-                        return d;
-        }
+        d = strcmp_ptr(x->interface, y->interface);
+        if (d != 0)
+                return d;
 
         d = strcmp(x->type, y->type);
         if (d != 0)
                 return d;
 
-        if (!x->name && y->name)
-                return -1;
-        if (x->name && !y->name)
-                return 1;
-        if (x->name && y->name)
-                return strcmp(x->name, y->name);
-
-        return 0;
+        return strcmp_ptr(x->name, y->name);
 }
 
 static int member_compare_funcp(const void *a, const void *b) {
index f43433baa1817031fcfe89a8422298a9bc168f1e..7a1a4b6df24879b82e394cc377203c4cae43c871 100644 (file)
@@ -2083,6 +2083,18 @@ static void test_tempfn(void) {
         free(ret);
 }
 
+static void test_strcmp_ptr(void) {
+        assert_se(strcmp_ptr(NULL, NULL) == 0);
+        assert_se(strcmp_ptr("", NULL) > 0);
+        assert_se(strcmp_ptr("foo", NULL) > 0);
+        assert_se(strcmp_ptr(NULL, "") < 0);
+        assert_se(strcmp_ptr(NULL, "bar") < 0);
+        assert_se(strcmp_ptr("foo", "bar") > 0);
+        assert_se(strcmp_ptr("bar", "baz") < 0);
+        assert_se(strcmp_ptr("foo", "foo") == 0);
+        assert_se(strcmp_ptr("", "") == 0);
+}
+
 int main(int argc, char *argv[]) {
         log_parse_environment();
         log_open();
@@ -2169,6 +2181,7 @@ int main(int argc, char *argv[]) {
         test_shell_maybe_quote();
         test_parse_mode();
         test_tempfn();
+        test_strcmp_ptr();
 
         return 0;
 }