summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2018-09-18 08:39:24 +0900
committerYu Watanabe <watanabe.yu+github@gmail.com>2018-09-19 08:02:52 +0900
commit93bab288956f43c70f2b28a88efdc9effd951bb5 (patch)
tree453abf294f7bf2aba7ccf7ed37ccd669ec231c95 /src
parent6058516a14ada1748313af6783f5b4e7e3006654 (diff)
downloadsystemd-93bab288956f43c70f2b28a88efdc9effd951bb5.tar.gz
tree-wide: use typesafe_qsort()
Diffstat (limited to 'src')
-rw-r--r--src/analyze/analyze.c23
-rw-r--r--src/basic/calendarspec.c27
-rw-r--r--src/basic/conf-files.c12
-rw-r--r--src/basic/process-util.c6
-rw-r--r--src/basic/process-util.h2
-rw-r--r--src/basic/strv.c6
-rw-r--r--src/busctl/busctl.c8
-rw-r--r--src/cgtop/cgtop.c42
-rw-r--r--src/core/job.c13
-rw-r--r--src/core/namespace.c14
-rw-r--r--src/hwdb/hwdb.c11
-rw-r--r--src/journal/catalog.c19
-rw-r--r--src/journal/journal-file.c12
-rw-r--r--src/journal/journal-vacuum.c33
-rw-r--r--src/libsystemd-network/sd-lldp.c8
-rw-r--r--src/libsystemd/sd-bus/bus-match.c13
-rw-r--r--src/libsystemd/sd-device/device-enumerator.c12
-rw-r--r--src/libsystemd/sd-netlink/local-addresses.c30
-rw-r--r--src/machine/machinectl.c8
-rw-r--r--src/mount/mount-tool.c14
-rw-r--r--src/network/networkctl.c10
-rw-r--r--src/nspawn/nspawn-mount.c14
-rw-r--r--src/resolve/resolved-dns-dnssec.c23
-rw-r--r--src/resolve/resolved-dns-rr.h6
-rw-r--r--src/resolve/resolved-dns-trust-anchor.c8
-rw-r--r--src/resolve/resolved-mdns.c40
-rw-r--r--src/shared/bootspec.c8
-rw-r--r--src/shared/bus-unit-util.c15
-rw-r--r--src/shared/cgroup-show.c2
-rw-r--r--src/shared/efivars.c8
-rw-r--r--src/shared/uid-range.c21
-rw-r--r--src/systemctl/systemctl.c88
-rw-r--r--src/test/test-prioq.c14
-rw-r--r--src/tmpfiles/tmpfiles.c12
-rw-r--r--src/udev/udevadm-hwdb.c11
35 files changed, 231 insertions, 362 deletions
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c
index e2d22afe51..54dd7de105 100644
--- a/src/analyze/analyze.c
+++ b/src/analyze/analyze.c
@@ -41,8 +41,6 @@
#define SCALE_X (0.1 / 1000.0) /* pixels per us */
#define SCALE_Y (20.0)
-#define compare(a, b) (((a) > (b))? 1 : (((b) > (a))? -1 : 0))
-
#define svg(...) printf(__VA_ARGS__)
#define svg_bar(class, x1, x2, y) \
@@ -191,14 +189,12 @@ static int bus_get_unit_property_strv(sd_bus *bus, const char *path, const char
return 0;
}
-static int compare_unit_time(const void *a, const void *b) {
- return compare(((struct unit_times *)b)->time,
- ((struct unit_times *)a)->time);
+static int compare_unit_time(const struct unit_times *a, const struct unit_times *b) {
+ return CMP(b->time, a->time);
}
-static int compare_unit_start(const void *a, const void *b) {
- return compare(((struct unit_times *)a)->activating,
- ((struct unit_times *)b)->activating);
+static int compare_unit_start(const struct unit_times *a, const struct unit_times *b) {
+ return CMP(a->activating, b->activating);
}
static void unit_times_free(struct unit_times *t) {
@@ -629,7 +625,7 @@ static int analyze_plot(int argc, char *argv[], void *userdata) {
if (n <= 0)
return n;
- qsort(times, n, sizeof(struct unit_times), compare_unit_start);
+ typesafe_qsort(times, n, compare_unit_start);
width = SCALE_X * (boot->firmware_time + boot->finish_time);
if (width < 800.0)
@@ -854,8 +850,7 @@ static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, cha
static Hashmap *unit_times_hashmap;
-static int list_dependencies_compare(const void *_a, const void *_b) {
- const char **a = (const char**) _a, **b = (const char**) _b;
+static int list_dependencies_compare(char * const *a, char * const *b) {
usec_t usa = 0, usb = 0;
struct unit_times *times;
@@ -866,7 +861,7 @@ static int list_dependencies_compare(const void *_a, const void *_b) {
if (times)
usb = times->activated;
- return usb - usa;
+ return CMP(usb, usa);
}
static bool times_in_range(const struct unit_times *times, const struct boot_times *boot) {
@@ -891,7 +886,7 @@ static int list_dependencies_one(sd_bus *bus, const char *name, unsigned int lev
if (r < 0)
return r;
- qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
+ typesafe_qsort(deps, strv_length(deps), list_dependencies_compare);
r = acquire_boot_times(bus, &boot);
if (r < 0)
@@ -1056,7 +1051,7 @@ static int analyze_blame(int argc, char *argv[], void *userdata) {
if (n <= 0)
return n;
- qsort(times, n, sizeof(struct unit_times), compare_unit_time);
+ typesafe_qsort(times, n, compare_unit_time);
(void) pager_open(arg_no_pager, false);
diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c
index 8cb645aeac..dafc09e8f8 100644
--- a/src/basic/calendarspec.c
+++ b/src/basic/calendarspec.c
@@ -57,25 +57,18 @@ CalendarSpec* calendar_spec_free(CalendarSpec *c) {
return mfree(c);
}
-static int component_compare(const void *_a, const void *_b) {
- CalendarComponent * const *a = _a, * const *b = _b;
-
- if ((*a)->start < (*b)->start)
- return -1;
- if ((*a)->start > (*b)->start)
- return 1;
+static int component_compare(CalendarComponent * const *a, CalendarComponent * const *b) {
+ int r;
- if ((*a)->stop < (*b)->stop)
- return -1;
- if ((*a)->stop > (*b)->stop)
- return 1;
+ r = CMP((*a)->start, (*b)->start);
+ if (r != 0)
+ return r;
- if ((*a)->repeat < (*b)->repeat)
- return -1;
- if ((*a)->repeat > (*b)->repeat)
- return 1;
+ r = CMP((*a)->stop, (*b)->stop);
+ if (r != 0)
+ return r;
- return 0;
+ return CMP((*a)->repeat, (*b)->repeat);
}
static void normalize_chain(CalendarComponent **c) {
@@ -103,7 +96,7 @@ static void normalize_chain(CalendarComponent **c) {
for (i = *c; i; i = i->next)
*(j++) = i;
- qsort(b, n, sizeof(CalendarComponent*), component_compare);
+ typesafe_qsort(b, n, component_compare);
b[n-1]->next = NULL;
next = b[n-1];
diff --git a/src/basic/conf-files.c b/src/basic/conf-files.c
index d6ef0e941e..d03366077d 100644
--- a/src/basic/conf-files.c
+++ b/src/basic/conf-files.c
@@ -134,12 +134,8 @@ static int files_add(
return 0;
}
-static int base_cmp(const void *a, const void *b) {
- const char *s1, *s2;
-
- s1 = *(char * const *)a;
- s2 = *(char * const *)b;
- return strcmp(basename(s1), basename(s2));
+static int base_cmp(char * const *a, char * const *b) {
+ return strcmp(basename(*a), basename(*b));
}
static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) {
@@ -176,7 +172,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
if (!files)
return -ENOMEM;
- qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
+ typesafe_qsort(files, hashmap_size(fh), base_cmp);
*strv = files;
return 0;
@@ -200,7 +196,7 @@ int conf_files_insert(char ***strv, const char *root, char **dirs, const char *p
for (i = 0; i < strv_length(*strv); i++) {
int c;
- c = base_cmp(*strv + i, &path);
+ c = base_cmp((char* const*) *strv + i, (char* const*) &path);
if (c == 0) {
char **dir;
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index d6ee8b62b8..c887f9708b 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -1104,11 +1104,9 @@ void valgrind_summary_hack(void) {
#endif
}
-int pid_compare_func(const void *a, const void *b) {
- const pid_t *p = a, *q = b;
-
+int pid_compare_func(const pid_t *a, const pid_t *b) {
/* Suitable for usage in qsort() */
- return CMP(*p, *q);
+ return CMP(*a, *b);
}
int ioprio_parse_priority(const char *s, int *ret) {
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
index a5bb072b25..7ef45dc92b 100644
--- a/src/basic/process-util.h
+++ b/src/basic/process-util.h
@@ -108,7 +108,7 @@ static inline void* PID_TO_PTR(pid_t pid) {
void valgrind_summary_hack(void);
-int pid_compare_func(const void *a, const void *b);
+int pid_compare_func(const pid_t *a, const pid_t *b);
static inline bool nice_is_valid(int n) {
return n >= PRIO_MIN && n < PRIO_MAX;
diff --git a/src/basic/strv.c b/src/basic/strv.c
index abf3fc4c7b..f7741d1878 100644
--- a/src/basic/strv.c
+++ b/src/basic/strv.c
@@ -711,14 +711,12 @@ bool strv_overlap(char **a, char **b) {
return false;
}
-static int str_compare(const void *_a, const void *_b) {
- const char **a = (const char**) _a, **b = (const char**) _b;
-
+static int str_compare(char * const *a, char * const *b) {
return strcmp(*a, *b);
}
char **strv_sort(char **l) {
- qsort_safe(l, strv_length(l), sizeof(char*), str_compare);
+ typesafe_qsort(l, strv_length(l), str_compare);
return l;
}
diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c
index 9dc522ad18..1fe6500ae8 100644
--- a/src/busctl/busctl.c
+++ b/src/busctl/busctl.c
@@ -739,10 +739,8 @@ static int member_compare_func(const void *a, const void *b) {
return strcmp_ptr(x->name, y->name);
}
-static int member_compare_funcp(const void *a, const void *b) {
- const Member *const * x = (const Member *const *) a, * const *y = (const Member *const *) b;
-
- return member_compare_func(*x, *y);
+static int member_compare_funcp(Member * const *a, Member * const *b) {
+ return member_compare_func(*a, *b);
}
static void member_free(Member *m) {
@@ -1063,7 +1061,7 @@ static int introspect(int argc, char **argv, void *userdata) {
if (result_width > 40)
result_width = 40;
- qsort(sorted, k, sizeof(Member*), member_compare_funcp);
+ typesafe_qsort(sorted, k, member_compare_funcp);
if (arg_legend) {
printf("%-*s %-*s %-*s %-*s %s\n",
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
index cd338cbb31..1976d99597 100644
--- a/src/cgtop/cgtop.c
+++ b/src/cgtop/cgtop.c
@@ -524,8 +524,9 @@ static int refresh(const char *root, Hashmap *a, Hashmap *b, unsigned iteration)
return 0;
}
-static int group_compare(const void*a, const void *b) {
- const Group *x = *(Group**)a, *y = *(Group**)b;
+static int group_compare(Group * const *a, Group * const *b) {
+ const Group *x = *a, *y = *b;
+ int r;
if (arg_order != ORDER_TASKS || arg_recursive) {
/* Let's make sure that the parent is always before
@@ -547,29 +548,26 @@ static int group_compare(const void*a, const void *b) {
case ORDER_CPU:
if (arg_cpu_type == CPU_PERCENT) {
if (x->cpu_valid && y->cpu_valid) {
- if (x->cpu_fraction > y->cpu_fraction)
- return -1;
- else if (x->cpu_fraction < y->cpu_fraction)
- return 1;
+ r = CMP(y->cpu_fraction, x->cpu_fraction);
+ if (r != 0)
+ return r;
} else if (x->cpu_valid)
return -1;
else if (y->cpu_valid)
return 1;
} else {
- if (x->cpu_usage > y->cpu_usage)
- return -1;
- else if (x->cpu_usage < y->cpu_usage)
- return 1;
+ r = CMP(y->cpu_usage, x->cpu_usage);
+ if (r != 0)
+ return r;
}
break;
case ORDER_TASKS:
if (x->n_tasks_valid && y->n_tasks_valid) {
- if (x->n_tasks > y->n_tasks)
- return -1;
- else if (x->n_tasks < y->n_tasks)
- return 1;
+ r = CMP(y->n_tasks, x->n_tasks);
+ if (r != 0)
+ return r;
} else if (x->n_tasks_valid)
return -1;
else if (y->n_tasks_valid)
@@ -579,10 +577,9 @@ static int group_compare(const void*a, const void *b) {
case ORDER_MEMORY:
if (x->memory_valid && y->memory_valid) {
- if (x->memory > y->memory)
- return -1;
- else if (x->memory < y->memory)
- return 1;
+ r = CMP(y->memory, x->memory);
+ if (r != 0)
+ return r;
} else if (x->memory_valid)
return -1;
else if (y->memory_valid)
@@ -592,10 +589,9 @@ static int group_compare(const void*a, const void *b) {
case ORDER_IO:
if (x->io_valid && y->io_valid) {
- if (x->io_input_bps + x->io_output_bps > y->io_input_bps + y->io_output_bps)
- return -1;
- else if (x->io_input_bps + x->io_output_bps < y->io_input_bps + y->io_output_bps)
- return 1;
+ r = CMP(y->io_input_bps + y->io_output_bps, x->io_input_bps + x->io_output_bps);
+ if (r != 0)
+ return r;
} else if (x->io_valid)
return -1;
else if (y->io_valid)
@@ -624,7 +620,7 @@ static void display(Hashmap *a) {
if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid)
array[n++] = g;
- qsort_safe(array, n, sizeof(Group*), group_compare);
+ typesafe_qsort(array, n, group_compare);
/* Find the longest names in one run */
for (j = 0; j < n; j++) {
diff --git a/src/core/job.c b/src/core/job.c
index 6c62cdf595..6b17cc1d6f 100644
--- a/src/core/job.c
+++ b/src/core/job.c
@@ -1384,15 +1384,8 @@ void job_add_to_gc_queue(Job *j) {
j->in_gc_queue = true;
}
-static int job_compare(const void *a, const void *b) {
- Job *x = *(Job**) a, *y = *(Job**) b;
-
- if (x->id < y->id)
- return -1;
- if (x->id > y->id)
- return 1;
-
- return 0;
+static int job_compare(Job * const *a, Job * const *b) {
+ return CMP((*a)->id, (*b)->id);
}
static size_t sort_job_list(Job **list, size_t n) {
@@ -1400,7 +1393,7 @@ static size_t sort_job_list(Job **list, size_t n) {
size_t a, b;
/* Order by numeric IDs */
- qsort_safe(list, n, sizeof(Job*), job_compare);
+ typesafe_qsort(list, n, job_compare);
/* Filter out duplicates */
for (a = 0, b = 0; a < n; a++) {
diff --git a/src/core/namespace.c b/src/core/namespace.c
index 1c4e684057..c7cc1c7550 100644
--- a/src/core/namespace.c
+++ b/src/core/namespace.c
@@ -397,22 +397,16 @@ static int append_protect_system(MountEntry **p, ProtectSystem protect_system, b
}
}
-static int mount_path_compare(const void *a, const void *b) {
- const MountEntry *p = a, *q = b;
+static int mount_path_compare(const MountEntry *a, const MountEntry *b) {
int d;
/* If the paths are not equal, then order prefixes first */
- d = path_compare(mount_entry_path(p), mount_entry_path(q));
+ d = path_compare(mount_entry_path(a), mount_entry_path(b));
if (d != 0)
return d;
/* If the paths are equal, check the mode */
- if (p->mode < q->mode)
- return -1;
- if (p->mode > q->mode)
- return 1;
-
- return 0;
+ return CMP((int) a->mode, (int) b->mode);
}
static int prefix_where_needed(MountEntry *m, size_t n, const char *root_directory) {
@@ -1132,7 +1126,7 @@ static void normalize_mounts(const char *root_directory, MountEntry *mounts, siz
assert(n_mounts);
assert(mounts || *n_mounts == 0);
- qsort_safe(mounts, *n_mounts, sizeof(MountEntry), mount_path_compare);
+ typesafe_qsort(mounts, *n_mounts, mount_path_compare);
drop_duplicates(mounts, n_mounts);
drop_outside_root(root_directory, mounts, n_mounts);
diff --git a/src/hwdb/hwdb.c b/src/hwdb/hwdb.c
index 8a99510619..913cefaec6 100644
--- a/src/hwdb/hwdb.c
+++ b/src/hwdb/hwdb.c
@@ -76,11 +76,8 @@ struct trie_value_entry {
uint16_t file_priority;
};
-static int trie_children_cmp(const void *v1, const void *v2) {
- const struct trie_child_entry *n1 = v1;
- const struct trie_child_entry *n2 = v2;
-
- return n1->c - n2->c;
+static int trie_children_cmp(const struct trie_child_entry *a, const struct trie_child_entry *b) {
+ return CMP(a->c, b->c);
}
static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t c) {
@@ -96,7 +93,7 @@ static int node_add_child(struct trie *trie, struct trie_node *node, struct trie
node->children[node->children_count].c = c;
node->children[node->children_count].child = node_child;
node->children_count++;
- qsort(node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp);
+ typesafe_qsort(node->children, node->children_count, trie_children_cmp);
trie->nodes_count++;
return 0;
@@ -107,7 +104,7 @@ static struct trie_node *node_lookup(const struct trie_node *node, uint8_t c) {
struct trie_child_entry search;
search.c = c;
- child = bsearch_safe(&search, node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp);
+ child = bsearch_safe(&search, node->children, node->children_count, sizeof(struct trie_child_entry), (comparison_fn_t) trie_children_cmp);
if (child)
return child->child;
return NULL;
diff --git a/src/journal/catalog.c b/src/journal/catalog.c
index 4ae1136e86..c59bf52d74 100644
--- a/src/journal/catalog.c
+++ b/src/journal/catalog.c
@@ -56,23 +56,22 @@ static void catalog_hash_func(const void *p, struct siphash *state) {
siphash24_compress(i->language, strlen(i->language), state);
}
-static int catalog_compare_func(const void *a, const void *b) {
- const CatalogItem *i = a, *j = b;
+static int catalog_compare_func(const CatalogItem *a, const CatalogItem *b) {
unsigned k;
int r;
- for (k = 0; k < ELEMENTSOF(j->id.bytes); k++) {
- r = CMP(i->id.bytes[k], j->id.bytes[k]);
+ for (k = 0; k < ELEMENTSOF(b->id.bytes); k++) {
+ r = CMP(a->id.bytes[k], b->id.bytes[k]);
if (r != 0)
return r;
}
- return strcmp(i->language, j->language);
+ return strcmp(a->language, b->language);
}
const struct hash_ops catalog_hash_ops = {
.hash = catalog_hash_func,
- .compare = catalog_compare_func
+ .compare = (comparison_fn_t) catalog_compare_func,
};
static bool next_header(const char **s) {
@@ -495,7 +494,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
}
assert(n == hashmap_size(h));
- qsort_safe(items, n, sizeof(CatalogItem), catalog_compare_func);
+ typesafe_qsort(items, n, catalog_compare_func);
strbuf_complete(sb);
@@ -567,21 +566,21 @@ static const char *find_id(void *p, sd_id128_t id) {
strncpy(key.language, loc, sizeof(key.language));
key.language[strcspn(key.language, ".@")] = 0;
- f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
+ f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), (comparison_fn_t) catalog_compare_func);
if (!f) {
char *e;
e = strchr(key.language, '_');
if (e) {
*e = 0;
- f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
+ f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), (comparison_fn_t) catalog_compare_func);
}
}
}
if (!f) {
zero(key.language);
- f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), catalog_compare_func);
+ f = bsearch(&key, (const uint8_t*) p + le64toh(h->header_size), le64toh(h->n_items), le64toh(h->catalog_item_size), (comparison_fn_t) catalog_compare_func);
}
if (!f)
diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c
index 62e7f68a13..ee6e25e5e8 100644
--- a/src/journal/journal-file.c
+++ b/src/journal/journal-file.c
@@ -1933,14 +1933,8 @@ int journal_file_enable_post_change_timer(JournalFile *f, sd_event *e, usec_t t)
return r;
}
-static int entry_item_cmp(const void *_a, const void *_b) {
- const EntryItem *a = _a, *b = _b;
-
- if (le64toh(a->object_offset) < le64toh(b->object_offset))
- return -1;
- if (le64toh(a->object_offset) > le64toh(b->object_offset))
- return 1;
- return 0;
+static int entry_item_cmp(const EntryItem *a, const EntryItem *b) {
+ return CMP(le64toh(a->object_offset), le64toh(b->object_offset));
}
int journal_file_append_entry(
@@ -1999,7 +1993,7 @@ int journal_file_append_entry(
/* Order by the position on disk, in order to improve seek
* times for rotating media. */
- qsort_safe(items, n_iovec, sizeof(EntryItem), entry_item_cmp);
+ typesafe_qsort(items, n_iovec, entry_item_cmp);
r = journal_file_append_entry_internal(f, ts, boot_id, xor_hash, items, n_iovec, seqnum, ret, offset);
diff --git a/src/journal/journal-vacuum.c b/src/journal/journal-vacuum.c
index 8d3ae71440..840fb69ee0 100644
--- a/src/journal/journal-vacuum.c
+++ b/src/journal/journal-vacuum.c
@@ -29,30 +29,21 @@ struct vacuum_info {
bool have_seqnum;
};
-static int vacuum_compare(const void *_a, const void *_b) {
- const struct vacuum_info *a, *b;
-
- a = _a;
- b = _b;
+static int vacuum_compare(const struct vacuum_info *a, const struct vacuum_info *b) {
+ int r;
if (a->have_seqnum && b->have_seqnum &&
- sd_id128_equal(a->seqnum_id, b->seqnum_id)) {
- if (a->seqnum < b->seqnum)
- return -1;
- else if (a->seqnum > b->seqnum)
- return 1;
- else
- return 0;
- }
+ sd_id128_equal(a->seqnum_id, b->seqnum_id))
+ return CMP(a->seqnum, b->seqnum);
- if (a->realtime < b->realtime)
- return -1;
- else if (a->realtime > b->realtime)
- return 1;
- else if (a->have_seqnum && b->have_seqnum)
+ r = CMP(a->realtime, b->realtime);
+ if (r != 0)
+ return r;
+
+ if (a->have_seqnum && b->have_seqnum)
return memcmp(&a->seqnum_id, &b->seqnum_id, 16);
- else
- return strcmp(a->filename, b->filename);
+
+ return strcmp(a->filename, b->filename);
}
static void patch_realtime(
@@ -292,7 +283,7 @@ int journal_directory_vacuum(
sum += size;
}
- qsort_safe(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
+ typesafe_qsort(list, n_list, vacuum_compare);
for (i = 0; i < n_list; i++) {
unsigned left;
diff --git a/src/libsystemd-network/sd-lldp.c b/src/libsystemd-network/sd-lldp.c
index e3e6ac34d8..4edcc6dff4 100644
--- a/src/libsystemd-network/sd-lldp.c
+++ b/src/libsystemd-network/sd-lldp.c
@@ -371,10 +371,8 @@ _public_ int sd_lldp_new(sd_lldp **ret) {
return 0;
}
-static int neighbor_compare_func(const void *a, const void *b) {
- const sd_lldp_neighbor * const*x = a, * const *y = b;
-
- return lldp_neighbor_id_hash_ops.compare(&(*x)->id, &(*y)->id);
+static int neighbor_compare_func(sd_lldp_neighbor * const *a, sd_lldp_neighbor * const *b) {
+ return lldp_neighbor_id_hash_ops.compare(&(*a)->id, &(*b)->id);
}
static int on_timer_event(sd_event_source *s, uint64_t usec, void *userdata) {
@@ -462,7 +460,7 @@ _public_ int sd_lldp_get_neighbors(sd_lldp *lldp, sd_lldp_neighbor ***ret) {
assert((size_t) k == hashmap_size(lldp->neighbor_by_id));
/* Return things in a stable order */
- qsort(l, k, sizeof(sd_lldp_neighbor*), neighbor_compare_func);
+ typesafe_qsort(l, k, neighbor_compare_func);
*ret = l;
return k;
diff --git a/src/libsystemd/sd-bus/bus-match.c b/src/libsystemd/sd-bus/bus-match.c
index 6ac57d48e3..37b851b94c 100644
--- a/src/libsystemd/sd-bus/bus-match.c
+++ b/src/libsystemd/sd-bus/bus-match.c
@@ -760,15 +760,8 @@ enum bus_match_node_type bus_match_node_type_from_string(const char *k, size_t n
return -EINVAL;
}
-static int match_component_compare(const void *a, const void *b) {
- const struct bus_match_component *x = a, *y = b;
-
- if (x->type < y->type)
- return -1;
- if (x->type > y->type)
- return 1;
-
- return 0;
+static int match_component_compare(const struct bus_match_component *a, const struct bus_match_component *b) {
+ return CMP(a->type, b->type);
}
void bus_match_parse_free(struct bus_match_component *components, unsigned n_components) {
@@ -901,7 +894,7 @@ int bus_match_parse(
}
/* Order the whole thing, so that we always generate the same tree */
- qsort_safe(components, n_components, sizeof(struct bus_match_component), match_component_compare);
+ typesafe_qsort(components, n_components, match_component_compare);
/* Check for duplicates */
for (i = 0; i+1 < n_components; i++)
diff --git a/src/libsystemd/sd-device/device-enumerator.c b/src/libsystemd/sd-device/device-enumerator.c
index 0e3915f96c..27cc358d68 100644
--- a/src/libsystemd/sd-device/device-enumerator.c
+++ b/src/libsystemd/sd-device/device-enumerator.c
@@ -249,10 +249,11 @@ int device_enumerator_add_match_is_initialized(sd_device_enumerator *enumerator)
return 0;
}
-static int device_compare(const void *_a, const void *_b) {
+static int device_compare(sd_device * const *_a, sd_device * const *_b) {
sd_device *a = *(sd_device **)_a, *b = *(sd_device **)_b;
const char *devpath_a, *devpath_b, *sound_a;
bool delay_a, delay_b;
+ int r;
assert_se(sd_device_get_devpath(a, &devpath_a) >= 0);
assert_se(sd_device_get_devpath(b, &devpath_b) >= 0);
@@ -293,8 +294,9 @@ static int device_compare(const void *_a, const void *_b) {
/* md and dm devices are enumerated after all other devices */
delay_a = strstr(devpath_a, "/block/md") || strstr(devpath_a, "/block/dm-");
delay_b = strstr(devpath_b, "/block/md") || strstr(devpath_b, "/block/dm-");
- if (delay_a != delay_b)
- return delay_a - delay_b;
+ r = CMP(delay_a, delay_b);
+ if (r != 0)
+ return r;
return strcmp(devpath_a, devpath_b);
}
@@ -830,7 +832,7 @@ int device_enumerator_scan_devices(sd_device_enumerator *enumerator) {
r = k;
}
- qsort_safe(enumerator->devices, enumerator->n_devices, sizeof(sd_device *), device_compare);
+ typesafe_qsort(enumerator->devices, enumerator->n_devices, device_compare);
enumerator->scan_uptodate = true;
enumerator->type = DEVICE_ENUMERATION_TYPE_DEVICES;
@@ -914,7 +916,7 @@ int device_enumerator_scan_subsystems(sd_device_enumerator *enumerator) {
}
}
- qsort_safe(enumerator->devices, enumerator->n_devices, sizeof(sd_device *), device_compare);
+ typesafe_qsort(enumerator->devices, enumerator->n_devices, device_compare);
enumerator->scan_uptodate = true;
enumerator->type = DEVICE_ENUMERATION_TYPE_SUBSYSTEMS;
diff --git a/src/libsystemd/sd-netlink/local-addresses.c b/src/libsystemd/sd-netlink/local-addresses.c
index 5467ba432f..5c37279bd2 100644
--- a/src/libsystemd/sd-netlink/local-addresses.c
+++ b/src/libsystemd/sd-netlink/local-addresses.c
@@ -7,8 +7,8 @@
#include "macro.h"
#include "netlink-util.h"
-static int address_compare(const void *_a, const void *_b) {
- const struct local_address *a = _a, *b = _b;
+static int address_compare(const struct local_address *a, const struct local_address *b) {
+ int r;
/* Order lowest scope first, IPv4 before IPv6, lowest interface index first */
@@ -17,20 +17,17 @@ static int address_compare(const void *_a, const void *_b) {
if (a->family == AF_INET6 && b->family == AF_INET)
return 1;
- if (a->scope < b->scope)
- return -1;
- if (a->scope > b->scope)
- return 1;
+ r = CMP(a->scope, b->scope);
+ if (r != 0)
+ return r;
- if (a->metric < b->metric)
- return -1;
- if (a->metric > b->metric)
- return 1;
+ r = CMP(a->metric, b->metric);
+ if (r != 0)
+ return r;
- if (a->ifindex < b->ifindex)
- return -1;
- if (a->ifindex > b->ifindex)
- return 1;
+ r = CMP(a->ifindex, b->ifindex);
+ if (r != 0)
+ return r;
return memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family));
}
@@ -137,7 +134,7 @@ int local_addresses(sd_netlink *context, int ifindex, int af, struct local_addre
n_list++;
};
- qsort_safe(list, n_list, sizeof(struct local_address), address_compare);
+ typesafe_qsort(list, n_list, address_compare);
*ret = TAKE_PTR(list);
@@ -248,8 +245,7 @@ int local_gateways(sd_netlink *context, int ifindex, int af, struct local_addres
n_list++;
}
- if (n_list > 0)
- qsort(list, n_list, sizeof(struct local_address), address_compare);
+ typesafe_qsort(list, n_list, address_compare);
*ret = TAKE_PTR(list);
diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c
index 83787015d2..47ccb352bf 100644
--- a/src/machine/machinectl.c
+++ b/src/machine/machinectl.c
@@ -2378,10 +2378,8 @@ typedef struct TransferInfo {
double progress;
} TransferInfo;
-static int compare_transfer_info(const void *a, const void *b) {
- const TransferInfo *x = a, *y = b;
-
- return strcmp(x->local, y->local);
+static int compare_transfer_info(const TransferInfo *a, const TransferInfo *b) {
+ return strcmp(a->local, b->local);
}
static int list_transfers(int argc, char *argv[], void *userdata) {
@@ -2449,7 +2447,7 @@ static int list_transfers(int argc, char *argv[], void *userdata) {
if (r < 0)
return bus_log_parse_error(r);
- qsort_safe(transfers, n_transfers, sizeof(TransferInfo), compare_transfer_info);
+ typesafe_qsort(transfers, n_transfers, compare_transfer_info);
if (arg_legend && n_transfers > 0)
printf("%-*s %-*s %-*s %-*s %-*s\n",
diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c
index 56ded606f5..3cdf86ed47 100644
--- a/src/mount/mount-tool.c
+++ b/src/mount/mount-tool.c
@@ -1372,17 +1372,15 @@ struct item {
char* columns[_COLUMN_MAX];
};
-static int compare_item(const void *a, const void *b) {
- const struct item *x = a, *y = b;
-
- if (x->columns[COLUMN_NODE] == y->columns[COLUMN_NODE])
+static int compare_item(const struct item *a, const struct item *b) {
+ if (a->columns[COLUMN_NODE] == b->columns[COLUMN_NODE])
return 0;
- if (!x->columns[COLUMN_NODE])
+ if (!a->columns[COLUMN_NODE])
return 1;
- if (!y->columns[COLUMN_NODE])
+ if (!b->columns[COLUMN_NODE])
return -1;
- return path_compare(x->columns[COLUMN_NODE], y->columns[COLUMN_NODE]);
+ return path_compare(a->columns[COLUMN_NODE], b->columns[COLUMN_NODE]);
}
static int list_devices(void) {
@@ -1485,7 +1483,7 @@ static int list_devices(void) {
goto finish;
}
- qsort_safe(items, n, sizeof(struct item), compare_item);
+ typesafe_qsort(items, n, compare_item);
(void) pager_open(arg_no_pager, false);
diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index 3d81b72eed..aaf1a14168 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -104,10 +104,8 @@ typedef struct LinkInfo {
bool has_mtu:1;
} LinkInfo;
-static int link_info_compare(const void *a, const void *b) {
- const LinkInfo *x = a, *y = b;
-
- return x->ifindex - y->ifindex;
+static int link_info_compare(const LinkInfo *a, const LinkInfo *b) {
+ return CMP(a->ifindex, b->ifindex);
}
static int decode_link(sd_netlink_message *m, LinkInfo *info) {
@@ -190,7 +188,7 @@ static int acquire_link_info_strv(sd_netlink *rtnl, char **l, LinkInfo **ret) {
c++;
}
- qsort_safe(links, c, sizeof(LinkInfo), link_info_compare);
+ typesafe_qsort(links, c, link_info_compare);
*ret = TAKE_PTR(links);
@@ -230,7 +228,7 @@ static int acquire_link_info_all(sd_netlink *rtnl, LinkInfo **ret) {
c++;
}
- qsort_safe(links, c, sizeof(LinkInfo), link_info_compare);
+ typesafe_qsort(links, c, link_info_compare);
*ret = TAKE_PTR(links);
diff --git a/src/nspawn/nspawn-mount.c b/src/nspawn/nspawn-mount.c
index 995022272a..5bef6aef59 100644
--- a/src/nspawn/nspawn-mount.c
+++ b/src/nspawn/nspawn-mount.c
@@ -69,20 +69,14 @@ void custom_mount_free_all(CustomMount *l, size_t n) {
free(l);
}
-static int custom_mount_compare(const void *a, const void *b) {
- const CustomMount *x = a, *y = b;
+static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
int r;
- r = path_compare(x->destination, y->destination);
+ r = path_compare(a->destination, b->destination);
if (r != 0)
return r;
- if (x->type < y->type)
- return -1;
- if (x->type > y->type)
- return 1;
-
- return 0;
+ return CMP(a->type, b->type);
}
static bool source_path_is_valid(const char *p) {
@@ -116,7 +110,7 @@ int custom_mount_prepare_all(const char *dest, CustomMount *l, size_t n) {
assert(l || n == 0);
/* Order the custom mounts, and make sure we have a working directory */
- qsort_safe(l, n, sizeof(CustomMount), custom_mount_compare);
+ typesafe_qsort(l, n, custom_mount_compare);
for (i = 0; i < n; i++) {
CustomMount *m = l + i;
diff --git a/src/resolve/resolved-dns-dnssec.c b/src/resolve/resolved-dns-dnssec.c
index 2c8514a72a..86a9a58c5b 100644
--- a/src/resolve/resolved-dns-dnssec.c
+++ b/src/resolve/resolved-dns-dnssec.c
@@ -114,32 +114,25 @@ int dnssec_canonicalize(const char *n, char *buffer, size_t buffer_max) {
#if HAVE_GCRYPT
-static int rr_compare(const void *a, const void *b) {
- DnsResourceRecord **x = (DnsResourceRecord**) a, **y = (DnsResourceRecord**) b;
+static int rr_compare(DnsResourceRecord * const *a, DnsResourceRecord * const *b) {
+ const DnsResourceRecord *x = *a, *y = *b;
size_t m;
int r;
/* Let's order the RRs according to RFC 4034, Section 6.3 */
assert(x);
- assert(*x);
- assert((*x)->wire_format);
+ assert(x->wire_format);
assert(y);
- assert(*y);
- assert((*y)->wire_format);
+ assert(y->wire_format);
- m = MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(*x), DNS_RESOURCE_RECORD_RDATA_SIZE(*y));
+ m = MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y));
- r = memcmp(DNS_RESOURCE_RECORD_RDATA(*x), DNS_RESOURCE_RECORD_RDATA(*y), m);
+ r = memcmp(DNS_RESOURCE_RECORD_RDATA(x), DNS_RESOURCE_RECORD_RDATA(y), m);
if (r != 0)
return r;
- if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) < DNS_RESOURCE_RECORD_RDATA_SIZE(*y))
- return -1;
- else if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) > DNS_RESOURCE_RECORD_RDATA_SIZE(*y))
- return 1;
-
- return 0;
+ return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y));
}
static int dnssec_rsa_verify_raw(
@@ -806,7 +799,7 @@ int dnssec_verify_rrset(
return -ENODATA;
/* Bring the RRs into canonical order */
- qsort_safe(list, n, sizeof(DnsResourceRecord*), rr_compare);
+ typesafe_qsort(list, n, rr_compare);
f = open_memstream(&sig_data, &sig_size);
if (!f)
diff --git a/src/resolve/resolved-dns-rr.h b/src/resolve/resolved-dns-rr.h
index 83acf3b880..d5e74e4937 100644
--- a/src/resolve/resolved-dns-rr.h
+++ b/src/resolve/resolved-dns-rr.h
@@ -244,7 +244,7 @@ struct DnsResourceRecord {
};
};
-static inline const void* DNS_RESOURCE_RECORD_RDATA(DnsResourceRecord *rr) {
+static inline const void* DNS_RESOURCE_RECORD_RDATA(const DnsResourceRecord *rr) {
if (!rr)
return NULL;
@@ -255,7 +255,7 @@ static inline const void* DNS_RESOURCE_RECORD_RDATA(DnsResourceRecord *rr) {
return (uint8_t*) rr->wire_format + rr->wire_format_rdata_offset;
}
-static inline size_t DNS_RESOURCE_RECORD_RDATA_SIZE(DnsResourceRecord *rr) {
+static inline size_t DNS_RESOURCE_RECORD_RDATA_SIZE(const DnsResourceRecord *rr) {
if (!rr)
return 0;
if (!rr->wire_format)
@@ -265,7 +265,7 @@ static inline size_t DNS_RESOURCE_RECORD_RDATA_SIZE(DnsResourceRecord *rr) {
return rr->wire_format_size - rr->wire_format_rdata_offset;
}
-static inline uint8_t DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(DnsResourceRecord *rr) {
+static inline uint8_t DNS_RESOURCE_RECORD_OPT_VERSION_SUPPORTED(const DnsResourceRecord *rr) {
assert(rr);
assert(rr->key->type == DNS_TYPE_OPT);
diff --git a/src/resolve/resolved-dns-trust-anchor.c b/src/resolve/resolved-dns-trust-anchor.c
index bf5b07cdd3..dc6b746189 100644
--- a/src/resolve/resolved-dns-trust-anchor.c
+++ b/src/resolve/resolved-dns-trust-anchor.c
@@ -464,10 +464,8 @@ static int dns_trust_anchor_load_files(
return 0;
}
-static int domain_name_cmp(const void *a, const void *b) {
- char **x = (char**) a, **y = (char**) b;
-
- return dns_name_compare_func(*x, *y);
+static int domain_name_cmp(char * const *a, char * const *b) {
+ return dns_name_compare_func(*a, *b);
}
static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
@@ -497,7 +495,7 @@ static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
if (!l)
return log_oom();
- qsort_safe(l, set_size(d->negative_by_name), sizeof(char*), domain_name_cmp);
+ typesafe_qsort(l, set_size(d->negative_by_name), domain_name_cmp);
j = strv_join(l, " ");
if (!j)
diff --git a/src/resolve/resolved-mdns.c b/src/resolve/resolved-mdns.c
index 71a30ae8f8..e70138181a 100644
--- a/src/resolve/resolved-mdns.c
+++ b/src/resolve/resolved-mdns.c
@@ -53,50 +53,41 @@ eaddrinuse:
return 0;
}
-static int mdns_rr_compare(const void *a, const void *b) {
- DnsResourceRecord **x = (DnsResourceRecord**) a, **y = (DnsResourceRecord**) b;
+static int mdns_rr_compare(DnsResourceRecord * const *a, DnsResourceRecord * const *b) {
+ DnsResourceRecord *x = *(DnsResourceRecord **) a, *y = *(DnsResourceRecord **) b;
size_t m;
int r;
assert(x);
- assert(*x);
assert(y);
- assert(*y);
- if (CLEAR_CACHE_FLUSH((*x)->key->class) < CLEAR_CACHE_FLUSH((*y)->key->class))
- return -1;
- else if (CLEAR_CACHE_FLUSH((*x)->key->class) > CLEAR_CACHE_FLUSH((*y)->key->class))
- return 1;
+ r = CMP(CLEAR_CACHE_FLUSH(x->key->class), CLEAR_CACHE_FLUSH(y->key->class));
+ if (r != 0)
+ return r;
- if ((*x)->key->type < (*y)->key->type)
- return -1;
- else if ((*x)->key->type > (*y)->key->type)
- return 1;
+ r = CMP(x->key->type, y->key->type);
+ if (r != 0)
+ return r;
- r = dns_resource_record_to_wire_format(*x, false);
+ r = dns_resource_record_to_wire_format(x, false);
if (r < 0) {
log_warning_errno(r, "Can't wire-format RR: %m");
return 0;
}
- r = dns_resource_record_to_wire_format(*y, false);
+ r = dns_resource_record_to_wire_format(y, false);
if (r < 0) {
log_warning_errno(r, "Can't wire-format RR: %m");
return 0;
}
- m = MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(*x), DNS_RESOURCE_RECORD_RDATA_SIZE(*y));
+ m = MIN(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y));
- r = memcmp(DNS_RESOURCE_RECORD_RDATA(*x), DNS_RESOURCE_RECORD_RDATA(*y), m);
+ r = memcmp(DNS_RESOURCE_RECORD_RDATA(x), DNS_RESOURCE_RECORD_RDATA(y), m);
if (r != 0)
return r;
- if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) < DNS_RESOURCE_RECORD_RDATA_SIZE(*y))
- return -1;
- else if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) > DNS_RESOURCE_RECORD_RDATA_SIZE(*y))
- return 1;
-
- return 0;
+ return CMP(DNS_RESOURCE_RECORD_RDATA_SIZE(x), DNS_RESOURCE_RECORD_RDATA_SIZE(y));
}
static int proposed_rrs_cmp(DnsResourceRecord **x, unsigned x_size, DnsResourceRecord **y, unsigned y_size) {
@@ -151,7 +142,7 @@ static int mdns_packet_extract_matching_rrs(DnsPacket *p, DnsResourceKey *key, D
list[n++] = p->answer->items[i].rr;
}
assert(n == size);
- qsort_safe(list, size, sizeof(DnsResourceRecord*), mdns_rr_compare);
+ typesafe_qsort(list, size, mdns_rr_compare);
*ret_rrs = TAKE_PTR(list);
@@ -171,7 +162,8 @@ static int mdns_do_tiebreak(DnsResourceKey *key, DnsAnswer *answer, DnsPacket *p
DNS_ANSWER_FOREACH(rr, answer)
our[i++] = rr;
- qsort_safe(our, size, sizeof(DnsResourceRecord*), mdns_rr_compare);
+
+ typesafe_qsort(our, size, mdns_rr_compare);
r = mdns_packet_extract_matching_rrs(p, key, &remote);
if (r < 0)
diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c
index 47a7dbafbd..8d5a421144 100644
--- a/src/shared/bootspec.c
+++ b/src/shared/bootspec.c
@@ -202,10 +202,8 @@ int boot_loader_read_conf(const char *path, BootConfig *config) {
return 0;
}
-static int boot_entry_compare(const void *a, const void *b) {
- const BootEntry *aa = a, *bb = b;
-
- return str_verscmp(aa->filename, bb->filename);
+static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
+ return str_verscmp(a->filename, b->filename);
}
int boot_entries_find(const char *dir, BootEntry **ret_entries, size_t *ret_n_entries) {
@@ -234,7 +232,7 @@ int boot_entries_find(const char *dir, BootEntry **ret_entries, size_t *ret_n_en
n++;
}
- qsort_safe(array, n, sizeof(BootEntry), boot_entry_compare);
+ typesafe_qsort(array, n, boot_entry_compare);
*ret_entries = array;
*ret_n_entries = n;
diff --git a/src/shared/bus-unit-util.c b/src/shared/bus-unit-util.c
index e1354b0d0f..4becc8c944 100644
--- a/src/shared/bus-unit-util.c
+++ b/src/shared/bus-unit-util.c
@@ -2206,13 +2206,8 @@ static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
free(cg);
}
-static int cgroup_info_compare_func(const void *a, const void *b) {
- const struct CGroupInfo *x = *(const struct CGroupInfo* const*) a, *y = *(const struct CGroupInfo* const*) b;
-
- assert(x);
- assert(y);
-
- return strcmp(x->cgroup_path, y->cgroup_path);
+static int cgroup_info_compare_func(struct CGroupInfo * const *a, struct CGroupInfo * const *b) {
+ return strcmp((*a)->cgroup_path, (*b)->cgroup_path);
}
static int dump_processes(
@@ -2249,7 +2244,7 @@ static int dump_processes(
pids[n++] = PTR_TO_PID(pidp);
assert(n == hashmap_size(cg->pids));
- qsort_safe(pids, n, sizeof(pid_t), pid_compare_func);
+ typesafe_qsort(pids, n, pid_compare_func);
width = DECIMAL_STR_WIDTH(pids[n-1]);
@@ -2291,7 +2286,7 @@ static int dump_processes(
LIST_FOREACH(siblings, child, cg->children)
children[n++] = child;
assert(n == cg->n_children);
- qsort_safe(children, n, sizeof(struct CGroupInfo*), cgroup_info_compare_func);
+ typesafe_qsort(children, n, cgroup_info_compare_func);
if (n_columns != 0)
n_columns = MAX(LESS_BY(n_columns, 2U), 20U);
@@ -2378,7 +2373,7 @@ static int dump_extra_processes(
if (n == 0)
return 0;
- qsort_safe(pids, n, sizeof(pid_t), pid_compare_func);
+ typesafe_qsort(pids, n, pid_compare_func);
width = DECIMAL_STR_WIDTH(pids[n-1]);
for (k = 0; k < n; k++) {
diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c
index 4d1a90bd55..801cf133f9 100644
--- a/src/shared/cgroup-show.c
+++ b/src/shared/cgroup-show.c
@@ -38,7 +38,7 @@ static void show_pid_array(
if (n_pids == 0)
return;
- qsort(pids, n_pids, sizeof(pid_t), pid_compare_func);
+ typesafe_qsort(pids, n_pids, pid_compare_func);
/* Filter duplicates */
for (j = 0, i = 1; i < n_pids; i++) {
diff --git a/src/shared/efivars.c b/src/shared/efivars.c
index fcc0db8b0b..e96748efde 100644
--- a/src/shared/efivars.c
+++ b/src/shared/efivars.c
@@ -563,10 +563,8 @@ static int boot_id_hex(const char s[4]) {
return id;
}
-static int cmp_uint16(const void *_a, const void *_b) {
- const uint16_t *a = _a, *b = _b;
-
- return (int)*a - (int)*b;
+static int cmp_uint16(const uint16_t *a, const uint16_t *b) {
+ return CMP(*a, *b);
}
int efi_get_boot_options(uint16_t **options) {
@@ -604,7 +602,7 @@ int efi_get_boot_options(uint16_t **options) {
list[count++] = id;
}
- qsort_safe(list, count, sizeof(uint16_t), cmp_uint16);
+ typesafe_qsort(list, count, cmp_uint16);
*options = TAKE_PTR(list);
diff --git a/src/shared/uid-range.c b/src/shared/uid-range.c
index 434ce6ff4d..5fa7bd277e 100644
--- a/src/shared/uid-range.c
+++ b/src/shared/uid-range.c
@@ -8,6 +8,7 @@
#include "macro.h"
#include "uid-range.h"
#include "user-util.h"
+#include "util.h"
static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) {
assert(range);
@@ -45,20 +46,14 @@ static void uid_range_coalesce(UidRange **p, unsigned *n) {
}
}
-static int uid_range_compare(const void *a, const void *b) {
- const UidRange *x = a, *y = b;
-
- if (x->start < y->start)
- return -1;
- if (x->start > y->start)
- return 1;
+static int uid_range_compare(const UidRange *a, const UidRange *b) {
+ int r;
- if (x->nr < y->nr)
- return -1;
- if (x->nr > y->nr)
- return 1;
+ r = CMP(a->start, b->start);
+ if (r != 0)
+ return r;
- return 0;
+ return CMP(a->nr, b->nr);
}
int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr) {
@@ -102,7 +97,7 @@ int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t nr) {
x->nr = nr;
}
- qsort(*p, *n, sizeof(UidRange), uid_range_compare);
+ typesafe_qsort(*p, *n, uid_range_compare);
uid_range_coalesce(p, n);
return *n;
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
index 1543038d21..ce5cbe7c13 100644
--- a/src/systemctl/systemctl.c
+++ b/src/systemctl/systemctl.c
@@ -316,25 +316,24 @@ static bool install_client_side(void) {
return false;
}
-static int compare_unit_info(const void *a, const void *b) {
- const UnitInfo *u = a, *v = b;
+static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) {
const char *d1, *d2;
int r;
/* First, order by machine */
- if (!u->machine && v->machine)
+ if (!a->machine && b->machine)
return -1;
- if (u->machine && !v->machine)
+ if (a->machine && !b->machine)
return 1;
- if (u->machine && v->machine) {
- r = strcasecmp(u->machine, v->machine);
+ if (a->machine && b->machine) {
+ r = strcasecmp(a->machine, b->machine);
if (r != 0)
return r;
}
/* Second, order by unit type */
- d1 = strrchr(u->id, '.');
- d2 = strrchr(v->id, '.');
+ d1 = strrchr(a->id, '.');
+ d2 = strrchr(b->id, '.');
if (d1 && d2) {
r = strcasecmp(d1, d2);
if (r != 0)
@@ -342,7 +341,7 @@ static int compare_unit_info(const void *a, const void *b) {
}
/* Third, order by name */
- return strcasecmp(u->id, v->id);
+ return strcasecmp(a->id, b->id);
}
static const char* unit_type_suffix(const char *name) {
@@ -756,7 +755,7 @@ static int list_units(int argc, char *argv[], void *userdata) {
if (r < 0)
return r;
- qsort_safe(unit_infos, r, sizeof(UnitInfo), compare_unit_info);
+ typesafe_qsort(unit_infos, r, compare_unit_info);
return output_units_list(unit_infos, r);
}
@@ -851,7 +850,7 @@ struct socket_info {
};
static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) {
- int o;
+ int r;
assert(a);
assert(b);
@@ -861,16 +860,16 @@ static int socket_info_compare(const struct socket_info *a, const struct socket_
if (a->machine && !b->machine)
return 1;
if (a->machine && b->machine) {
- o = strcasecmp(a->machine, b->machine);
- if (o != 0)
- return o;
+ r = strcasecmp(a->machine, b->machine);
+ if (r != 0)
+ return r;
}
- o = strcmp(a->path, b->path);
- if (o == 0)
- o = strcmp(a->type, b->type);
+ r = strcmp(a->path, b->path);
+ if (r == 0)
+ r = strcmp(a->type, b->type);
- return o;
+ return r;
}
static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) {
@@ -1006,8 +1005,7 @@ static int list_sockets(int argc, char *argv[], void *userdata) {
listening = triggered = NULL; /* avoid cleanup */
}
- qsort_safe(socket_infos, cs, sizeof(struct socket_info),
- (__compar_fn_t) socket_info_compare);
+ typesafe_qsort(socket_infos, cs, socket_info_compare);
output_sockets_list(socket_infos, cs);
@@ -1100,7 +1098,7 @@ struct timer_info {
};
static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) {
- int o;
+ int r;
assert(a);
assert(b);
@@ -1110,15 +1108,14 @@ static int timer_info_compare(const struct timer_info *a, const struct timer_inf
if (a->machine && !b->machine)
return 1;
if (a->machine && b->machine) {
- o = strcasecmp(a->machine, b->machine);
- if (o != 0)
- return o;
+ r = strcasecmp(a->machine, b->machine);
+ if (r != 0)
+ return r;
}
- if (a->next_elapse < b->next_elapse)
- return -1;
- if (a->next_elapse > b->next_elapse)
- return 1;
+ r = CMP(a->next_elapse, b->next_elapse);
+ if (r != 0)
+ return r;
return strcmp(a->id, b->id);
}
@@ -1311,8 +1308,7 @@ static int list_timers(int argc, char *argv[], void *userdata) {
};
}
- qsort_safe(timer_infos, c, sizeof(struct timer_info),
- (__compar_fn_t) timer_info_compare);
+ typesafe_qsort(timer_infos, c, timer_info_compare);
output_timers_list(timer_infos, c);
@@ -1323,12 +1319,11 @@ static int list_timers(int argc, char *argv[], void *userdata) {
return r;
}
-static int compare_unit_file_list(const void *a, const void *b) {
+static int compare_unit_file_list(const UnitFileList *a, const UnitFileList *b) {
const char *d1, *d2;
- const UnitFileList *u = a, *v = b;
- d1 = strrchr(u->path, '.');
- d2 = strrchr(v->path, '.');
+ d1 = strrchr(a->path, '.');
+ d2 = strrchr(b->path, '.');
if (d1 && d2) {
int r;
@@ -1338,7 +1333,7 @@ static int compare_unit_file_list(const void *a, const void *b) {
return r;
}
- return strcasecmp(basename(u->path), basename(v->path));
+ return strcasecmp(basename(a->path), basename(b->path));
}
static bool output_show_unit_file(const UnitFileList *u, char **states, char **patterns) {
@@ -1558,7 +1553,7 @@ static int list_unit_files(int argc, char *argv[], void *userdata) {
(void) pager_open(arg_no_pager, false);
- qsort_safe(units, c, sizeof(UnitFileList), compare_unit_file_list);
+ typesafe_qsort(units, c, compare_unit_file_list);
output_unit_file_list(units, c);
if (install_client_side())
@@ -1680,9 +1675,7 @@ static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, cha
return 0;
}
-static int list_dependencies_compare(const void *_a, const void *_b) {
- const char **a = (const char**) _a, **b = (const char**) _b;
-
+static int list_dependencies_compare(char * const *a, char * const *b) {
if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET)
return 1;
if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET)
@@ -1714,7 +1707,7 @@ static int list_dependencies_one(
if (r < 0)
return r;
- qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
+ typesafe_qsort(deps, strv_length(deps), list_dependencies_compare);
STRV_FOREACH(c, deps) {
if (strv_contains(*units, *c)) {
@@ -1839,13 +1832,14 @@ static void free_machines_list(struct machine_info *machine_infos, int n) {
free(machine_infos);
}
-static int compare_machine_info(const void *a, const void *b) {
- const struct machine_info *u = a, *v = b;
+static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) {
+ int r;
- if (u->is_host != v->is_host)
- return u->is_host > v->is_host ? -1 : 1;
+ r = CMP(b->is_host, a->is_host);
+ if (r != 0)
+ return r;
- return strcasecmp(u->name, v->name);
+ return strcasecmp(a->name, b->name);
}
static int get_machine_properties(sd_bus *bus, struct machine_info *mi) {
@@ -2033,7 +2027,7 @@ static int list_machines(int argc, char *argv[], void *userdata) {
(void) pager_open(arg_no_pager, false);
- qsort_safe(machine_infos, r, sizeof(struct machine_info), compare_machine_info);
+ typesafe_qsort(machine_infos, r, compare_machine_info);
output_machines_list(machine_infos, r);
free_machines_list(machine_infos, r);
@@ -5169,7 +5163,7 @@ static int show_all(
c = (unsigned) r;
- qsort_safe(unit_infos, c, sizeof(UnitInfo), compare_unit_info);
+ typesafe_qsort(unit_infos, c, compare_unit_info);
for (u = unit_infos; u < unit_infos + c; u++) {
_cleanup_free_ char *p = NULL;
diff --git a/src/test/test-prioq.c b/src/test/test-prioq.c
index 89c41d8ce7..f7fb5ce422 100644
--- a/src/test/test-prioq.c
+++ b/src/test/test-prioq.c
@@ -10,16 +10,8 @@
#define SET_SIZE 1024*4
-static int unsigned_compare(const void *a, const void *b) {
- const unsigned *x = a, *y = b;
-
- if (*x < *y)
- return -1;
-
- if (*x > *y)
- return 1;
-
- return 0;
+static int unsigned_compare(const unsigned *a, const unsigned *b) {
+ return CMP(*a, *b);
}
static void test_unsigned(void) {
@@ -39,7 +31,7 @@ static void test_unsigned(void) {
assert_se(prioq_put(q, UINT_TO_PTR(u), NULL) >= 0);
}
- qsort(buffer, ELEMENTSOF(buffer), sizeof(buffer[0]), unsigned_compare);
+ typesafe_qsort(buffer, ELEMENTSOF(buffer), unsigned_compare);
for (i = 0; i < ELEMENTSOF(buffer); i++) {
unsigned u;
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
index b91b212a70..e29f706fc0 100644
--- a/src/tmpfiles/tmpfiles.c
+++ b/src/tmpfiles/tmpfiles.c
@@ -2317,18 +2317,16 @@ static void item_array_free(ItemArray *a) {
free(a);
}
-static int item_compare(const void *a, const void *b) {
- const Item *x = a, *y = b;
-
+static int item_compare(const Item *a, const Item *b) {
/* Make sure that the ownership taking item is put first, so
* that we first create the node, and then can adjust it */
- if (takes_ownership(x->type) && !takes_ownership(y->type))
+ if (takes_ownership(a->type) && !takes_ownership(b->type))
return -1;
- if (!takes_ownership(x->type) && takes_ownership(y->type))
+ if (!takes_ownership(a->type) && takes_ownership(b->type))
return 1;
- return (int) x->type - (int) y->type;
+ return CMP(a->type, b->type);
}
static bool item_compatible(Item *a, Item *b) {
@@ -2793,7 +2791,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, bool
memcpy(existing->items + existing->count++, &i, sizeof(i));
/* Sort item array, to enforce stable ordering of application */
- qsort_safe(existing->items, existing->count, sizeof(Item), item_compare);
+ typesafe_qsort(existing->items, existing->count, item_compare);
zero(i);
return 0;
diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c
index 9dbbd1bf95..3e9a194532 100644
--- a/src/udev/udevadm-hwdb.c
+++ b/src/udev/udevadm-hwdb.c
@@ -72,11 +72,8 @@ struct trie_value_entry {
size_t value_off;
};
-static int trie_children_cmp(const void *v1, const void *v2) {
- const struct trie_child_entry *n1 = v1;
- const struct trie_child_entry *n2 = v2;
-
- return n1->c - n2->c;
+static int trie_children_cmp(const struct trie_child_entry *a, const struct trie_child_entry *b) {
+ return CMP(a->c, b->c);
}
static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t c) {
@@ -92,7 +89,7 @@ static int node_add_child(struct trie *trie, struct trie_node *node, struct trie
node->children[node->children_count].c = c;
node->children[node->children_count].child = node_child;
node->children_count++;
- qsort(node->children, node->children_count, sizeof(struct trie_child_entry), trie_children_cmp);
+ typesafe_qsort(node->children, node->children_count, trie_children_cmp);
trie->nodes_count++;
return 0;
@@ -105,7 +102,7 @@ static struct trie_node *node_lookup(const struct trie_node *node, uint8_t c) {
search.c = c;
child = bsearch_safe(&search,
node->children, node->children_count, sizeof(struct trie_child_entry),
- trie_children_cmp);
+ (comparison_fn_t) trie_children_cmp);
if (child)
return child->child;
return NULL;