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" #include "util.h"
int strcmp_ptr(const char *a, const char *b) { int strcmp_ptr(const char *a, const char *b) {
/* Like strcmp(), but tries to make sense of NULL pointers */ /* Like strcmp(), but tries to make sense of NULL pointers */
if (a && b) if (a && b)
return strcmp(a, b); return strcmp(a, b);
return CMP(a, b); /* Direct comparison of pointers, one of which is NULL */
}
if (!a && b) int strcasecmp_ptr(const char *a, const char *b) {
return -1; /* Like strcasecmp(), but tries to make sense of NULL pointers */
if (a && !b) if (a && b)
return 1; return strcasecmp(a, b);
return CMP(a, b); /* Direct comparison of pointers, one of which is NULL */
return 0;
} }
char* endswith(const char *s, const char *postfix) { char* endswith(const char *s, const char *postfix) {

View File

@ -27,6 +27,7 @@
#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0) #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
int strcmp_ptr(const char *a, const char *b) _pure_; 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) { static inline bool streq_ptr(const char *a, const char *b) {
return strcmp_ptr(a, b) == 0; 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) { static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) {
const char *d1, *d2;
int r; int r;
/* First, order by machine */ /* First, order by machine */
if (!a->machine && b->machine) r = strcasecmp_ptr(a->machine, b->machine);
return -1; if (r != 0)
if (a->machine && !b->machine) return r;
return 1;
if (a->machine && b->machine) {
r = strcasecmp(a->machine, b->machine);
if (r != 0)
return r;
}
/* Second, order by unit type */ /* Second, order by unit type */
d1 = strrchr(a->id, '.'); r = strcasecmp_ptr(strrchr(a->id, '.'), strrchr(b->id, '.'));
d2 = strrchr(b->id, '.'); if (r != 0)
if (d1 && d2) { return r;
r = strcasecmp(d1, d2);
if (r != 0)
return r;
}
/* Third, order by name */ /* Third, order by name */
return strcasecmp(a->id, b->id); 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(a);
assert(b); assert(b);
if (!a->machine && b->machine) r = strcasecmp_ptr(a->machine, b->machine);
return -1; if (r != 0)
if (a->machine && !b->machine) return r;
return 1;
if (a->machine && b->machine) {
r = strcasecmp(a->machine, b->machine);
if (r != 0)
return r;
}
r = strcmp(a->path, b->path); r = strcmp(a->path, b->path);
if (r == 0) if (r != 0)
r = strcmp(a->type, b->type); return r;
return r; return strcmp(a->type, b->type);
} }
static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { 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(a);
assert(b); assert(b);
if (!a->machine && b->machine) r = strcasecmp_ptr(a->machine, b->machine);
return -1; if (r != 0)
if (a->machine && !b->machine) return r;
return 1;
if (a->machine && b->machine) {
r = strcasecmp(a->machine, b->machine);
if (r != 0)
return r;
}
r = CMP(a->next_elapse, b->next_elapse); r = CMP(a->next_elapse, b->next_elapse);
if (r != 0) if (r != 0)