Introduce strcasecmp_ptr() and use it in a few places

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2020-06-02 17:31:34 +02:00
parent 6773049a4c
commit 6cf3011c6c
3 changed files with 24 additions and 45 deletions

View File

@ -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) {

View 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;

View 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)