From 6cf3011c6c88633fdd32d898e72d9b8ad40cc453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 2 Jun 2020 17:31:34 +0200 Subject: [PATCH] Introduce strcasecmp_ptr() and use it in a few places --- src/basic/string-util.c | 15 +++++------ src/basic/string-util.h | 1 + src/systemctl/systemctl.c | 53 +++++++++++---------------------------- 3 files changed, 24 insertions(+), 45 deletions(-) diff --git a/src/basic/string-util.c b/src/basic/string-util.c index 9983aa826e..755a37f667 100644 --- a/src/basic/string-util.c +++ b/src/basic/string-util.c @@ -19,18 +19,19 @@ #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; +int strcasecmp_ptr(const char *a, const char *b) { + /* Like strcasecmp(), but tries to make sense of NULL pointers */ - if (a && !b) - return 1; - - 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) { diff --git a/src/basic/string-util.h b/src/basic/string-util.h index 2a344b996f..09131455bf 100644 --- a/src/basic/string-util.h +++ b/src/basic/string-util.h @@ -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; diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index ae5c691057..e085ff99e9 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -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)