tree-wide: use typesafe_qsort()

This commit is contained in:
Yu Watanabe 2018-09-18 08:39:24 +09:00
parent 6058516a14
commit 93bab28895
35 changed files with 243 additions and 374 deletions

View File

@ -41,8 +41,6 @@
#define SCALE_X (0.1 / 1000.0) /* pixels per us */ #define SCALE_X (0.1 / 1000.0) /* pixels per us */
#define SCALE_Y (20.0) #define SCALE_Y (20.0)
#define compare(a, b) (((a) > (b))? 1 : (((b) > (a))? -1 : 0))
#define svg(...) printf(__VA_ARGS__) #define svg(...) printf(__VA_ARGS__)
#define svg_bar(class, x1, x2, y) \ #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; return 0;
} }
static int compare_unit_time(const void *a, const void *b) { static int compare_unit_time(const struct unit_times *a, const struct unit_times *b) {
return compare(((struct unit_times *)b)->time, return CMP(b->time, a->time);
((struct unit_times *)a)->time);
} }
static int compare_unit_start(const void *a, const void *b) { static int compare_unit_start(const struct unit_times *a, const struct unit_times *b) {
return compare(((struct unit_times *)a)->activating, return CMP(a->activating, b->activating);
((struct unit_times *)b)->activating);
} }
static void unit_times_free(struct unit_times *t) { 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) if (n <= 0)
return n; 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); width = SCALE_X * (boot->firmware_time + boot->finish_time);
if (width < 800.0) 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 Hashmap *unit_times_hashmap;
static int list_dependencies_compare(const void *_a, const void *_b) { static int list_dependencies_compare(char * const *a, char * const *b) {
const char **a = (const char**) _a, **b = (const char**) _b;
usec_t usa = 0, usb = 0; usec_t usa = 0, usb = 0;
struct unit_times *times; struct unit_times *times;
@ -866,7 +861,7 @@ static int list_dependencies_compare(const void *_a, const void *_b) {
if (times) if (times)
usb = times->activated; 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) { 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) if (r < 0)
return r; 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); r = acquire_boot_times(bus, &boot);
if (r < 0) if (r < 0)
@ -1056,7 +1051,7 @@ static int analyze_blame(int argc, char *argv[], void *userdata) {
if (n <= 0) if (n <= 0)
return n; 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); (void) pager_open(arg_no_pager, false);

View File

@ -57,25 +57,18 @@ CalendarSpec* calendar_spec_free(CalendarSpec *c) {
return mfree(c); return mfree(c);
} }
static int component_compare(const void *_a, const void *_b) { static int component_compare(CalendarComponent * const *a, CalendarComponent * const *b) {
CalendarComponent * const *a = _a, * const *b = _b; int r;
if ((*a)->start < (*b)->start) r = CMP((*a)->start, (*b)->start);
return -1; if (r != 0)
if ((*a)->start > (*b)->start) return r;
return 1;
if ((*a)->stop < (*b)->stop) r = CMP((*a)->stop, (*b)->stop);
return -1; if (r != 0)
if ((*a)->stop > (*b)->stop) return r;
return 1;
if ((*a)->repeat < (*b)->repeat) return CMP((*a)->repeat, (*b)->repeat);
return -1;
if ((*a)->repeat > (*b)->repeat)
return 1;
return 0;
} }
static void normalize_chain(CalendarComponent **c) { static void normalize_chain(CalendarComponent **c) {
@ -103,7 +96,7 @@ static void normalize_chain(CalendarComponent **c) {
for (i = *c; i; i = i->next) for (i = *c; i; i = i->next)
*(j++) = i; *(j++) = i;
qsort(b, n, sizeof(CalendarComponent*), component_compare); typesafe_qsort(b, n, component_compare);
b[n-1]->next = NULL; b[n-1]->next = NULL;
next = b[n-1]; next = b[n-1];

View File

@ -134,12 +134,8 @@ static int files_add(
return 0; return 0;
} }
static int base_cmp(const void *a, const void *b) { static int base_cmp(char * const *a, char * const *b) {
const char *s1, *s2; return strcmp(basename(*a), basename(*b));
s1 = *(char * const *)a;
s2 = *(char * const *)b;
return strcmp(basename(s1), basename(s2));
} }
static int conf_files_list_strv_internal(char ***strv, const char *suffix, const char *root, unsigned flags, char **dirs) { 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) if (!files)
return -ENOMEM; return -ENOMEM;
qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp); typesafe_qsort(files, hashmap_size(fh), base_cmp);
*strv = files; *strv = files;
return 0; 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++) { for (i = 0; i < strv_length(*strv); i++) {
int c; int c;
c = base_cmp(*strv + i, &path); c = base_cmp((char* const*) *strv + i, (char* const*) &path);
if (c == 0) { if (c == 0) {
char **dir; char **dir;

View File

@ -1104,11 +1104,9 @@ void valgrind_summary_hack(void) {
#endif #endif
} }
int pid_compare_func(const void *a, const void *b) { int pid_compare_func(const pid_t *a, const pid_t *b) {
const pid_t *p = a, *q = b;
/* Suitable for usage in qsort() */ /* Suitable for usage in qsort() */
return CMP(*p, *q); return CMP(*a, *b);
} }
int ioprio_parse_priority(const char *s, int *ret) { int ioprio_parse_priority(const char *s, int *ret) {

View File

@ -108,7 +108,7 @@ static inline void* PID_TO_PTR(pid_t pid) {
void valgrind_summary_hack(void); 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) { static inline bool nice_is_valid(int n) {
return n >= PRIO_MIN && n < PRIO_MAX; return n >= PRIO_MIN && n < PRIO_MAX;

View File

@ -711,14 +711,12 @@ bool strv_overlap(char **a, char **b) {
return false; return false;
} }
static int str_compare(const void *_a, const void *_b) { static int str_compare(char * const *a, char * const *b) {
const char **a = (const char**) _a, **b = (const char**) _b;
return strcmp(*a, *b); return strcmp(*a, *b);
} }
char **strv_sort(char **l) { 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; return l;
} }

View File

@ -739,10 +739,8 @@ static int member_compare_func(const void *a, const void *b) {
return strcmp_ptr(x->name, y->name); return strcmp_ptr(x->name, y->name);
} }
static int member_compare_funcp(const void *a, const void *b) { static int member_compare_funcp(Member * const *a, Member * const *b) {
const Member *const * x = (const Member *const *) a, * const *y = (const Member *const *) b; return member_compare_func(*a, *b);
return member_compare_func(*x, *y);
} }
static void member_free(Member *m) { static void member_free(Member *m) {
@ -1063,7 +1061,7 @@ static int introspect(int argc, char **argv, void *userdata) {
if (result_width > 40) if (result_width > 40)
result_width = 40; result_width = 40;
qsort(sorted, k, sizeof(Member*), member_compare_funcp); typesafe_qsort(sorted, k, member_compare_funcp);
if (arg_legend) { if (arg_legend) {
printf("%-*s %-*s %-*s %-*s %s\n", printf("%-*s %-*s %-*s %-*s %s\n",

View File

@ -524,8 +524,9 @@ static int refresh(const char *root, Hashmap *a, Hashmap *b, unsigned iteration)
return 0; return 0;
} }
static int group_compare(const void*a, const void *b) { static int group_compare(Group * const *a, Group * const *b) {
const Group *x = *(Group**)a, *y = *(Group**)b; const Group *x = *a, *y = *b;
int r;
if (arg_order != ORDER_TASKS || arg_recursive) { if (arg_order != ORDER_TASKS || arg_recursive) {
/* Let's make sure that the parent is always before /* 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: case ORDER_CPU:
if (arg_cpu_type == CPU_PERCENT) { if (arg_cpu_type == CPU_PERCENT) {
if (x->cpu_valid && y->cpu_valid) { if (x->cpu_valid && y->cpu_valid) {
if (x->cpu_fraction > y->cpu_fraction) r = CMP(y->cpu_fraction, x->cpu_fraction);
return -1; if (r != 0)
else if (x->cpu_fraction < y->cpu_fraction) return r;
return 1;
} else if (x->cpu_valid) } else if (x->cpu_valid)
return -1; return -1;
else if (y->cpu_valid) else if (y->cpu_valid)
return 1; return 1;
} else { } else {
if (x->cpu_usage > y->cpu_usage) r = CMP(y->cpu_usage, x->cpu_usage);
return -1; if (r != 0)
else if (x->cpu_usage < y->cpu_usage) return r;
return 1;
} }
break; break;
case ORDER_TASKS: case ORDER_TASKS:
if (x->n_tasks_valid && y->n_tasks_valid) { if (x->n_tasks_valid && y->n_tasks_valid) {
if (x->n_tasks > y->n_tasks) r = CMP(y->n_tasks, x->n_tasks);
return -1; if (r != 0)
else if (x->n_tasks < y->n_tasks) return r;
return 1;
} else if (x->n_tasks_valid) } else if (x->n_tasks_valid)
return -1; return -1;
else if (y->n_tasks_valid) else if (y->n_tasks_valid)
@ -579,10 +577,9 @@ static int group_compare(const void*a, const void *b) {
case ORDER_MEMORY: case ORDER_MEMORY:
if (x->memory_valid && y->memory_valid) { if (x->memory_valid && y->memory_valid) {
if (x->memory > y->memory) r = CMP(y->memory, x->memory);
return -1; if (r != 0)
else if (x->memory < y->memory) return r;
return 1;
} else if (x->memory_valid) } else if (x->memory_valid)
return -1; return -1;
else if (y->memory_valid) else if (y->memory_valid)
@ -592,10 +589,9 @@ static int group_compare(const void*a, const void *b) {
case ORDER_IO: case ORDER_IO:
if (x->io_valid && y->io_valid) { if (x->io_valid && y->io_valid) {
if (x->io_input_bps + x->io_output_bps > y->io_input_bps + y->io_output_bps) r = CMP(y->io_input_bps + y->io_output_bps, x->io_input_bps + x->io_output_bps);
return -1; if (r != 0)
else if (x->io_input_bps + x->io_output_bps < y->io_input_bps + y->io_output_bps) return r;
return 1;
} else if (x->io_valid) } else if (x->io_valid)
return -1; return -1;
else if (y->io_valid) 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) if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid)
array[n++] = g; array[n++] = g;
qsort_safe(array, n, sizeof(Group*), group_compare); typesafe_qsort(array, n, group_compare);
/* Find the longest names in one run */ /* Find the longest names in one run */
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {

View File

@ -1384,15 +1384,8 @@ void job_add_to_gc_queue(Job *j) {
j->in_gc_queue = true; j->in_gc_queue = true;
} }
static int job_compare(const void *a, const void *b) { static int job_compare(Job * const *a, Job * const *b) {
Job *x = *(Job**) a, *y = *(Job**) b; return CMP((*a)->id, (*b)->id);
if (x->id < y->id)
return -1;
if (x->id > y->id)
return 1;
return 0;
} }
static size_t sort_job_list(Job **list, size_t n) { 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; size_t a, b;
/* Order by numeric IDs */ /* Order by numeric IDs */
qsort_safe(list, n, sizeof(Job*), job_compare); typesafe_qsort(list, n, job_compare);
/* Filter out duplicates */ /* Filter out duplicates */
for (a = 0, b = 0; a < n; a++) { for (a = 0, b = 0; a < n; a++) {

View File

@ -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) { static int mount_path_compare(const MountEntry *a, const MountEntry *b) {
const MountEntry *p = a, *q = b;
int d; int d;
/* If the paths are not equal, then order prefixes first */ /* 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) if (d != 0)
return d; return d;
/* If the paths are equal, check the mode */ /* If the paths are equal, check the mode */
if (p->mode < q->mode) return CMP((int) a->mode, (int) b->mode);
return -1;
if (p->mode > q->mode)
return 1;
return 0;
} }
static int prefix_where_needed(MountEntry *m, size_t n, const char *root_directory) { 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(n_mounts);
assert(mounts || *n_mounts == 0); 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_duplicates(mounts, n_mounts);
drop_outside_root(root_directory, mounts, n_mounts); drop_outside_root(root_directory, mounts, n_mounts);

View File

@ -76,11 +76,8 @@ struct trie_value_entry {
uint16_t file_priority; uint16_t file_priority;
}; };
static int trie_children_cmp(const void *v1, const void *v2) { static int trie_children_cmp(const struct trie_child_entry *a, const struct trie_child_entry *b) {
const struct trie_child_entry *n1 = v1; return CMP(a->c, b->c);
const struct trie_child_entry *n2 = v2;
return n1->c - n2->c;
} }
static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t 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].c = c;
node->children[node->children_count].child = node_child; node->children[node->children_count].child = node_child;
node->children_count++; 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++; trie->nodes_count++;
return 0; 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; struct trie_child_entry search;
search.c = c; 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) if (child)
return child->child; return child->child;
return NULL; return NULL;

View File

@ -56,23 +56,22 @@ static void catalog_hash_func(const void *p, struct siphash *state) {
siphash24_compress(i->language, strlen(i->language), state); siphash24_compress(i->language, strlen(i->language), state);
} }
static int catalog_compare_func(const void *a, const void *b) { static int catalog_compare_func(const CatalogItem *a, const CatalogItem *b) {
const CatalogItem *i = a, *j = b;
unsigned k; unsigned k;
int r; int r;
for (k = 0; k < ELEMENTSOF(j->id.bytes); k++) { for (k = 0; k < ELEMENTSOF(b->id.bytes); k++) {
r = CMP(i->id.bytes[k], j->id.bytes[k]); r = CMP(a->id.bytes[k], b->id.bytes[k]);
if (r != 0) if (r != 0)
return r; return r;
} }
return strcmp(i->language, j->language); return strcmp(a->language, b->language);
} }
const struct hash_ops catalog_hash_ops = { const struct hash_ops catalog_hash_ops = {
.hash = catalog_hash_func, .hash = catalog_hash_func,
.compare = catalog_compare_func .compare = (comparison_fn_t) catalog_compare_func,
}; };
static bool next_header(const char **s) { 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)); assert(n == hashmap_size(h));
qsort_safe(items, n, sizeof(CatalogItem), catalog_compare_func); typesafe_qsort(items, n, catalog_compare_func);
strbuf_complete(sb); 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)); strncpy(key.language, loc, sizeof(key.language));
key.language[strcspn(key.language, ".@")] = 0; 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) { if (!f) {
char *e; char *e;
e = strchr(key.language, '_'); e = strchr(key.language, '_');
if (e) { if (e) {
*e = 0; *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) { if (!f) {
zero(key.language); 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) if (!f)

View File

@ -1933,14 +1933,8 @@ int journal_file_enable_post_change_timer(JournalFile *f, sd_event *e, usec_t t)
return r; return r;
} }
static int entry_item_cmp(const void *_a, const void *_b) { static int entry_item_cmp(const EntryItem *a, const EntryItem *b) {
const EntryItem *a = _a, *b = _b; return CMP(le64toh(a->object_offset), le64toh(b->object_offset));
if (le64toh(a->object_offset) < le64toh(b->object_offset))
return -1;
if (le64toh(a->object_offset) > le64toh(b->object_offset))
return 1;
return 0;
} }
int journal_file_append_entry( 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 /* Order by the position on disk, in order to improve seek
* times for rotating media. */ * 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); r = journal_file_append_entry_internal(f, ts, boot_id, xor_hash, items, n_iovec, seqnum, ret, offset);

View File

@ -29,30 +29,21 @@ struct vacuum_info {
bool have_seqnum; bool have_seqnum;
}; };
static int vacuum_compare(const void *_a, const void *_b) { static int vacuum_compare(const struct vacuum_info *a, const struct vacuum_info *b) {
const struct vacuum_info *a, *b; int r;
a = _a;
b = _b;
if (a->have_seqnum && b->have_seqnum && if (a->have_seqnum && b->have_seqnum &&
sd_id128_equal(a->seqnum_id, b->seqnum_id)) { sd_id128_equal(a->seqnum_id, b->seqnum_id))
if (a->seqnum < b->seqnum) return CMP(a->seqnum, b->seqnum);
return -1;
else if (a->seqnum > b->seqnum)
return 1;
else
return 0;
}
if (a->realtime < b->realtime) r = CMP(a->realtime, b->realtime);
return -1; if (r != 0)
else if (a->realtime > b->realtime) return r;
return 1;
else if (a->have_seqnum && b->have_seqnum) if (a->have_seqnum && b->have_seqnum)
return memcmp(&a->seqnum_id, &b->seqnum_id, 16); 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( static void patch_realtime(
@ -292,7 +283,7 @@ int journal_directory_vacuum(
sum += size; 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++) { for (i = 0; i < n_list; i++) {
unsigned left; unsigned left;

View File

@ -371,10 +371,8 @@ _public_ int sd_lldp_new(sd_lldp **ret) {
return 0; return 0;
} }
static int neighbor_compare_func(const void *a, const void *b) { static int neighbor_compare_func(sd_lldp_neighbor * const *a, sd_lldp_neighbor * const *b) {
const sd_lldp_neighbor * const*x = a, * const *y = b; return lldp_neighbor_id_hash_ops.compare(&(*a)->id, &(*b)->id);
return lldp_neighbor_id_hash_ops.compare(&(*x)->id, &(*y)->id);
} }
static int on_timer_event(sd_event_source *s, uint64_t usec, void *userdata) { 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)); assert((size_t) k == hashmap_size(lldp->neighbor_by_id));
/* Return things in a stable order */ /* 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; *ret = l;
return k; return k;

View File

@ -760,15 +760,8 @@ enum bus_match_node_type bus_match_node_type_from_string(const char *k, size_t n
return -EINVAL; return -EINVAL;
} }
static int match_component_compare(const void *a, const void *b) { static int match_component_compare(const struct bus_match_component *a, const struct bus_match_component *b) {
const struct bus_match_component *x = a, *y = b; return CMP(a->type, b->type);
if (x->type < y->type)
return -1;
if (x->type > y->type)
return 1;
return 0;
} }
void bus_match_parse_free(struct bus_match_component *components, unsigned n_components) { 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 */ /* 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 */ /* Check for duplicates */
for (i = 0; i+1 < n_components; i++) for (i = 0; i+1 < n_components; i++)

View File

@ -249,10 +249,11 @@ int device_enumerator_add_match_is_initialized(sd_device_enumerator *enumerator)
return 0; 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; sd_device *a = *(sd_device **)_a, *b = *(sd_device **)_b;
const char *devpath_a, *devpath_b, *sound_a; const char *devpath_a, *devpath_b, *sound_a;
bool delay_a, delay_b; bool delay_a, delay_b;
int r;
assert_se(sd_device_get_devpath(a, &devpath_a) >= 0); assert_se(sd_device_get_devpath(a, &devpath_a) >= 0);
assert_se(sd_device_get_devpath(b, &devpath_b) >= 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 */ /* md and dm devices are enumerated after all other devices */
delay_a = strstr(devpath_a, "/block/md") || strstr(devpath_a, "/block/dm-"); delay_a = strstr(devpath_a, "/block/md") || strstr(devpath_a, "/block/dm-");
delay_b = strstr(devpath_b, "/block/md") || strstr(devpath_b, "/block/dm-"); delay_b = strstr(devpath_b, "/block/md") || strstr(devpath_b, "/block/dm-");
if (delay_a != delay_b) r = CMP(delay_a, delay_b);
return delay_a - delay_b; if (r != 0)
return r;
return strcmp(devpath_a, devpath_b); return strcmp(devpath_a, devpath_b);
} }
@ -830,7 +832,7 @@ int device_enumerator_scan_devices(sd_device_enumerator *enumerator) {
r = k; 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->scan_uptodate = true;
enumerator->type = DEVICE_ENUMERATION_TYPE_DEVICES; 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->scan_uptodate = true;
enumerator->type = DEVICE_ENUMERATION_TYPE_SUBSYSTEMS; enumerator->type = DEVICE_ENUMERATION_TYPE_SUBSYSTEMS;

View File

@ -7,8 +7,8 @@
#include "macro.h" #include "macro.h"
#include "netlink-util.h" #include "netlink-util.h"
static int address_compare(const void *_a, const void *_b) { static int address_compare(const struct local_address *a, const struct local_address *b) {
const struct local_address *a = _a, *b = _b; int r;
/* Order lowest scope first, IPv4 before IPv6, lowest interface index first */ /* 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) if (a->family == AF_INET6 && b->family == AF_INET)
return 1; return 1;
if (a->scope < b->scope) r = CMP(a->scope, b->scope);
return -1; if (r != 0)
if (a->scope > b->scope) return r;
return 1;
if (a->metric < b->metric) r = CMP(a->metric, b->metric);
return -1; if (r != 0)
if (a->metric > b->metric) return r;
return 1;
if (a->ifindex < b->ifindex) r = CMP(a->ifindex, b->ifindex);
return -1; if (r != 0)
if (a->ifindex > b->ifindex) return r;
return 1;
return memcmp(&a->address, &b->address, FAMILY_ADDRESS_SIZE(a->family)); 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++; n_list++;
}; };
qsort_safe(list, n_list, sizeof(struct local_address), address_compare); typesafe_qsort(list, n_list, address_compare);
*ret = TAKE_PTR(list); *ret = TAKE_PTR(list);
@ -248,8 +245,7 @@ int local_gateways(sd_netlink *context, int ifindex, int af, struct local_addres
n_list++; n_list++;
} }
if (n_list > 0) typesafe_qsort(list, n_list, address_compare);
qsort(list, n_list, sizeof(struct local_address), address_compare);
*ret = TAKE_PTR(list); *ret = TAKE_PTR(list);

View File

@ -2378,10 +2378,8 @@ typedef struct TransferInfo {
double progress; double progress;
} TransferInfo; } TransferInfo;
static int compare_transfer_info(const void *a, const void *b) { static int compare_transfer_info(const TransferInfo *a, const TransferInfo *b) {
const TransferInfo *x = a, *y = b; return strcmp(a->local, b->local);
return strcmp(x->local, y->local);
} }
static int list_transfers(int argc, char *argv[], void *userdata) { 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) if (r < 0)
return bus_log_parse_error(r); 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) if (arg_legend && n_transfers > 0)
printf("%-*s %-*s %-*s %-*s %-*s\n", printf("%-*s %-*s %-*s %-*s %-*s\n",

View File

@ -1372,17 +1372,15 @@ struct item {
char* columns[_COLUMN_MAX]; char* columns[_COLUMN_MAX];
}; };
static int compare_item(const void *a, const void *b) { static int compare_item(const struct item *a, const struct item *b) {
const struct item *x = a, *y = b; if (a->columns[COLUMN_NODE] == b->columns[COLUMN_NODE])
if (x->columns[COLUMN_NODE] == y->columns[COLUMN_NODE])
return 0; return 0;
if (!x->columns[COLUMN_NODE]) if (!a->columns[COLUMN_NODE])
return 1; return 1;
if (!y->columns[COLUMN_NODE]) if (!b->columns[COLUMN_NODE])
return -1; 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) { static int list_devices(void) {
@ -1485,7 +1483,7 @@ static int list_devices(void) {
goto finish; goto finish;
} }
qsort_safe(items, n, sizeof(struct item), compare_item); typesafe_qsort(items, n, compare_item);
(void) pager_open(arg_no_pager, false); (void) pager_open(arg_no_pager, false);

View File

@ -104,10 +104,8 @@ typedef struct LinkInfo {
bool has_mtu:1; bool has_mtu:1;
} LinkInfo; } LinkInfo;
static int link_info_compare(const void *a, const void *b) { static int link_info_compare(const LinkInfo *a, const LinkInfo *b) {
const LinkInfo *x = a, *y = b; return CMP(a->ifindex, b->ifindex);
return x->ifindex - y->ifindex;
} }
static int decode_link(sd_netlink_message *m, LinkInfo *info) { 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++; c++;
} }
qsort_safe(links, c, sizeof(LinkInfo), link_info_compare); typesafe_qsort(links, c, link_info_compare);
*ret = TAKE_PTR(links); *ret = TAKE_PTR(links);
@ -230,7 +228,7 @@ static int acquire_link_info_all(sd_netlink *rtnl, LinkInfo **ret) {
c++; c++;
} }
qsort_safe(links, c, sizeof(LinkInfo), link_info_compare); typesafe_qsort(links, c, link_info_compare);
*ret = TAKE_PTR(links); *ret = TAKE_PTR(links);

View File

@ -69,20 +69,14 @@ void custom_mount_free_all(CustomMount *l, size_t n) {
free(l); free(l);
} }
static int custom_mount_compare(const void *a, const void *b) { static int custom_mount_compare(const CustomMount *a, const CustomMount *b) {
const CustomMount *x = a, *y = b;
int r; int r;
r = path_compare(x->destination, y->destination); r = path_compare(a->destination, b->destination);
if (r != 0) if (r != 0)
return r; return r;
if (x->type < y->type) return CMP(a->type, b->type);
return -1;
if (x->type > y->type)
return 1;
return 0;
} }
static bool source_path_is_valid(const char *p) { 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); assert(l || n == 0);
/* Order the custom mounts, and make sure we have a working directory */ /* 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++) { for (i = 0; i < n; i++) {
CustomMount *m = l + i; CustomMount *m = l + i;

View File

@ -114,32 +114,25 @@ int dnssec_canonicalize(const char *n, char *buffer, size_t buffer_max) {
#if HAVE_GCRYPT #if HAVE_GCRYPT
static int rr_compare(const void *a, const void *b) { static int rr_compare(DnsResourceRecord * const *a, DnsResourceRecord * const *b) {
DnsResourceRecord **x = (DnsResourceRecord**) a, **y = (DnsResourceRecord**) b; const DnsResourceRecord *x = *a, *y = *b;
size_t m; size_t m;
int r; int r;
/* Let's order the RRs according to RFC 4034, Section 6.3 */ /* Let's order the RRs according to RFC 4034, Section 6.3 */
assert(x); assert(x);
assert(*x); assert(x->wire_format);
assert((*x)->wire_format);
assert(y); 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) if (r != 0)
return r; return r;
if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) < DNS_RESOURCE_RECORD_RDATA_SIZE(*y)) return CMP(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;
} }
static int dnssec_rsa_verify_raw( static int dnssec_rsa_verify_raw(
@ -806,7 +799,7 @@ int dnssec_verify_rrset(
return -ENODATA; return -ENODATA;
/* Bring the RRs into canonical order */ /* 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); f = open_memstream(&sig_data, &sig_size);
if (!f) if (!f)

View File

@ -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) if (!rr)
return NULL; 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; 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) if (!rr)
return 0; return 0;
if (!rr->wire_format) 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; 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);
assert(rr->key->type == DNS_TYPE_OPT); assert(rr->key->type == DNS_TYPE_OPT);

View File

@ -464,10 +464,8 @@ static int dns_trust_anchor_load_files(
return 0; return 0;
} }
static int domain_name_cmp(const void *a, const void *b) { static int domain_name_cmp(char * const *a, char * const *b) {
char **x = (char**) a, **y = (char**) b; return dns_name_compare_func(*a, *b);
return dns_name_compare_func(*x, *y);
} }
static int dns_trust_anchor_dump(DnsTrustAnchor *d) { static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
@ -497,7 +495,7 @@ static int dns_trust_anchor_dump(DnsTrustAnchor *d) {
if (!l) if (!l)
return log_oom(); 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, " "); j = strv_join(l, " ");
if (!j) if (!j)

View File

@ -53,50 +53,41 @@ eaddrinuse:
return 0; return 0;
} }
static int mdns_rr_compare(const void *a, const void *b) { static int mdns_rr_compare(DnsResourceRecord * const *a, DnsResourceRecord * const *b) {
DnsResourceRecord **x = (DnsResourceRecord**) a, **y = (DnsResourceRecord**) b; DnsResourceRecord *x = *(DnsResourceRecord **) a, *y = *(DnsResourceRecord **) b;
size_t m; size_t m;
int r; int r;
assert(x); assert(x);
assert(*x);
assert(y); assert(y);
assert(*y);
if (CLEAR_CACHE_FLUSH((*x)->key->class) < CLEAR_CACHE_FLUSH((*y)->key->class)) r = CMP(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;
if ((*x)->key->type < (*y)->key->type)
return -1;
else if ((*x)->key->type > (*y)->key->type)
return 1;
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);
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));
r = memcmp(DNS_RESOURCE_RECORD_RDATA(*x), DNS_RESOURCE_RECORD_RDATA(*y), m);
if (r != 0) if (r != 0)
return r; return r;
if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) < DNS_RESOURCE_RECORD_RDATA_SIZE(*y)) r = CMP(x->key->type, y->key->type);
return -1; if (r != 0)
else if (DNS_RESOURCE_RECORD_RDATA_SIZE(*x) > DNS_RESOURCE_RECORD_RDATA_SIZE(*y)) return r;
return 1;
return 0; 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);
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));
r = memcmp(DNS_RESOURCE_RECORD_RDATA(x), DNS_RESOURCE_RECORD_RDATA(y), m);
if (r != 0)
return r;
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) { 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; list[n++] = p->answer->items[i].rr;
} }
assert(n == size); assert(n == size);
qsort_safe(list, size, sizeof(DnsResourceRecord*), mdns_rr_compare); typesafe_qsort(list, size, mdns_rr_compare);
*ret_rrs = TAKE_PTR(list); *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) DNS_ANSWER_FOREACH(rr, answer)
our[i++] = rr; 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); r = mdns_packet_extract_matching_rrs(p, key, &remote);
if (r < 0) if (r < 0)

View File

@ -202,10 +202,8 @@ int boot_loader_read_conf(const char *path, BootConfig *config) {
return 0; return 0;
} }
static int boot_entry_compare(const void *a, const void *b) { static int boot_entry_compare(const BootEntry *a, const BootEntry *b) {
const BootEntry *aa = a, *bb = b; return str_verscmp(a->filename, b->filename);
return str_verscmp(aa->filename, bb->filename);
} }
int boot_entries_find(const char *dir, BootEntry **ret_entries, size_t *ret_n_entries) { 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++; n++;
} }
qsort_safe(array, n, sizeof(BootEntry), boot_entry_compare); typesafe_qsort(array, n, boot_entry_compare);
*ret_entries = array; *ret_entries = array;
*ret_n_entries = n; *ret_n_entries = n;

View File

@ -2206,13 +2206,8 @@ static void remove_cgroup(Hashmap *cgroups, struct CGroupInfo *cg) {
free(cg); free(cg);
} }
static int cgroup_info_compare_func(const void *a, const void *b) { static int cgroup_info_compare_func(struct CGroupInfo * const *a, struct CGroupInfo * const *b) {
const struct CGroupInfo *x = *(const struct CGroupInfo* const*) a, *y = *(const struct CGroupInfo* const*) b; return strcmp((*a)->cgroup_path, (*b)->cgroup_path);
assert(x);
assert(y);
return strcmp(x->cgroup_path, y->cgroup_path);
} }
static int dump_processes( static int dump_processes(
@ -2249,7 +2244,7 @@ static int dump_processes(
pids[n++] = PTR_TO_PID(pidp); pids[n++] = PTR_TO_PID(pidp);
assert(n == hashmap_size(cg->pids)); 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]); width = DECIMAL_STR_WIDTH(pids[n-1]);
@ -2291,7 +2286,7 @@ static int dump_processes(
LIST_FOREACH(siblings, child, cg->children) LIST_FOREACH(siblings, child, cg->children)
children[n++] = child; children[n++] = child;
assert(n == cg->n_children); 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) if (n_columns != 0)
n_columns = MAX(LESS_BY(n_columns, 2U), 20U); n_columns = MAX(LESS_BY(n_columns, 2U), 20U);
@ -2378,7 +2373,7 @@ static int dump_extra_processes(
if (n == 0) if (n == 0)
return 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]); width = DECIMAL_STR_WIDTH(pids[n-1]);
for (k = 0; k < n; k++) { for (k = 0; k < n; k++) {

View File

@ -38,7 +38,7 @@ static void show_pid_array(
if (n_pids == 0) if (n_pids == 0)
return; return;
qsort(pids, n_pids, sizeof(pid_t), pid_compare_func); typesafe_qsort(pids, n_pids, pid_compare_func);
/* Filter duplicates */ /* Filter duplicates */
for (j = 0, i = 1; i < n_pids; i++) { for (j = 0, i = 1; i < n_pids; i++) {

View File

@ -563,10 +563,8 @@ static int boot_id_hex(const char s[4]) {
return id; return id;
} }
static int cmp_uint16(const void *_a, const void *_b) { static int cmp_uint16(const uint16_t *a, const uint16_t *b) {
const uint16_t *a = _a, *b = _b; return CMP(*a, *b);
return (int)*a - (int)*b;
} }
int efi_get_boot_options(uint16_t **options) { int efi_get_boot_options(uint16_t **options) {
@ -604,7 +602,7 @@ int efi_get_boot_options(uint16_t **options) {
list[count++] = id; list[count++] = id;
} }
qsort_safe(list, count, sizeof(uint16_t), cmp_uint16); typesafe_qsort(list, count, cmp_uint16);
*options = TAKE_PTR(list); *options = TAKE_PTR(list);

View File

@ -8,6 +8,7 @@
#include "macro.h" #include "macro.h"
#include "uid-range.h" #include "uid-range.h"
#include "user-util.h" #include "user-util.h"
#include "util.h"
static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) { static bool uid_range_intersect(UidRange *range, uid_t start, uid_t nr) {
assert(range); 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) { static int uid_range_compare(const UidRange *a, const UidRange *b) {
const UidRange *x = a, *y = b; int r;
if (x->start < y->start) r = CMP(a->start, b->start);
return -1; if (r != 0)
if (x->start > y->start) return r;
return 1;
if (x->nr < y->nr) return CMP(a->nr, b->nr);
return -1;
if (x->nr > y->nr)
return 1;
return 0;
} }
int uid_range_add(UidRange **p, unsigned *n, uid_t start, uid_t 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; x->nr = nr;
} }
qsort(*p, *n, sizeof(UidRange), uid_range_compare); typesafe_qsort(*p, *n, uid_range_compare);
uid_range_coalesce(p, n); uid_range_coalesce(p, n);
return *n; return *n;

View File

@ -316,25 +316,24 @@ static bool install_client_side(void) {
return false; return false;
} }
static int compare_unit_info(const void *a, const void *b) { static int compare_unit_info(const UnitInfo *a, const UnitInfo *b) {
const UnitInfo *u = a, *v = b;
const char *d1, *d2; const char *d1, *d2;
int r; int r;
/* First, order by machine */ /* First, order by machine */
if (!u->machine && v->machine) if (!a->machine && b->machine)
return -1; return -1;
if (u->machine && !v->machine) if (a->machine && !b->machine)
return 1; return 1;
if (u->machine && v->machine) { if (a->machine && b->machine) {
r = strcasecmp(u->machine, v->machine); r = strcasecmp(a->machine, b->machine);
if (r != 0) if (r != 0)
return r; return r;
} }
/* Second, order by unit type */ /* Second, order by unit type */
d1 = strrchr(u->id, '.'); d1 = strrchr(a->id, '.');
d2 = strrchr(v->id, '.'); d2 = strrchr(b->id, '.');
if (d1 && d2) { if (d1 && d2) {
r = strcasecmp(d1, d2); r = strcasecmp(d1, d2);
if (r != 0) if (r != 0)
@ -342,7 +341,7 @@ static int compare_unit_info(const void *a, const void *b) {
} }
/* Third, order by name */ /* 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) { 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) if (r < 0)
return r; 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); 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) { static int socket_info_compare(const struct socket_info *a, const struct socket_info *b) {
int o; int r;
assert(a); assert(a);
assert(b); assert(b);
@ -861,16 +860,16 @@ static int socket_info_compare(const struct socket_info *a, const struct socket_
if (a->machine && !b->machine) if (a->machine && !b->machine)
return 1; return 1;
if (a->machine && b->machine) { if (a->machine && b->machine) {
o = strcasecmp(a->machine, b->machine); r = strcasecmp(a->machine, b->machine);
if (o != 0) if (r != 0)
return o; return r;
} }
o = strcmp(a->path, b->path); r = strcmp(a->path, b->path);
if (o == 0) if (r == 0)
o = strcmp(a->type, b->type); r = strcmp(a->type, b->type);
return o; return r;
} }
static int output_sockets_list(struct socket_info *socket_infos, unsigned cs) { 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 */ listening = triggered = NULL; /* avoid cleanup */
} }
qsort_safe(socket_infos, cs, sizeof(struct socket_info), typesafe_qsort(socket_infos, cs, socket_info_compare);
(__compar_fn_t) socket_info_compare);
output_sockets_list(socket_infos, cs); 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) { static int timer_info_compare(const struct timer_info *a, const struct timer_info *b) {
int o; int r;
assert(a); assert(a);
assert(b); 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) if (a->machine && !b->machine)
return 1; return 1;
if (a->machine && b->machine) { if (a->machine && b->machine) {
o = strcasecmp(a->machine, b->machine); r = strcasecmp(a->machine, b->machine);
if (o != 0) if (r != 0)
return o; return r;
} }
if (a->next_elapse < b->next_elapse) r = CMP(a->next_elapse, b->next_elapse);
return -1; if (r != 0)
if (a->next_elapse > b->next_elapse) return r;
return 1;
return strcmp(a->id, b->id); 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), typesafe_qsort(timer_infos, c, timer_info_compare);
(__compar_fn_t) timer_info_compare);
output_timers_list(timer_infos, c); output_timers_list(timer_infos, c);
@ -1323,12 +1319,11 @@ static int list_timers(int argc, char *argv[], void *userdata) {
return r; 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 char *d1, *d2;
const UnitFileList *u = a, *v = b;
d1 = strrchr(u->path, '.'); d1 = strrchr(a->path, '.');
d2 = strrchr(v->path, '.'); d2 = strrchr(b->path, '.');
if (d1 && d2) { if (d1 && d2) {
int r; int r;
@ -1338,7 +1333,7 @@ static int compare_unit_file_list(const void *a, const void *b) {
return r; 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) { 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); (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); output_unit_file_list(units, c);
if (install_client_side()) if (install_client_side())
@ -1680,9 +1675,7 @@ static int list_dependencies_get_dependencies(sd_bus *bus, const char *name, cha
return 0; return 0;
} }
static int list_dependencies_compare(const void *_a, const void *_b) { static int list_dependencies_compare(char * const *a, char * const *b) {
const char **a = (const char**) _a, **b = (const char**) _b;
if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET) if (unit_name_to_type(*a) == UNIT_TARGET && unit_name_to_type(*b) != UNIT_TARGET)
return 1; return 1;
if (unit_name_to_type(*a) != UNIT_TARGET && unit_name_to_type(*b) == UNIT_TARGET) 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) if (r < 0)
return r; 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) { STRV_FOREACH(c, deps) {
if (strv_contains(*units, *c)) { if (strv_contains(*units, *c)) {
@ -1839,13 +1832,14 @@ static void free_machines_list(struct machine_info *machine_infos, int n) {
free(machine_infos); free(machine_infos);
} }
static int compare_machine_info(const void *a, const void *b) { static int compare_machine_info(const struct machine_info *a, const struct machine_info *b) {
const struct machine_info *u = a, *v = b; int r;
if (u->is_host != v->is_host) r = CMP(b->is_host, a->is_host);
return u->is_host > v->is_host ? -1 : 1; 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) { 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); (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); output_machines_list(machine_infos, r);
free_machines_list(machine_infos, r); free_machines_list(machine_infos, r);
@ -5169,7 +5163,7 @@ static int show_all(
c = (unsigned) r; 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++) { for (u = unit_infos; u < unit_infos + c; u++) {
_cleanup_free_ char *p = NULL; _cleanup_free_ char *p = NULL;

View File

@ -10,16 +10,8 @@
#define SET_SIZE 1024*4 #define SET_SIZE 1024*4
static int unsigned_compare(const void *a, const void *b) { static int unsigned_compare(const unsigned *a, const unsigned *b) {
const unsigned *x = a, *y = b; return CMP(*a, *b);
if (*x < *y)
return -1;
if (*x > *y)
return 1;
return 0;
} }
static void test_unsigned(void) { 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); 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++) { for (i = 0; i < ELEMENTSOF(buffer); i++) {
unsigned u; unsigned u;

View File

@ -2317,18 +2317,16 @@ static void item_array_free(ItemArray *a) {
free(a); free(a);
} }
static int item_compare(const void *a, const void *b) { static int item_compare(const Item *a, const Item *b) {
const Item *x = a, *y = b;
/* Make sure that the ownership taking item is put first, so /* Make sure that the ownership taking item is put first, so
* that we first create the node, and then can adjust it */ * 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; return -1;
if (!takes_ownership(x->type) && takes_ownership(y->type)) if (!takes_ownership(a->type) && takes_ownership(b->type))
return 1; return 1;
return (int) x->type - (int) y->type; return CMP(a->type, b->type);
} }
static bool item_compatible(Item *a, Item *b) { 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)); memcpy(existing->items + existing->count++, &i, sizeof(i));
/* Sort item array, to enforce stable ordering of application */ /* 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); zero(i);
return 0; return 0;

View File

@ -72,11 +72,8 @@ struct trie_value_entry {
size_t value_off; size_t value_off;
}; };
static int trie_children_cmp(const void *v1, const void *v2) { static int trie_children_cmp(const struct trie_child_entry *a, const struct trie_child_entry *b) {
const struct trie_child_entry *n1 = v1; return CMP(a->c, b->c);
const struct trie_child_entry *n2 = v2;
return n1->c - n2->c;
} }
static int node_add_child(struct trie *trie, struct trie_node *node, struct trie_node *node_child, uint8_t 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].c = c;
node->children[node->children_count].child = node_child; node->children[node->children_count].child = node_child;
node->children_count++; 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++; trie->nodes_count++;
return 0; return 0;
@ -105,7 +102,7 @@ static struct trie_node *node_lookup(const struct trie_node *node, uint8_t c) {
search.c = c; search.c = c;
child = bsearch_safe(&search, child = bsearch_safe(&search,
node->children, node->children_count, sizeof(struct trie_child_entry), node->children, node->children_count, sizeof(struct trie_child_entry),
trie_children_cmp); (comparison_fn_t) trie_children_cmp);
if (child) if (child)
return child->child; return child->child;
return NULL; return NULL;